自定义请求和响应

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

预处理通过调用document使用OperationRequestPreprocessorOperationResponsePreprocessor. 您可以使用静态preprocessRequestpreprocessResponse方法Preprocessors. 以下示例显示了如何执行此作:spring-doc.cadn.net.cn

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

模拟视频
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 应用一个响应预处理器来漂亮地打印其内容。
Web测试客户端
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

模拟视频
this.mockMvc.perform(get("/"))
	.andExpect(status().isOk())
	.andDo(document("index", links(linkWithRel("self").description("Canonical self link"))));
Web测试客户端
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格式化请求或响应的内容,使其更易于阅读。spring-doc.cadn.net.cn

如果您正在记录基于超媒体的 API,您可能希望鼓励客户端使用链接而不是使用硬编码 URI 来导航 API。 一种方法是限制文档中 URI 的使用。maskLinksPreprocessorshref响应中的任何链接的…​. 如果需要,还可以指定不同的替换。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