如何使用Spring RestDocs来记录WebFlux响应的字段?

huangapple go评论71阅读模式
英文:

How do you use Spring RestDocs to document the fields of WebFlux responses?

问题

我有一个以FluxMono形式提供响应的 API,这些响应进而以服务器发送事件的形式提供 JSON 负载。

我还在使用Spring RestDocs来记录该负载的内容。这些内容是在WebFluxTest中生成的,

我可以使用PayloadDocumentation.responseBody()生成一个简单的 ResponseBody 片段,但是当我尝试使用字段描述符描述字段时...

@WebFluxTest
@AutoConfigureRestDocs
@ContextConfiguration(classes = ArticleHandler.class)
class HandlerTest { 
...

    @Test
    void testGetArticle() {
        webClient.get()
                .uri("/articles/{id}", "article-id")
                .exchange()
                .expectStatus().isOk().expectBody().consumeWith(
                        document("article", PayloadDocumentation.responseFields(fieldWithPath("id")
                        .type(JsonFieldType.STRING)
                        .description("博客文章的唯一标识符")));
    }
}

我得到了以下失败的测试:

[Fatal Error] :1:1: Content is not allowed in prolog.

无法处理 text/event-stream;charset=UTF-8 内容,因为无法将其解析为 JSON 或 XML
org.springframework.restdocs.payload.PayloadHandlingException: 无法处理 text/event-stream;charset=UTF-8 内容,因为无法将其解析为 JSON 或 XML
	at org.springframework.restdocs.payload.ContentHandler.forContentWithDescriptors(ContentHandler.java:69)
	at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:157)
	at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:78)
	at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:191)
	at org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.lambda$document$0(WebTestClientRestDocumentation.java:77)
	at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodyContentSpec.lambda$consumeWith$3(DefaultWebTestClient.java:564)
	at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:206)
	at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodyContentSpec.consumeWith(DefaultWebTestClient.java:564)
	at com.project.blog.article.HandlerTest.testGetArticle(HandlerTest.java:62)

是否有任何方法可以记录我在text/event-stream负载中发出的对象?

英文:

I have an API that serves up responses as Flux and Mono which in turn provides a JSON payload as a Server Send Event.

I am also using Spring RestDocs to document the contents of that payload. These are generated in a WebFluxTest and an

I am able to generate a simple ResponseBody snippet using PayloadDocumentation.responseBody(), but when I try to describe the fields using field descriptors...

@WebFluxTest
@AutoConfigureRestDocs
@ContextConfiguration(classes = ArticleHandler.class)
class HandlerTest { 
...

    @Test
    void testGetArticle() {
        webClient.get()
                .uri("/articles/{id}", "article-id")
                .exchange()
                .expectStatus().isOk().expectBody().consumeWith(
                        document("article", PayloadDocumentation.responseFields(fieldWithPath("id")
                        .type(JsonFieldType.STRING)
                        .description("Unique ID for blog article")));
    }
}

I get the following failing test:

[Fatal Error] :1:1: Content is not allowed in prolog.

Cannot handle text/event-stream;charset=UTF-8 content as it could not be parsed as JSON or XML
org.springframework.restdocs.payload.PayloadHandlingException: Cannot handle text/event-stream;charset=UTF-8 content as it could not be parsed as JSON or XML
	at org.springframework.restdocs.payload.ContentHandler.forContentWithDescriptors(ContentHandler.java:69)
	at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:157)
	at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:78)
	at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:191)
	at org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.lambda$document$0(WebTestClientRestDocumentation.java:77)
	at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodyContentSpec.lambda$consumeWith$3(DefaultWebTestClient.java:564)
	at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:206)
	at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodyContentSpec.consumeWith(DefaultWebTestClient.java:564)
	at com.project.blog.article.HandlerTest.testGetArticle(HandlerTest.java:62)

Are there any way that I can document the objects emitted in my text/event-stream payload?

答案1

得分: 2

Edit: 供参考,以下是“实际示例”中的完整类:https://github.com/michael-simons/neo4j-from-the-jvm-ecosystem/blob/master/tck/src/test/java/org/neo4j/examples/jvm/tck/TckTest.java#L221-L257

我按照Andy的建议实现了一个类似这样的预处理器:

import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;

import org.springframework.restdocs.operation.preprocess.ContentModifyingOperationPreprocessor;

class Whatever {

	@Test
	@DisplayName("GET /api/movies")
	void verifyGetListOfMovies(@Autowired WebTestClient webclient) {
	
		var movies = webclient.get().uri("/movies")
			.exchange()
			.expectStatus().isOk()
			.expectHeader().value(HttpHeaders.CONTENT_TYPE,
				s -> {
					var mediaType = MediaType.parseMediaType(s);
					var compatibleMediaTypeReturned = mediaType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) ||
													  mediaType.isCompatibleWith(MediaType.APPLICATION_JSON);
					assertThat(compatibleMediaTypeReturned).isTrue();
				})
			.expectBodyList(Movie.class)
			.consumeWith(document("movies", preprocessResponse(new ContentModifyingOperationPreprocessor(
					(bytes, mediaType) -> {
						var result = new StringBuilder().append("[");
						try (var scanner = new Scanner(new ByteArrayInputStream(bytes), mediaType.getCharset()).useDelimiter("(\r?\n){2}")) {
							while (scanner.hasNext()) {
								String statement = scanner.next().trim().replaceAll("^data: *", "").trim();
								if (statement.isEmpty()) {
									continue;
								}
								result.append(statement).append(",");
							}
						}
						result.replace(result.length() - 1, result.length(), "]");
						return result.toString().getBytes(mediaType.getCharset());
					})
			), responseFields(
				fieldWithPath("born").description("人物出生年份。"),
				fieldWithPath("name").description("人物姓名。"),
				fieldWithPath("id").description("Neo4j 内部 ID。"))
			))
			.returnResult()
			.getResponseBody();
	}
}
英文:

Edit: For reference, here is the full class in "action": https://github.com/michael-simons/neo4j-from-the-jvm-ecosystem/blob/master/tck/src/test/java/org/neo4j/examples/jvm/tck/TckTest.java#L221-L257

I followed Andy's advice and implemented a preprocessor like this:

import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;


import org.springframework.restdocs.operation.preprocess.ContentModifyingOperationPreprocessor;

class Whatever {

	@Test
	@DisplayName("GET /api/movies")
	void verifyGetListOfMovies(@Autowired WebTestClient webclient) {
	
		var movies = webclient.get().uri("/movies")
			.exchange()
			.expectStatus().isOk()
			.expectHeader().value(HttpHeaders.CONTENT_TYPE,
				s -> {
					var mediaType = MediaType.parseMediaType(s);
					var compatibleMediaTypeReturned = mediaType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) ||
													  mediaType.isCompatibleWith(MediaType.APPLICATION_JSON);
					assertThat(compatibleMediaTypeReturned).isTrue();
				})
			.expectBodyList(Movie.class)
			.consumeWith(document("movies", preprocessResponse(new ContentModifyingOperationPreprocessor(
					(bytes, mediaType) -> {
						var result = new StringBuilder().append("[");
						try (var scanner = new Scanner(new ByteArrayInputStream(bytes), mediaType.getCharset()).useDelimiter("(\r?\n){2}")) {
							while (scanner.hasNext()) {
								String statement = scanner.next().trim().replaceAll("^data: *", "").trim();
								if (statement.isEmpty()) {
									continue;
								}
								result.append(statement).append(",");
							}
						}
						result.replace(result.length() - 1, result.length(), "]");
						return result.toString().getBytes(mediaType.getCharset());
					})
			), responseFields(
				fieldWithPath("born").description("The year in which the person was born."),
				fieldWithPath("name").description("The name of the person."),
				fieldWithPath("id").description("The neo4j internal id."))
			))
			.returnResult()
			.getResponseBody();
	}
}

huangapple
  • 本文由 发表于 2020年9月12日 03:45:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63853416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定