1
1
//! HTTP RequestUris
2
+ use std:: fmt:: { Display , self } ;
2
3
use std:: str:: FromStr ;
3
4
use url:: Url ;
4
5
use url:: ParseError as UrlError ;
@@ -72,6 +73,17 @@ impl FromStr for RequestUri {
72
73
}
73
74
}
74
75
76
+ impl Display for RequestUri {
77
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
78
+ match * self {
79
+ RequestUri :: AbsolutePath ( ref path) => f. write_str ( path) ,
80
+ RequestUri :: AbsoluteUri ( ref url) => write ! ( f, "{}" , url) ,
81
+ RequestUri :: Authority ( ref path) => f. write_str ( path) ,
82
+ RequestUri :: Star => f. write_str ( "*" )
83
+ }
84
+ }
85
+ }
86
+
75
87
#[ test]
76
88
fn test_uri_fromstr ( ) {
77
89
fn read ( s : & str , result : RequestUri ) {
@@ -83,3 +95,16 @@ fn test_uri_fromstr() {
83
95
read ( "hyper.rs" , RequestUri :: Authority ( "hyper.rs" . to_owned ( ) ) ) ;
84
96
read ( "/" , RequestUri :: AbsolutePath ( "/" . to_owned ( ) ) ) ;
85
97
}
98
+
99
+ #[ test]
100
+ fn test_uri_display ( ) {
101
+ fn assert_display ( expected_string : & str , request_uri : RequestUri ) {
102
+ assert_eq ! ( expected_string, format!( "{}" , request_uri) ) ;
103
+ }
104
+
105
+ assert_display ( "*" , RequestUri :: Star ) ;
106
+ assert_display ( "http://hyper.rs/" , RequestUri :: AbsoluteUri ( Url :: parse ( "http://hyper.rs/" ) . unwrap ( ) ) ) ;
107
+ assert_display ( "hyper.rs" , RequestUri :: Authority ( "hyper.rs" . to_owned ( ) ) ) ;
108
+ assert_display ( "/" , RequestUri :: AbsolutePath ( "/" . to_owned ( ) ) ) ;
109
+
110
+ }
0 commit comments