Struct Server

Source
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

Source

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.

Source

pub fn addr(&self) -> SocketAddr

Returns the socket address the server is listening on.

Source

pub fn url(&self, path_and_query: &str) -> Uri

Returns a valid request URL for the given path and query string.

Source

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.

Source

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.

Source

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.

Source

pub async fn await_concurrent_req_count( &self, target_count: u64, timeout: Duration, ) -> Result<(), Error>

Await concurrent_req_count reaching a certain number. This polls every 10ms and times out after the given duration.

Source

pub fn close(&self)

close kills the server and aborts all pending requests. This does not block for all requests to finish.

Trait Implementations§

Source§

impl Clone for Server

Source§

fn clone(&self) -> Server

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Server

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Server

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Server

§

impl !RefUnwindSafe for Server

§

impl Send for Server

§

impl Sync for Server

§

impl Unpin for Server

§

impl !UnwindSafe for Server

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.