1
+ mod test_utils;
1
2
use async_std:: prelude:: * ;
2
3
use async_std:: task;
3
4
use std:: time:: Duration ;
@@ -8,20 +9,21 @@ use tide::{Request, Response, StatusCode};
8
9
#[ test]
9
10
fn hello_world ( ) -> Result < ( ) , http_types:: Error > {
10
11
task:: block_on ( async {
11
- let server = task:: spawn ( async {
12
+ let port = test_utils:: find_port ( ) . await ;
13
+ let server = task:: spawn ( async move {
12
14
let mut app = tide:: new ( ) ;
13
15
app. at ( "/" ) . get ( |mut req : Request < ( ) > | async move {
14
16
assert_eq ! ( req. body_string( ) . await . unwrap( ) , "nori" . to_string( ) ) ;
15
17
let res = Response :: new ( StatusCode :: Ok ) . body_string ( "says hello" . to_string ( ) ) ;
16
18
Ok ( res)
17
19
} ) ;
18
- app. listen ( "localhost:8080" ) . await ?;
20
+ app. listen ( & port ) . await ?;
19
21
Result :: < ( ) , http_types:: Error > :: Ok ( ( ) )
20
22
} ) ;
21
23
22
- let client = task:: spawn ( async {
24
+ let client = task:: spawn ( async move {
23
25
task:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
24
- let string = surf:: get ( "http://localhost:8080" )
26
+ let string = surf:: get ( format ! ( "http://{}" , port ) )
25
27
. body_string ( "nori" . to_string ( ) )
26
28
. recv_string ( )
27
29
. await ?;
@@ -36,17 +38,18 @@ fn hello_world() -> Result<(), http_types::Error> {
36
38
#[ test]
37
39
fn echo_server ( ) -> Result < ( ) , http_types:: Error > {
38
40
task:: block_on ( async {
39
- let server = task:: spawn ( async {
41
+ let port = test_utils:: find_port ( ) . await ;
42
+ let server = task:: spawn ( async move {
40
43
let mut app = tide:: new ( ) ;
41
44
app. at ( "/" ) . get ( |req| async move { Ok ( req) } ) ;
42
45
43
- app. listen ( "localhost:8081" ) . await ?;
46
+ app. listen ( & port ) . await ?;
44
47
Result :: < ( ) , http_types:: Error > :: Ok ( ( ) )
45
48
} ) ;
46
49
47
- let client = task:: spawn ( async {
50
+ let client = task:: spawn ( async move {
48
51
task:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
49
- let string = surf:: get ( "http://localhost:8081" )
52
+ let string = surf:: get ( format ! ( "http://{}" , port ) )
50
53
. body_string ( "chashu" . to_string ( ) )
51
54
. recv_string ( )
52
55
. await ?;
@@ -66,7 +69,8 @@ fn json() -> Result<(), http_types::Error> {
66
69
}
67
70
68
71
task:: block_on ( async {
69
- let server = task:: spawn ( async {
72
+ let port = test_utils:: find_port ( ) . await ;
73
+ let server = task:: spawn ( async move {
70
74
let mut app = tide:: new ( ) ;
71
75
app. at ( "/" ) . get ( |mut req : Request < ( ) > | async move {
72
76
let mut counter: Counter = req. body_json ( ) . await . unwrap ( ) ;
@@ -75,13 +79,13 @@ fn json() -> Result<(), http_types::Error> {
75
79
let res = Response :: new ( StatusCode :: Ok ) . body_json ( & counter) ?;
76
80
Ok ( res)
77
81
} ) ;
78
- app. listen ( "localhost:8082" ) . await ?;
82
+ app. listen ( & port ) . await ?;
79
83
Result :: < ( ) , http_types:: Error > :: Ok ( ( ) )
80
84
} ) ;
81
85
82
- let client = task:: spawn ( async {
86
+ let client = task:: spawn ( async move {
83
87
task:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
84
- let counter: Counter = surf:: get ( "http://localhost:8082" )
88
+ let counter: Counter = surf:: get ( format ! ( "http://{}" , & port ) )
85
89
. body_json ( & Counter { count : 0 } ) ?
86
90
. recv_json ( )
87
91
. await ?;
0 commit comments