pub struct SimpleHandler<F>{ /* private fields */ }Expand description
Simple handler wrapper for testing
Provides a convenient way to create handlers from closures for testing purposes.
The handler function can be any closure that takes a Request and returns a
Result<Response>.
§Examples
§Basic usage
use reinhardt_testkit::mock::SimpleHandler;
use reinhardt_http::{Request, Response};
use reinhardt_http::Handler;
let handler = SimpleHandler::new(|req: Request| {
Ok(Response::ok().with_body("Hello, World!"))
});
// Use handler in tests§With path-based routing
use reinhardt_testkit::mock::SimpleHandler;
use reinhardt_http::{Request, Response};
let handler = SimpleHandler::new(|req: Request| {
match req.path() {
"/" => Ok(Response::ok().with_body("Home")),
"/api" => Ok(Response::ok().with_body(r#"{"status": "ok"}"#)),
_ => Ok(Response::not_found().with_body("Not Found")),
}
});§With custom logic
use reinhardt_testkit::mock::SimpleHandler;
use reinhardt_http::{Request, Response};
use std::sync::{Arc, Mutex};
let call_count = Arc::new(Mutex::new(0));
let call_count_clone = call_count.clone();
let handler = SimpleHandler::new(move |req: Request| {
let mut count = call_count_clone.lock().unwrap();
*count += 1;
Ok(Response::ok().with_body(format!("Call count: {}", *count)))
});Implementations§
Source§impl<F> SimpleHandler<F>
impl<F> SimpleHandler<F>
Sourcepub fn new(handler_fn: F) -> SimpleHandler<F>
pub fn new(handler_fn: F) -> SimpleHandler<F>
Create a new SimpleHandler with the given handler function
§Arguments
handler_fn- A closure that processes requests and returns responses
§Examples
use reinhardt_testkit::mock::SimpleHandler;
use reinhardt_http::{Request, Response};
let handler = SimpleHandler::new(|req| {
Ok(Response::ok().with_body("Success"))
});Trait Implementations§
Source§impl<F> Handler for SimpleHandler<F>
impl<F> Handler for SimpleHandler<F>
Auto Trait Implementations§
impl<F> Freeze for SimpleHandler<F>where
F: Freeze,
impl<F> RefUnwindSafe for SimpleHandler<F>where
F: RefUnwindSafe,
impl<F> Send for SimpleHandler<F>
impl<F> Sync for SimpleHandler<F>
impl<F> Unpin for SimpleHandler<F>where
F: Unpin,
impl<F> UnsafeUnpin for SimpleHandler<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for SimpleHandler<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more