Browse Source

Polishing contribution

See gh-24632
pull/24645/head
Rossen Stoyanchev 5 years ago
parent
commit
7e03728b2c
  1. 4
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java
  2. 34
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitterTests.java

4
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,7 +39,7 @@ import org.springframework.util.StringUtils; @@ -39,7 +39,7 @@ import org.springframework.util.StringUtils;
*/
public class SseEmitter extends ResponseBodyEmitter {
static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8);
private static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8);
/**
* Create a new SseEmitter instance.

34
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.web.servlet.mvc.method.annotation;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
@ -35,6 +36,9 @@ import static org.springframework.web.servlet.mvc.method.annotation.SseEmitter.e @@ -35,6 +36,9 @@ import static org.springframework.web.servlet.mvc.method.annotation.SseEmitter.e
*/
public class SseEmitterTests {
private static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
private SseEmitter emitter;
private TestHandler handler;
@ -52,18 +56,18 @@ public class SseEmitterTests { @@ -52,18 +56,18 @@ public class SseEmitterTests {
public void send() throws Exception {
this.emitter.send("foo");
this.handler.assertSentObjectCount(3);
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
this.handler.assertObject(1, "foo");
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
}
@Test
public void sendWithMediaType() throws Exception {
this.emitter.send("foo", MediaType.TEXT_PLAIN);
this.handler.assertSentObjectCount(3);
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
this.handler.assertObject(1, "foo", MediaType.TEXT_PLAIN);
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
}
@Test
@ -76,40 +80,40 @@ public class SseEmitterTests { @@ -76,40 +80,40 @@ public class SseEmitterTests {
public void sendEventWithDataLine() throws Exception {
this.emitter.send(event().data("foo"));
this.handler.assertSentObjectCount(3);
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
this.handler.assertObject(1, "foo");
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
}
@Test
public void sendEventWithTwoDataLines() throws Exception {
this.emitter.send(event().data("foo").data("bar"));
this.handler.assertSentObjectCount(5);
this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8);
this.handler.assertObject(1, "foo");
this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(2, "\ndata:", TEXT_PLAIN_UTF8);
this.handler.assertObject(3, "bar");
this.handler.assertObject(4, "\n\n", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(4, "\n\n", TEXT_PLAIN_UTF8);
}
@Test
public void sendEventFull() throws Exception {
this.emitter.send(event().comment("blah").name("test").reconnectTime(5000L).id("1").data("foo"));
this.handler.assertSentObjectCount(3);
this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", TEXT_PLAIN_UTF8);
this.handler.assertObject(1, "foo");
this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8);
}
@Test
public void sendEventFullWithTwoDataLinesInTheMiddle() throws Exception {
this.emitter.send(event().comment("blah").data("foo").data("bar").name("test").reconnectTime(5000L).id("1"));
this.handler.assertSentObjectCount(5);
this.handler.assertObject(0, ":blah\ndata:", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(0, ":blah\ndata:", TEXT_PLAIN_UTF8);
this.handler.assertObject(1, "foo");
this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(2, "\ndata:", TEXT_PLAIN_UTF8);
this.handler.assertObject(3, "bar");
this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", SseEmitter.TEXT_PLAIN);
this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", TEXT_PLAIN_UTF8);
}

Loading…
Cancel
Save