自定义请求和响应
在某些情况下,您可能不想完全按照发送的方式记录请求或完全按照收到的响应记录。 Spring REST Docs 提供了许多预处理器,可用于在记录请求或响应之前对其进行修改。
预处理通过调用document
使用OperationRequestPreprocessor
或OperationResponsePreprocessor
.
您可以使用静态preprocessRequest
和preprocessResponse
方法Preprocessors
. 以下示例显示了如何执行此作:
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 | 应用一个响应预处理器来漂亮地打印其内容。 |
或者,您可能希望将相同的预处理器应用于每个测试。
您可以使用RestDocumentationConfigurer
API 在您的@Before
配置预处理器的方法。
例如,要删除Foo
header 并漂亮打印所有响应,您可以执行以下作之一(取决于您的测试环境):
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
响应中的任何链接的…
.
如果需要,还可以指定不同的替换。
修改标题
您可以使用modifyHeaders
上Preprocessors
以添加、设置和删除请求或响应标头。
替换模式
replacePattern
上Preprocessors
提供了一种通用机制,用于替换请求或响应中的内容。
与正则表达式匹配的任何匹配项都将被替换。
修改 URI
如果您使用 MockMvc 或未绑定到服务器的 WebTestClient,则应通过更改配置来自定义 URI。 |
您可以使用modifyUris
上Preprocessors
以修改请求或响应中的任何 URI。
使用绑定到服务器的 REST Assured 或 WebTestClient 时,这允许您在测试服务的本地实例时自定义文档中显示的 URI。
编写您自己的预处理器
如果其中一个内置预处理器不能满足你的需求,你可以通过实现OperationPreprocessor
接口。
然后,您可以以与任何内置预处理器完全相同的方式使用自定义预处理器。
如果只想修改请求或响应的内容(正文),请考虑实现ContentModifier
接口并将其与内置的ContentModifyingOperationPreprocessor
.