在 wiremock 的存根响应中插入 cookie

Inserting cookie in stubbed response from wiremock

我有一个简单的接线端点存根。调用此存根的库期望在响应中出现一个 cookie。有没有一种简单的方法可以在 wiremock 配置的响应中提供 cookie

存根端点的示例代码:

    stubFor(post(urlPathEqualTo("/endpoint"))
                .willReturn(aResponse()
                .withStatus(OK.getStatusCode())
                //with a cookie;

这可行吗?我正在使用以下 wiremock 版本

<dependency>
  <groupId>com.github.tomakehurst</groupId>
  <artifactId>wiremock</artifactId>
  <version>2.19.0</version>
</dependency>

Cookie 只不过是具有属性名称的 HTTP header:"Set-Cookie"。下面的示例适用于 JSON 变体,但应该很容易转换为 Java 样式:.withHeader("Set-Cookie", "JSESSIONID=dcba")));

{
    "metadata": {
        "title": "Cookie example",
        "description": "Example to return a Cookie",
    },
    "request": {
        "method": "ANY",
        "urlPath": "/returnCookie"
    },
    "response": {
        "status": 200,
        "headers": {
            "Set-Cookie": ["JSESSIONID=ABSCDEDASDSSDSSE.oai007; path=/; Secure; HttpOnly"]
        },
        "body": "This stores a cookie";
    }
}