java - Spring REST 文档 - 更新 REST 确保测试生成的响应片段中的 URI

标签 java spring rest-assured spring-restdocs

将 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 requestPreprocessorOperationResponsePreprocessor responsePreprocessor、Snippet...片段)

https://docs.spring.io/spring-restdocs/docs/current/api/org/springframework/restdocs/restassured3/RestAssuredRestDocumentation.html#document-java.lang.String-org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor-org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor-org.springframework.restdocs.snippet.Snippet...-

一旦我做出了改变,一切就按预期进行了。 干杯!

最佳答案

是的,有。来自 the documentation :

modifyUris on RestAssuredPreprocessors 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/

相关文章:

Java:为什么在读取文本文件的情况下\n 被认为是 2 个字符?

java - JFileChooser 和 Bufferedreader 遇到问题

java - 尝试通过 SSL 连接到服务器的 SSLHandshakeException

mysql - Hibernate 一对多,我不想使用多个实体的主键

java - Hibernate Criteria 过滤器实体,其中 ManyToMany 关系包含多个对象

java - REST ASSURED TEST 创建我自己的 given() 方法

java - 安心API测试中如何传递数组post请求?

java - 严重 : Parse error in application web. xml 文件

java - 无法启动 Tomcat 服务器实例

java - REST 确保异步 - 找不到方法