From 9bd0f29ca3396924a607e8a1c5312f79ae157cab Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 17 Mar 2019 10:11:10 -0700 Subject: [PATCH] add tests for error and some responders --- src/error.rs | 31 +++++++++++++++++++++++++++++++ src/responder.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/error.rs b/src/error.rs index 2231473f2..fc0f9fdf3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -142,3 +142,34 @@ impl ResponseError for ReadlinesError { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_urlencoded_error() { + let resp: HttpResponse = UrlencodedError::Overflow.error_response(); + assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); + let resp: HttpResponse = UrlencodedError::UnknownLength.error_response(); + assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED); + let resp: HttpResponse = UrlencodedError::ContentType.error_response(); + assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + } + + #[test] + fn test_json_payload_error() { + let resp: HttpResponse = JsonPayloadError::Overflow.error_response(); + assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); + let resp: HttpResponse = JsonPayloadError::ContentType.error_response(); + assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + } + + #[test] + fn test_readlines_error() { + let resp: HttpResponse = ReadlinesError::LimitOverflow.error_response(); + assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); + let resp: HttpResponse = ReadlinesError::EncodingError.error_response(); + assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + } +} diff --git a/src/responder.rs b/src/responder.rs index 871670bdf..50467883c 100644 --- a/src/responder.rs +++ b/src/responder.rs @@ -380,6 +380,15 @@ pub(crate) mod tests { HeaderValue::from_static("text/plain; charset=utf-8") ); + let resp: HttpResponse = + block_on((&"test".to_string()).respond_to(&req)).unwrap(); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.body().bin_ref(), b"test"); + assert_eq!( + resp.headers().get(CONTENT_TYPE).unwrap(), + HeaderValue::from_static("text/plain; charset=utf-8") + ); + let resp: HttpResponse = block_on(Bytes::from_static(b"test").respond_to(&req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); @@ -398,10 +407,32 @@ pub(crate) mod tests { HeaderValue::from_static("application/octet-stream") ); + // InternalError let resp: HttpResponse = error::InternalError::new("err", StatusCode::BAD_REQUEST) .respond_to(&req) .unwrap(); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); } + + #[test] + fn test_result_responder() { + let req = TestRequest::default().to_http_request(); + + // Result + let resp: HttpResponse = + block_on(Ok::<_, Error>("test".to_string()).respond_to(&req)).unwrap(); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.body().bin_ref(), b"test"); + assert_eq!( + resp.headers().get(CONTENT_TYPE).unwrap(), + HeaderValue::from_static("text/plain; charset=utf-8") + ); + + let res = block_on( + Err::(error::InternalError::new("err", StatusCode::BAD_REQUEST)) + .respond_to(&req), + ); + assert!(res.is_err()); + } }