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
8pub fn text_response(
9    content: &str,
10    content_type: Option<&str>,
11    custom_headers: Option<&HashMap<String, Option<String>>>,
12    request_headers: &HeaderMap,
13) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
14    let mut response_handler = ResponseHandler::default();
15    if let Some(custom_headers) = custom_headers {
16        response_handler = response_handler.with_headers(custom_headers.to_owned());
17    }
18    if let Some(content_type) = content_type {
19        response_handler = response_handler.with_header("content-type", Some(content_type));
20    }
21    response_handler
22        .with_headers(match custom_headers {
23            Some(x) => x.to_owned(),
24            None => HashMap::new(),
25        })
26        .with_text(content, content_type)
27        .into_response(request_headers)
28}