Skip to content

Is it possible to assert on received request? #152

@mawkler

Description

@mawkler

Hi! I would like to assert on the received request body of a mock, but can't figure out how to acheive this.

To illustrate, let's say that I want to verify that a specific mock has received a request body that matches:

{
  "foo": "bar"
}

I tried to do this with Matchers:

let expected_body = json!({ "foo": "bar" });

wiremock::Mock::given(method("POST"))
    .and(wiremock::matchers::path("/foo"))
    .and(body_json(expected_body));

, but it seems that the mock now simply returns 404 if the body doesn't match. This does fail the test, but it fails it in a very subtle way. I would like for my tests to fail more explicitly with a message like "Mock received incorrect body ..." from assert_eq!.

I tried creating my custom Matcher that asserts on the body, but then I realized that since the mock runs in a totally separate process from the tests, this will just crash the mock server, which fails the test in an arguably even more subtle way.

I could check MockServer::received_requests, but this returns all requests for the entire test, which means that I have to somehow figure out which request was received by which mock.

Is there a better way to acheive this that I'm missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions