英文:
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
:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论