@@ -2,6 +2,22 @@ use reqwest::Client;
2
2
use robust_rust:: run;
3
3
use std:: net:: TcpListener ;
4
4
5
+ #[ allow( clippy:: let_underscore_future) ]
6
+ fn spawn_app ( ) -> String {
7
+ let listener = TcpListener :: bind ( "127.0.0.1:0" ) . expect ( "Failed to bind random port" ) ;
8
+
9
+ // Retrieve the port assigned to us by the OS
10
+ let port = listener. local_addr ( ) . unwrap ( ) . port ( ) ;
11
+
12
+ // Launch our application as a background task
13
+ let server = run ( listener) . expect ( "Failed to bind address" ) ;
14
+
15
+ let _ = tokio:: spawn ( server) ;
16
+
17
+ // Return the application address to the caller!
18
+ format ! ( "http://127.0.0.1:{}" , port)
19
+ }
20
+
5
21
#[ tokio:: test]
6
22
async fn health_check_works ( ) {
7
23
// Arrange
@@ -17,21 +33,53 @@ async fn health_check_works() {
17
33
. expect ( "Failed to execute request." ) ;
18
34
// Assert
19
35
assert ! ( response. status( ) . is_success( ) ) ;
20
- assert_eq ! ( Some ( 19 ) , response. content_length( ) ) ;
36
+ assert_eq ! ( Some ( 0 ) , response. content_length( ) ) ;
21
37
}
22
38
23
- #[ allow( clippy:: let_underscore_future) ]
24
- fn spawn_app ( ) -> String {
25
- let listener = TcpListener :: bind ( "127.0.0.1:0" ) . expect ( "Failed to bind random port" ) ;
26
-
27
- // Retrieve the port assigned to us by the OS
28
- let port = listener. local_addr ( ) . unwrap ( ) . port ( ) ;
29
-
30
- // Launch our application as a background task
31
- let server = run ( listener) . expect ( "Failed to bind address" ) ;
32
-
33
- let _ = tokio:: spawn ( server) ;
39
+ #[ tokio:: test]
40
+ async fn subscribe_returns_a_200_for_valid_form_data ( ) {
41
+ // Arrange
42
+ let app_address = spawn_app ( ) ;
43
+ let client = reqwest:: Client :: new ( ) ;
44
+ // Act
45
+ let body = "name=le%20guin&email=ursula_le_guin%40gmail.com" ;
46
+ let response = client
47
+ . post ( & format ! ( "{}/subscriptions" , & app_address) )
48
+ . header ( "Content-Type" , "application/x-www-form-urlencoded" )
49
+ . body ( body)
50
+ . send ( )
51
+ . await
52
+ . expect ( "Failed to execute request." ) ;
53
+ // Assert
54
+ assert_eq ! ( 200 , response. status( ) . as_u16( ) ) ;
55
+ }
34
56
35
- // Return the application address to the caller!
36
- format ! ( "http://127.0.0.1:{}" , port)
57
+ #[ tokio:: test]
58
+ async fn subscribe_returns_a_400_when_data_is_missing ( ) {
59
+ // Arrange
60
+ let app_address = spawn_app ( ) ;
61
+ let client = reqwest:: Client :: new ( ) ;
62
+ let test_cases = vec ! [
63
+ ( "name=le%20guin" , "missing the email" ) ,
64
+ ( "email=ursula_le_guin%40gmail.com" , "missing the name" ) ,
65
+ ( "" , "missing both name and email" ) ,
66
+ ] ;
67
+ for ( invalid_body, error_message) in test_cases {
68
+ // Act
69
+ let response = client
70
+ . post ( & format ! ( "{}/subscriptions" , & app_address) )
71
+ . header ( "Content-Type" , "application/x-www-form-urlencoded" )
72
+ . body ( invalid_body)
73
+ . send ( )
74
+ . await
75
+ . expect ( "Failed to execute request." ) ;
76
+ // Assert
77
+ assert_eq ! (
78
+ 400 ,
79
+ response. status( ) . as_u16( ) ,
80
+ // Additional customised error message on test failure
81
+ "The API did not fail with 400 Bad Request when the payload was {}." ,
82
+ error_message
83
+ ) ;
84
+ }
37
85
}
0 commit comments