Polishing
This commit is contained in:
@@ -199,8 +199,8 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
|
||||
@Nullable MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||
|
||||
if (this.encoder instanceof HttpMessageEncoder) {
|
||||
HttpMessageEncoder<?> httpEncoder = (HttpMessageEncoder<?>) this.encoder;
|
||||
return httpEncoder.getEncodeHints(actualType, elementType, mediaType, request, response);
|
||||
HttpMessageEncoder<?> encoder = (HttpMessageEncoder<?>) this.encoder;
|
||||
return encoder.getEncodeHints(actualType, elementType, mediaType, request, response);
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -63,13 +63,13 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
|
||||
private static final List<String> SAFE_METHODS = Arrays.asList("GET", "HEAD");
|
||||
|
||||
/**
|
||||
* Pattern matching ETag multiple field values in headers such as "If-Match", "If-None-Match"
|
||||
* Pattern matching ETag multiple field values in headers such as "If-Match", "If-None-Match".
|
||||
* @see <a href="https://tools.ietf.org/html/rfc7232#section-2.3">Section 2.3 of RFC 7232</a>
|
||||
*/
|
||||
private static final Pattern ETAG_HEADER_VALUE_PATTERN = Pattern.compile("\\*|\\s*((W\\/)?(\"[^\"]*\"))\\s*,?");
|
||||
|
||||
/**
|
||||
* Date formats as specified in the HTTP RFC
|
||||
* Date formats as specified in the HTTP RFC.
|
||||
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
|
||||
*/
|
||||
private static final String[] DATE_FORMATS = new String[] {
|
||||
|
||||
@@ -310,10 +310,10 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
}
|
||||
// We will perform this validation...
|
||||
etag = padEtagIfNecessary(etag);
|
||||
for (String clientETag : ifNoneMatch) {
|
||||
for (String clientEtag : ifNoneMatch) {
|
||||
// Compare weak/strong ETags as per https://tools.ietf.org/html/rfc7232#section-2.3
|
||||
if (StringUtils.hasLength(clientETag) &&
|
||||
clientETag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", ""))) {
|
||||
if (StringUtils.hasLength(clientEtag) &&
|
||||
clientEtag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", ""))) {
|
||||
this.notModified = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -32,6 +32,8 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ServerSentEventHttpMessageReader}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
@@ -42,24 +44,21 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
|
||||
@Test
|
||||
public void cantRead() {
|
||||
assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class),
|
||||
new MediaType("foo", "bar")));
|
||||
assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class), new MediaType("foo", "bar")));
|
||||
assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
assertTrue(messageReader.canRead(ResolvableType.forClass(Object.class),
|
||||
new MediaType("text", "event-stream")));
|
||||
assertTrue(messageReader.canRead(ResolvableType.forClass(ServerSentEvent.class),
|
||||
new MediaType("foo", "bar")));
|
||||
assertTrue(messageReader.canRead(ResolvableType.forClass(Object.class), new MediaType("text", "event-stream")));
|
||||
assertTrue(messageReader.canRead(ResolvableType.forClass(ServerSentEvent.class), new MediaType("foo", "bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readServerSentEvents() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(
|
||||
"id:c42\nevent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:bar\n\n" +
|
||||
"id:c43\nevent:bar\nretry:456\ndata:baz\n\n");
|
||||
"id:c43\nevent:bar\nretry:456\ndata:baz\n\n");
|
||||
|
||||
Flux<ServerSentEvent> events = this.messageReader
|
||||
.read(ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class),
|
||||
|
||||
@@ -41,6 +41,7 @@ import static org.springframework.core.ResolvableType.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ServerSentEventHttpMessageWriter}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
@@ -48,14 +49,12 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll
|
||||
|
||||
private static final Map<String, Object> HINTS = Collections.emptyMap();
|
||||
|
||||
|
||||
private ServerSentEventHttpMessageWriter messageWriter =
|
||||
new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder());
|
||||
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
|
||||
assertTrue(this.messageWriter.canWrite(forClass(Object.class), null));
|
||||
assertFalse(this.messageWriter.canWrite(forClass(Object.class), new MediaType("foo", "bar")));
|
||||
|
||||
@@ -69,7 +68,6 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll
|
||||
|
||||
@Test
|
||||
public void writeServerSentEvent() {
|
||||
|
||||
ServerSentEvent<?> event = ServerSentEvent.builder().data("bar").id("c42").event("foo")
|
||||
.comment("bla\nbla bla\nbla bla bla").retry(Duration.ofMillis(123L)).build();
|
||||
|
||||
@@ -134,7 +132,6 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll
|
||||
|
||||
@Test // SPR-14899
|
||||
public void writePojoWithPrettyPrint() {
|
||||
|
||||
ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build();
|
||||
this.messageWriter = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder(mapper));
|
||||
|
||||
@@ -172,8 +169,8 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll
|
||||
testWrite(source, MediaType.TEXT_EVENT_STREAM, response, clazz);
|
||||
}
|
||||
|
||||
private <T> void testWrite(Publisher<T> source, MediaType mediaType, MockServerHttpResponse response,
|
||||
Class<T> clazz) {
|
||||
private <T> void testWrite(
|
||||
Publisher<T> source, MediaType mediaType, MockServerHttpResponse response, Class<T> clazz) {
|
||||
|
||||
this.messageWriter.write(source, forClass(clazz), mediaType, response, HINTS)
|
||||
.block(Duration.ofMillis(5000));
|
||||
|
||||
Reference in New Issue
Block a user