use http::StatusCode;
use jmap_server::{RequestError, problem_response, request_error};
use jmap_types::JmapError;
use jmap_types::problem::ProblemDetails;
use serde_json::{Value, json};
fn body(resp: &http::Response<String>) -> Value {
serde_json::from_str(resp.body()).unwrap()
}
#[test]
fn about_blank_for_a_plain_status() {
let resp = problem_response(
StatusCode::UNAUTHORIZED,
&ProblemDetails::for_status(401).with_title("Unauthorized"),
None,
);
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
assert_eq!(resp.headers()["content-type"], "application/problem+json");
assert!(resp.headers().get("content-language").is_none());
assert_eq!(
body(&resp),
json!({ "type": "about:blank", "status": 401, "title": "Unauthorized" })
);
}
#[test]
fn localized_title_sets_content_language() {
let resp = problem_response(
StatusCode::NOT_FOUND,
&ProblemDetails::for_status(404)
.with_title("Introuvable")
.with_instance("/download/a1/b1/x"),
Some("fr"),
);
assert_eq!(resp.headers()["content-language"], "fr");
assert_eq!(body(&resp)["instance"], "/download/a1/b1/x");
}
#[test]
fn invalid_content_language_is_dropped() {
let resp = problem_response(
StatusCode::BAD_REQUEST,
&ProblemDetails::for_status(400),
Some("fr\n"),
);
assert!(resp.headers().get("content-language").is_none());
}
#[test]
fn request_error_problem_details() {
let err: RequestError = request_error(JmapError::limit("maxSizeRequest"));
assert_eq!(err.status(), StatusCode::BAD_REQUEST);
let details = err.problem_details().with_title("Request too large");
assert_eq!(
serde_json::to_value(&details).unwrap(),
json!({
"type": "urn:ietf:params:jmap:error:limit",
"status": 400,
"title": "Request too large",
"limit": "maxSizeRequest"
})
);
assert_eq!(
body(&err.into_response()),
json!({ "type": "urn:ietf:params:jmap:error:limit", "status": 400, "limit": "maxSizeRequest" })
);
}