From 525f308c4ab097e7683da1c41a80abfb01157723 Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Wed, 26 Jan 2022 09:13:31 -0800 Subject: [PATCH] preliminary testing framework; don't rely on this (yet) Signed-off-by: Erik Hollensbe --- src/app.rs | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index dfb11df..d14178a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,6 @@ use std::{convert::Infallible, net::SocketAddr, sync::Arc}; -use http::{Method, Request, Response, StatusCode}; +use http::{HeaderMap, Method, Request, Response, StatusCode}; use hyper::{server::conn::Http, service::service_fn, Body}; use tokio::{net::TcpListener, sync::Mutex}; @@ -180,3 +180,136 @@ impl App< } } } + +pub struct TestService { + app: App, + headers: Option, +} + +impl TestService { + pub fn new(app: App) -> Self { + Self { app, headers: None } + } + + pub fn with_headers(&self, headers: http::HeaderMap) -> Self { + Self { + app: self.app.clone(), + headers: Some(headers), + } + } + + pub async fn dispatch(&self, req: Request) -> Response { + self.app.dispatch(req).await.unwrap() + } + + fn populate_headers(&self, mut req: http::request::Builder) -> http::request::Builder { + if let Some(include_headers) = self.headers.clone() { + for (header, value) in include_headers.clone() { + if let Some(header) = header { + req = req.header(header, value.clone()); + } + } + } + + req + } + + pub async fn get(&self, path: &str) -> Response { + let req = self.populate_headers(Request::builder()); + + self.app + .dispatch(req.uri(path).body(Body::default()).unwrap()) + .await + .unwrap() + } + + pub async fn post(&self, path: &str, body: Body) -> Response { + let req = self.populate_headers(Request::builder()); + + self.app + .dispatch(req.method(Method::POST).uri(path).body(body).unwrap()) + .await + .unwrap() + } + + pub async fn delete(&self, path: &str) -> Response { + let req = self.populate_headers(Request::builder()); + self.app + .dispatch( + req.method(Method::DELETE) + .uri(path) + .body(Body::default()) + .unwrap(), + ) + .await + .unwrap() + } + + pub async fn put(&self, path: &str, body: Body) -> Response { + let req = self.populate_headers(Request::builder()); + self.app + .dispatch(req.method(Method::PUT).uri(path).body(body).unwrap()) + .await + .unwrap() + } + + pub async fn options(&self, path: &str) -> Response { + let req = self.populate_headers(Request::builder()); + self.app + .dispatch( + req.method(Method::OPTIONS) + .uri(path) + .body(Body::default()) + .unwrap(), + ) + .await + .unwrap() + } + + pub async fn patch(&self, path: &str, body: Body) -> Response { + let req = self.populate_headers(Request::builder()); + self.app + .dispatch(req.method(Method::PATCH).uri(path).body(body).unwrap()) + .await + .unwrap() + } + + pub async fn head(&self, path: &str) -> Response { + let req = self.populate_headers(Request::builder()); + self.app + .dispatch( + req.method(Method::HEAD) + .uri(path) + .body(Body::default()) + .unwrap(), + ) + .await + .unwrap() + } + + pub async fn trace(&self, path: &str) -> Response { + let req = self.populate_headers(Request::builder()); + self.app + .dispatch( + req.method(Method::TRACE) + .uri(path) + .body(Body::default()) + .unwrap(), + ) + .await + .unwrap() + } + + pub async fn connect(&self, path: &str) -> Response { + let req = self.populate_headers(Request::builder()); + self.app + .dispatch( + req.method(Method::CONNECT) + .uri(path) + .body(Body::default()) + .unwrap(), + ) + .await + .unwrap() + } +}