added endpoint client
This commit is contained in:
@ -8,6 +8,18 @@ pub struct TestApp {
|
|||||||
pub connection_pool: Pool<Postgres>,
|
pub connection_pool: Pool<Postgres>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TestApp {
|
||||||
|
pub async fn post_subscriptions(&self, body: String) -> reqwest::Response {
|
||||||
|
reqwest::Client::new()
|
||||||
|
.post(&format!("{}/subscriptions", &self.address))
|
||||||
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
.body(body)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static TRACING: Lazy<()> = Lazy::new(|| {
|
static TRACING: Lazy<()> = Lazy::new(|| {
|
||||||
let default_filter_level = "info".to_string();
|
let default_filter_level = "info".to_string();
|
||||||
let subscriber_name = "test".to_string();
|
let subscriber_name = "test".to_string();
|
||||||
|
|||||||
@ -4,16 +4,7 @@ use sqlx::query;
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn subscribe_returns_a_200_for_valid_form_data() {
|
async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||||
let app = spawn_app().await;
|
let app = spawn_app().await;
|
||||||
let client = reqwest::Client::new();
|
let response = app.post_subscriptions("name=andre&email=andre.heber@gmx.net".to_string()).await;
|
||||||
|
|
||||||
let body = "name=andre&email=andre.heber@gmx.net";
|
|
||||||
let response = client
|
|
||||||
.post(&format!("{}/subscriptions", &app.address))
|
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
.body(body)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
assert_eq!(200, response.status().as_u16());
|
assert_eq!(200, response.status().as_u16());
|
||||||
|
|
||||||
@ -28,22 +19,15 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn subscribe_returns_a_400_when_data_is_missing() {
|
async fn subscribe_returns_a_400_when_data_is_missing() {
|
||||||
let app = spawn_app().await;
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let test_cases = vec![
|
let test_cases = vec![
|
||||||
("name=le%20guin", "missing the email"),
|
("name=le%20guin", "missing the email"),
|
||||||
("email=ursula_le_guin%40gmail.com", "missing the name"),
|
("email=ursula_le_guin%40gmail.com", "missing the name"),
|
||||||
("", "missing both name and email"),
|
("", "missing both name and email"),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
let app = spawn_app().await;
|
||||||
for (invalid_body, error_message) in test_cases {
|
for (invalid_body, error_message) in test_cases {
|
||||||
let response = client
|
let response = app.post_subscriptions(invalid_body.to_string()).await;
|
||||||
.post(&format!("{}/subscriptions", &app.address))
|
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
.body(invalid_body)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
400,
|
400,
|
||||||
@ -56,8 +40,6 @@ async fn subscribe_returns_a_400_when_data_is_missing() {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
||||||
let app = spawn_app().await;
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let test_cases = vec![
|
let test_cases = vec![
|
||||||
("name=&email=ursula_le_guin%40gmail.com", "name is empty"),
|
("name=&email=ursula_le_guin%40gmail.com", "name is empty"),
|
||||||
("name=le%20guin&email=", "email is empty"),
|
("name=le%20guin&email=", "email is empty"),
|
||||||
@ -65,14 +47,9 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
|||||||
("name=Ursula&email=definitely-not-an-email", "invalid email"),
|
("name=Ursula&email=definitely-not-an-email", "invalid email"),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
let app = spawn_app().await;
|
||||||
for (invalid_body, error_message) in test_cases {
|
for (invalid_body, error_message) in test_cases {
|
||||||
let response = client
|
let response = app.post_subscriptions(invalid_body.to_string()).await;
|
||||||
.post(&format!("{}/subscriptions", &app.address))
|
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
.body(invalid_body)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.expect("Failed to execute request.");
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
400,
|
400,
|
||||||
|
|||||||
Reference in New Issue
Block a user