1
1
use crate :: helpers:: spawn_app;
2
+ use wiremock:: matchers:: { method, path} ;
3
+ use wiremock:: { Mock , ResponseTemplate } ;
4
+
5
+ #[ tokio:: test]
6
+ async fn subscribe_sends_a_confirmation_email_for_valid_data ( ) {
7
+ let app = spawn_app ( ) . await ;
8
+ let body = "name=le%20guin&email=ursula_le_guin%40gmail.com" ;
9
+
10
+ Mock :: given ( path ( "/email" ) )
11
+ . and ( method ( "POST" ) )
12
+ . respond_with ( ResponseTemplate :: new ( 200 ) )
13
+ . expect ( 1 )
14
+ . mount ( & app. mock_server )
15
+ . await ;
16
+
17
+ let response = app. post_subscriptions ( body. into ( ) ) . await ;
18
+ assert_eq ! ( 200 , response. status( ) . as_u16( ) ) ;
19
+ }
2
20
3
21
#[ tokio:: test]
4
22
async fn subscribe_returns_a_200_for_valid_form_data ( ) {
5
23
// Arrange
6
24
let app_details = spawn_app ( ) . await ;
7
25
let connection = & app_details. db_pool ;
8
- let client = reqwest:: Client :: new ( ) ;
9
26
10
27
// Act
11
28
let body = "name=le%20guid&email=ursula_le_guin%40gmail.com" ;
12
- let response = client
13
- . post ( & format ! ( "{}/subscriptions" , & app_details. address) )
14
- . header ( "Content-Type" , "application/x-www-form-urlencoded" )
15
- . body ( body)
16
- . send ( )
17
- . await
18
- . expect ( "Failed to execute request." ) ;
29
+ let response = app_details. post_subscriptions ( body. into ( ) ) . await ;
19
30
// Assert
20
31
assert_eq ! ( 200 , response. status( ) . as_u16( ) ) ;
21
32
22
33
let saved = sqlx:: query!( "SELECT email, name FROM subscriptions" , )
23
34
. fetch_one ( connection)
24
35
. await
25
36
. expect ( "Failed to fetch saved subscription." ) ;
26
- println ! ( "saved: {:?}" , saved ) ;
37
+
27
38
assert_eq ! ( saved
. email
, "[email protected] " ) ;
28
39
assert_eq ! ( saved. name, "le guid" ) ;
29
40
}
@@ -32,21 +43,14 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
32
43
async fn subscribe_returns_a_400_when_data_is_missing ( ) {
33
44
// Arrange
34
45
let app_details = spawn_app ( ) . await ;
35
- let client = reqwest:: Client :: new ( ) ;
36
46
let test_cases = vec ! [
37
47
( "name=le%20guin" , "missing the email" ) ,
38
48
( "email=ursula_le_guin%40gmail.com" , "missing the name" ) ,
39
49
( "" , "missing both name and email" ) ,
40
50
] ;
41
51
for ( invalid_body, error_message) in test_cases {
42
52
// Act
43
- let response = client
44
- . post ( & format ! ( "{}/subscriptions" , & app_details. address) )
45
- . header ( "Content-Type" , "application/x-www-form-urlencoded" )
46
- . body ( invalid_body)
47
- . send ( )
48
- . await
49
- . expect ( "Failed to execute request." ) ;
53
+ let response = app_details. post_subscriptions ( invalid_body. into ( ) ) . await ;
50
54
// Assert
51
55
assert_eq ! (
52
56
400 ,
@@ -62,7 +66,6 @@ async fn subscribe_returns_a_400_when_data_is_missing() {
62
66
async fn subscribe_returns_a_400_when_fields_are_present_but_empty ( ) {
63
67
// Arrange
64
68
let app_details = spawn_app ( ) . await ;
65
- let client = reqwest:: Client :: new ( ) ;
66
69
let test_cases = vec ! [
67
70
( "name=&email=ursula_le_guin%40gmail.com" , "empty name" ) ,
68
71
( "name=le%20guin&email=" , "empty email" ) ,
@@ -73,13 +76,7 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_empty() {
73
76
] ;
74
77
for ( invalid_body, error_message) in test_cases {
75
78
// Act
76
- let response = client
77
- . post ( & format ! ( "{}/subscriptions" , & app_details. address) )
78
- . header ( "Content-Type" , "application/x-www-form-urlencoded" )
79
- . body ( invalid_body)
80
- . send ( )
81
- . await
82
- . expect ( "Failed to execute request." ) ;
79
+ let response = app_details. post_subscriptions ( invalid_body. into ( ) ) . await ;
83
80
// Assert
84
81
assert_eq ! (
85
82
400 ,
0 commit comments