pub struct Server { /* private fields */ }
Expand description
Listens on a random port, running the given function to handle each request.
See the crate documentation for an example.
Implementations§
Source§impl Server
impl Server
Sourcepub async fn new<H: Handler + Clone + Send + Sync + 'static>(
handler: H,
) -> Result<Self, Error>
pub async fn new<H: Handler + Clone + Send + Sync + 'static>( handler: H, ) -> Result<Self, Error>
Creates a new HTTP server on a random port running the given handler. The handler will be called on every request, and the total request count can be retrieved with server.req_count().
The server can be safely cloned and used from multiple threads. When the final reference to the server is dropped, the server will be shut down and all pending requests will be aborted. Aborting the server will happen in the background and will not block.
The remote socket address is added as a SocketAddr extension to the request object.
Sourcepub fn addr(&self) -> SocketAddr
pub fn addr(&self) -> SocketAddr
Returns the socket address the server is listening on.
Sourcepub fn url(&self, path_and_query: &str) -> Uri
pub fn url(&self, path_and_query: &str) -> Uri
Returns a valid request URL for the given path and query string.
Sourcepub fn req_count(&self) -> u64
pub fn req_count(&self) -> u64
Returns the number of requests handled by the server. This value is incremented after the request handler has finished, but before the response has been sent.
At the end of tests, this should be asserted to be equal to the amount of requests sent.
Any panics in the request handler may result in the counter becoming out of sync.
Sourcepub async fn await_req_count(
&self,
target_count: u64,
timeout: Duration,
) -> Result<(), Error>
pub async fn await_req_count( &self, target_count: u64, timeout: Duration, ) -> Result<(), Error>
Await req_count reaching a certain number. This polls every 10ms and times out after the given duration.
Sourcepub fn concurrent_req_count(&self) -> u64
pub fn concurrent_req_count(&self) -> u64
Returns the number of concurrent requests currently being handled by the server. A concurrent request is measured by incrementing the counter before the request handler is called, and decrementing it after the request handler has finished. The response may still be in the process of being sent when the counter is decremented.
Any panics in the request handler may result in the counter becoming out of sync.