use http::{Response, StatusCode};
use crate::Error;
use super::scenario::Scenario;
#[test]
fn provide_successful_response() {
let scenario = Scenario::builder().get("/path").build();
let reply = scenario.to_provide_response();
let response = Response::builder()
.status(StatusCode::OK)
.header("content-type", "text/plain")
.body(())
.unwrap();
let reply = reply.provide(response).unwrap();
assert!(reply.inner().response.is_some());
assert!(reply.inner().state.writer.is_some());
let status = reply.inner().response.as_ref().unwrap().prelude().1;
assert_eq!(status, StatusCode::OK);
}
#[test]
fn provide_error_response() {
let scenario = Scenario::builder().get("/path").build();
let reply = scenario.to_provide_response();
let response = Response::builder()
.status(StatusCode::NOT_FOUND)
.header("content-type", "text/plain")
.body(())
.unwrap();
let reply = reply.provide(response).unwrap();
assert!(reply.inner().response.is_some());
assert!(reply.inner().state.writer.is_some());
let status = reply.inner().response.as_ref().unwrap().prelude().1;
assert_eq!(status, StatusCode::NOT_FOUND);
}
#[test]
fn provide_response_after_reject_100() {
let scenario = Scenario::builder()
.post("/path")
.header("expect", "100-continue")
.build();
let reply = scenario.to_send_100();
let reply = reply.reject();
let response = Response::builder()
.status(StatusCode::BAD_REQUEST)
.header("content-type", "text/plain")
.body(())
.unwrap();
let reply = reply.provide(response).unwrap();
assert!(reply.inner().response.is_some());
assert!(reply.inner().state.writer.is_some());
let status = reply.inner().response.as_ref().unwrap().prelude().1;
assert_eq!(status, StatusCode::BAD_REQUEST);
}
#[test]
fn error_when_providing_success_after_reject_100() {
let scenario = Scenario::builder()
.post("/path")
.header("expect", "100-continue")
.build();
let reply = scenario.to_send_100();
let reply = reply.reject();
let response = Response::builder()
.status(StatusCode::OK)
.header("content-type", "text/plain")
.body(())
.unwrap();
let result = reply.provide(response);
assert!(result.is_err());
match result.unwrap_err() {
Error::BadReject100Status(status) => {
assert_eq!(status, StatusCode::OK);
}
_ => panic!("Expected BadReject100Status error"),
}
}
#[test]
fn provide_response_with_content_length() {
let scenario = Scenario::builder().get("/path").build();
let reply = scenario.to_provide_response();
let response = Response::builder()
.status(StatusCode::OK)
.header("content-type", "text/plain")
.header("content-length", "11")
.body(())
.unwrap();
let reply = reply.provide(response).unwrap();
assert!(reply.inner().response.is_some());
assert!(reply.inner().state.writer.is_some());
let has_content_length = reply
.inner()
.response
.as_ref()
.unwrap()
.headers()
.any(|(name, value)| name == "content-length" && value == "11");
assert!(has_content_length);
}
#[test]
fn provide_response_with_chunked_encoding() {
let scenario = Scenario::builder().get("/path").build();
let reply = scenario.to_provide_response();
let response = Response::builder()
.status(StatusCode::OK)
.header("content-type", "text/plain")
.header("transfer-encoding", "chunked")
.body(())
.unwrap();
let reply = reply.provide(response).unwrap();
assert!(reply.inner().response.is_some());
assert!(reply.inner().state.writer.is_some());
let has_chunked = reply
.inner()
.response
.as_ref()
.unwrap()
.headers()
.any(|(name, value)| name == "transfer-encoding" && value == "chunked");
assert!(has_chunked);
}