@@ -567,6 +567,120 @@ fn test_origin_unicode_serialization() {
567567 }
568568}
569569
570+ #[ test]
571+ fn test_socket_addrs ( ) {
572+ use std:: net:: ToSocketAddrs ;
573+
574+ let data = [
575+ ( "https://127.0.0.1/" , "127.0.0.1" , 443 ) ,
576+ ( "https://127.0.0.1:9742/" , "127.0.0.1" , 9742 ) ,
577+ ( "custom-protocol://127.0.0.1:9742/" , "127.0.0.1" , 9742 ) ,
578+ ( "custom-protocol://127.0.0.1/" , "127.0.0.1" , 9743 ) ,
579+ ( "https://[::1]/" , "::1" , 443 ) ,
580+ ( "https://[::1]:9742/" , "::1" , 9742 ) ,
581+ ( "custom-protocol://[::1]:9742/" , "::1" , 9742 ) ,
582+ ( "custom-protocol://[::1]/" , "::1" , 9743 ) ,
583+ ( "https://localhost/" , "localhost" , 443 ) ,
584+ ( "https://localhost:9742/" , "localhost" , 9742 ) ,
585+ ( "custom-protocol://localhost:9742/" , "localhost" , 9742 ) ,
586+ ( "custom-protocol://localhost/" , "localhost" , 9743 ) ,
587+ ] ;
588+
589+ for ( url_string, host, port) in & data {
590+ let url = url:: Url :: parse ( url_string) . unwrap ( ) ;
591+ let addrs = url
592+ . socket_addrs ( || match url. scheme ( ) {
593+ "custom-protocol" => Some ( 9743 ) ,
594+ _ => None ,
595+ } )
596+ . unwrap ( ) ;
597+ assert_eq ! (
598+ Some ( addrs[ 0 ] ) ,
599+ ( * host, * port) . to_socket_addrs( ) . unwrap( ) . next( )
600+ ) ;
601+ }
602+ }
603+
604+ #[ test]
605+ fn test_no_base_url ( ) {
606+ let mut no_base_url = Url :: parse ( "mailto:test@example.net" ) . unwrap ( ) ;
607+
608+ assert ! ( no_base_url. cannot_be_a_base( ) ) ;
609+ assert ! ( no_base_url. path_segments( ) . is_none( ) ) ;
610+ assert ! ( no_base_url. path_segments_mut( ) . is_err( ) ) ;
611+ assert ! ( no_base_url. set_host( Some ( "foo" ) ) . is_err( ) ) ;
612+ assert ! ( no_base_url. set_ip_host( "127.0.0.1" . parse( ) . unwrap( ) ) . is_err( ) ) ;
613+
614+ no_base_url. set_path ( "/foo" ) ;
615+ assert_eq ! ( no_base_url. path( ) , "%2Ffoo" ) ;
616+ }
617+
618+ #[ test]
619+ fn test_domain ( ) {
620+ let url = Url :: parse ( "https://127.0.0.1/" ) . unwrap ( ) ;
621+ assert_eq ! ( url. domain( ) , None ) ;
622+
623+ let url = Url :: parse ( "mailto:test@example.net" ) . unwrap ( ) ;
624+ assert_eq ! ( url. domain( ) , None ) ;
625+
626+ let url = Url :: parse ( "https://example.com/" ) . unwrap ( ) ;
627+ assert_eq ! ( url. domain( ) , Some ( "example.com" ) ) ;
628+ }
629+
630+ #[ test]
631+ fn test_query ( ) {
632+ let url = Url :: parse ( "https://example.com/products?page=2#fragment" ) . unwrap ( ) ;
633+ assert_eq ! ( url. query( ) , Some ( "page=2" ) ) ;
634+ assert_eq ! (
635+ url. query_pairs( ) . next( ) ,
636+ Some ( ( Cow :: Borrowed ( "page" ) , Cow :: Borrowed ( "2" ) ) )
637+ ) ;
638+
639+ let url = Url :: parse ( "https://example.com/products" ) . unwrap ( ) ;
640+ assert ! ( url. query( ) . is_none( ) ) ;
641+ assert_eq ! ( url. query_pairs( ) . count( ) , 0 ) ;
642+
643+ let url = Url :: parse ( "https://example.com/?country=español" ) . unwrap ( ) ;
644+ assert_eq ! ( url. query( ) , Some ( "country=espa%C3%B1ol" ) ) ;
645+ assert_eq ! (
646+ url. query_pairs( ) . next( ) ,
647+ Some ( ( Cow :: Borrowed ( "country" ) , Cow :: Borrowed ( "español" ) ) )
648+ ) ;
649+
650+ let url = Url :: parse ( "https://example.com/products?page=2&sort=desc" ) . unwrap ( ) ;
651+ assert_eq ! ( url. query( ) , Some ( "page=2&sort=desc" ) ) ;
652+ let mut pairs = url. query_pairs ( ) ;
653+ assert_eq ! ( pairs. count( ) , 2 ) ;
654+ assert_eq ! (
655+ pairs. next( ) ,
656+ Some ( ( Cow :: Borrowed ( "page" ) , Cow :: Borrowed ( "2" ) ) )
657+ ) ;
658+ assert_eq ! (
659+ pairs. next( ) ,
660+ Some ( ( Cow :: Borrowed ( "sort" ) , Cow :: Borrowed ( "desc" ) ) )
661+ ) ;
662+ }
663+
664+ #[ test]
665+ fn test_fragment ( ) {
666+ let url = Url :: parse ( "https://example.com/#fragment" ) . unwrap ( ) ;
667+ assert_eq ! ( url. fragment( ) , Some ( "fragment" ) ) ;
668+
669+ let url = Url :: parse ( "https://example.com/" ) . unwrap ( ) ;
670+ assert_eq ! ( url. fragment( ) , None ) ;
671+ }
672+
673+ #[ test]
674+ fn test_set_ip_host ( ) {
675+ let mut url = Url :: parse ( "http://example.com" ) . unwrap ( ) ;
676+
677+ url. set_ip_host ( "127.0.0.1" . parse ( ) . unwrap ( ) ) ;
678+ assert_eq ! ( url. host_str( ) , Some ( "127.0.0.1" ) ) ;
679+
680+ url. set_ip_host ( "::1" . parse ( ) . unwrap ( ) ) ;
681+ assert_eq ! ( url. host_str( ) , Some ( "[::1]" ) ) ;
682+ }
683+
570684#[ test]
571685fn test_windows_unc_path ( ) {
572686 if !cfg ! ( windows) {
0 commit comments