【问题标题】:Spring REST Docs - Update URIs in Response Snippets Generated From REST Assured TestsSpring REST Docs - 更新从 REST 保证测试生成的响应片段中的 URI
【发布时间】:2017-09-05 10:25:59
【问题描述】:

在将 REST Assured 与 REST Docs 结合使用时,我对请求的端口更新位置有疑问,但响应中的所有 HATEOAS 链接都指向运行测试的任何地址。

从 REST Docs 文档中,我了解了如何使用预处理器更新请求:

.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. 下面的回答,但想为遇到相同问题的任何人提供更多信息 -

我的问题是我试图添加 document 过滤器两次:

.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...sn-ps)

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...-

一旦我做出改变,一切都按预期工作。 干杯!

【问题讨论】:

    标签: java spring rest-assured spring-restdocs


    【解决方案1】:

    是的,有。来自the documentation

    modifyUris on RestAssuredPreprocessors 可用于修改请求或响应中的任何 URI。使用 REST Assured 时,这允许您在测试服务的本地实例时自定义出现在文档中的 URI。

    【讨论】:

    • 感谢 Andy 的澄清,感谢 Spring 提供的所有出色的工作和支持!接受了您的回答,并在我的问题中添加了更多详细信息。
    【解决方案2】:

    下面的代码会很有帮助。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.restdocs.RestDocsMockMvcConfigurationCustomizer;
    import org.springframework.boot.test.context.TestConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.restdocs.operation.preprocess.UriModifyingOperationPreprocessor;
    
    import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyUris;
    import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
    
    @TestConfiguration
    public class RestDocsConfiguration {
    
        @Autowired
        TestProperties testProperties;
    
        @Bean
        public RestDocsMockMvcConfigurationCustomizer restDocsMockMvcConfigurationCustomizer() {
            UriModifyingOperationPreprocessor uriPreprocessor = modifyUris()
                    .scheme(testProperties.getScheme())
                    .host(testProperties.getHost())
                    .removePort();
    
            return configurer -> configurer.operationPreprocessors()
                    .withRequestDefaults(prettyPrint(), uriPreprocessor)
                    .withResponseDefaults(prettyPrint(), uriPreprocessor);
        }
    
    }
    

    【讨论】:

    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多