Polishing in MockServerHttpRequest

Closes gh-25109
This commit is contained in:
Mihai Dumitrescu 2020-05-28 13:40:40 +02:00 committed by Rossen Stoyanchev
parent 30351c257c
commit b31b8ce6c1
3 changed files with 80 additions and 81 deletions

View File

@ -44,6 +44,7 @@ import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
/**
@ -55,11 +56,10 @@ import org.springframework.web.util.UriComponentsBuilder;
*/
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private final HttpMethod httpMethod;
@Nullable
private final String customHttpMethod;
/**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>).
*/
private final String httpMethodValue;
private final MultiValueMap<String, HttpCookie> cookies;
@ -74,16 +74,13 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final Flux<DataBuffer> body;
private MockServerHttpRequest(@Nullable HttpMethod httpMethod, @Nullable String customHttpMethod,
private MockServerHttpRequest(String httpMethodValue,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
super(uri, contextPath, headers);
Assert.isTrue(httpMethod != null || customHttpMethod != null, "HTTP method must not be null");
this.httpMethod = httpMethod;
this.customHttpMethod = customHttpMethod;
this.httpMethodValue = httpMethodValue;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
@ -93,14 +90,15 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
@Nullable
public HttpMethod getMethod() {
return this.httpMethod;
return HttpMethod.resolve(httpMethodValue);
}
@Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() {
return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod);
return httpMethodValue;
}
@Override
@ -232,24 +230,23 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " +
"please use the variant that accepts a String based HTTP method.");
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
}
/**
* Create a builder with a raw HTTP method value that is outside the range
* Create a builder with a raw HTTP methodValue value that is outside the range
* of {@link HttpMethod} enum values.
* @param method the HTTP method value
* @param methodValue the HTTP methodValue value
* @param urlTemplate the URL template
* @param vars variables to expand into the template
* @return the created builder
* @throws IllegalArgumentException if methodValue is null, empty String or contains only white characters
* @since 5.2.7
*/
public static BodyBuilder method(String method, String urlTemplate, Object... vars) {
public static BodyBuilder method(String methodValue, String urlTemplate, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
return new DefaultBodyBuilder(methodValue, url);
}
@ -431,12 +428,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
@Nullable
private final HttpMethod method;
@Nullable
private final String customMethod;
private final String methodValue;
private final URI url;
@ -458,23 +450,18 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private SslInfo sslInfo;
DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
this.customMethod = null;
protected DefaultBodyBuilder(String methodValue, URI url) {
Assert.isTrue(StringUtils.hasLength(methodValue) &&
StringUtils.hasLength(methodValue.trim()), "HttpMethod is required. " +
"Please initialize it to non empty value");
this.methodValue = methodValue.trim();
this.url = url;
}
DefaultBodyBuilder(String method, URI url) {
HttpMethod resolved = HttpMethod.resolve(method);
if (resolved != null) {
this.method = resolved;
this.customMethod = null;
}
else {
this.method = null;
this.customMethod = method;
}
protected DefaultBodyBuilder(HttpMethod method, URI url) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " +
"please use the variant that accepts a String based HTTP method.");
this.methodValue = method.name();
this.url = url;
}
@ -611,7 +598,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
applyCookiesIfNecessary();
return new MockServerHttpRequest(this.method, this.customMethod, getUrlToUse(), this.contextPath,
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
}

View File

@ -17,11 +17,18 @@
package org.springframework.mock.http.server.reactive;
import java.util.Arrays;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.util.UriComponentsBuilder;
import static org.assertj.core.api.Assertions.assertThat;
@ -56,4 +63,22 @@ class MockServerHttpRequestTests {
assertThat(request.getURI().toString()).isEqualTo("/foo%20bar?a=b&name%20A=value%20A1&name%20A=value%20A2&name%20B=value%20B1");
}
@ParameterizedTest
@MethodSource("invalidMockServerHttpRequestBuilds")
void httpMethodNotNullOrEmpty(Executable executable) {
IllegalArgumentException expectedIllegalArgumentException = Assertions.assertThrows(IllegalArgumentException.class,
executable);
assertThat(expectedIllegalArgumentException.getMessage()).contains("HttpMethod is required.");
}
static Stream<Executable> invalidMockServerHttpRequestBuilds() {
String uriTemplate = "/foo bar?a=b";
return Stream.of(
() -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build(),
() -> MockServerHttpRequest.method((HttpMethod) null, uriTemplate).build(),
() -> MockServerHttpRequest.method((String) null, uriTemplate).build(),
() -> MockServerHttpRequest.method("", uriTemplate).build(),
() -> MockServerHttpRequest.method(" ", uriTemplate).build()
);
}
}

View File

@ -44,6 +44,7 @@ import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
/**
@ -55,11 +56,10 @@ import org.springframework.web.util.UriComponentsBuilder;
*/
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private final HttpMethod httpMethod;
@Nullable
private final String customHttpMethod;
/**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>).
*/
private final String httpMethodValue;
private final MultiValueMap<String, HttpCookie> cookies;
@ -74,16 +74,13 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final Flux<DataBuffer> body;
private MockServerHttpRequest(@Nullable HttpMethod httpMethod, @Nullable String customHttpMethod,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
private MockServerHttpRequest(String httpMethodValue,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
super(uri, contextPath, headers);
Assert.isTrue(httpMethod != null || customHttpMethod != null, "HTTP method must not be null");
this.httpMethod = httpMethod;
this.customHttpMethod = customHttpMethod;
this.httpMethodValue = httpMethodValue;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
@ -93,14 +90,15 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
@Nullable
public HttpMethod getMethod() {
return this.httpMethod;
return HttpMethod.resolve(httpMethodValue);
}
@Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() {
return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod);
return httpMethodValue;
}
@Override
@ -232,24 +230,23 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " +
"please use the variant that accepts a String based HTTP method.");
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
}
/**
* Create a builder with a raw HTTP method value that is outside the range
* Create a builder with a raw HTTP methodValue value that is outside the range
* of {@link HttpMethod} enum values.
* @param method the HTTP method value
* @param methodValue the HTTP methodValue value
* @param urlTemplate the URL template
* @param vars variables to expand into the template
* @return the created builder
* @throws IllegalArgumentException if methodValue is null, empty String or contains only white characters
* @since 5.2.7
*/
public static BodyBuilder method(String method, String urlTemplate, Object... vars) {
public static BodyBuilder method(String methodValue, String urlTemplate, Object... vars) {
URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
return new DefaultBodyBuilder(method, url);
return new DefaultBodyBuilder(methodValue, url);
}
@ -431,12 +428,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
@Nullable
private final HttpMethod method;
@Nullable
private final String customMethod;
private final String methodValue;
private final URI url;
@ -458,23 +450,18 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private SslInfo sslInfo;
DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
this.customMethod = null;
protected DefaultBodyBuilder(String methodValue, URI url) {
Assert.isTrue(StringUtils.hasLength(methodValue) &&
StringUtils.hasLength(methodValue.trim()), "HttpMethod is required. " +
"Please initialize it to non empty value");
this.methodValue = methodValue.trim();
this.url = url;
}
DefaultBodyBuilder(String method, URI url) {
HttpMethod resolved = HttpMethod.resolve(method);
if (resolved != null) {
this.method = resolved;
this.customMethod = null;
}
else {
this.method = null;
this.customMethod = method;
}
protected DefaultBodyBuilder(HttpMethod method, URI url) {
Assert.notNull(method, "HttpMethod is required. If testing a custom HTTP method, " +
"please use the variant that accepts a String based HTTP method.");
this.methodValue = method.name();
this.url = url;
}
@ -611,7 +598,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
applyCookiesIfNecessary();
return new MockServerHttpRequest(this.method, this.customMethod, getUrlToUse(), this.contextPath,
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
}