Wiremock无法匹配请求。

huangapple go评论80阅读模式
英文:

Wiremock failed to match request

问题

我正在尝试断言在OAuth2流程期间调用的先前存根化的端点:

stubFor(
    post(urlPathEqualTo("/token"))
        .withHeader(AUTHORIZATION, equalTo("Basic cGluLmFwaS5jbGllbnRJZDpwaW4uYXBpLmNsaWVudFNlY3JldA=="))
        .withHeader(CONTENT_TYPE, equalTo("application/x-www-form-urlencoded;charset=UTF-8"))
        .withHeader(ACCEPT, equalTo(APPLICATION_JSON_VALUE + "," + APPLICATION_FORM_URLENCODED_VALUE))
        .willReturn(aResponse()
            .withStatus(HttpStatus.OK.value())
            .withHeader(CONTENT_TYPE, "application/json")
            .withHeader(CONNECTION, "Close")
            .withBody("{" +
                "\"access_token\": \"62466f2c-ff9e-4c6c-a866-b8296cf78041\"," +
                "\"scope\": \"trust read write\"," +
                "\"token_type\": \"bearer\"," +
                "\"expires_in\": 20300" +
                "}")));

并且

verify(postRequestedFor(urlPathEqualTo("/token")));

有一段日志,存根化/实际请求看起来是相等的,但是Wiremock报错“Header does not match”:
[![enter image description here][1]][1]

请问有人可以建议可能出了什么问题吗?提前谢谢!

  [1]: https://i.stack.imgur.com/2p7uV.png
英文:

I'm trying to assert previously stubbed endpoint that is being called during the OAuth2 flow:

        stubFor(
        post(urlPathEqualTo("/token"))
            .withHeader(AUTHORIZATION, equalTo("Basic cGluLmFwaS5jbGllbnRJZDpwaW4uYXBpLmNsaWVudFNlY3JldA=="))
            .withHeader(CONTENT_TYPE, equalTo("application/x-www-form-urlencoded;charset=UTF-8"))
            .withHeader(ACCEPT, equalTo(APPLICATION_JSON_VALUE + "," + APPLICATION_FORM_URLENCODED_VALUE))
            .willReturn(aResponse()
                .withStatus(HttpStatus.OK.value())
                .withHeader(CONTENT_TYPE, "application/json")
                .withHeader(CONNECTION, "Close")
                .withBody("{\n" +
                    "  \"access_token\": \"62466f2c-ff9e-4c6c-a866-b8296cf78041\",\n" +
                    "  \"scope\": \"trust read write\",\n" +
                    "  \"token_type\": \"bearer\",\n" +
                    "  \"expires_in\": 20300,\n" +
                    "}")));

and

verify(postRequestedFor(urlPathEqualTo("/token")));

There is a piece of log, stubbed/actual requests look equals, but Wiremock says Header does not match:
Wiremock无法匹配请求。

Could someone, please, suggest what can be wrong here? Thank you in advance!

答案1

得分: 1

这对我来说看起来像是逗号后面有空格的问题,尝试一下:

.withHeader(ACCEPT equalTo(APPLICATION_JSON_VALUE + " " + APPLICATION_FORM_URLENCODED_VALUE))

顺便提一下,使用 equalTo 来比较标头可能会不稳定。你可以考虑使用 containing

post(urlPathEqualTo("/token"))
    .withHeader(ACCEPT, containing(APPLICATION_JSON_VALUE))
    .withHeader(ACCEPT, containing(APPLICATION_FORM_URLENCODED_VALUE))
英文:

To me it looks like an issue with a space after comma, try:

.withHeader(ACCEPT, equalTo(APPLICATION_JSON_VALUE + ", " + APPLICATION_FORM_URLENCODED_VALUE))

By the way, comparing headers with equalTo might be flaky. You might consider using containing:

post(urlPathEqualTo("/token"))
    .withHeader(ACCEPT, containing(APPLICATION_JSON_VALUE))
    .withHeader(ACCEPT, containing(APPLICATION_FORM_URLENCODED_VALUE))

huangapple
  • 本文由 发表于 2020年10月13日 01:32:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64322641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定