@@ -551,22 +551,27 @@ impl HttpServer {
551
551
#[ cfg( test) ]
552
552
mod tests {
553
553
use super :: * ;
554
- use std:: fs;
555
554
use std:: io:: { Read , Write } ;
556
555
use std:: os:: unix:: net:: UnixStream ;
557
556
558
557
use common:: Body ;
558
+ use utils:: tempfile:: TempFile ;
559
+
560
+ fn get_path_to_socket ( ) -> TempFile {
561
+ let mut path_to_socket = TempFile :: new ( ) . unwrap ( ) ;
562
+ path_to_socket. remove ( ) . unwrap ( ) ;
563
+ path_to_socket
564
+ }
559
565
560
566
#[ test]
561
567
fn test_wait_one_connection ( ) {
562
- let path_to_socket = "/tmp/test_socket_http_server1.sock" ;
563
- fs:: remove_file ( path_to_socket) . unwrap_or_default ( ) ;
568
+ let path_to_socket = get_path_to_socket ( ) ;
564
569
565
- let mut server = HttpServer :: new ( path_to_socket. to_string ( ) ) . unwrap ( ) ;
570
+ let mut server = HttpServer :: new ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
566
571
server. start_server ( ) . unwrap ( ) ;
567
572
568
573
// Test one incoming connection.
569
- let mut socket = UnixStream :: connect ( path_to_socket) . unwrap ( ) ;
574
+ let mut socket = UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
570
575
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
571
576
572
577
socket
@@ -592,19 +597,17 @@ mod tests {
592
597
593
598
let mut buf: [ u8 ; 1024 ] = [ 0 ; 1024 ] ;
594
599
assert ! ( socket. read( & mut buf[ ..] ) . unwrap( ) > 0 ) ;
595
- fs:: remove_file ( path_to_socket) . unwrap ( ) ;
596
600
}
597
601
598
602
#[ test]
599
603
fn test_wait_concurrent_connections ( ) {
600
- let path_to_socket = "/tmp/test_socket_http_server2.sock" ;
601
- fs:: remove_file ( path_to_socket) . unwrap_or_default ( ) ;
604
+ let path_to_socket = get_path_to_socket ( ) ;
602
605
603
- let mut server = HttpServer :: new ( path_to_socket. to_string ( ) ) . unwrap ( ) ;
606
+ let mut server = HttpServer :: new ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
604
607
server. start_server ( ) . unwrap ( ) ;
605
608
606
609
// Test two concurrent connections.
607
- let mut first_socket = UnixStream :: connect ( path_to_socket) . unwrap ( ) ;
610
+ let mut first_socket = UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
608
611
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
609
612
610
613
first_socket
@@ -614,7 +617,7 @@ mod tests {
614
617
Content-Type: application/json\r \n \r \n whatever body",
615
618
)
616
619
. unwrap ( ) ;
617
- let mut second_socket = UnixStream :: connect ( path_to_socket) . unwrap ( ) ;
620
+ let mut second_socket = UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
618
621
619
622
let mut req_vec = server. requests ( ) . unwrap ( ) ;
620
623
let server_request = req_vec. remove ( 0 ) ;
@@ -666,19 +669,17 @@ mod tests {
666
669
assert ! ( second_socket. read( & mut buf[ ..] ) . unwrap( ) > 0 ) ;
667
670
second_socket. shutdown ( std:: net:: Shutdown :: Both ) . unwrap ( ) ;
668
671
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
669
- fs:: remove_file ( path_to_socket) . unwrap ( ) ;
670
672
}
671
673
672
674
#[ test]
673
675
fn test_wait_expect_connection ( ) {
674
- let path_to_socket = "/tmp/test_socket_http_server3.sock" ;
675
- fs:: remove_file ( path_to_socket) . unwrap_or_default ( ) ;
676
+ let path_to_socket = get_path_to_socket ( ) ;
676
677
677
- let mut server = HttpServer :: new ( path_to_socket. to_string ( ) ) . unwrap ( ) ;
678
+ let mut server = HttpServer :: new ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
678
679
server. start_server ( ) . unwrap ( ) ;
679
680
680
681
// Test one incoming connection with `Expect: 100-continue`.
681
- let mut socket = UnixStream :: connect ( path_to_socket) . unwrap ( ) ;
682
+ let mut socket = UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
682
683
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
683
684
684
685
socket
@@ -717,42 +718,37 @@ mod tests {
717
718
718
719
let mut buf: [ u8 ; 1024 ] = [ 0 ; 1024 ] ;
719
720
assert ! ( socket. read( & mut buf[ ..] ) . unwrap( ) > 0 ) ;
720
- fs:: remove_file ( path_to_socket) . unwrap ( ) ;
721
721
}
722
722
723
723
#[ test]
724
724
fn test_wait_many_connections ( ) {
725
- let path_to_socket = "/tmp/test_socket_http_server4.sock" ;
726
- fs:: remove_file ( path_to_socket) . unwrap_or_default ( ) ;
725
+ let path_to_socket = get_path_to_socket ( ) ;
727
726
728
- let mut server = HttpServer :: new ( path_to_socket. to_string ( ) ) . unwrap ( ) ;
727
+ let mut server = HttpServer :: new ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
729
728
server. start_server ( ) . unwrap ( ) ;
730
729
731
730
let mut sockets: Vec < UnixStream > = Vec :: with_capacity ( 11 ) ;
732
731
for _ in 0 ..MAX_CONNECTIONS {
733
- sockets. push ( UnixStream :: connect ( path_to_socket) . unwrap ( ) ) ;
732
+ sockets. push ( UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ) ;
734
733
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
735
734
}
736
735
737
- sockets. push ( UnixStream :: connect ( path_to_socket) . unwrap ( ) ) ;
736
+ sockets. push ( UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ) ;
738
737
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
739
738
let mut buf: [ u8 ; 120 ] = [ 0 ; 120 ] ;
740
739
sockets[ MAX_CONNECTIONS ] . read_exact ( & mut buf) . unwrap ( ) ;
741
740
assert_eq ! ( & buf[ ..] , SERVER_FULL_ERROR_MESSAGE ) ;
742
-
743
- fs:: remove_file ( path_to_socket) . unwrap ( ) ;
744
741
}
745
742
746
743
#[ test]
747
744
fn test_wait_parse_error ( ) {
748
- let path_to_socket = "/tmp/test_socket_http_server5.sock" ;
749
- fs:: remove_file ( path_to_socket) . unwrap_or_default ( ) ;
745
+ let path_to_socket = get_path_to_socket ( ) ;
750
746
751
- let mut server = HttpServer :: new ( path_to_socket. to_string ( ) ) . unwrap ( ) ;
747
+ let mut server = HttpServer :: new ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
752
748
server. start_server ( ) . unwrap ( ) ;
753
749
754
750
// Test one incoming connection.
755
- let mut socket = UnixStream :: connect ( path_to_socket) . unwrap ( ) ;
751
+ let mut socket = UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
756
752
socket. set_nonblocking ( true ) . unwrap ( ) ;
757
753
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
758
754
@@ -775,22 +771,19 @@ mod tests {
775
771
Content-Length: 80\r \n \r \n { \" error\" : \" Invalid header.\n \
776
772
All previous unanswered requests will be dropped.\" }";
777
773
assert_eq ! ( & buf[ ..] , & error_message[ ..] ) ;
778
-
779
- fs:: remove_file ( path_to_socket) . unwrap ( ) ;
780
774
}
781
775
782
776
#[ test]
783
777
fn test_wait_in_flight_responses ( ) {
784
- let path_to_socket = "/tmp/test_socket_http_server6.sock" ;
785
- fs:: remove_file ( path_to_socket) . unwrap_or_default ( ) ;
778
+ let path_to_socket = get_path_to_socket ( ) ;
786
779
787
- let mut server = HttpServer :: new ( path_to_socket. to_string ( ) ) . unwrap ( ) ;
780
+ let mut server = HttpServer :: new ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
788
781
server. start_server ( ) . unwrap ( ) ;
789
782
790
783
// Test a connection dropped and then a new one appearing
791
784
// before the user had a chance to send the response to the
792
785
// first one.
793
- let mut first_socket = UnixStream :: connect ( path_to_socket) . unwrap ( ) ;
786
+ let mut first_socket = UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
794
787
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
795
788
796
789
first_socket
@@ -806,7 +799,7 @@ mod tests {
806
799
807
800
first_socket. shutdown ( std:: net:: Shutdown :: Both ) . unwrap ( ) ;
808
801
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
809
- let mut second_socket = UnixStream :: connect ( path_to_socket) . unwrap ( ) ;
802
+ let mut second_socket = UnixStream :: connect ( path_to_socket. as_path ( ) ) . unwrap ( ) ;
810
803
second_socket. set_nonblocking ( true ) . unwrap ( ) ;
811
804
assert ! ( server. requests( ) . unwrap( ) . is_empty( ) ) ;
812
805
@@ -858,6 +851,5 @@ mod tests {
858
851
assert ! ( second_socket. read( & mut buf[ ..] ) . unwrap( ) > 0 ) ;
859
852
second_socket. shutdown ( std:: net:: Shutdown :: Both ) . unwrap ( ) ;
860
853
assert ! ( server. requests( ) . is_ok( ) ) ;
861
- fs:: remove_file ( path_to_socket) . unwrap ( ) ;
862
854
}
863
855
}
0 commit comments