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    cors_allow_credentials_origins: &[String],
18) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
19    ResponseHandler::default()
20        .with_status(status_code)
21        .with_custom_headers(headers)
22        .into_response(request_headers, cors_allow_credentials_origins)
23}
24
25/// custom status code response with message in body
26///
27/// See [`status_code_response`] for why `headers` is applied after
28/// `with_text` — an explicit `content-type` in `headers` must win over
29/// the `text/plain` default `with_text` sets.
30pub fn status_code_response_with_message(
31    status_code: &StatusCode,
32    message: &str,
33    headers: Option<&HashMap<String, Option<String>>>,
34    request_headers: &HeaderMap,
35    cors_allow_credentials_origins: &[String],
36) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
37    ResponseHandler::default()
38        .with_status(status_code)
39        .with_text(message, None)
40        .with_custom_headers(headers)
41        .into_response(request_headers, cors_allow_credentials_origins)
42}