1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 23:51:06 +01:00

add tests for error and some responders

This commit is contained in:
Nikolay Kim 2019-03-17 10:11:10 -07:00
parent c14c66d2b0
commit 9bd0f29ca3
2 changed files with 62 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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<I, E>
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::<String, _>(error::InternalError::new("err", StatusCode::BAD_REQUEST))
.respond_to(&req),
);
assert!(res.is_err());
}
}