在来自Wiremock的存根响应中插入Cookie

时间:2019-03-13 13:45:28

标签: cookies wiremock

我有一个用于端点的简单的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>

1 个答案:

答案 0 :(得分:1)

cookie只是具有属性名称:“ Set-Cookie”的HTTP标头。以下示例适用于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";
    }
}