use http::header::HeaderName;
use crate::service::MockResponse;
const X_AMZN_ERRORTYPE: HeaderName = HeaderName::from_static("x-amzn-errortype");
pub fn json_error_response(status: u16, error_type: &str, message: &str) -> MockResponse {
let body = format!(
r#"{{"__type":"{}","message":"{}"}}"#,
escape_json_string(error_type),
escape_json_string(message),
);
MockResponse::json(status, body)
}
pub fn rest_json_error(status: u16, code: &str, message: &str) -> MockResponse {
let body = format!(
r#"{{"Type":"User","Message":"{}"}}"#,
escape_json_string(message),
);
let mut resp = MockResponse::rest_json(status, body);
resp.headers.insert(X_AMZN_ERRORTYPE, code.parse().unwrap());
resp
}
pub fn body_has_top_level_field(body: &[u8], key: &str) -> bool {
serde_json::from_slice::<serde_json::Value>(body)
.ok()
.and_then(|v| v.as_object().map(|o| o.contains_key(key)))
.unwrap_or(false)
}
fn escape_json_string(s: &str) -> String {
s.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n")
.replace('\r', "\\r")
.replace('\t', "\\t")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_json_error_response() {
let resp = json_error_response(400, "ValidationException", "Missing 'KeyId'");
assert_eq!(resp.status, 400);
let body: serde_json::Value = serde_json::from_slice(&resp.body).unwrap();
assert_eq!(body["__type"], "ValidationException");
assert_eq!(body["message"], "Missing 'KeyId'");
}
#[test]
fn test_rest_json_error() {
let resp = rest_json_error(404, "NotFoundException", "Resource not found");
assert_eq!(resp.status, 404);
assert_eq!(
resp.headers.get("x-amzn-errortype").unwrap(),
"NotFoundException"
);
let body: serde_json::Value = serde_json::from_slice(&resp.body).unwrap();
assert_eq!(body["Message"], "Resource not found");
}
#[test]
fn test_escape_json_string() {
assert_eq!(escape_json_string(r#"say "hi""#), r#"say \"hi\""#);
assert_eq!(escape_json_string("line\nnew"), "line\\nnew");
}
}