use doido_controller::respond::Format;
use doido_controller::Context;
use http::StatusCode;
use serde_json::json;
fn ctx_with_accept(accept: &str) -> Context {
let parts = http::Request::builder()
.uri("/posts")
.header("accept", accept)
.body(())
.unwrap()
.into_parts()
.0;
Context::from_request_parts(parts)
}
fn ctx_with_content_type(content_type: &str) -> Context {
let parts = http::Request::builder()
.uri("/posts")
.header("content-type", content_type)
.body(())
.unwrap()
.into_parts()
.0;
Context::from_request_parts(parts)
}
#[test]
fn wants_json_keys_body_parsing_on_content_type() {
let ctx = ctx_with_content_type("application/json");
assert!(ctx.is_json_request(), "content-type json => json request");
assert!(ctx.wants_json());
assert_eq!(ctx.negotiated_format(), Format::Any);
let form = ctx_with_content_type("application/x-www-form-urlencoded");
assert!(!form.is_json_request());
assert!(!form.wants_json());
let accept = ctx_with_accept("application/json");
assert!(!accept.is_json_request());
assert!(accept.wants_json());
}
#[test]
fn negotiates_json_and_serves_the_json_branch() {
let ctx = ctx_with_accept("application/json");
assert_eq!(ctx.negotiated_format(), Format::Json);
let resp = ctx
.respond_to()
.html(|| ctx.status(200))
.json(|| ctx.json(json!({"ok": true})))
.finish();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get("content-type").unwrap(),
"application/json"
);
}
#[test]
fn negotiates_html_and_serves_the_html_branch() {
let ctx = ctx_with_accept("text/html");
let resp = ctx
.respond_to()
.html(|| ctx.status(201))
.json(|| ctx.json(json!({})))
.finish();
assert_eq!(resp.status(), StatusCode::CREATED);
}
#[test]
fn requested_format_without_a_handler_is_406() {
let ctx = ctx_with_accept("application/json");
let resp = ctx.respond_to().html(|| ctx.status(200)).finish();
assert_eq!(resp.status(), StatusCode::NOT_ACCEPTABLE);
}
#[test]
fn json_path_extension_wins_over_accept() {
let parts = http::Request::builder()
.uri("/posts.json")
.header("accept", "text/html")
.body(())
.unwrap()
.into_parts()
.0;
let ctx = Context::from_request_parts(parts);
assert_eq!(ctx.negotiated_format(), Format::Json);
}