Skip to content

Commit 417522e

Browse files
committed
Mock handler
1 parent f03b7f3 commit 417522e

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub enum RouteKind {
5252
File(FileRoute),
5353
Proxy(ProxyRoute),
5454
Json(JsonRoute),
55+
Mock(MockRoute),
5556
}
5657

5758
#[derive(Debug, Deserialize)]
@@ -71,6 +72,13 @@ pub struct JsonRoute {
7172
pub pretty: bool,
7273
}
7374

75+
#[derive(Debug, Deserialize)]
76+
pub struct MockRoute {
77+
#[serde(with = "http_serde::status_code")]
78+
pub status: http::StatusCode,
79+
pub body: Option<serde_json::Value>,
80+
}
81+
7482
#[derive(Debug, Deserialize)]
7583
#[serde(rename_all = "kebab-case")]
7684
pub struct ProxyRoute {
@@ -95,6 +103,7 @@ impl Route {
95103
RouteKind::File(file) => file.validate(),
96104
RouteKind::Proxy(proxy) => proxy.validate(),
97105
RouteKind::Json(json) => json.validate(),
106+
RouteKind::Mock(_) => Ok(()),
98107
}
99108
}
100109
}

src/handler/mock.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use hyper::Body;
2+
3+
use crate::{config, response};
4+
5+
#[derive(Debug)]
6+
pub struct MockHandler {
7+
config: config::MockRoute,
8+
}
9+
10+
impl MockHandler {
11+
pub fn new(config: config::MockRoute) -> Self {
12+
MockHandler { config }
13+
}
14+
15+
pub async fn handle(
16+
&self,
17+
_: http::Request<Body>,
18+
) -> Result<http::Response<Body>, (http::Request<Body>, http::Response<Body>)> {
19+
match &self.config.body {
20+
Some(value) => {
21+
let mut response = response::json(value);
22+
*response.status_mut() = self.config.status;
23+
Ok(response)
24+
}
25+
None => Ok(response::from_status(self.config.status)),
26+
}
27+
}
28+
}

src/handler/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod fs;
22
mod json;
3+
mod mock;
34
mod proxy;
45

56
use anyhow::Result;
@@ -8,6 +9,7 @@ use regex::Regex;
89

910
use self::fs::{DirHandler, FileHandler};
1011
use self::json::JsonHandler;
12+
use self::mock::MockHandler;
1113
use self::proxy::ProxyHandler;
1214
use crate::config;
1315

@@ -24,6 +26,7 @@ pub enum HandlerKind {
2426
Dir(DirHandler),
2527
Proxy(ProxyHandler),
2628
Json(JsonHandler),
29+
Mock(MockHandler),
2730
}
2831

2932
#[derive(Debug)]
@@ -50,6 +53,7 @@ impl Handler {
5053
config::RouteKind::Dir(dir) => HandlerKind::Dir(DirHandler::new(dir)),
5154
config::RouteKind::Proxy(proxy) => HandlerKind::Proxy(ProxyHandler::new(proxy)),
5255
config::RouteKind::Json(json) => HandlerKind::Json(JsonHandler::new(json).await?),
56+
config::RouteKind::Mock(mock) => HandlerKind::Mock(MockHandler::new(mock)),
5357
};
5458

5559
Ok(Handler {
@@ -73,6 +77,7 @@ impl Handler {
7377
HandlerKind::Dir(dir) => dir.handle(request, &path).await,
7478
HandlerKind::Proxy(proxy) => proxy.handle(request, &path).await,
7579
HandlerKind::Json(json) => json.handle(request, &path).await,
80+
HandlerKind::Mock(mock) => mock.handle(request).await,
7681
};
7782

7883
if let Ok(response) = &mut result {

0 commit comments

Comments
 (0)