mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2025-01-31 11:02:29 +00:00
Do not allocate.
Unhappy path tests.
This commit is contained in:
parent
8fe02f6c42
commit
391f29de3d
1 changed files with 67 additions and 13 deletions
|
@ -27,11 +27,11 @@ impl EmailClient {
|
|||
) -> Result<(), reqwest::Error> {
|
||||
let url = format!("{}/email", self.base_url);
|
||||
let request_body = SendEmailRequest {
|
||||
from: self.sender.as_ref().to_owned(),
|
||||
to: recipient.as_ref().to_owned(),
|
||||
subject: subject.into(),
|
||||
html_body: html_content.into(),
|
||||
text_body: text_content.into(),
|
||||
from: self.sender.as_ref(),
|
||||
to: recipient.as_ref(),
|
||||
subject,
|
||||
html_body: html_content,
|
||||
text_body: text_content,
|
||||
};
|
||||
self.http_client
|
||||
.post(&url)
|
||||
|
@ -45,22 +45,23 @@ impl EmailClient {
|
|||
|
||||
#[derive(serde::Serialize)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
struct SendEmailRequest {
|
||||
from: String,
|
||||
to: String,
|
||||
subject: String,
|
||||
html_body: String,
|
||||
text_body: String,
|
||||
struct SendEmailRequest<'a> {
|
||||
from: &'a str,
|
||||
to: &'a str,
|
||||
subject: &'a str,
|
||||
html_body: &'a str,
|
||||
text_body: &'a str,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::domain::SubscriberEmail;
|
||||
use crate::email_client::EmailClient;
|
||||
use claim::{assert_err, assert_ok};
|
||||
use fake::faker::internet::en::SafeEmail;
|
||||
use fake::faker::lorem::en::{Paragraph, Sentence};
|
||||
use fake::{Fake, Faker};
|
||||
use wiremock::matchers::{header, header_exists, method, path};
|
||||
use wiremock::matchers::{any, header, header_exists, method, path};
|
||||
use wiremock::{Mock, MockServer, Request, ResponseTemplate};
|
||||
|
||||
struct SendEmailBodyMatcher;
|
||||
|
@ -81,7 +82,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn send_email_fires_a_request_to_base_url() {
|
||||
async fn send_email_sends_the_expected_request() {
|
||||
// Arrange
|
||||
let mock_server = MockServer::start().await;
|
||||
let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
|
||||
|
@ -108,4 +109,57 @@ mod tests {
|
|||
|
||||
// Assert
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn send_email_succeeds_if_the_server_returns_200() {
|
||||
// Arrange
|
||||
let mock_server = MockServer::start().await;
|
||||
let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
|
||||
let email_client = EmailClient::new(mock_server.uri(), sender, Faker.fake());
|
||||
|
||||
let subscriber_email = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
|
||||
let subject: String = Sentence(1..2).fake();
|
||||
let content: String = Paragraph(1..10).fake();
|
||||
|
||||
Mock::given(any())
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&mock_server)
|
||||
.await;
|
||||
|
||||
// Act
|
||||
let outcome = email_client
|
||||
.send_email(subscriber_email, &subject, &content, &content)
|
||||
.await;
|
||||
|
||||
// Assert
|
||||
assert_ok!(outcome);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn send_email_fails_if_the_server_returns_500() {
|
||||
// Arrange
|
||||
let mock_server = MockServer::start().await;
|
||||
let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
|
||||
let email_client = EmailClient::new(mock_server.uri(), sender, Faker.fake());
|
||||
|
||||
let subscriber_email = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
|
||||
let subject: String = Sentence(1..2).fake();
|
||||
let content: String = Paragraph(1..10).fake();
|
||||
|
||||
Mock::given(any())
|
||||
// Not a 200 anymore!
|
||||
.respond_with(ResponseTemplate::new(500))
|
||||
.expect(1)
|
||||
.mount(&mock_server)
|
||||
.await;
|
||||
|
||||
// Act
|
||||
let outcome = email_client
|
||||
.send_email(subscriber_email, &subject, &content, &content)
|
||||
.await;
|
||||
|
||||
// Assert
|
||||
assert_err!(outcome);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue