自定义请求和响应

在某些情况下,您不希望完全按照发送请求的方式记录请求,或者不希望完全按照收到的方式记录响应。 Spring REST Docs 提供了许多预处理器,可用于在记录请求或响应之前对其进行修改。spring-doc.cadn.net.cn

预处理是通过调用document替换为OperationRequestPreprocessorOperationResponsePreprocessor. 您可以通过静态preprocessRequestpreprocessResponse方法Preprocessors. 以下示例说明如何执行此作:spring-doc.cadn.net.cn

MockMvc
this.mockMvc.perform(get("/"))
	.andExpect(status().isOk())
	.andDo(document("index", preprocessRequest(modifyHeaders().remove("Foo")), (1)
			preprocessResponse(prettyPrint()))); (2)
1 应用一个请求预处理器,该处理器会删除名为Foo.
2 应用一个响应预处理器,该预处理器可以漂亮地打印其内容。
WebTest客户端
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 应用一个响应预处理器,该预处理器可以漂亮地打印其内容。

或者,您可能希望将相同的预处理器应用于每个测试。 您可以使用RestDocumentationConfigurerAPI 中@Before方法来配置预处理器。 例如,要删除Fooheader 并从所有请求中打印所有响应,您可以执行以下作之一(取决于您的测试环境):spring-doc.cadn.net.cn

MockMvc
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 应用一个响应预处理器,该预处理器可以漂亮地打印其内容。
WebTest客户端
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 应用一个响应预处理器,该预处理器可以漂亮地打印其内容。

然后,在每个测试中,您可以执行特定于该测试的任何配置。 以下示例说明如何执行此作:spring-doc.cadn.net.cn

MockMvc
this.mockMvc.perform(get("/"))
	.andExpect(status().isOk())
	.andDo(document("index", links(linkWithRel("self").description("Canonical self link"))));
WebTest客户端
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. 有关更多详细信息,请参阅下文spring-doc.cadn.net.cn

预处理器

漂亮的印刷

prettyPrintPreprocessors设置 Request 或 Response 内容的格式,使其更易于阅读。spring-doc.cadn.net.cn

如果要记录基于超媒体的 API,则可能需要鼓励客户端使用链接而不是使用硬编码的 URI 来导航 API。 一种方法是在文档中限制 URI 的使用。maskLinksPreprocessors替换href响应中具有…​. 如果您愿意,您还可以指定不同的替换。spring-doc.cadn.net.cn

修改标头

您可以使用modifyHeadersPreprocessors添加、设置和删除请求或响应标头。spring-doc.cadn.net.cn

替换模式

replacePatternPreprocessors提供用于替换请求或响应中内容的通用机制。 与正则表达式匹配的任何匹配项都将被替换。spring-doc.cadn.net.cn

修改 URI

如果使用 MockMvc 或未绑定到服务器的 WebTestClient,则应通过更改配置来自定义 URI。

您可以使用modifyUrisPreprocessors修改请求或响应中的任何 URI。 当使用绑定到服务器的 REST Assured 或 WebTestClient 时,这允许您在测试服务的本地实例时自定义文档中显示的 URI。spring-doc.cadn.net.cn

编写您自己的预处理器

如果其中一个内置预处理器不能满足您的需求,您可以通过实现OperationPreprocessor接口。 然后,您可以按照与任何内置预处理器完全相同的方式使用自定义预处理器。spring-doc.cadn.net.cn

如果您只想修改请求或响应的内容(正文),请考虑实现ContentModifier界面并将其与内置的ContentModifyingOperationPreprocessor.spring-doc.cadn.net.cn