@@ -567,6 +567,122 @@ fn test_origin_unicode_serialization() {
567
567
}
568
568
}
569
569
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
613
+ . set_ip_host( "127.0.0.1" . parse( ) . unwrap( ) )
614
+ . is_err( ) ) ;
615
+
616
+ no_base_url. set_path ( "/foo" ) ;
617
+ assert_eq ! ( no_base_url. path( ) , "%2Ffoo" ) ;
618
+ }
619
+
620
+ #[ test]
621
+ fn test_domain ( ) {
622
+ let url = Url :: parse ( "https://127.0.0.1/" ) . unwrap ( ) ;
623
+ assert_eq ! ( url. domain( ) , None ) ;
624
+
625
+ let url = Url :: parse ( "mailto:test@example.net" ) . unwrap ( ) ;
626
+ assert_eq ! ( url. domain( ) , None ) ;
627
+
628
+ let url = Url :: parse ( "https://example.com/" ) . unwrap ( ) ;
629
+ assert_eq ! ( url. domain( ) , Some ( "example.com" ) ) ;
630
+ }
631
+
632
+ #[ test]
633
+ fn test_query ( ) {
634
+ let url = Url :: parse ( "https://example.com/products?page=2#fragment" ) . unwrap ( ) ;
635
+ assert_eq ! ( url. query( ) , Some ( "page=2" ) ) ;
636
+ assert_eq ! (
637
+ url. query_pairs( ) . next( ) ,
638
+ Some ( ( Cow :: Borrowed ( "page" ) , Cow :: Borrowed ( "2" ) ) )
639
+ ) ;
640
+
641
+ let url = Url :: parse ( "https://example.com/products" ) . unwrap ( ) ;
642
+ assert ! ( url. query( ) . is_none( ) ) ;
643
+ assert_eq ! ( url. query_pairs( ) . count( ) , 0 ) ;
644
+
645
+ let url = Url :: parse ( "https://example.com/?country=español" ) . unwrap ( ) ;
646
+ assert_eq ! ( url. query( ) , Some ( "country=espa%C3%B1ol" ) ) ;
647
+ assert_eq ! (
648
+ url. query_pairs( ) . next( ) ,
649
+ Some ( ( Cow :: Borrowed ( "country" ) , Cow :: Borrowed ( "español" ) ) )
650
+ ) ;
651
+
652
+ let url = Url :: parse ( "https://example.com/products?page=2&sort=desc" ) . unwrap ( ) ;
653
+ assert_eq ! ( url. query( ) , Some ( "page=2&sort=desc" ) ) ;
654
+ let mut pairs = url. query_pairs ( ) ;
655
+ assert_eq ! ( pairs. count( ) , 2 ) ;
656
+ assert_eq ! (
657
+ pairs. next( ) ,
658
+ Some ( ( Cow :: Borrowed ( "page" ) , Cow :: Borrowed ( "2" ) ) )
659
+ ) ;
660
+ assert_eq ! (
661
+ pairs. next( ) ,
662
+ Some ( ( Cow :: Borrowed ( "sort" ) , Cow :: Borrowed ( "desc" ) ) )
663
+ ) ;
664
+ }
665
+
666
+ #[ test]
667
+ fn test_fragment ( ) {
668
+ let url = Url :: parse ( "https://example.com/#fragment" ) . unwrap ( ) ;
669
+ assert_eq ! ( url. fragment( ) , Some ( "fragment" ) ) ;
670
+
671
+ let url = Url :: parse ( "https://example.com/" ) . unwrap ( ) ;
672
+ assert_eq ! ( url. fragment( ) , None ) ;
673
+ }
674
+
675
+ #[ test]
676
+ fn test_set_ip_host ( ) {
677
+ let mut url = Url :: parse ( "http://example.com" ) . unwrap ( ) ;
678
+
679
+ url. set_ip_host ( "127.0.0.1" . parse ( ) . unwrap ( ) ) . unwrap ( ) ;
680
+ assert_eq ! ( url. host_str( ) , Some ( "127.0.0.1" ) ) ;
681
+
682
+ url. set_ip_host ( "::1" . parse ( ) . unwrap ( ) ) . unwrap ( ) ;
683
+ assert_eq ! ( url. host_str( ) , Some ( "[::1]" ) ) ;
684
+ }
685
+
570
686
#[ test]
571
687
fn test_windows_unc_path ( ) {
572
688
if !cfg ! ( windows) {
0 commit comments