@@ -90,89 +90,156 @@ mod tests {
9090 use super :: * ;
9191
9292 #[ test]
93- fn next_path_segment_empty ( ) {
94- let uri = Uri :: from_static ( "/" ) ;
93+ fn next_path_segment_skips_empty_segments ( ) {
94+ let uri = Uri :: from_static ( "///a//b/c/// " ) ;
9595 let mut parser = WellKnownUriParser :: new ( & uri) ;
96- assert_eq ! ( parser. next_path_segment( ) , None ) ;
97- }
98-
99- #[ test]
100- fn next_path_segment_single ( ) {
101- let uri = Uri :: from_static ( "/test/" ) ;
102- let mut parser = WellKnownUriParser :: new ( & uri) ;
103- assert_eq ! ( parser. next_path_segment( ) , Some ( "test" ) ) ;
104- assert_eq ! ( parser. next_path_segment( ) , None ) ;
105- }
10696
107- #[ test]
108- fn next_path_segment_multiple ( ) {
109- let uri = Uri :: from_static ( "/a/b/c/" ) ;
110- let mut parser = WellKnownUriParser :: new ( & uri) ;
11197 assert_eq ! ( parser. next_path_segment( ) , Some ( "a" ) ) ;
11298 assert_eq ! ( parser. next_path_segment( ) , Some ( "b" ) ) ;
11399 assert_eq ! ( parser. next_path_segment( ) , Some ( "c" ) ) ;
114100 assert_eq ! ( parser. next_path_segment( ) , None ) ;
115101 }
116102
117103 #[ test]
118- fn next_path_segment_with_leading_slash ( ) {
119- let uri = Uri :: from_static ( "//a/b/" ) ;
120- let mut parser = WellKnownUriParser :: new ( & uri) ;
121- assert_eq ! ( parser. next_path_segment( ) , Some ( "a" ) ) ;
122- assert_eq ! ( parser. next_path_segment( ) , Some ( "b" ) ) ;
123- assert_eq ! ( parser. next_path_segment( ) , None ) ;
104+ fn parse_returns_none_for_non_well_known_paths ( ) {
105+ for uri in [
106+ Uri :: default ( ) ,
107+ Uri :: from_static ( "/" ) ,
108+ Uri :: from_static ( "///" ) ,
109+ Uri :: from_static ( "/other-path" ) ,
110+ Uri :: from_static ( "/.well-known/" ) ,
111+ ] {
112+ assert ! ( WellKnownUri :: parse( & uri) . unwrap( ) . is_none( ) ) ;
113+ }
124114 }
125115
126116 #[ test]
127- fn next_path_segment_only_slashes ( ) {
128- let uri = Uri :: from_static ( "///" ) ;
129- let mut parser = WellKnownUriParser :: new ( & uri) ;
130- assert_eq ! ( parser. next_path_segment( ) , None ) ;
117+ fn parse_unsupported_well_known_name ( ) {
118+ let uri = Uri :: from_static ( "/.well-known/unknown/value" ) ;
119+ let result = WellKnownUri :: parse ( & uri) . unwrap ( ) . unwrap ( ) ;
120+ let WellKnownUri :: Unsupported ( suffix) = result else {
121+ panic ! ( "not parsed as unsupported" )
122+ } ;
123+
124+ assert_eq ! ( suffix, "unknown" ) ;
131125 }
132126
133127 #[ test]
134- fn parse_empty_uri ( ) {
135- let uri = Uri :: default ( ) ;
136- assert ! ( WellKnownUri :: parse( & uri) . unwrap( ) . is_none( ) ) ;
128+ fn parse_easy_proxy_dispatch ( ) {
129+ let uri = Uri :: from_static ( "/.well-known/easy-proxy/http/target.example/8080/path?q=1" ) ;
130+ let result = WellKnownUri :: parse ( & uri) . unwrap ( ) . unwrap ( ) ;
131+ let WellKnownUri :: EasyProxy ( protocol, target, target_uri) = result else {
132+ panic ! ( "not parsed as easy-proxy" )
133+ } ;
134+
135+ assert_eq ! ( protocol, HttpProxySubProtocol :: HttpForward ) ;
136+ assert_eq ! ( target. host_str( ) , "target.example" ) ;
137+ assert_eq ! ( target. port( ) , 8080 ) ;
138+ assert_eq ! (
139+ target_uri,
140+ Uri :: from_static( "http://target.example:8080/path?q=1" )
141+ ) ;
137142 }
138143
139144 #[ test]
140- fn parse_non_well_known ( ) {
141- let uri = Uri :: from_static ( "/other-path" ) ;
142- assert ! ( WellKnownUri :: parse( & uri) . unwrap( ) . is_none( ) ) ;
145+ fn parse_easy_proxy_ipv6_dispatch ( ) {
146+ let uri = Uri :: from_static ( "/.well-known/easy-proxy/https/2001%3Adb8%3A%3A2/8443/api" ) ;
147+ let result = WellKnownUri :: parse ( & uri) . unwrap ( ) . unwrap ( ) ;
148+ let WellKnownUri :: EasyProxy ( protocol, target, target_uri) = result else {
149+ panic ! ( "not parsed as easy-proxy" )
150+ } ;
151+
152+ assert_eq ! ( protocol, HttpProxySubProtocol :: HttpsForward ) ;
153+ assert_eq ! ( target. host_str( ) , "2001:db8::2" ) ;
154+ assert_eq ! ( target. port( ) , 8443 ) ;
155+ assert_eq ! (
156+ target_uri,
157+ Uri :: from_static( "https://[2001:db8::2]:8443/api" )
158+ ) ;
143159 }
144160
145161 #[ test]
146- fn parse_missing_protocol ( ) {
147- let uri = Uri :: from_static ( "/.well-known/" ) ;
148- assert ! ( WellKnownUri :: parse( & uri) . unwrap( ) . is_none( ) ) ;
162+ fn parse_masque_udp_dispatch ( ) {
163+ let uri = Uri :: from_static ( "/.well-known/masque/udp/192.0.2.1/53" ) ;
164+ let result = WellKnownUri :: parse ( & uri) . unwrap ( ) . unwrap ( ) ;
165+ let WellKnownUri :: Masque ( HttpMasque :: Udp ( addr) ) = result else {
166+ panic ! ( "not parsed as masque udp" )
167+ } ;
168+
169+ assert_eq ! ( addr. host_str( ) , "192.0.2.1" ) ;
170+ assert_eq ! ( addr. port( ) , 53 ) ;
149171 }
150172
151173 #[ test]
152- fn parse_unsupported_protocol ( ) {
153- let uri = Uri :: from_static ( "/.well-known/unknown " ) ;
174+ fn parse_masque_udp_ipv6_dispatch ( ) {
175+ let uri = Uri :: from_static ( "/.well-known/masque/udp/2001%3Adb8%3A%3A3/53 " ) ;
154176 let result = WellKnownUri :: parse ( & uri) . unwrap ( ) . unwrap ( ) ;
155- assert_eq ! ( result. suffix( ) , "unknown" )
177+ let WellKnownUri :: Masque ( HttpMasque :: Udp ( addr) ) = result else {
178+ panic ! ( "not parsed as masque udp" )
179+ } ;
180+
181+ assert_eq ! ( addr. host_str( ) , "2001:db8::3" ) ;
182+ assert_eq ! ( addr. port( ) , 53 ) ;
156183 }
157184
158185 #[ test]
159- fn parse_easy_proxy ( ) {
160- let uri = Uri :: from_static ( "/.well-known/easy-proxy/http/target .com/80/path?query=1 " ) ;
186+ fn parse_masque_ip_dispatch ( ) {
187+ let uri = Uri :: from_static ( "/.well-known/masque/ip/example .com/17 " ) ;
161188 let result = WellKnownUri :: parse ( & uri) . unwrap ( ) . unwrap ( ) ;
162- assert_eq ! ( result. suffix( ) , "easy-proxy" ) ;
189+ let WellKnownUri :: Masque ( HttpMasque :: Ip ( host, proto) ) = result else {
190+ panic ! ( "not parsed as masque ip" )
191+ } ;
192+
193+ assert_eq ! ( host. unwrap( ) . to_string( ) , "example.com" ) ;
194+ assert_eq ! ( proto, Some ( 17 ) ) ;
163195 }
164196
165197 #[ test]
166- fn parse_masque_udp ( ) {
167- let uri = Uri :: from_static ( "/.well-known/masque/udp/192.0.2.1/53 " ) ;
198+ fn parse_masque_ip_ipv6_dispatch ( ) {
199+ let uri = Uri :: from_static ( "/.well-known/masque/ip/2001%3Adb8%3A%3A4/17 " ) ;
168200 let result = WellKnownUri :: parse ( & uri) . unwrap ( ) . unwrap ( ) ;
169- assert_eq ! ( result. suffix( ) , "masque" ) ;
201+ let WellKnownUri :: Masque ( HttpMasque :: Ip ( host, proto) ) = result else {
202+ panic ! ( "not parsed as masque ip" )
203+ } ;
204+
205+ assert_eq ! ( host. unwrap( ) . to_string( ) , "2001:db8::4" ) ;
206+ assert_eq ! ( proto, Some ( 17 ) ) ;
170207 }
171208
172209 #[ test]
173- fn parse_error_invalid_masque_segment ( ) {
174- let uri = Uri :: from_static ( "/.well-known/masque/invalid " ) ;
210+ fn parse_unsupported_masque_segment ( ) {
211+ let uri = Uri :: from_static ( "/.well-known/masque/unknown-protocol " ) ;
175212 let result = WellKnownUri :: parse ( & uri) . unwrap ( ) . unwrap ( ) ;
176- assert_eq ! ( result. suffix( ) , "masque/invalid" )
213+ let WellKnownUri :: Unsupported ( suffix) = result else {
214+ panic ! ( "not parsed as unsupported" )
215+ } ;
216+
217+ assert_eq ! ( suffix, "masque/unknown-protocol" ) ;
218+ assert_eq ! (
219+ WellKnownUri :: Unsupported ( suffix) . suffix( ) ,
220+ "masque/unknown-protocol"
221+ ) ;
222+ }
223+
224+ #[ test]
225+ fn parse_propagates_easy_proxy_error ( ) {
226+ let uri = Uri :: from_static ( "/.well-known/easy-proxy/" ) ;
227+ let err = WellKnownUri :: parse ( & uri) . unwrap_err ( ) ;
228+
229+ assert ! ( matches!(
230+ err,
231+ UriParseError :: RequiredFieldNotFound ( "scheme" )
232+ ) ) ;
233+ }
234+
235+ #[ test]
236+ fn parse_propagates_masque_error ( ) {
237+ let uri = Uri :: from_static ( "/.well-known/masque/" ) ;
238+ let err = WellKnownUri :: parse ( & uri) . unwrap_err ( ) ;
239+
240+ assert ! ( matches!(
241+ err,
242+ UriParseError :: RequiredFieldNotFound ( "segment" )
243+ ) ) ;
177244 }
178245}
0 commit comments