How to verify variable items with WireMock's RequestBodyMatching

I often use WireMock when I want to perform a mocked test of the other party. Request matching can also be performed, which is very convenient.

stubFor(post(urlEqualTo("/mockurl"))
    .withRequestBody(equalToJson(REQUEST_JSON_STRING))
    .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
    .withBody(RESPONSE_JSON_STRING)));

If you write as above, RESPONSE_JSON_STRING will be returned only when a JSON message that matches REQUEST_JSON_STRING is accepted.

However,

--If the app issues a random string --If the app uses the current time

There are many items that are difficult to match.

{
    "token": "uPx2bB9"
    "accept_date": "2018-01-01T00:00:00.000Z",
}

Also, if you want to read REQUEST_JSON_STRING from a file, you don't want to prepare as many files as there are variable items.

In such a case, add an argument to ʻequalToJson`.

StringValuePattern com.github.tomakehurst.wiremock.client.WireMock.equalToJson(String value, boolean ignoreArrayOrder, boolean ignoreExtraElements)

By setting ʻignoreExtraElements = true, elements that do not exist in value` will be ignored.

stubFor(post(urlEqualTo("/mockurl"))
    .withRequestBody(equalToJson(REQUEST_JSON_STRING, true, true))
    .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
    .withBody(RESPONSE_JSON_STRING)));

If you set it as above,

REQUEST_JSON_STRING

{
    "name": "hoge",
    "data": {
        "value": 1000
    }
}

Actual request:

{
    "name": "hoge",
    "data": {
        "token": "uPx2bB9",
        "id": "abcde",
        "value": 1000
    }
}

But the matching is successful (the items token and ʻidare ignored). You can also usematchingJsonPath` to validate ignored items.

StringValuePattern com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath(String value)

If you want to verify that the element exists (When the current time or random character string is issued by the app and it is difficult to compare)

stubFor(post(urlEqualTo("/mockurl"))
    .withRequestBody(equalToJson(REQUEST_JSON_STRING, true, true))
    .withRequestBody(matchingJsonPath("$.data.token")) // "token"Verify that there is an item
    .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
    .withBody(RESPONSE_JSON_STRING)));

If you also want to verify the value of an element (For example, when you want to verify variable elements according to test data)

stubFor(post(urlEqualTo("/mockurl"))
    .withRequestBody(equalToJson(REQUEST_JSON_STRING, true, true))
    .withRequestBody(matchingJsonPath("$.data[?(@.id == 'abcde')]")) // "id"Validate the value of
    .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
    .withBody(RESPONSE_JSON_STRING)));

Recommended Posts

How to verify variable items with WireMock's RequestBodyMatching
How to number (number) with html.erb
How to update with activerecord-import
How to scroll horizontally with ScrollView
How to get started with slim
How to enclose any character with "~"
How to use mssql-tools with alpine
How to write Java variable declaration
How to get along with Rails
How to start Camunda with Docker
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
[Note] How to get started with Rspec
How to do API-based control with cancancan
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to handle sign-in errors with devise
How to delete data with foreign key
How to monitor nginx with docker-compose with datadog
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to download Oracle JDK 8 rpm with curl
[Java] How to test for null with JUnit
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
How to mock each case with PowerMock + Mockito1x
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to save to multiple tables with one input
How to test interrupts during Thread.sleep with JUnit
How to use built-in h2db with spring boot
How to search multiple columns with gem ransack
How to use Java framework with AWS Lambda! ??
How to create multiple pull-down menus with ActiveHash
How to use Java API with lambda expression
How to give your image to someone with docker
How to insert all at once with MyBatis
How to write test code with Basic authentication
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
How to get resource files out with spring-boot
How to create member variables with JPA Model
How to use nginx-ingress-controller with Docker for Mac
[Rails] How to build an environment with Docker
How to avoid exceptions with Java's equals method
How to redirect after user login with Spring-security
How to deploy
How to use a structure with variable length array or bit field in Ruby-FFI
How to embed JavaScript variables in HTML with Thymeleaf
How to implement UICollectionView in Swift with code only
How to change the action with multiple submit buttons
How to sort in ascending / descending order with SQLite
How to make a factory with a model with polymorphic association