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`, via
10/// `with_custom_headers` (RFC 065), so an explicit `content-type` in
11/// `custom_headers` wins over both `with_text`'s `text/plain` default
12/// and `content_type` (itself only an inferred default - e.g. a
13/// file-extension guess). This was previously reversed: `with_text` ran
14/// last and unconditionally overwrote any explicit `content-type` a
15/// caller had set (RFC 045 Defect 1b).
16pub fn text_response(
17    content: &str,
18    content_type: Option<&str>,
19    custom_headers: Option<&HashMap<String, Option<String>>>,
20    request_headers: &HeaderMap,
21) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
22    ResponseHandler::default()
23        .with_text(content, content_type)
24        .with_custom_headers(custom_headers)
25        .into_response(request_headers)
26}