Polishing ResponseBodyEmitterReturnValueHandlerTests

This commit is contained in:
rstoyanchev
2022-09-13 21:14:50 +01:00
parent 0d5c78d992
commit d581d48d24

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -85,7 +85,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
@Test @Test
public void supportsReturnTypes() throws Exception { public void supportsReturnTypes() {
assertThat(this.handler.supportsReturnType( assertThat(this.handler.supportsReturnType(
on(TestController.class).resolveReturnType(ResponseBodyEmitter.class))).isTrue(); on(TestController.class).resolveReturnType(ResponseBodyEmitter.class))).isTrue();
@@ -105,7 +105,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
} }
@Test @Test
public void doesNotSupportReturnTypes() throws Exception { public void doesNotSupportReturnTypes() {
assertThat(this.handler.supportsReturnType( assertThat(this.handler.supportsReturnType(
on(TestController.class).resolveReturnType(ResponseEntity.class, String.class))).isFalse(); on(TestController.class).resolveReturnType(ResponseEntity.class, String.class))).isFalse();
@@ -142,9 +142,10 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
bean.setName("Jason"); bean.setName("Jason");
emitter.send(bean); emitter.send(bean);
assertThat(this.response.getContentAsString()).isEqualTo(("{\"id\":1,\"name\":\"Joe\"}\n" + assertThat(this.response.getContentAsString()).isEqualTo(("""
"{\"id\":2,\"name\":\"John\"}\n" + {"id":1,"name":"Joe"}
"{\"id\":3,\"name\":\"Jason\"}")); {"id":2,"name":"John"}
{"id":3,"name":"Jason"}"""));
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext(); MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
assertThat(asyncContext.getDispatchedPath()).isNull(); assertThat(asyncContext.getDispatchedPath()).isNull();
@@ -212,13 +213,15 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
comment("a test").name("update").id("1").reconnectTime(5000L).data(bean1).data(bean2)); comment("a test").name("update").id("1").reconnectTime(5000L).data(bean1).data(bean2));
assertThat(this.response.getContentType()).isEqualTo("text/event-stream"); assertThat(this.response.getContentType()).isEqualTo("text/event-stream");
assertThat(this.response.getContentAsString()).isEqualTo((":a test\n" + assertThat(this.response.getContentAsString()).isEqualTo(("""
"event:update\n" + :a test
"id:1\n" + event:update
"retry:5000\n" + id:1
"data:{\"id\":1,\"name\":\"Joe\"}\n" + retry:5000
"data:{\"id\":2,\"name\":\"John\"}\n" + data:{"id":1,"name":"Joe"}
"\n")); data:{"id":2,"name":"John"}
"""));
} }
@Test @Test
@@ -227,17 +230,11 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
this.request.addHeader("Accept", "text/event-stream"); this.request.addHeader("Accept", "text/event-stream");
MethodParameter type = on(TestController.class).resolveReturnType(Flux.class, String.class); MethodParameter type = on(TestController.class).resolveReturnType(Flux.class, String.class);
Sinks.Many<String> sink = Sinks.many().unicast().onBackpressureBuffer(); this.handler.handleReturnValue(Flux.just("foo", "bar", "baz"), type, this.mavContainer, this.webRequest);
this.handler.handleReturnValue(sink.asFlux(), type, this.mavContainer, this.webRequest);
assertThat(this.request.isAsyncStarted()).isTrue(); assertThat(this.request.isAsyncStarted()).isTrue();
assertThat(this.response.getStatus()).isEqualTo(200); assertThat(this.response.getStatus()).isEqualTo(200);
sink.tryEmitNext("foo");
sink.tryEmitNext("bar");
sink.tryEmitNext("baz");
sink.tryEmitComplete();
assertThat(this.response.getContentType()).isEqualTo("text/event-stream"); assertThat(this.response.getContentType()).isEqualTo("text/event-stream");
assertThat(this.response.getContentAsString()).isEqualTo("data:foo\n\ndata:bar\n\ndata:baz\n\n"); assertThat(this.response.getContentAsString()).isEqualTo("data:foo\n\ndata:bar\n\ndata:baz\n\n");
} }
@@ -247,16 +244,12 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
this.request.addHeader("Accept", "text/event-stream"); this.request.addHeader("Accept", "text/event-stream");
IllegalStateException ex = new IllegalStateException("wah wah");
MethodParameter type = on(TestController.class).resolveReturnType(Flux.class, String.class); MethodParameter type = on(TestController.class).resolveReturnType(Flux.class, String.class);
Sinks.Many<String> sink = Sinks.many().unicast().onBackpressureBuffer(); this.handler.handleReturnValue(Flux.error(ex), type, this.mavContainer, this.webRequest);
this.handler.handleReturnValue(sink.asFlux(), type, this.mavContainer, this.webRequest);
assertThat(this.request.isAsyncStarted()).isTrue(); assertThat(this.request.isAsyncStarted()).isTrue();
IllegalStateException ex = new IllegalStateException("wah wah");
sink.tryEmitError(ex);
sink.tryEmitComplete();
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest); WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
assertThat(asyncManager.getConcurrentResult()).isSameAs(ex); assertThat(asyncManager.getConcurrentResult()).isSameAs(ex);
assertThat(this.response.getContentType()).isNull(); assertThat(this.response.getContentType()).isNull();
@@ -290,8 +283,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
@Test @Test
public void responseEntityFlux() throws Exception { public void responseEntityFlux() throws Exception {
Sinks.Many<String> sink = Sinks.many().unicast().onBackpressureBuffer(); ResponseEntity<Flux<String>> entity = ResponseEntity.ok().body(Flux.just("foo", "bar", "baz"));
ResponseEntity<Flux<String>> entity = ResponseEntity.ok().body(sink.asFlux());
ResolvableType bodyType = forClassWithGenerics(Flux.class, String.class); ResolvableType bodyType = forClassWithGenerics(Flux.class, String.class);
MethodParameter type = on(TestController.class).resolveReturnType(ResponseEntity.class, bodyType); MethodParameter type = on(TestController.class).resolveReturnType(ResponseEntity.class, bodyType);
this.handler.handleReturnValue(entity, type, this.mavContainer, this.webRequest); this.handler.handleReturnValue(entity, type, this.mavContainer, this.webRequest);
@@ -299,11 +291,6 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
assertThat(this.request.isAsyncStarted()).isTrue(); assertThat(this.request.isAsyncStarted()).isTrue();
assertThat(this.response.getStatus()).isEqualTo(200); assertThat(this.response.getStatus()).isEqualTo(200);
sink.tryEmitNext("foo");
sink.tryEmitNext("bar");
sink.tryEmitNext("baz");
sink.tryEmitComplete();
assertThat(this.response.getContentType()).isEqualTo("text/plain"); assertThat(this.response.getContentType()).isEqualTo("text/plain");
assertThat(this.response.getContentAsString()).isEqualTo("foobarbaz"); assertThat(this.response.getContentAsString()).isEqualTo("foobarbaz");
} }
@@ -324,7 +311,7 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
} }
@SuppressWarnings("unused") @SuppressWarnings({"unused", "ConstantConditions"})
private static class TestController { private static class TestController {
private ResponseBodyEmitter h1() { return null; } private ResponseBodyEmitter h1() { return null; }