mirror of
https://github.com/actix/actix-web.git
synced 2024-12-30 12:00:38 +00:00
add tests for error and some responders
This commit is contained in:
parent
c14c66d2b0
commit
9bd0f29ca3
2 changed files with 62 additions and 0 deletions
31
src/error.rs
31
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -380,6 +380,15 @@ pub(crate) mod tests {
|
||||||
HeaderValue::from_static("text/plain; charset=utf-8")
|
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 =
|
let resp: HttpResponse =
|
||||||
block_on(Bytes::from_static(b"test").respond_to(&req)).unwrap();
|
block_on(Bytes::from_static(b"test").respond_to(&req)).unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
@ -398,10 +407,32 @@ pub(crate) mod tests {
|
||||||
HeaderValue::from_static("application/octet-stream")
|
HeaderValue::from_static("application/octet-stream")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// InternalError
|
||||||
let resp: HttpResponse =
|
let resp: HttpResponse =
|
||||||
error::InternalError::new("err", StatusCode::BAD_REQUEST)
|
error::InternalError::new("err", StatusCode::BAD_REQUEST)
|
||||||
.respond_to(&req)
|
.respond_to(&req)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue