|
| 1 | +//! Tests for the transform module. |
| 2 | +
|
| 3 | +use heph::test; |
| 4 | +use heph_http::body::OneshotBody; |
| 5 | +use heph_http::handler::{Handler, Middleware}; |
| 6 | +use heph_http::transform::{Body, Cloned, Path, TransformMiddleware}; |
| 7 | +use heph_http::{Header, HeaderName, Headers, Method, Request, Response, StatusCode, Version}; |
| 8 | + |
| 9 | +const REQ_BODY: &'static str = "test_body"; |
| 10 | +const OK_BODY: &'static str = "good"; |
| 11 | +const BAD_BODY: &'static str = "bad"; |
| 12 | +const HOST: &'static str = "localhost"; |
| 13 | + |
| 14 | +type TestBody = OneshotBody<'static>; |
| 15 | + |
| 16 | +struct Error(&'static str); |
| 17 | + |
| 18 | +impl From<Error> for Response<TestBody> { |
| 19 | + fn from(err: Error) -> Response<TestBody> { |
| 20 | + Response::bad_request().with_body(err.0.into()) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +struct Text(&'static str); |
| 25 | + |
| 26 | +impl From<Text> for Response<TestBody> { |
| 27 | + fn from(txt: Text) -> Response<TestBody> { |
| 28 | + Response::ok().with_body(txt.0.into()) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn transform_middleware() { |
| 34 | + // Test the type conversion in `TransformMiddleware`. |
| 35 | + |
| 36 | + async fn handler( |
| 37 | + method: Method, |
| 38 | + cloned_path: Cloned<Path>, |
| 39 | + path: Path, |
| 40 | + version: Version, |
| 41 | + cloned_headers: Cloned<Headers>, |
| 42 | + headers: Headers, |
| 43 | + cloned_body: Cloned<Body<TestBody>>, |
| 44 | + body: Body<TestBody>, |
| 45 | + ) -> Result<Text, Error> { |
| 46 | + assert_eq!(method, Method::Get); |
| 47 | + assert_eq!((cloned_path.0).0, path.0); |
| 48 | + assert_eq!(version, Version::Http11); |
| 49 | + let cloned_headers = cloned_headers.0; |
| 50 | + assert_eq!(cloned_headers.len(), 1); |
| 51 | + assert_eq!(headers.len(), 1); |
| 52 | + assert_eq!( |
| 53 | + cloned_headers |
| 54 | + .get_value::<&str>(&HeaderName::HOST) |
| 55 | + .unwrap() |
| 56 | + .unwrap(), |
| 57 | + HOST, |
| 58 | + ); |
| 59 | + assert_eq!( |
| 60 | + headers |
| 61 | + .get_value::<&str>(&HeaderName::HOST) |
| 62 | + .unwrap() |
| 63 | + .unwrap(), |
| 64 | + HOST, |
| 65 | + ); |
| 66 | + assert_eq!((cloned_body.0).0, REQ_BODY); |
| 67 | + assert_eq!(body.0, REQ_BODY); |
| 68 | + |
| 69 | + if path.0 == "/ok" { |
| 70 | + Ok(Text(OK_BODY)) |
| 71 | + } else { |
| 72 | + Err(Error(BAD_BODY)) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + let middleware = TransformMiddleware::wrap(handler); |
| 77 | + |
| 78 | + let tests = [ |
| 79 | + ("/ok", StatusCode::OK, OK_BODY), |
| 80 | + ("/error", StatusCode::BAD_REQUEST, BAD_BODY), |
| 81 | + ]; |
| 82 | + for (path, expected_status, expected_body) in tests { |
| 83 | + let request = Request::new( |
| 84 | + Method::Get, |
| 85 | + path.into(), |
| 86 | + Version::Http11, |
| 87 | + { |
| 88 | + let mut headers = Headers::EMPTY; |
| 89 | + headers.append(Header::new(HeaderName::HOST, HOST.as_bytes())); |
| 90 | + headers |
| 91 | + }, |
| 92 | + TestBody::from(REQ_BODY), |
| 93 | + ); |
| 94 | + let response: Response<TestBody> = test::block_on(middleware.handle(request)); |
| 95 | + assert_eq!(response.status(), expected_status); |
| 96 | + assert_eq!(response.body(), expected_body); |
| 97 | + } |
| 98 | +} |
0 commit comments