Polish MockServerHttpRequest

Issue: SPR-14421
This commit is contained in:
Rossen Stoyanchev
2016-09-02 11:54:19 -04:00
parent 2cd3135c3b
commit e17132c217
50 changed files with 200 additions and 208 deletions

View File

@@ -16,18 +16,22 @@
package org.springframework.http.server.reactive;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
* Mock implementation of {@link ServerHttpRequest}.
* @author Rossen Stoyanchev
@@ -36,46 +40,73 @@ public class MockServerHttpRequest implements ServerHttpRequest {
private HttpMethod httpMethod;
private URI uri;
private URI url;
private MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
private final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
private HttpHeaders headers = new HttpHeaders();
private final HttpHeaders headers = new HttpHeaders();
private MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
private final MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
private Flux<DataBuffer> body;
private Flux<DataBuffer> body = Flux.empty();
public MockServerHttpRequest(HttpMethod httpMethod, URI uri) {
/**
* Create a new instance where the HTTP method and/or URL can be set later
* via {@link #setHttpMethod(HttpMethod)} and {@link #setUri(URI)}.
*/
public MockServerHttpRequest() {
}
/**
* Convenience alternative to {@link #MockServerHttpRequest(HttpMethod, URI)}
* that accepts a String URL.
*/
public MockServerHttpRequest(HttpMethod httpMethod, String url) {
this(httpMethod, (url != null ? URI.create(url) : null));
}
/**
* Create a new instance with the given HTTP method and URL.
*/
public MockServerHttpRequest(HttpMethod httpMethod, URI url) {
this.httpMethod = httpMethod;
this.uri = uri;
this.url = url;
}
public MockServerHttpRequest(Publisher<DataBuffer> body, HttpMethod httpMethod,
URI uri) {
this.body = Flux.from(body);
this.httpMethod = httpMethod;
this.uri = uri;
}
@Override
public HttpMethod getMethod() {
return this.httpMethod;
}
public void setHttpMethod(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
}
@Override
public URI getURI() {
return this.uri;
public HttpMethod getMethod() {
return this.httpMethod;
}
public void setUri(URI uri) {
this.uri = uri;
public MockServerHttpRequest setUri(String url) {
this.url = URI.create(url);
return this;
}
public MockServerHttpRequest setUri(URI uri) {
this.url = uri;
return this;
}
@Override
public URI getURI() {
return this.url;
}
public MockServerHttpRequest addHeader(String name, String value) {
getHeaders().add(name, value);
return this;
}
public MockServerHttpRequest setHeader(String name, String value) {
getHeaders().set(name, value);
return this;
}
@Override
@@ -93,13 +124,32 @@ public class MockServerHttpRequest implements ServerHttpRequest {
return this.cookies;
}
public MockServerHttpRequest setBody(Publisher<DataBuffer> body) {
this.body = Flux.from(body);
return this;
}
public MockServerHttpRequest setBody(String body) {
DataBuffer buffer = toDataBuffer(body, StandardCharsets.UTF_8);
this.body = Flux.just(buffer);
return this;
}
public MockServerHttpRequest setBody(String body, Charset charset) {
DataBuffer buffer = toDataBuffer(body, charset);
this.body = Flux.just(buffer);
return this;
}
private DataBuffer toDataBuffer(String body, Charset charset) {
byte[] bytes = body.getBytes(charset);
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
return new DefaultDataBufferFactory().wrap(byteBuffer);
}
@Override
public Flux<DataBuffer> getBody() {
return this.body;
}
public Mono<Void> writeWith(Publisher<DataBuffer> body) {
this.body = Flux.from(body);
return this.body.then();
}
}

View File

@@ -39,15 +39,15 @@ public class MockServerHttpResponse implements ServerHttpResponse {
private HttpStatus status;
private HttpHeaders headers = new HttpHeaders();
private final HttpHeaders headers = new HttpHeaders();
private MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
private Publisher<DataBuffer> body;
private Publisher<Publisher<DataBuffer>> bodyWithFlushes;
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
private DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@Override
@@ -101,6 +101,7 @@ public class MockServerHttpResponse implements ServerHttpResponse {
@Override
public DataBufferFactory bufferFactory() {
return this.dataBufferFactory;
return this.bufferFactory;
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.web.server.adapter;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.time.Instant;
@@ -80,7 +79,7 @@ public class DefaultServerWebExchangeCheckNotModifiedTests {
currentDate = Instant.now().truncatedTo(ChronoUnit.SECONDS);
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
request = new MockServerHttpRequest(method, new URI("http://example.org"));
request = new MockServerHttpRequest(method, "http://example.org");
response = new MockServerHttpResponse();
exchange = new DefaultServerWebExchange(request, response, new MockWebSessionManager());
}
@@ -287,7 +286,7 @@ public class DefaultServerWebExchangeCheckNotModifiedTests {
public void checkNotModifiedTimestampWithLengthPart() throws Exception {
long epochTime = dateFormat.parse(CURRENT_TIME).getTime();
request.setHttpMethod(HttpMethod.GET);
request.getHeaders().add("If-Modified-Since", "Wed, 09 Apr 2014 09:57:42 GMT; length=13774");
request.setHeader("If-Modified-Since", "Wed, 09 Apr 2014 09:57:42 GMT; length=13774");
assertTrue(exchange.checkNotModified(Instant.ofEpochMilli(epochTime)));
@@ -299,7 +298,7 @@ public class DefaultServerWebExchangeCheckNotModifiedTests {
public void checkModifiedTimestampWithLengthPart() throws Exception {
long epochTime = dateFormat.parse(CURRENT_TIME).getTime();
request.setHttpMethod(HttpMethod.GET);
request.getHeaders().add("If-Modified-Since", "Tue, 08 Apr 2014 09:57:42 GMT; length=13774");
request.setHeader("If-Modified-Since", "Tue, 08 Apr 2014 09:57:42 GMT; length=13774");
assertFalse(exchange.checkNotModified(Instant.ofEpochMilli(epochTime)));

View File

@@ -16,8 +16,6 @@
package org.springframework.web.server.handler;
import java.net.URI;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Mono;
@@ -50,9 +48,8 @@ public class ExceptionHandlingHttpHandlerTests {
@Before
public void setUp() throws Exception {
URI uri = new URI("http://localhost:8080");
WebSessionManager sessionManager = new MockWebSessionManager();
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, uri);
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "http://localhost:8080");
this.response = new MockServerHttpResponse();
this.exchange = new DefaultServerWebExchange(request, this.response, sessionManager);
this.targetHandler = new StubWebHandler(new IllegalStateException("boo"));

View File

@@ -16,8 +16,6 @@
package org.springframework.web.server.handler;
import java.net.URI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
@@ -56,7 +54,7 @@ public class FilteringWebHandlerTests {
@Before
public void setUp() throws Exception {
this.request = new MockServerHttpRequest(HttpMethod.GET, new URI("http://localhost"));
this.request = new MockServerHttpRequest(HttpMethod.GET, "http://localhost");
this.response = new MockServerHttpResponse();
}

View File

@@ -16,7 +16,6 @@
package org.springframework.web.server.handler;
import java.net.URI;
import java.time.Duration;
import org.junit.Before;
@@ -33,7 +32,8 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.MockWebSessionManager;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
/**
* Unit tests for {@link ResponseStatusExceptionHandler}.
@@ -54,8 +54,7 @@ public class ResponseStatusExceptionHandlerTests {
this.handler = new ResponseStatusExceptionHandler();
this.response = new MockServerHttpResponse();
this.exchange = new DefaultServerWebExchange(
new MockServerHttpRequest(HttpMethod.GET, new URI("/path")),
this.response,
new MockServerHttpRequest(HttpMethod.GET, "/path"), this.response,
new MockWebSessionManager());
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.web.server.session;
import java.net.URI;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
@@ -57,7 +56,7 @@ public class DefaultWebSessionManagerTests {
public void setUp() throws Exception {
this.manager.setSessionIdResolver(this.idResolver);
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, new URI("/path"));
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path");
MockServerHttpResponse response = new MockServerHttpResponse();
this.exchange = new DefaultServerWebExchange(request, response, this.manager);
}