Struct noir::HttpRequest
[−]
[src]
pub struct HttpRequest<A: HttpApi> { // some fields omitted }
A HTTP request for API testing.
The request is automatically started once the instance goes out of scope
and any set up expectations are asserted and logged to stdout
in case of
failure.
HttpRequest::collect()
can be used to manually start the request.
Example Usage
Api::get("/") .with_headers(headers![ Connection::close(), Accept(vec![ qitem(Mime(TopLevel::Text, SubLevel::Plain, vec![])) ]) ]) .provide(responses![ ExternalResource.get("/") .with_status(StatusCode::Ok) .with_body("Hello World") .expected_header(Connection::close()) ]) .expected_status(StatusCode::Ok) .expected_body("Hello World")
Panics
If any of the set up expectations fail.
Methods
impl<A: HttpApi> HttpRequest<A>
[src]
fn with_headers(self, headers: Vec<HttpHeader>) -> Self
Sets additional headers to be send with the request.
Use the headers![...]
macro to easily create a vector containing
concrete types of the hyper::Header
trait for use with this method.
fn with_header<H: Header + HeaderFormat>(self, header: H) -> Self
Sets one additional header to be send with the request.
fn with_query(self, query: HttpQueryString) -> Self
Sets the request's query string.
This will override any existing query string previously set or derived from the request's path.
fn with_body<S: Into<HttpBody>>(self, body: S) -> Self
Sets the request body.
Also sets the Content-Type
header of the request based on the type
of the body
argument.
The set Content-Type
can be overridden via HttpRequest::with_header
.
fn with_options(self, options: Options) -> Self
Sets the request's configuration options.
This allows to change or override the default request behaviour.
fn expected_status(self, status_code: StatusCode) -> Self
Sets the expected response status for the request.
Test Failure
If the actual response status does not match the expected one.
Test Failure Examples
fn expected_header<H: Header + HeaderFormat>(self, header: H) -> Self
Sets one additional header that should be present on the response.
Test Failure
If the header is either missing from the response or its value does not match the expected one.
fn unexpected_header<H: Header + HeaderFormat>(self) -> Self
Sets one additional header that should be absent from the response.
Test Failure
If the header is present on the response.
Test Failure Examples
fn expected_headers(self, headers: Vec<HttpHeader>) -> Self
Sets additional headers that should be present on the response.
Use the headers![...]
macro to easily create a vector containing
concrete types of the hyper::Header
trait for use with this method.
Test Failure
If one or more of the headers are either missing from the response or their values differ from the expected ones.
fn expected_body<S: Into<HttpBody>>(self, body: S) -> Self
Sets the expected response body for the request.
The expected and the actual body are compared based on the MIME type of the response.
text/*
These Compared as strings, if no other character encoding is set in the response's MIME type, UTF-8 will be used as the default.
application/json
JSON objects are deep compared, but additional keys on response objects are ignored.
This allows for simpler and more fine grained assertions against JSON responses.
All other mime types
These are compared on a byte by byte basis.
Test Failure
If the actual response body does not match the expected one.
Test Failure Examples
From test_body_expected_raw_mismatch
(Collapse)
From test_body_text_mismatch_diff_added
(Expand)
From test_body_with_expected_json_mismatch
(Expand)
fn expected_exact_body<S: Into<HttpBody>>(self, body: S) -> Self
Sets the expected response body for the request (exact version).
This method is based on HttpRequest::expected_body()
but performs
additional comparison based on the mime type of the response:
application/json
In contrast to HttpResponse::expected_body()
additional keys on
response objects are compared and will fail the test.
All other mime types
See HttpResponse::expected_body()
.
Test Failure
If the actual response body does not match the expected one.
fn provide(self, resources: Vec<Box<MockResponse>>) -> Self
Provides additional mocked responses from endpoints for the time of the currently executing request.
Use the responses![...]
macro to easily create a vector containing
concrete types of the MockResponse
trait for use with this method.
Test Failure
If one or more of the provided responses is not called or does not match its expectations.
fn dump(self) -> Self
Dumps the response headers and body this request.
Test Failure
Always.
Test Failure Examples
From test_dump_response_with_raw_body
(Collapse)
From test_dump_response_with_text_body
(Expand)
From test_dump_response_with_json_body
(Expand)
fn mocks<T: MockProvider + 'static>(self, mocks: Vec<T>) -> Self
Provides additional mocks which will be active for the time of the currently executing request.
Use the mocks![...]
macro to easily create a vector containing
concrete types of the MockProvider
trait for use with this method.
fn collect(self) -> Result<(), String>
Directly execute the test request and collect any error message output.
Trait Implementations
impl<A: HttpApi> Drop for HttpRequest<A>
[src]
Automatically starts the request and logs any errors from set up expectations
to stdout
.
Panics
If any of the set up expectations fail.