Skip to content
This repository was archived by the owner on Nov 13, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub struct HandlerBuilder {
path: String,
method: Method,
headers: HeaderMap,
status_code: StatusCode
status_code: StatusCode,
response: Vec<u8>,
}

#[allow(dead_code)]
Expand All @@ -22,7 +23,8 @@ impl HandlerBuilder {
path: String::from(path),
method: Method::GET,
headers: HeaderMap::new(),
status_code: StatusCode::INTERNAL_SERVER_ERROR
status_code: StatusCode::INTERNAL_SERVER_ERROR,
response: Vec::new(),
}
}

Expand All @@ -41,16 +43,33 @@ impl HandlerBuilder {
self
}

pub fn response(mut self, response: Vec<u8>) -> HandlerBuilder {
self.response = response;
self
}

pub fn build(self) -> HandlerCallback {
let Self { path, method, status_code, headers } = self;
let Self {
path,
method,
status_code,
headers,
response,
} = self;
Arc::new(move |req: Request<Body>| {
let cloned_path = path.clone();
let cloned_method = method.clone();
let cloned_headers = headers.clone();
let cloned_response = response.clone();
Box::pin(async move {
if req.uri().path().eq(cloned_path.as_str()) && req.method().eq(&cloned_method)
&& Self::contains_headers(req.headers(), &cloned_headers) {
Ok(hyper::Response::builder().status(status_code).body(Body::empty()).unwrap())
if req.uri().path().eq(cloned_path.as_str())
&& req.method().eq(&cloned_method)
&& Self::contains_headers(req.headers(), &cloned_headers)
{
Ok(hyper::Response::builder()
.status(status_code)
.body(cloned_response.into())
.unwrap())
} else {
Ok(hyper::Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR).body(Body::empty()).unwrap())
}
Expand Down
23 changes: 20 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ impl AsyncTestContext for HttpTestContext {

#[cfg(test)]
mod test {
use hyper::{StatusCode, Method, Request, Body, HeaderMap, Client};
use crate::{HttpTestContext};
use test_context::test_context;
use crate::handler::HandlerBuilder;
use crate::HttpTestContext;
use hyper::{body::HttpBody, Body, Client, HeaderMap, Method, Request, StatusCode};
use test_context::test_context;

#[test_context(HttpTestContext)]
#[tokio::test]
Expand Down Expand Up @@ -153,6 +153,23 @@ mod test {
assert_eq!(200, resp.status());
}

#[test_context(HttpTestContext)]
#[tokio::test]
async fn test_get_with_response(ctx: &mut HttpTestContext) {
ctx.add(
HandlerBuilder::new("/response")
.status_code(StatusCode::OK)
.response("Hello test".into())
.build(),
);

let resp = Client::new().get(ctx.uri("/response")).await.unwrap();
assert_eq!(200, resp.status());

let body = resp.into_body().data().await.unwrap().unwrap();
assert_eq!("Hello test", body);
}

#[test_context(HttpTestContext)]
#[tokio::test]
async fn test_post_endpoint(ctx: &mut HttpTestContext) {
Expand Down