Avoid java.util.Optional signatures for simple field access
Issue: SPR-15576
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -29,9 +29,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.json.Jackson2JsonDecoder;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
@@ -41,6 +39,7 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
private ServerSentEventHttpMessageReader messageReader =
|
||||
new ServerSentEventHttpMessageReader(new Jackson2JsonDecoder());
|
||||
|
||||
|
||||
@Test
|
||||
public void cantRead() {
|
||||
assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class),
|
||||
@@ -58,7 +57,6 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
|
||||
@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");
|
||||
@@ -69,18 +67,18 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
|
||||
StepVerifier.create(events)
|
||||
.consumeNextWith(event -> {
|
||||
assertEquals("c42", event.id().get());
|
||||
assertEquals("foo", event.event().get());
|
||||
assertEquals(Duration.ofMillis(123), event.retry().get());
|
||||
assertEquals("bla\nbla bla\nbla bla bla", event.comment().get());
|
||||
assertEquals("bar", event.data().get());
|
||||
assertEquals("c42", event.id());
|
||||
assertEquals("foo", event.event());
|
||||
assertEquals(Duration.ofMillis(123), event.retry());
|
||||
assertEquals("bla\nbla bla\nbla bla bla", event.comment());
|
||||
assertEquals("bar", event.data());
|
||||
})
|
||||
.consumeNextWith(event -> {
|
||||
assertEquals("c43", event.id().get());
|
||||
assertEquals("bar", event.event().get());
|
||||
assertEquals(Duration.ofMillis(456), event.retry().get());
|
||||
assertFalse(event.comment().isPresent());
|
||||
assertEquals("baz", event.data().get());
|
||||
assertEquals("c43", event.id());
|
||||
assertEquals("bar", event.event());
|
||||
assertEquals(Duration.ofMillis(456), event.retry());
|
||||
assertNull(event.comment());
|
||||
assertEquals("baz", event.data());
|
||||
})
|
||||
.expectComplete()
|
||||
.verify();
|
||||
@@ -88,7 +86,6 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
|
||||
@Test
|
||||
public void readServerSentEventsWithMultipleChunks() {
|
||||
|
||||
MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
||||
.body(Flux.just(
|
||||
stringBuffer("id:c42\nev"),
|
||||
@@ -101,18 +98,18 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
|
||||
StepVerifier.create(events)
|
||||
.consumeNextWith(event -> {
|
||||
assertEquals("c42", event.id().get());
|
||||
assertEquals("foo", event.event().get());
|
||||
assertEquals(Duration.ofMillis(123), event.retry().get());
|
||||
assertEquals("bla\nbla bla\nbla bla bla", event.comment().get());
|
||||
assertEquals("bar", event.data().get());
|
||||
assertEquals("c42", event.id());
|
||||
assertEquals("foo", event.event());
|
||||
assertEquals(Duration.ofMillis(123), event.retry());
|
||||
assertEquals("bla\nbla bla\nbla bla bla", event.comment());
|
||||
assertEquals("bar", event.data());
|
||||
})
|
||||
.consumeNextWith(event -> {
|
||||
assertEquals("c43", event.id().get());
|
||||
assertEquals("bar", event.event().get());
|
||||
assertEquals(Duration.ofMillis(456), event.retry().get());
|
||||
assertFalse(event.comment().isPresent());
|
||||
assertEquals("baz", event.data().get());
|
||||
assertEquals("c43", event.id());
|
||||
assertEquals("bar", event.event());
|
||||
assertEquals(Duration.ofMillis(456), event.retry());
|
||||
assertNull(event.comment());
|
||||
assertEquals("baz", event.data());
|
||||
})
|
||||
.expectComplete()
|
||||
.verify();
|
||||
@@ -120,7 +117,6 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
|
||||
@Test
|
||||
public void readString() {
|
||||
|
||||
String body = "data:foo\ndata:bar\n\ndata:baz\n\n";
|
||||
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
|
||||
|
||||
@@ -136,7 +132,6 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
|
||||
@Test
|
||||
public void readPojo() {
|
||||
|
||||
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(
|
||||
"data:{\"foo\": \"foofoo\", \"bar\": \"barbar\"}\n\n" +
|
||||
"data:{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}\n\n");
|
||||
@@ -157,9 +152,8 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test // SPR-15331
|
||||
@Test // SPR-15331
|
||||
public void decodeFullContentAsString() {
|
||||
|
||||
String body = "data:foo\ndata:bar\n\ndata:baz\n\n";
|
||||
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.http.server.reactive;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Optional;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.handler.codec.http.cookie.Cookie;
|
||||
@@ -59,19 +58,20 @@ public class RxNettyServerHttpRequest extends AbstractServerHttpRequest {
|
||||
throws URISyntaxException {
|
||||
|
||||
super(initUri(request, remoteAddress), initHeaders(request));
|
||||
|
||||
Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null");
|
||||
this.request = request;
|
||||
|
||||
Assert.notNull(dataBufferFactory, "NettyDataBufferFactory must not be null");
|
||||
this.dataBufferFactory = dataBufferFactory;
|
||||
|
||||
this.remoteAddress = remoteAddress;
|
||||
}
|
||||
|
||||
private static URI initUri(HttpServerRequest<ByteBuf> request, InetSocketAddress remoteAddress)
|
||||
throws URISyntaxException {
|
||||
|
||||
Assert.notNull(request, "'request' must not be null");
|
||||
Assert.notNull(request, "HttpServerRequest must not be null");
|
||||
String requestUri = request.getUri();
|
||||
return remoteAddress != null ? createUrl(remoteAddress, requestUri) : URI.create(requestUri);
|
||||
return (remoteAddress != null ? createUrl(remoteAddress, requestUri) : URI.create(requestUri));
|
||||
}
|
||||
|
||||
private static URI createUrl(InetSocketAddress address, String requestUri) throws URISyntaxException {
|
||||
@@ -110,8 +110,8 @@ public class RxNettyServerHttpRequest extends AbstractServerHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<InetSocketAddress> getRemoteAddress() {
|
||||
return Optional.ofNullable(this.remoteAddress);
|
||||
public InetSocketAddress getRemoteAddress() {
|
||||
return this.remoteAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -47,18 +47,18 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class RxNettyServerHttpResponse extends AbstractServerHttpResponse {
|
||||
|
||||
private final HttpServerResponse<ByteBuf> response;
|
||||
|
||||
private static final ByteBuf FLUSH_SIGNAL = Unpooled.buffer(0, 0);
|
||||
|
||||
// 8 Kb flush threshold to avoid blocking RxNetty when the send buffer has reached the high watermark
|
||||
private static final long FLUSH_THRESHOLD = 8192;
|
||||
|
||||
public RxNettyServerHttpResponse(HttpServerResponse<ByteBuf> response,
|
||||
NettyDataBufferFactory dataBufferFactory) {
|
||||
super(dataBufferFactory);
|
||||
Assert.notNull(response, "'response' must not be null.");
|
||||
|
||||
private final HttpServerResponse<ByteBuf> response;
|
||||
|
||||
|
||||
public RxNettyServerHttpResponse(HttpServerResponse<ByteBuf> response, NettyDataBufferFactory dataBufferFactory) {
|
||||
super(dataBufferFactory);
|
||||
Assert.notNull(response, "HttpServerResponse must not be null");
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
@@ -113,8 +113,12 @@ public class RxNettyServerHttpResponse extends AbstractServerHttpResponse {
|
||||
if (!httpCookie.getMaxAge().isNegative()) {
|
||||
cookie.setMaxAge(httpCookie.getMaxAge().getSeconds());
|
||||
}
|
||||
httpCookie.getDomain().ifPresent(cookie::setDomain);
|
||||
httpCookie.getPath().ifPresent(cookie::setPath);
|
||||
if (httpCookie.getDomain() != null) {
|
||||
cookie.setDomain(httpCookie.getDomain());
|
||||
}
|
||||
if (httpCookie.getPath() != null) {
|
||||
cookie.setPath(httpCookie.getPath());
|
||||
}
|
||||
cookie.setSecure(httpCookie.isSecure());
|
||||
cookie.setHttpOnly(httpCookie.isHttpOnly());
|
||||
this.response.addCookie(cookie);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.mock.http.server.reactive.test;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -22,7 +23,6 @@ import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -66,8 +66,7 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
private MockServerHttpRequest(HttpMethod httpMethod, URI uri, String contextPath,
|
||||
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
||||
InetSocketAddress remoteAddress,
|
||||
Publisher<? extends DataBuffer> body) {
|
||||
InetSocketAddress remoteAddress, Publisher<? extends DataBuffer> body) {
|
||||
|
||||
super(uri, headers);
|
||||
this.httpMethod = httpMethod;
|
||||
@@ -89,8 +88,8 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<InetSocketAddress> getRemoteAddress() {
|
||||
return Optional.ofNullable(this.remoteAddress);
|
||||
public InetSocketAddress getRemoteAddress() {
|
||||
return this.remoteAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user