pub struct TestServer { /* private fields */ }
Expand description

Controls the listener life cycle and creates new resources

Implementations§

source§

impl TestServer

source

pub fn new() -> Result<TestServer, Error>

Creates a listener that is bounded to a free port in localhost. Listener is closed when the value is dropped.

Any request for non-defined resources will return 404.

let server = TestServer::new().unwrap();
source

pub fn new_with_port(port: u16) -> Result<TestServer, Error>

Same behaviour as new, but tries to bound to given port instead of looking for a free one.

let server = TestServer::new_with_port(8080).unwrap();
source

pub fn port(&self) -> u16

Returns associated port number.

let server = TestServer::new().unwrap();

assert!(server.port() > 0);
source

pub fn close(&self)

Closes listener. Server stops receiving connections. Do nothing if listener is already closed.

In most the cases this method is not required as the listener is automatically closed when the value is dropped.

let server = TestServer::new().unwrap();

server.close();
source

pub fn create_resource(&self, uri: &str) -> Resource

Creates a new resource. By default resources answer “200 Ok”.

Check Resource for all possible configurations.

let server = TestServer::new().unwrap();
let resource = server.create_resource("/user/settings");
source

pub fn requests(&self) -> Receiver<Request>

Retrieves information on new requests.

let server = TestServer::new().unwrap();

for request in server.requests().iter() {
    assert_eq!(request.url, "/endpoint");
    assert_eq!(request.method, "GET");
    assert_eq!(request.headers.get("Content-Type").unwrap(), "text");
}

Trait Implementations§

source§

impl Drop for TestServer

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.