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 ()

Response Failure: POST request to "http://localhost:4000/echo" returned 1 error(s)   1) Response raw body data does not match, expected the following 16 bytes:   [0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89, 0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78]   but got the following 16 bytes instead:   [0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89]

From test_body_text_mismatch_diff_added ()

Response Failure: GET request to "http://localhost:4000/get/hello" returned 1 error(s)   1) Response text body does not match, expected:   "Hello \n"   but got:   "Hello World"   difference:   "Hello World \n"

From test_body_with_expected_json_mismatch ()

Response Failure: POST request to "http://localhost:4000/echo" returned 1 error(s)   1) Response body JSON does not match:   - json.key: String does not match, expected:   "different value"   but got:   "value"   difference:   "different value"   - json.list: Array with 2 item(s) does not match expected length of 3   - json.some.very.deeply.nested.array: Array with 3 item(s) does not match expected length of 2   - json.some.very.deeply.nested.array[0]: Boolean (false) does not match expected value (true)   - json.some.very.deeply.nested.array[1]: Boolean (true) does not match expected value (false)   - json: Object is missing 1 key(s) (missing)

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 ()

Response Failure: POST request to "http://localhost:4000/echo" returned 1 error(s)   1) Response headers dump:   Accept: application/json Content-Length: 64 Content-Type: application/octet-stream Date: Host: localhost:4000   Response raw body dump of 64 bytes:   [0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89, 0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89, 0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89, 0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89]

From test_dump_response_with_text_body ()

Response Failure: POST request to "http://localhost:4000/echo" returned 1 error(s)   1) Response headers dump:   Accept: application/json Content-Length: 39 Content-Type: text/plain Date: Host: localhost:4000   Response body dump:   "Hello World Body Text JSON \nA new line."

From test_dump_response_with_json_body ()

Response Failure: POST request to "http://localhost:4000/echo" returned 1 error(s)   1) Response headers dump:   Accept: application/json Content-Length: 58 Content-Type: application/json Date: Host: localhost:4000   Response body dump:   { "array": [ 0, 1, 2, 3 ], "key": "Value", "null": null, "number": 123 }

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.

fn drop(&mut self)

A method called when the value goes out of scope. Read more