Skip to content

REST Docs WireMock stubs don't include cookie matching from MockMvc requests #2410

@marcingrzejszczak

Description

@marcingrzejszczak

When using Spring Cloud Contract's REST Docs integration (WireMockRestDocs.verify() and SpringCloudContractRestDocs.dslContract()), cookies added to MockMvc requests are not captured in the generated WireMock stub JSON files.

Use Case

E2E tests that need to select different stub responses based on test scenarios. The approach is to use a test_user cookie to identify which scenario the E2E test wants (e.g., test_user=free for free tier user, test_user=admin for admin user).

MockMvc tests include the cookie:

  mockMvc.perform(get("/api/billing")
          .cookie(new Cookie("test_user", "free")))
      .andExpect(status().isOk())
      .andDo(WireMockRestDocs.verify())
      .andDo(document("billing/get-free",
          SpringCloudContractRestDocs.dslContract()));

Expected Behavior

The generated WireMock stub should include cookie matching:

  {
    "request": {
      "urlPath": "/api/billing",
      "method": "GET",
      "cookies": {
        "test_user": {
          "equalTo": "free"
        }
      }
    },
    "response": {
      "status": 200,
      "body": "..."
    }
  }

Actual Behavior

The generated stub has no cookie matching:

  {
    "request": {
      "urlPath": "/api/billing",
      "method": "GET"
    },
    "response": {
      "status": 200,
      "body": "..."
    }
  }

This means when multiple stubs exist for the same endpoint (e.g., billing/get-free.json, billing/get-starter.json, billing/get-pro.json), WireMock can't differentiate between them and returns an
arbitrary match.

Current Workaround

use the verify().wiremock(...withCookie(...)) method

Environment

  • Spring Cloud Contract: 2025.1.0
  • Spring Boot: 4.0.2
  • WireMock: 3.13.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions