Skip to content

Commit

Permalink
feat: TestResponse::assert_text_from_file (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephLenton authored Sep 23, 2024
1 parent e08bef3 commit 4f42d4f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "axum-test"
authors = ["Joseph Lenton <josephlenton@gmail.com>"]
version = "15.7.3"
version = "15.7.4"
rust-version = "1.75"
edition = "2021"
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions files/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello!
46 changes: 41 additions & 5 deletions src/test_response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::internals::RequestPathFormatter;
use crate::internals::StatusCodeFormatter;
use anyhow::Context;
use bytes::Bytes;
use cookie::Cookie;
Expand All @@ -13,16 +15,14 @@ use serde::de::DeserializeOwned;
use std::convert::AsRef;
use std::fmt::Debug;
use std::fmt::Display;
use std::fs::read_to_string;
use std::fs::File;
use std::io::BufReader;
use url::Url;

#[cfg(feature = "pretty-assertions")]
use pretty_assertions::{assert_eq, assert_ne};

use crate::internals::RequestPathFormatter;
use crate::internals::StatusCodeFormatter;

#[cfg(feature = "ws")]
use crate::internals::TestResponseWebSocket;
#[cfg(feature = "ws")]
Expand Down Expand Up @@ -727,6 +727,12 @@ impl TestResponse {
);
}

#[track_caller]
pub fn assert_text_from_file(&self, path: &str) {
let expected = read_to_string(path).unwrap();
self.assert_text(expected);
}

/// Deserializes the contents of the request as Json,
/// and asserts it matches the value given.
///
Expand Down Expand Up @@ -773,7 +779,7 @@ impl TestResponse {
pub fn assert_json_from_file(&self, path: &str) {
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
let expected: serde_json::Value = serde_json::from_reader(reader).unwrap();
let expected = serde_json::from_reader::<_, serde_json::Value>(reader).unwrap();
self.assert_json(&expected);
}

Expand All @@ -797,7 +803,7 @@ impl TestResponse {
pub fn assert_yaml_from_file(&self, path: &str) {
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
let expected: serde_yaml::Value = serde_yaml::from_reader(reader).unwrap();
let expected = serde_yaml::from_reader::<_, serde_yaml::Value>(reader).unwrap();
self.assert_yaml(&expected);
}

Expand Down Expand Up @@ -1491,6 +1497,36 @@ mod test_assert_text_contains {
}
}

#[cfg(test)]
mod test_assert_text_from_file {
use crate::TestServer;
use axum::routing::get;
use axum::routing::Router;

#[tokio::test]
async fn it_should_match_from_file() {
let app = Router::new().route(&"/text", get(|| async { "hello!" }));
let server = TestServer::new(app).unwrap();

server
.get(&"/text")
.await
.assert_text_from_file("files/example.txt");
}

#[tokio::test]
#[should_panic]
async fn it_should_panic_when_not_match_the_file() {
let app = Router::new().route(&"/text", get(|| async { "🦊" }));
let server = TestServer::new(app).unwrap();

server
.get(&"/text")
.await
.assert_text_from_file("files/example.txt");
}
}

#[cfg(test)]
mod test_assert_json {
use crate::TestServer;
Expand Down

0 comments on commit 4f42d4f

Please sign in to comment.