将 REST Assured 与 REST Docs 结合使用时,我遇到了请求端口更新位置的问题,但响应中的所有 HATEOAS 链接都指向运行测试的任何地址。
从 REST 文档文档中,我了解了如何使用预处理器更新请求:
.addFilter(document("{class-name}/{method-name}/{step}", preprocessRequest(
modifyUris().scheme("http")
.host("localhost")
.port(9999),
removeHeaders("Accept"))))
但找不到是否支持修改响应中的端口。例如,当我想将配置中的端口设置为9999时:
curl-request.adoc: (这很好:localhost:9999)
$ curl 'localhost:9999/request/data' -i
response-body.adoc: (我想将 localhost:51123 更改为 localhost:9999)
{
"_links" : {
"requests" : {
"href" : "localhost:51123/request/data/requests{?page,size,sort,projection}",
"templated" : true
},
"users" : {
"href" : "localhost:51123/request/data/users{?projection}",
"templated" : true
},
"profile" : {
"href" : "localhost:51123/request/data/profile"
}
}
}
是否有任何可接受的方式使用 REST Docs 或 REST Assured 来修改响应的内容?我想我可以创建一个 @AfterClass 方法来解析更新这些资源,但我希望有更干净的东西。
[后续] 我接受了下面 Andy W. 的回答,但想为遇到同样问题的人提供更多信息 -
我的问题是我尝试添加文档过滤器两次:
.addFilters(Arrays.asList(
document("{class-name}/{method-name}/{step}",
preprocessRequest(modifyUris().scheme("http")
.host("localhost")
.port(9999))),
document("{class-name}/{method-name}/{step}",
preprocessResponse(modifyUris().scheme("http")
.host("localhost")
.port(9999))))
对比使用参数调用文档方法:
RestDocumentationFilter 文档(字符串标识符、OperationRequestPreprocessor requestPreprocessor、OperationResponsePreprocessor responsePreprocessor、Snippet...片段)
一旦我做出了改变,一切就按预期进行了。 干杯!
最佳答案
是的,有。来自 the documentation :
modifyUris
onRestAssuredPreprocessors
can be used to modify any URIs in a request or a response. When using REST Assured, this allows you to customize the URIs that appear in the documentation while testing a local instance of the service.
关于java - Spring REST 文档 - 更新 REST 确保测试生成的响应片段中的 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46052718/