Skip to main content

apimock_server/response/
text_response.rs

1use hyper::HeaderMap;
2
3use std::collections::HashMap;
4
5use crate::{response_handler::ResponseHandler, types::BoxBody};
6
7/// plain text response
8///
9/// `custom_headers` is applied *after* `with_text`, so an explicit
10/// `content-type` in `custom_headers` wins over both `with_text`'s
11/// `text/plain` default and `content_type` (itself only an inferred
12/// default - e.g. a file-extension guess). This was previously reversed:
13/// `with_text` ran last and unconditionally overwrote any explicit
14/// `content-type` a caller had set (RFC 045 Defect 1b).
15pub fn text_response(
16    content: &str,
17    content_type: Option<&str>,
18    custom_headers: Option<&HashMap<String, Option<String>>>,
19    request_headers: &HeaderMap,
20) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
21    let mut response_handler = ResponseHandler::default().with_text(content, content_type);
22    if let Some(custom_headers) = custom_headers {
23        response_handler = response_handler.with_headers(custom_headers.to_owned());
24    }
25    response_handler.into_response(request_headers)
26}