2017-09-05 20 views
1

当使用REST Assured和REST Docs时,我有一个问题,请求的端口已更新,但响应中的所有HATEOAS链接指向任何解决测试运行。Spring REST Docs - 从REST确保测试生成的响应代码段中更新URI

从REST文档的文档,我看到如何使用预处理器更新的要求:

.addFilter(document("{class-name}/{method-name}/{step}", preprocessRequest(
    modifyUris().scheme("http") 
      .host("localhost") 
      .port(9999), 
    removeHeaders("Accept")))) 

,但无法找到,如果有支持修改端口INT的响应。举例来说,当我想在配置中的端口设置为9999:

卷曲request.adoc:(这是一件好事:本地主机:9999)

$ curl 'localhost:9999/request/data' -i 

响应体。 ADOC:(我想改变本地主机: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方法来解析更新这些资源,但我希望有更清洁的东西。


[后续] 我接受了来自安迪·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,片段...片段)

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

一旦我做了改变这一切都按预期。 干杯!

回答

1

是的,有。从the documentation

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

+0

谢谢安迪的澄清,并为所有的真棒工作和支持你在春天提供的所有! 接受你的答案,并添加一些更多的细节,我的问题。 – Groater