Spring REST 文档 - 更新从 REST Assured 测试生成的响应片段中的 URI

Spring REST Docs - Update URIs in Response Snippets Generated From REST Assured Tests

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

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

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

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.

下面的代码会有所帮助。

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);
    }

}