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
impl Whyhttp
Sourcepub fn run() -> Self
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://"));Sourcepub fn when(&self) -> WhenWhyhttpRequest
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);Sourcepub fn should(&self) -> ShouldWhyhttpRequest
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);Sourcepub fn response(&self) -> WhyhttpResponse
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
impl Whyhttp
Sourcepub fn when_path<S: Into<String>>(&self, path: S) -> WhenWhyhttpRequest
pub fn when_path<S: Into<String>>(&self, path: S) -> WhenWhyhttpRequest
Shorthand for self.when().path(path).
Sourcepub fn when_method<S: Into<String>>(&self, method: S) -> WhenWhyhttpRequest
pub fn when_method<S: Into<String>>(&self, method: S) -> WhenWhyhttpRequest
Shorthand for self.when().method(method).
Sourcepub fn when_query<K: Into<String>, V: Into<String>>(
&self,
key: K,
value: V,
) -> WhenWhyhttpRequest
pub fn when_query<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> WhenWhyhttpRequest
Shorthand for self.when().query(key, value).
Sourcepub fn when_query_exists<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest
pub fn when_query_exists<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest
Shorthand for self.when().query_exists(key).
Sourcepub fn when_without_query<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest
pub fn when_without_query<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest
Shorthand for self.when().without_query(key).
Sourcepub fn when_header<K: Into<String>, V: Into<String>>(
&self,
key: K,
value: V,
) -> WhenWhyhttpRequest
pub fn when_header<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> WhenWhyhttpRequest
Shorthand for self.when().header(key, value).
Sourcepub fn when_header_exists<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest
pub fn when_header_exists<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest
Shorthand for self.when().header_exists(key).
Sourcepub fn when_without_header<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest
pub fn when_without_header<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest
Shorthand for self.when().without_header(key).
Sourcepub fn when_body<S: Into<String>>(&self, body: S) -> WhenWhyhttpRequest
pub fn when_body<S: Into<String>>(&self, body: S) -> WhenWhyhttpRequest
Shorthand for self.when().body(body).
Sourcepub fn when_without_body(&self) -> WhenWhyhttpRequest
pub fn when_without_body(&self) -> WhenWhyhttpRequest
Shorthand for self.when().without_body().
Source§impl Whyhttp
impl Whyhttp
Sourcepub fn should_path<S: Into<String>>(&self, path: S) -> ShouldWhyhttpRequest
pub fn should_path<S: Into<String>>(&self, path: S) -> ShouldWhyhttpRequest
Shorthand for self.should().path(path).
Sourcepub fn should_method<S: Into<String>>(&self, method: S) -> ShouldWhyhttpRequest
pub fn should_method<S: Into<String>>(&self, method: S) -> ShouldWhyhttpRequest
Shorthand for self.should().method(method).
Sourcepub fn should_query<K: Into<String>, V: Into<String>>(
&self,
key: K,
value: V,
) -> ShouldWhyhttpRequest
pub fn should_query<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> ShouldWhyhttpRequest
Shorthand for self.should().query(key, value).
Sourcepub fn should_query_exists<K: Into<String>>(
&self,
key: K,
) -> ShouldWhyhttpRequest
pub fn should_query_exists<K: Into<String>>( &self, key: K, ) -> ShouldWhyhttpRequest
Shorthand for self.should().query_exists(key).
Sourcepub fn should_without_query<K: Into<String>>(
&self,
key: K,
) -> ShouldWhyhttpRequest
pub fn should_without_query<K: Into<String>>( &self, key: K, ) -> ShouldWhyhttpRequest
Shorthand for self.should().without_query(key).
Sourcepub fn should_header<K: Into<String>, V: Into<String>>(
&self,
key: K,
value: V,
) -> ShouldWhyhttpRequest
pub fn should_header<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> ShouldWhyhttpRequest
Shorthand for self.should().header(key, value).
Sourcepub fn should_header_exists<K: Into<String>>(
&self,
key: K,
) -> ShouldWhyhttpRequest
pub fn should_header_exists<K: Into<String>>( &self, key: K, ) -> ShouldWhyhttpRequest
Shorthand for self.should().header_exists(key).
Sourcepub fn should_without_header<K: Into<String>>(
&self,
key: K,
) -> ShouldWhyhttpRequest
pub fn should_without_header<K: Into<String>>( &self, key: K, ) -> ShouldWhyhttpRequest
Shorthand for self.should().without_header(key).
Sourcepub fn should_body<S: Into<String>>(&self, body: S) -> ShouldWhyhttpRequest
pub fn should_body<S: Into<String>>(&self, body: S) -> ShouldWhyhttpRequest
Shorthand for self.should().body(body).
Sourcepub fn should_without_body(&self) -> ShouldWhyhttpRequest
pub fn should_without_body(&self) -> ShouldWhyhttpRequest
Shorthand for self.should().without_body().
Sourcepub fn should_times(&self, times: u16) -> ShouldWhyhttpRequest
pub fn should_times(&self, times: u16) -> ShouldWhyhttpRequest
Shorthand for self.should().times(times).
Source§impl Whyhttp
impl Whyhttp
Sourcepub fn response_status(&self, status: u16) -> WhyhttpResponse
pub fn response_status(&self, status: u16) -> WhyhttpResponse
Shorthand for self.response().status(status).
Sourcepub fn response_header<K: Into<String>, V: Into<String>>(
&self,
key: K,
value: V,
) -> WhyhttpResponse
pub fn response_header<K: Into<String>, V: Into<String>>( &self, key: K, value: V, ) -> WhyhttpResponse
Shorthand for self.response().header(key, value).
Sourcepub fn response_body<S: Into<String>>(&self, body: S) -> WhyhttpResponse
pub fn response_body<S: Into<String>>(&self, body: S) -> WhyhttpResponse
Shorthand for self.response().body(body).