Skip to main content

Whyhttp

Struct Whyhttp 

Source
pub struct Whyhttp { /* private fields */ }
Expand description

HTTP mock server for use in tests.

Start with Whyhttp::run, configure expectations via the builder chain, then point your HTTP client at url.

On drop, any unfulfilled or violated expectations are printed and the the test panics on drop - so a failing mock automatically fails the test.

See the crate-level documentation for a full example.

Implementations§

Source§

impl Whyhttp

Source

pub fn run() -> Self

Starts the mock server on an ephemeral port.

The server runs in a background thread and shuts down automatically when this instance is dropped. Unfulfilled expectations cause a panic on drop.

let server = whyhttp::Whyhttp::run();
assert!(server.url().starts_with("http://"));
Source

pub fn when(&self) -> WhenWhyhttpRequest

Creates a new expectation and enters the routing phase.

The returned WhenWhyhttpRequest lets you add routing matchers. Call response to configure the reply, or should to add validating matchers first.

let server = whyhttp::Whyhttp::run();
server.when().path("/api").method("POST").response().status(201);
Source

pub fn should(&self) -> ShouldWhyhttpRequest

Creates a new expectation and enters the validation phase.

Shorthand for self.when().should(). The expectation has no routing matchers, so it matches every request.

let server = whyhttp::Whyhttp::run();
// Assert every request carries this header, regardless of path.
server.should().header("x-request-id", "abc").response().status(200);
Source

pub fn response(&self) -> WhyhttpResponse

Creates a new expectation and enters the response phase.

Shorthand for self.when().response(). No routing or validating matchers: the expectation matches every request.

let server = whyhttp::Whyhttp::run();
server.response().status(503).body("unavailable");
Source§

impl Whyhttp

Source

pub fn addr(&self) -> SocketAddr

Returns the server’s socket address.

Source

pub fn url(&self) -> String

Returns the server’s base URL (e.g. http://127.0.0.1:PORT).

Source§

impl Whyhttp

Source

pub fn when_path<S: Into<String>>(&self, path: S) -> WhenWhyhttpRequest

Shorthand for self.when().path(path).

Source

pub fn when_method<S: Into<String>>(&self, method: S) -> WhenWhyhttpRequest

Shorthand for self.when().method(method).

Source

pub fn when_query<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> WhenWhyhttpRequest

Shorthand for self.when().query(key, value).

Source

pub fn when_query_exists<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest

Shorthand for self.when().query_exists(key).

Source

pub fn when_without_query<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest

Shorthand for self.when().without_query(key).

Source

pub fn when_header<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> WhenWhyhttpRequest

Shorthand for self.when().header(key, value).

Source

pub fn when_header_exists<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest

Shorthand for self.when().header_exists(key).

Source

pub fn when_without_header<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest

Shorthand for self.when().without_header(key).

Source

pub fn when_body<S: Into<String>>(&self, body: S) -> WhenWhyhttpRequest

Shorthand for self.when().body(body).

Source

pub fn when_without_body(&self) -> WhenWhyhttpRequest

Shorthand for self.when().without_body().

Source§

impl Whyhttp

Source

pub fn should_path<S: Into<String>>(&self, path: S) -> ShouldWhyhttpRequest

Shorthand for self.should().path(path).

Source

pub fn should_method<S: Into<String>>(&self, method: S) -> ShouldWhyhttpRequest

Shorthand for self.should().method(method).

Source

pub fn should_query<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> ShouldWhyhttpRequest

Shorthand for self.should().query(key, value).

Source

pub fn should_query_exists<K: Into<String>>( &self, key: K, ) -> ShouldWhyhttpRequest

Shorthand for self.should().query_exists(key).

Source

pub fn should_without_query<K: Into<String>>( &self, key: K, ) -> ShouldWhyhttpRequest

Shorthand for self.should().without_query(key).

Source

pub fn should_header<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> ShouldWhyhttpRequest

Shorthand for self.should().header(key, value).

Source

pub fn should_header_exists<K: Into<String>>( &self, key: K, ) -> ShouldWhyhttpRequest

Shorthand for self.should().header_exists(key).

Source

pub fn should_without_header<K: Into<String>>( &self, key: K, ) -> ShouldWhyhttpRequest

Shorthand for self.should().without_header(key).

Source

pub fn should_body<S: Into<String>>(&self, body: S) -> ShouldWhyhttpRequest

Shorthand for self.should().body(body).

Source

pub fn should_without_body(&self) -> ShouldWhyhttpRequest

Shorthand for self.should().without_body().

Source

pub fn should_times(&self, times: u16) -> ShouldWhyhttpRequest

Shorthand for self.should().times(times).

Source§

impl Whyhttp

Source

pub fn response_status(&self, status: u16) -> WhyhttpResponse

Shorthand for self.response().status(status).

Source

pub fn response_header<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> WhyhttpResponse

Shorthand for self.response().header(key, value).

Source

pub fn response_body<S: Into<String>>(&self, body: S) -> WhyhttpResponse

Shorthand for self.response().body(body).

Auto Trait Implementations§

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> 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, 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.