-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
412ef3c
commit c775aea
Showing
7 changed files
with
168 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{Request, StatusCode}, | ||
}; | ||
use axum::http::StatusCode; | ||
use sqlx::{Pool, Postgres}; | ||
use tower::ServiceExt; // for `call`, `oneshot`, and `ready` | ||
|
||
#[sqlx::test] | ||
async fn index(pool: Pool<Postgres>) -> anyhow::Result<()> { | ||
let app = crate::server::app(pool).await.unwrap(); | ||
use super::util::TestApp; | ||
|
||
let response = app | ||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap()) | ||
.await | ||
.unwrap(); | ||
#[test_log::test(sqlx::test)] | ||
async fn index(pool: Pool<Postgres>) -> anyhow::Result<()> { | ||
let mut app = TestApp::new(pool).await; | ||
|
||
assert_eq!(response.status(), StatusCode::SEE_OTHER); | ||
app.req() | ||
.expect_status(StatusCode::SEE_OTHER) | ||
.get("/") | ||
.await; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
//! for information on why our tests are inside the `src` folder. | ||
mod index; | ||
mod users; | ||
mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use axum::Router; | ||
use sqlx::{Pool, Postgres}; | ||
|
||
use crate::server::app; | ||
|
||
use self::request_builder::RequestBuilder; | ||
|
||
pub mod request_builder; | ||
|
||
pub struct TestApp { | ||
router: Router, | ||
} | ||
|
||
impl TestApp { | ||
pub async fn new(pool: Pool<Postgres>) -> Self { | ||
TestApp { | ||
router: app(pool).await.unwrap(), | ||
} | ||
} | ||
pub fn req(&mut self) -> RequestBuilder { | ||
RequestBuilder::new(&mut self.router) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use askama_axum::IntoResponse; | ||
use axum::{ | ||
body::Body, | ||
http::{self, HeaderMap, Request, Response, StatusCode}, | ||
Form, Router, | ||
}; | ||
use http_body_util::BodyExt; | ||
use mime_guess::mime; | ||
use serde::Serialize; | ||
use tower::{Service, ServiceExt}; | ||
use visdom::Vis; | ||
|
||
pub struct RequestBuilder<'app> { | ||
router: &'app mut axum::Router, | ||
/// This is the HTTP status that we expect the backend to return. | ||
/// If it returns a different status, we'll panic. | ||
expected_status: StatusCode, | ||
} | ||
|
||
impl<'app> RequestBuilder<'app> { | ||
pub fn new(router: &'app mut Router) -> Self { | ||
RequestBuilder { | ||
router: router, | ||
expected_status: StatusCode::OK, | ||
} | ||
} | ||
|
||
pub fn expect_status(mut self, expected: StatusCode) -> Self { | ||
self.expected_status = expected; | ||
self | ||
} | ||
|
||
pub async fn post<Input>(mut self, url: &str, input: &Input) -> TestResponse | ||
where | ||
Input: Serialize, | ||
{ | ||
let request = Request::builder() | ||
.method(http::Method::POST) | ||
.uri(url) | ||
.header( | ||
http::header::CONTENT_TYPE, | ||
mime::APPLICATION_WWW_FORM_URLENCODED.as_ref(), | ||
) | ||
.body(Form(input).into_response().into_body()) | ||
.unwrap(); | ||
|
||
let response = ServiceExt::<Request<Body>>::ready(&mut self.router) | ||
.await | ||
.unwrap() | ||
.call(request) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(response.status(), self.expected_status); | ||
|
||
TestResponse { response } | ||
} | ||
|
||
pub async fn get(mut self, url: &str) -> TestResponse { | ||
let request = Request::builder().uri(url).body(Body::empty()).unwrap(); | ||
|
||
let response = ServiceExt::<Request<Body>>::ready(&mut self.router) | ||
.await | ||
.unwrap() | ||
.call(request) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(response.status(), self.expected_status); | ||
TestResponse { response: response } | ||
} | ||
} | ||
|
||
pub struct TestResponse { | ||
response: Response<Body>, | ||
} | ||
|
||
impl TestResponse { | ||
pub async fn dom(self) -> visdom::types::Elements<'static> { | ||
let body = self | ||
.response | ||
.into_body() | ||
.collect() | ||
.await | ||
.unwrap() | ||
.to_bytes() | ||
.to_vec(); | ||
Vis::load(String::from_utf8(body).unwrap()).unwrap() | ||
} | ||
|
||
pub fn headers(&self) -> &HeaderMap { | ||
self.response.headers() | ||
} | ||
} |