diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java
index e35e3f15e4..d963d3bc65 100644
--- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java
+++ b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java
@@ -59,7 +59,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
/**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. CONNECT).
*/
- private final String httpMethodValue;
+ private final String httpMethod;
private final MultiValueMap cookies;
@@ -74,13 +74,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final Flux body;
- private MockServerHttpRequest(String httpMethodValue,
+ private MockServerHttpRequest(String httpMethod,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher extends DataBuffer> body) {
super(uri, contextPath, headers);
- this.httpMethodValue = httpMethodValue;
+ Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
+ this.httpMethod = httpMethod;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
@@ -92,13 +93,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
@Nullable
public HttpMethod getMethod() {
- return HttpMethod.resolve(httpMethodValue);
+ return HttpMethod.resolve(this.httpMethod);
}
@Override
- @SuppressWarnings("ConstantConditions")
public String getMethodValue() {
- return httpMethodValue;
+ return this.httpMethod;
}
@Override
@@ -217,7 +217,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, URI url) {
- return new DefaultBodyBuilder(method, url);
+ Assert.notNull(method, "HTTP method is required. " +
+ "For a custom HTTP method, please provide a String HTTP method value.");
+ return new DefaultBodyBuilder(method.name(), url);
}
/**
@@ -225,28 +227,29 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* The given URI may contain query parameters, or those may be added later via
* {@link BaseBuilder#queryParam queryParam} builder methods.
* @param method the HTTP method (GET, POST, etc)
- * @param urlTemplate the URL template
+ * @param uri the URI template for the target URL
* @param vars variables to expand into the template
* @return the created builder
*/
- public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) {
- URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
- return new DefaultBodyBuilder(method, url);
+ public static BodyBuilder method(HttpMethod method, String uri, Object... vars) {
+ return method(method, toUri(uri, vars));
}
/**
- * Create a builder with a raw HTTP methodValue value that is outside the range
- * of {@link HttpMethod} enum values.
- * @param methodValue the HTTP methodValue value
- * @param urlTemplate the URL template
+ * Create a builder with a raw HTTP method value value that is outside the
+ * range of {@link HttpMethod} enum values.
+ * @param httpMethod the HTTP methodValue value
+ * @param uri the URI template for target the URL
* @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 methodValue, String urlTemplate, Object... vars) {
- URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
- return new DefaultBodyBuilder(methodValue, url);
+ public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
+ return new DefaultBodyBuilder(httpMethod, toUri(uri, vars));
+ }
+
+ private static URI toUri(String uri, Object[] vars) {
+ return UriComponentsBuilder.fromUriString(uri).buildAndExpand(vars).encode().toUri();
}
@@ -450,18 +453,13 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private SslInfo sslInfo;
- 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();
+ DefaultBodyBuilder(String method, URI url) {
+ this.methodValue = method;
this.url = url;
}
- 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();
+ DefaultBodyBuilder(HttpMethod httpMethod, URI url) {
+ this.methodValue = httpMethod.name();
this.url = url;
}
diff --git a/spring-test/src/test/java/org/springframework/mock/http/server/reactive/MockServerHttpRequestTests.java b/spring-test/src/test/java/org/springframework/mock/http/server/reactive/MockServerHttpRequestTests.java
index 52ebc906b1..1fecf99f47 100644
--- a/spring-test/src/test/java/org/springframework/mock/http/server/reactive/MockServerHttpRequestTests.java
+++ b/spring-test/src/test/java/org/springframework/mock/http/server/reactive/MockServerHttpRequestTests.java
@@ -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.
@@ -21,10 +21,10 @@ 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;
@@ -39,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class MockServerHttpRequestTests {
@Test
- void cookieHeaderSet() throws Exception {
+ void cookieHeaderSet() {
HttpCookie foo11 = new HttpCookie("foo1", "bar1");
HttpCookie foo12 = new HttpCookie("foo1", "bar2");
HttpCookie foo21 = new HttpCookie("foo2", "baz1");
@@ -54,7 +54,7 @@ class MockServerHttpRequestTests {
}
@Test
- void queryParams() throws Exception {
+ void queryParams() {
MockServerHttpRequest request = MockServerHttpRequest.get("/foo bar?a=b")
.queryParam("name A", "value A1", "value A2")
.queryParam("name B", "value B1")
@@ -64,14 +64,13 @@ class MockServerHttpRequestTests {
}
@ParameterizedTest
- @MethodSource("invalidMockServerHttpRequestBuilds")
+ @MethodSource
void httpMethodNotNullOrEmpty(Executable executable) {
- IllegalArgumentException expectedIllegalArgumentException = Assertions.assertThrows(IllegalArgumentException.class,
- executable);
- assertThat(expectedIllegalArgumentException.getMessage()).contains("HttpMethod is required.");
+ Exception ex = Assertions.assertThrows(IllegalArgumentException.class, executable);
+ assertThat(ex.getMessage()).contains("HTTP method is required.");
}
- static Stream invalidMockServerHttpRequestBuilds() {
+ static Stream httpMethodNotNullOrEmpty() {
String uriTemplate = "/foo bar?a=b";
return Stream.of(
() -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build(),
diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java
index 69d98c3b86..49162fda22 100644
--- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java
+++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java
@@ -59,7 +59,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
/**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. CONNECT).
*/
- private final String httpMethodValue;
+ private final String httpMethod;
private final MultiValueMap cookies;
@@ -74,13 +74,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final Flux body;
- private MockServerHttpRequest(String httpMethodValue,
- URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap cookies,
- @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
- @Nullable SslInfo sslInfo, Publisher extends DataBuffer> body) {
+ private MockServerHttpRequest(String httpMethod,
+ URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap cookies,
+ @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
+ @Nullable SslInfo sslInfo, Publisher extends DataBuffer> body) {
super(uri, contextPath, headers);
- this.httpMethodValue = httpMethodValue;
+ Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
+ this.httpMethod = httpMethod;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
@@ -92,13 +93,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
@Nullable
public HttpMethod getMethod() {
- return HttpMethod.resolve(httpMethodValue);
+ return HttpMethod.resolve(this.httpMethod);
}
@Override
- @SuppressWarnings("ConstantConditions")
public String getMethodValue() {
- return httpMethodValue;
+ return this.httpMethod;
}
@Override
@@ -217,7 +217,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, URI url) {
- return new DefaultBodyBuilder(method, url);
+ Assert.notNull(method, "HTTP method is required. " +
+ "For a custom HTTP method, please provide a String HTTP method value.");
+ return new DefaultBodyBuilder(method.name(), url);
}
/**
@@ -225,28 +227,29 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* The given URI may contain query parameters, or those may be added later via
* {@link BaseBuilder#queryParam queryParam} builder methods.
* @param method the HTTP method (GET, POST, etc)
- * @param urlTemplate the URL template
+ * @param uri the URI template for the target URL
* @param vars variables to expand into the template
* @return the created builder
*/
- public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) {
- URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
- return new DefaultBodyBuilder(method, url);
+ public static BodyBuilder method(HttpMethod method, String uri, Object... vars) {
+ return method(method, toUri(uri, vars));
}
/**
- * Create a builder with a raw HTTP methodValue value that is outside the range
- * of {@link HttpMethod} enum values.
- * @param methodValue the HTTP methodValue value
- * @param urlTemplate the URL template
+ * Create a builder with a raw HTTP method value value that is outside the
+ * range of {@link HttpMethod} enum values.
+ * @param httpMethod the HTTP methodValue value
+ * @param uri the URI template for target the URL
* @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 methodValue, String urlTemplate, Object... vars) {
- URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
- return new DefaultBodyBuilder(methodValue, url);
+ public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
+ return new DefaultBodyBuilder(httpMethod, toUri(uri, vars));
+ }
+
+ private static URI toUri(String uri, Object[] vars) {
+ return UriComponentsBuilder.fromUriString(uri).buildAndExpand(vars).encode().toUri();
}
@@ -450,18 +453,13 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private SslInfo sslInfo;
- 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();
+ DefaultBodyBuilder(String method, URI url) {
+ this.methodValue = method;
this.url = url;
}
- 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();
+ DefaultBodyBuilder(HttpMethod httpMethod, URI url) {
+ this.methodValue = httpMethod.name();
this.url = url;
}