Streaming Responses

您可以使用 WebTestClient 来测试 streaming responses,例如 Server-Sent 事件。但是,MockMvcWebTestClient 不支持无限流,因为无法从客户端取消服务器流。要测试无限流,您需要启动 bind to 正在运行的服务器,或在使用 Spring Boot 时使用https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-running-server[正在运行的服务器进行测试]。

You can use WebTestClient to test streaming responses such as Server-Sent Events. However, MockMvcWebTestClient doesn’t support infinite streams because there is no way to cancel the server stream from the client side. To test infinite streams, you’ll need to bind to a running server, or when using Spring Boot, test with a running server.

MockMvcWebTestClient 支持异步响应,甚至支持流响应。它的限制是它不能影响服务器停止,因此服务器必须自行完成响应的编写。

MockMvcWebTestClient does support asynchronous responses, and even streaming responses. The limitation is that it can’t influence the server to stop, and therefore the server must finish writing the response on its own.