test(web): improve coverage for route and default errors (#354)

* test(web/error): validate TimeoutError::Service status_code

* test(web/error): validate Utf8Error returns BAD_REQUEST status

* test(web/error): validate PayloadError::EncodingCorrupted returns BAD_REQUEST

* test(web/route): validate Route debug format

* test(web/route): validate Route and RouteService debug format

* test(web/route): avoid lambda in tests to keep coverage
This commit is contained in:
msga-mmm 2024-05-04 21:39:51 -05:00 committed by GitHub
parent 33490cd360
commit 49a4867005
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View file

@ -679,6 +679,7 @@ mod tests {
use std::io;
use super::*;
use crate::http;
use crate::http::client::error::{ConnectError, SendRequestError};
use crate::web::test::TestRequest;
@ -712,6 +713,12 @@ mod tests {
);
assert_eq!(resp.status(), StatusCode::GATEWAY_TIMEOUT);
let resp = WebResponseError::<DefaultError>::error_response(
&TimeoutError::<UrlencodedError>::Service(UrlencodedError::Chunked),
&req,
);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let resp = WebResponseError::<DefaultError>::error_response(
&SendRequestError::Connect(ConnectError::Timeout),
&req,
@ -752,6 +759,15 @@ mod tests {
let err = PayloadError::Decoding;
let resp = WebResponseError::<DefaultError>::error_response(&err, &req);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
#[allow(invalid_from_utf8)]
let err = std::str::from_utf8(b"\xF0").unwrap_err();
let resp = WebResponseError::<DefaultError>::error_response(&err, &req);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let err = http::error::PayloadError::EncodingCorrupted;
let resp = WebResponseError::<DefaultError>::error_response(&err, &req);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[test]

View file

@ -278,6 +278,8 @@ array_routes!(12, a, b, c, d, e, f, g, h, i, j, k, l);
#[cfg(test)]
mod tests {
use ntex_service::ServiceFactory;
use crate::http::{header, Method, StatusCode};
use crate::time::{sleep, Millis};
use crate::util::Bytes;
@ -372,5 +374,23 @@ mod tests {
let body = read_body(resp).await;
assert_eq!(body, Bytes::from_static(b"{\"name\":\"test\"}"));
let route: web::Route<DefaultError> = web::get();
let repr = format!("{:?}", route);
assert!(repr.contains("Route"));
assert!(repr
.contains("handler: Handler(\"ntex::web::route::Route::new::{{closure}}\")"));
assert!(repr.contains("methods: [GET]"));
assert!(repr.contains("guards: AllGuard()"));
assert!(route.create(()).await.is_ok());
let route_service = route.service();
let repr = format!("{:?}", route_service);
assert!(repr.contains("RouteService"));
assert!(repr
.contains("handler: Handler(\"ntex::web::route::Route::new::{{closure}}\")"));
assert!(repr.contains("methods: [GET]"));
assert!(repr.contains("guards: AllGuard()"));
}
}