pub struct TestServer { /* private fields */ }
Expand description
Provides a very simple HTTP server with warp
that can be used to test
requests.
To use this you must enable the server
feature in your Cargo.toml
.
The server is bound to the localhost
at a random port. The bound port
can be retrieved using the TestServer::port
method.
§Example
use tux::TestServer;
let server = TestServer::new_with_root_response("hello from server");
println!("server is listening at port {}", server.port());
let addr = format!("http://localhost:{}", server.port());
let resp = reqwest::blocking::get(addr).unwrap();
let text = resp.text().unwrap();
assert_eq!(text, "hello from server");
// dropping the value shuts down the server
drop(server);
Implementations§
Source§impl TestServer
impl TestServer
Sourcepub fn port(&self) -> u16
pub fn port(&self) -> u16
Returns the port number where the server is listening for incoming connections.
Sourcepub fn new_with_root_response(response: &'static str) -> Self
pub fn new_with_root_response(response: &'static str) -> Self
Creates a server with a root route that just responds with the given text.
Sourcepub fn new_with_ping_route(route: &'static str) -> Self
pub fn new_with_ping_route(route: &'static str) -> Self
Creates a server that will respond to any route and method with information about the incoming request.
The response is plain text with lines in the format key: value
.
The following keys are provided:
method
path
Sourcepub fn new_with_routes<F>(routes: F) -> TestServer
pub fn new_with_routes<F>(routes: F) -> TestServer
Creates a new server with custom routes.
§Example
let routes = warp::path::end().map(|| "hello");
let server = TestServer::new_with_routes(routes);
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TestServer
impl !RefUnwindSafe for TestServer
impl Send for TestServer
impl Sync for TestServer
impl Unpin for TestServer
impl !UnwindSafe for TestServer
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