Skip to main content

apimock_server/response/
status_code_response.rs

1use hyper::{HeaderMap, StatusCode};
2
3use std::collections::HashMap;
4
5use crate::{response_handler::ResponseHandler, types::BoxBody};
6
7/// custom status code response (body is empty)
8///
9/// `headers` are applied last, after the status is set, so an explicit
10/// header always wins over anything `ResponseHandler` would otherwise
11/// infer (RFC 045: an explicitly configured header wins over an
12/// inferred default).
13pub fn status_code_response(
14    status_code: &StatusCode,
15    headers: Option<&HashMap<String, Option<String>>>,
16    request_headers: &HeaderMap,
17) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
18    ResponseHandler::default()
19        .with_status(status_code)
20        .with_custom_headers(headers)
21        .into_response(request_headers)
22}
23
24/// custom status code response with message in body
25///
26/// See [`status_code_response`] for why `headers` is applied after
27/// `with_text` — an explicit `content-type` in `headers` must win over
28/// the `text/plain` default `with_text` sets.
29pub fn status_code_response_with_message(
30    status_code: &StatusCode,
31    message: &str,
32    headers: Option<&HashMap<String, Option<String>>>,
33    request_headers: &HeaderMap,
34) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
35    ResponseHandler::default()
36        .with_status(status_code)
37        .with_text(message, None)
38        .with_custom_headers(headers)
39        .into_response(request_headers)
40}