Skip to main content

apimock_server/
respond_response.rs

1//! Turn a matched `Respond` declaration into an HTTP response.
2//!
3//! # Why this is a server-side free function and not a `Respond` method
4//!
5//! Pre-5.0, `Respond::response(...)` lived on the type itself. That
6//! method built a `hyper::Response<BoxBody>` and touched the server's
7//! file-response / text-response / status-response helpers. To keep
8//! `apimock-routing` free of hyper-body construction (so a future GUI
9//! can depend on it cheaply), that work moved here.
10
11use apimock_routing::{ParsedRequest, Respond};
12use console::style;
13use std::path::Path;
14
15use crate::{
16    http_util::delay_response,
17    response::{
18        error_response::internal_server_error_response,
19        file_response::FileResponse,
20        status_code_response::{status_code_response, status_code_response_with_message},
21        text_response::text_response,
22    },
23    respond_util::full_file_path,
24    types::BoxBody,
25};
26
27/// Produce the HTTP response for a matched `Respond` declaration.
28///
29/// # Why the branches are ordered file → text → status → error
30///
31/// The fields are mutually specialised:
32/// - `file_path` serves a file (possibly with CSV→JSON conversion).
33/// - `text` + `status` yields a custom-status text response.
34/// - `text` alone yields a plain 200 text response.
35/// - `status` alone yields an empty body with that status.
36///
37/// `Respond::validate` rejects nonsensical combinations at startup, so
38/// hitting the final `Err` branch means something slipped past
39/// validation — a real bug, not user input.
40pub async fn respond_response(
41    respond: &Respond,
42    dir_prefix: &str,
43    parsed_request: &ParsedRequest,
44) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
45    if let Some(delay_ms) = respond.delay_response_milliseconds {
46        delay_response(delay_ms).await;
47    }
48
49    let request_headers = &parsed_request.component_parts.headers;
50
51    // file_path → file/CSV/JSON response
52    if let Some(file_path) = respond.file_path.as_ref() {
53        let Some(full_file_path) = full_file_path(file_path.as_str(), dir_prefix) else {
54            log::error!(
55                "{}:\n{} (prefix = {})",
56                style("file not found").red(),
57                file_path,
58                dir_prefix,
59            );
60            return internal_server_error_response(
61                "failed to get response file",
62                request_headers,
63            );
64        };
65
66        // dir_prefix is used only for the file-not-found message above;
67        // the actual read happens against the resolved full_file_path.
68        let _ = Path::new(dir_prefix);
69
70        return FileResponse::new_with_csv_records_jsonpath(
71            full_file_path.as_str(),
72            respond.headers.as_ref(),
73            respond.csv_records_key.clone(),
74            request_headers,
75        )
76        .file_content_response()
77        .await;
78    }
79
80    if let Some(text) = respond.text.as_ref() {
81        return match respond.status_code.as_ref() {
82            Some(status_code) => {
83                status_code_response_with_message(status_code, text.as_str(), request_headers)
84            }
85            None => text_response(text.as_str(), None, respond.headers.as_ref(), request_headers),
86        };
87    }
88
89    if let Some(status_code) = respond.status_code.as_ref() {
90        return status_code_response(status_code, request_headers);
91    }
92
93    internal_server_error_response("invalid respond def", request_headers)
94}