自定义请求和响应
可能存在某些情况,您不希望完全按照发送时的原样记录请求,或完全按照接收时的原样记录响应。 Spring REST Docs 提供了多种预处理器,可在记录文档之前对请求或响应进行修改。
预处理通过调用 document 并传入 OperationRequestPreprocessor 或 OperationResponsePreprocessor 来配置。
您可以通过 Preprocessors 上的静态 preprocessRequest 和 preprocessResponse 方法获取实例。
以下示例展示了如何实现:
this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andDo(document("index", preprocessRequest(modifyHeaders().remove("Foo")), (1)
preprocessResponse(prettyPrint()))); (2)
| 1 | 应用一个请求预处理器,移除名为 Foo 的请求头。 |
| 2 | 应用一个响应预处理器,以美化其内容输出。 |
this.webTestClient.get().uri("/").exchange().expectStatus().isOk().expectBody()
.consumeWith(document("index",
preprocessRequest(modifyHeaders().remove("Foo")), (1)
preprocessResponse(prettyPrint()))); (2)
| 1 | 应用一个请求预处理器,移除名为 Foo 的请求头。 |
| 2 | 应用一个响应预处理器,以美化其内容输出。 |
RestAssured.given(this.spec)
.filter(document("index", preprocessRequest(modifyHeaders().remove("Foo")), (1)
preprocessResponse(prettyPrint()))) (2)
.when()
.get("/")
.then()
.assertThat()
.statusCode(is(200));
| 1 | 应用一个请求预处理器,移除名为 Foo 的请求头。 |
| 2 | 应用一个响应预处理器,以美化其内容输出。 |
或者,您可能希望对每个测试应用相同的预处理器。
您可以通过在 @Before 方法中使用 RestDocumentationConfigurer API 来配置预处理器,从而实现这一点。
例如,要从所有请求中移除 Foo 头并美化打印所有响应,您可以执行以下操作之一(具体取决于您的测试环境):
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
.withRequestDefaults(modifyHeaders().remove("Foo")) (1)
.withResponseDefaults(prettyPrint())) (2)
.build();
}
| 1 | 应用一个请求预处理器,移除名为 Foo 的请求头。 |
| 2 | 应用一个响应预处理器,以美化其内容输出。 |
private WebTestClient webTestClient;
@Before
public void setup() {
this.webTestClient = WebTestClient.bindToApplicationContext(this.context)
.configureClient()
.filter(documentationConfiguration(this.restDocumentation)
.operationPreprocessors()
.withRequestDefaults(modifyHeaders().remove("Foo")) (1)
.withResponseDefaults(prettyPrint())) (2)
.build();
}
| 1 | 应用一个请求预处理器,移除名为 Foo 的请求头。 |
| 2 | 应用一个响应预处理器,以美化其内容输出。 |
private RequestSpecification spec;
@Before
public void setup() {
this.spec = new RequestSpecBuilder()
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
.withRequestDefaults(modifyHeaders().remove("Foo")) (1)
.withResponseDefaults(prettyPrint())) (2)
.build();
}
| 1 | 应用一个请求预处理器,移除名为 Foo 的请求头。 |
| 2 | 应用一个响应预处理器,以美化其内容输出。 |
然后,在每个测试中,您可以执行特定于该测试的任何配置。 以下示例展示了如何操作:
this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andDo(document("index", links(linkWithRel("self").description("Canonical self link"))));
this.webTestClient.get().uri("/").exchange().expectStatus().isOk()
.expectBody().consumeWith(document("index",
links(linkWithRel("self").description("Canonical self link"))));
RestAssured.given(this.spec)
.filter(document("index", links(linkWithRel("self").description("Canonical self link"))))
.when()
.get("/")
.then()
.assertThat()
.statusCode(is(200));
各种内置预处理器(包括上述示例)均可通过 Preprocessors 上的静态方法使用。
详见下文以获取更多信息。
预处理器
美观打印
prettyPrint 在 Preprocessors 上对请求或响应的内容进行格式化,使其更易于阅读。
链接掩码
如果您正在编写基于超媒体的 API 文档,您可能希望鼓励客户端通过使用链接而非硬编码的 URI 来导航 API。
实现这一点的一种方法是在文档中限制 URI 的使用。
maskLinks 在 Preprocessors 上会将响应中任何链接的 href 替换为 …。
如果您愿意,也可以指定不同的替换值。
修改请求头
您可以在 Preprocessors 上使用 modifyHeaders 来添加、设置和移除请求或响应头。
替换模式
replacePattern 在 Preprocessors 上提供了一种通用机制,用于替换请求或响应中的内容。
任何匹配正则表达式的出现都将被替换。
修改 URI
| 如果您使用的 MockMvc 或 WebTestClient 未绑定到服务器,则应通过更改配置来自定义 URI。 |
您可以在 Preprocessors 上使用 modifyUris 来修改请求或响应中的任何 URI。
当使用绑定到服务器的 REST Assured 或 WebTestClient 时,这允许您在测试服务的本地实例时自定义文档中显示的 URI。
编写您自己的预处理器
如果内置的预处理器之一无法满足您的需求,您可以通过实现 OperationPreprocessor 接口来编写自己的预处理器。
然后,您可以像使用任何内置预处理器一样使用您的自定义预处理器。
如果您只想修改请求或响应的内容(body),请考虑实现 ContentModifier 接口,并将其与内置的 ContentModifyingOperationPreprocessor 一起使用。