Struct noir::HttpResponse [] [src]

pub struct HttpResponse<E: HttpEndpoint> {
    // some fields omitted
}

A mocked HTTP response that is being provided to a testable API.

Methods

impl<E: HttpEndpoint> HttpResponse<E>
[src]

fn with_status(self, status_code: StatusCode) -> Self

Sets the response status.

fn with_headers(self, headers: Vec<HttpHeader>) -> Self

Sets additional headers to be send with the response.

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

fn with_query(self, query: HttpQueryString) -> Self

Sets the query string of the response's path.

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 response body.

Also sets the Content-Type header of the response based on the type of the body argument.

The set Content-Type can be overridden via HttpResponse::with_header.

fn with_error(self, error: Error) -> Self

Sets a low level IO error to be returned once the response is read.

fn with_options(self, options: Options) -> Self

Sets the response's configuration options.

This allows to change or override the default response behaviour.

fn expected_header<H: Header + HeaderFormat>(self, header: H) -> Self

Sets one additional header that should be present on the request to the response.

Test Failure

If the header is either missing from the request 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 request made to the response.

Test Failure

If the header is present on the request.

fn expected_headers(self, headers: Vec<HttpHeader>) -> Self

Sets additional headers that should be present on the request to 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 request or their values differ from the expected ones.

fn expected_body<S: Into<HttpBody>>(self, body: S) -> Self

Sets the expected request body for the response.

The expected and the actual body are compared based on the MIME type of the request.

text/*

These Compared as strings, if no other character encoding is set in the request's MIME type, UTF-8 will be used as the default.

application/json

JSON objects are deep compared, but additional keys on request objects are ignored.

This allows for simpler and more fine grained assertions against JSON requests.

All other mime types

These are compared on a byte by byte basis.

Test Failure

If the actual request body does not match the expected one.

Test Failure Examples

From test_provided_response_with_expected_body_raw_mismatch ()

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

From test_provided_response_with_expected_body_text_mismatch_diff_added ()

Response Failure: POST request to "http://localhost:4000/response/forward" returned 1 error(s)   1) Request Failure: POST response provided for "https://example.com/forward" returned 1 error(s)   1.1) Request text body does not match, expected:   "Response"   but got:   "Response Body"   difference:   "Response Body"

From test_provided_response_with_expected_body_json_mismatch ()

Response Failure: POST request to "http://localhost:4000/response/forward" returned 1 error(s)   1) Request Failure: POST response provided for "https://example.com/forward" returned 1 error(s)   1.1) Request 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)

From test_provided_response_with_expected_body_form_mismatch ()

Response Failure: POST request to "http://localhost:4000/response/forward" returned 1 error(s)   1) Request Failure: POST response provided for "https://example.com/forward" returned 1 error(s)   1.1) Request body form data does not match:   - form.array[]: Array with 4 item(s) does not match expected length of 5   - form.array[][3]: ArrayItem value does not match, expected:   "5\n"   but got:   "4"   difference:   "5\n 4"   - form.field: Field value does not match, expected:   "different someValue\n"   but got:   "someValue\n"   difference:   "different someValue\n"   - form.mismatchedType: Field value does not match, expected:   "array"   but got:   "plain"   difference:   "array plain"   - form.str_file: MIME type (text/html) does not match expected value (text/plain)   - form.vec_file: Filename ("other.bin") does not match expected value ("file.bin")   - form: Is missing 1 plain field(s) (missingField)

fn expected_exact_body<S: Into<HttpBody>>(self, body: S) -> Self

Sets the expected request body for the response, exact version.

This method is based on HttpRequest::expected_body() but performs additional comparison based on the mime type of the request:

application/json

In contrast to HttpResponse::expected_body() additional keys on request objects are compared and will fail the test.

All other mime types

See HttpResponse::expected_body().

Test Failure

If the actual request body does not match the expected one.

fn dump(self) -> Self

Dumps the request headers and body for this response.

Test Failure

Always.

Test Failure Examples

From test_provided_response_dump_raw ()

Response Failure: POST request to "http://localhost:4000/response/forward" returned 1 error(s)   1) Request Failure: POST response provided for "https://example.com/forward" returned 1 error(s)   1.1) Request headers dump:   Content-Length: 16 Content-Type: application/octet-stream Host: example.com   Request raw body dump of 16 bytes:   [0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89]

From test_provided_response_dump_text ()

Response Failure: POST request to "http://localhost:4000/response/forward" returned 1 error(s)   1) Request Failure: POST response provided for "https://example.com/forward" returned 1 error(s)   1.1) Request headers dump:   Content-Length: 13 Content-Type: text/plain Host: example.com   Request body dump:   "Response Body"

From test_provided_response_dump_json ()

Response Failure: POST request to "http://localhost:4000/response/forward" returned 1 error(s)   1) Request Failure: POST response provided for "https://example.com/forward" returned 1 error(s)   1.1) Request headers dump:   Content-Length: 121 Content-Type: application/json Host: example.com   Request body dump:   { "additional": 32, "key": "different value", "list": [ 2, 3 ], "some": { "very": { "deeply": { "nested": { "array": [ false, true, false ] } } } } }

Trait Implementations

impl<E: HttpEndpoint> MockResponse for HttpResponse<E>
[src]

fn matches(&self, request: &Box<MockRequest>) -> bool

Compare the response against any type implementing MockRequest and return true if they belong together. Read more

fn respond(&mut self, request: Box<MockRequest>) -> Result<Vec<u8>, Error>

Called when the implementation of MockResponse::matches return true. Read more

fn validate(&mut self, response_index: usize, request_index: usize) -> Vec<String>

If the response has a matching MockRequest, compare the two and return a vector of error messages listing any differences. Read more

fn validate_header(&self, error_count: usize) -> String

Return a header for use with the formatted error values returned by MockResponse::validate(). Read more