Changes because HttpMethod changed to class

This commit contains changes made because HttpMethod changed from enum
to class.

See gh-27697
This commit is contained in:
Arjen Poutsma
2021-11-25 13:45:34 +01:00
parent 6e335e3a9f
commit 7a4207cd7b
74 changed files with 337 additions and 274 deletions

View File

@@ -34,7 +34,7 @@ import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -589,18 +589,19 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Return the value of the {@code Access-Control-Allow-Methods} response header.
*/
public List<HttpMethod> getAccessControlAllowMethods() {
List<HttpMethod> result = new ArrayList<>();
String value = getFirst(ACCESS_CONTROL_ALLOW_METHODS);
if (value != null) {
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
List<HttpMethod> result = new ArrayList<>();
for (String token : tokens) {
HttpMethod resolved = HttpMethod.resolve(token);
if (resolved != null) {
result.add(resolved);
}
HttpMethod method = HttpMethod.valueOf(token);
result.add(method);
}
return result;
}
else {
return Collections.emptyList();
}
return result;
}
/**
@@ -682,7 +683,13 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
*/
@Nullable
public HttpMethod getAccessControlRequestMethod() {
return HttpMethod.resolve(getFirst(ACCESS_CONTROL_REQUEST_METHOD));
String requestMethod = getFirst(ACCESS_CONTROL_REQUEST_METHOD);
if (requestMethod != null) {
return HttpMethod.valueOf(requestMethod);
}
else {
return null;
}
}
/**
@@ -743,17 +750,15 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
String value = getFirst(ALLOW);
if (StringUtils.hasLength(value)) {
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
List<HttpMethod> result = new ArrayList<>(tokens.length);
Set<HttpMethod> result = new LinkedHashSet<>(tokens.length);
for (String token : tokens) {
HttpMethod resolved = HttpMethod.resolve(token);
if (resolved != null) {
result.add(resolved);
}
HttpMethod method = HttpMethod.valueOf(token);
result.add(method);
}
return EnumSet.copyOf(result);
return result;
}
else {
return EnumSet.noneOf(HttpMethod.class);
return Collections.emptySet();
}
}

View File

@@ -18,8 +18,6 @@ package org.springframework.http;
import java.net.URI;
import org.springframework.lang.Nullable;
/**
* Represents an HTTP request message, consisting of
* {@linkplain #getMethod() method} and {@linkplain #getURI() uri}.
@@ -31,22 +29,20 @@ public interface HttpRequest extends HttpMessage {
/**
* Return the HTTP method of the request.
* @return the HTTP method as an HttpMethod enum value, or {@code null}
* if not resolvable (e.g. in case of a non-standard HTTP method)
* @see #getMethodValue()
* @see HttpMethod#resolve(String)
* @return the HTTP method as an HttpMethod value
* @see HttpMethod#valueOf(String)
*/
@Nullable
default HttpMethod getMethod() {
return HttpMethod.resolve(getMethodValue());
}
HttpMethod getMethod();
/**
* Return the HTTP method of the request as a String value.
* @return the HTTP method as a plain String
* @since 5.0
* @see #getMethod()
* @deprecated in favor of {@link #getMethod()} and
* {@link HttpMethod#name()}
*/
@Deprecated
String getMethodValue();
/**

View File

@@ -21,7 +21,6 @@ import java.net.URI;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;
/**
@@ -41,12 +40,12 @@ final class BufferingClientHttpRequestWrapper extends AbstractBufferingClientHtt
@Override
@Nullable
public HttpMethod getMethod() {
return this.request.getMethod();
}
@Override
@Deprecated
public String getMethodValue() {
return this.request.getMethodValue();
}

View File

@@ -59,8 +59,13 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
this.httpContext = context;
}
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.httpRequest.getMethod());
}
@Override
@Deprecated
public String getMethodValue() {
return this.httpRequest.getMethod();
}

View File

@@ -273,26 +273,31 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
* @return the Commons HttpMethodBase object
*/
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
switch (httpMethod) {
case GET:
return new HttpGet(uri);
case HEAD:
return new HttpHead(uri);
case POST:
return new HttpPost(uri);
case PUT:
return new HttpPut(uri);
case PATCH:
return new HttpPatch(uri);
case DELETE:
return new HttpDelete(uri);
case OPTIONS:
return new HttpOptions(uri);
case TRACE:
return new HttpTrace(uri);
default:
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
if (HttpMethod.GET.equals(httpMethod)) {
return new HttpGet(uri);
}
else if (HttpMethod.HEAD.equals(httpMethod)) {
return new HttpHead(uri);
}
else if (HttpMethod.POST.equals(httpMethod)) {
return new HttpPost(uri);
}
else if (HttpMethod.PUT.equals(httpMethod)) {
return new HttpPut(uri);
}
else if (HttpMethod.PATCH.equals(httpMethod)) {
return new HttpPatch(uri);
}
else if (HttpMethod.DELETE.equals(httpMethod)) {
return new HttpDelete(uri);
}
else if (HttpMethod.OPTIONS.equals(httpMethod)) {
return new HttpOptions(uri);
}
else if (HttpMethod.TRACE.equals(httpMethod)) {
return new HttpTrace(uri);
}
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
}
/**

View File

@@ -31,6 +31,7 @@ import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.lang.Nullable;
@@ -64,8 +65,13 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR
this.httpContext = context;
}
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.httpRequest.getMethod());
}
@Override
@Deprecated
public String getMethodValue() {
return this.httpRequest.getMethod();
}

View File

@@ -25,7 +25,6 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
/**
@@ -62,6 +61,7 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
}
@Override
@Deprecated
public String getMethodValue() {
return this.method.name();
}
@@ -94,7 +94,6 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
}
else {
HttpMethod method = request.getMethod();
Assert.state(method != null, "No standard HTTP method");
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method);
request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
if (body.length > 0) {

View File

@@ -57,6 +57,7 @@ class OkHttp3ClientHttpRequest extends AbstractBufferingClientHttpRequest {
}
@Override
@Deprecated
public String getMethodValue() {
return this.method.name();
}

View File

@@ -47,8 +47,13 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp
this.outputStreaming = outputStreaming;
}
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.connection.getRequestMethod());
}
@Override
@Deprecated
public String getMethodValue() {
return this.connection.getRequestMethod();
}

View File

@@ -55,8 +55,13 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest {
this.outputStreaming = outputStreaming;
}
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.connection.getRequestMethod());
}
@Override
@Deprecated
public String getMethodValue() {
return this.connection.getRequestMethod();
}

View File

@@ -40,7 +40,6 @@ import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import static org.springframework.http.MediaType.ALL_VALUE;
@@ -74,9 +73,7 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
@Override
public HttpMethod getMethod() {
HttpMethod method = HttpMethod.resolve(this.httpRequest.getMethod());
Assert.state(method != null, "Method must not be null");
return method;
return HttpMethod.valueOf(this.httpRequest.getMethod());
}
@Override

View File

@@ -21,7 +21,6 @@ import java.net.URI;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -59,7 +58,6 @@ public class HttpRequestWrapper implements HttpRequest {
* Return the method of the wrapped request.
*/
@Override
@Nullable
public HttpMethod getMethod() {
return this.request.getMethod();
}
@@ -68,6 +66,7 @@ public class HttpRequestWrapper implements HttpRequest {
* Return the method value of the wrapped request.
*/
@Override
@Deprecated
public String getMethodValue() {
return this.request.getMethodValue();
}

View File

@@ -92,12 +92,12 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
}
@Override
@Nullable
public HttpMethod getMethod() {
return HttpMethod.resolve(this.servletRequest.getMethod());
return HttpMethod.valueOf(this.servletRequest.getMethod());
}
@Override
@Deprecated
public String getMethodValue() {
return this.servletRequest.getMethod();
}

View File

@@ -47,7 +47,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
private final HttpHeaders headers;
private String httpMethodValue;
private HttpMethod httpMethod;
@Nullable
private String uriPath;
@@ -71,7 +71,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
this.uri = original.getURI();
this.headers = HttpHeaders.writableHttpHeaders(original.getHeaders());
this.httpMethodValue = original.getMethodValue();
this.httpMethod = original.getMethod();
this.contextPath = original.getPath().contextPath().value();
this.remoteAddress = original.getRemoteAddress();
this.body = original.getBody();
@@ -81,7 +81,8 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
@Override
public ServerHttpRequest.Builder method(HttpMethod httpMethod) {
this.httpMethodValue = httpMethod.name();
Assert.notNull(httpMethod, "HttpMethod must not be null");
this.httpMethod = httpMethod;
return this;
}
@@ -132,7 +133,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
@Override
public ServerHttpRequest build() {
return new MutatedServerHttpRequest(getUriToUse(), this.contextPath,
this.httpMethodValue, this.sslInfo, this.remoteAddress, this.headers, this.body, this.originalRequest);
this.httpMethod, this.sslInfo, this.remoteAddress, this.headers, this.body, this.originalRequest);
}
private URI getUriToUse() {
@@ -176,7 +177,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
private static class MutatedServerHttpRequest extends AbstractServerHttpRequest {
private final String methodValue;
private final HttpMethod method;
@Nullable
private final SslInfo sslInfo;
@@ -190,11 +191,11 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
public MutatedServerHttpRequest(URI uri, @Nullable String contextPath,
String methodValue, @Nullable SslInfo sslInfo, @Nullable InetSocketAddress remoteAddress,
HttpMethod method, @Nullable SslInfo sslInfo, @Nullable InetSocketAddress remoteAddress,
HttpHeaders headers, Flux<DataBuffer> body, ServerHttpRequest originalRequest) {
super(uri, contextPath, headers);
this.methodValue = methodValue;
this.method = method;
this.remoteAddress = (remoteAddress != null ? remoteAddress : originalRequest.getRemoteAddress());
this.sslInfo = (sslInfo != null ? sslInfo : originalRequest.getSslInfo());
this.body = body;
@@ -202,8 +203,14 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
}
@Override
public HttpMethod getMethod() {
return this.method;
}
@Override
@Deprecated
public String getMethodValue() {
return this.methodValue;
return this.method.name();
}
@Override

View File

@@ -36,6 +36,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpLogging;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -138,8 +139,13 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
return uri;
}
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.request.method().name());
}
@Override
@Deprecated
public String getMethodValue() {
return this.request.method().name();
}

View File

@@ -61,12 +61,12 @@ public class ServerHttpRequestDecorator implements ServerHttpRequest {
}
@Override
@Nullable
public HttpMethod getMethod() {
return getDelegate().getMethod();
}
@Override
@Deprecated
public String getMethodValue() {
return getDelegate().getMethodValue();
}

View File

@@ -41,6 +41,7 @@ import org.springframework.core.io.buffer.DataBufferFactory;
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.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
@@ -158,8 +159,13 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
return (headers != null ? headers : headerValues);
}
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.request.getMethod());
}
@Override
@Deprecated
public String getMethodValue() {
return this.request.getMethod();
}

View File

@@ -35,6 +35,7 @@ import reactor.core.publisher.Flux;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
@@ -77,6 +78,12 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
}
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.exchange.getRequestMethod().toString());
}
@Override
@Deprecated
public String getMethodValue() {
return this.exchange.getRequestMethod().toString();
}

View File

@@ -16,10 +16,8 @@
package org.springframework.web;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Set;
import jakarta.servlet.ServletException;
@@ -117,14 +115,12 @@ public class HttpRequestMethodNotSupportedException extends ServletException {
if (this.supportedMethods == null) {
return null;
}
List<HttpMethod> supportedMethods = new ArrayList<>(this.supportedMethods.length);
Set<HttpMethod> supportedMethods = new LinkedHashSet<>(this.supportedMethods.length);
for (String value : this.supportedMethods) {
HttpMethod resolved = HttpMethod.resolve(value);
if (resolved != null) {
supportedMethods.add(resolved);
}
HttpMethod method = HttpMethod.valueOf(value);
supportedMethods.add(method);
}
return EnumSet.copyOf(supportedMethods);
return supportedMethods;
}
}

View File

@@ -118,9 +118,8 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
* Return the HTTP method of the request.
* @since 4.0.2
*/
@Nullable
public HttpMethod getHttpMethod() {
return HttpMethod.resolve(getRequest().getMethod());
return HttpMethod.valueOf(getRequest().getMethod());
}
@Override

View File

@@ -258,7 +258,7 @@ public class CorsConfiguration {
this.resolvedMethods = null;
break;
}
this.resolvedMethods.add(HttpMethod.resolve(method));
this.resolvedMethods.add(HttpMethod.valueOf(method));
}
}
else {
@@ -302,7 +302,7 @@ public class CorsConfiguration {
this.resolvedMethods = null;
}
else if (this.resolvedMethods != null) {
this.resolvedMethods.add(HttpMethod.resolve(method));
this.resolvedMethods.add(HttpMethod.valueOf(method));
}
}
}
@@ -447,7 +447,7 @@ public class CorsConfiguration {
if (this.allowedMethods == null) {
this.allowedMethods = DEFAULT_PERMIT_METHODS;
this.resolvedMethods = DEFAULT_PERMIT_METHODS
.stream().map(HttpMethod::resolve).collect(Collectors.toList());
.stream().map(HttpMethod::valueOf).collect(Collectors.toList());
}
if (this.allowedHeaders == null) {
this.allowedHeaders = DEFAULT_PERMIT_ALL;

View File

@@ -91,8 +91,7 @@ public class HiddenHttpMethodFilter implements WebFilter {
}
private ServerWebExchange mapExchange(ServerWebExchange exchange, String methodParamValue) {
HttpMethod httpMethod = HttpMethod.resolve(methodParamValue.toUpperCase(Locale.ENGLISH));
Assert.notNull(httpMethod, () -> "HttpMethod '" + methodParamValue + "' not supported");
HttpMethod httpMethod = HttpMethod.valueOf(methodParamValue.toUpperCase(Locale.ENGLISH));
if (ALLOWED_METHODS.contains(httpMethod)) {
return exchange.mutate().request(builder -> builder.method(httpMethod)).build();
}

View File

@@ -52,7 +52,6 @@ public interface MultipartHttpServletRequest extends HttpServletRequest, Multipa
/**
* Return this request's method as a convenient HttpMethod instance.
*/
@Nullable
HttpMethod getRequestMethod();
/**

View File

@@ -64,7 +64,7 @@ public abstract class AbstractMultipartHttpServletRequest extends HttpServletReq
@Override
public HttpMethod getRequestMethod() {
return HttpMethod.resolve(getRequest().getMethod());
return HttpMethod.valueOf(getRequest().getMethod());
}
@Override

View File

@@ -28,8 +28,8 @@ import java.util.Arrays;
import java.util.Base64;
import java.util.Calendar;
import java.util.Collections;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
@@ -129,7 +129,7 @@ public class HttpHeadersTests {
@Test
void allow() {
EnumSet<HttpMethod> methods = EnumSet.of(HttpMethod.GET, HttpMethod.POST);
Set<HttpMethod> methods = new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST));
headers.setAllow(methods);
assertThat(headers.getAllow()).as("Invalid Allow header").isEqualTo(methods);
assertThat(headers.getFirst("Allow")).as("Invalid Allow header").isEqualTo("GET,POST");

View File

@@ -268,6 +268,7 @@ public class InterceptingClientHttpRequestFactoryTests {
}
@Override
@Deprecated
public String getMethodValue() {
return method.name();
}

View File

@@ -25,9 +25,9 @@ import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
@@ -62,8 +62,8 @@ public class ClientHttpConnectorTests {
private static final int BUF_SIZE = 1024;
private static final EnumSet<HttpMethod> METHODS_WITH_BODY =
EnumSet.of(HttpMethod.PUT, HttpMethod.POST, HttpMethod.PATCH);
private static final Set<HttpMethod> METHODS_WITH_BODY =
Set.of(HttpMethod.PUT, HttpMethod.POST, HttpMethod.PATCH);
private final MockWebServer server = new MockWebServer();

View File

@@ -23,7 +23,6 @@ import java.lang.annotation.Target;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
@@ -275,7 +274,7 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
setUpClient(clientHttpRequestFactory);
Set<HttpMethod> allowed = template.optionsForAllow(new URI(baseUrl + "/get"));
assertThat(allowed).as("Invalid response").isEqualTo(EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE));
assertThat(allowed).as("Invalid response").isEqualTo(Set.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE));
}
@ParameterizedRestTemplateTest

View File

@@ -21,7 +21,6 @@ import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -580,7 +579,7 @@ class RestTemplateTests {
mockSentRequest(OPTIONS, "https://example.com");
mockResponseStatus(HttpStatus.OK);
HttpHeaders responseHeaders = new HttpHeaders();
EnumSet<HttpMethod> expected = EnumSet.of(GET, POST);
Set<HttpMethod> expected = Set.of(GET, POST);
responseHeaders.setAllow(expected);
given(response.getHeaders()).willReturn(responseHeaders);

View File

@@ -20,7 +20,6 @@ import java.time.Duration;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -75,16 +74,6 @@ public class HiddenHttpMethodFilterTests {
assertThat(this.filterChain.getHttpMethod()).isEqualTo(HttpMethod.DELETE);
}
@Test
public void filterWithInvalidMethodValue() {
StepVerifier.create(postForm("_method=INVALID"))
.consumeErrorWith(error -> {
assertThat(error).isInstanceOf(IllegalArgumentException.class);
assertThat(error.getMessage()).isEqualTo("HttpMethod 'INVALID' not supported");
})
.verify();
}
@Test
public void filterWithHttpPut() {

View File

@@ -32,6 +32,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.LinkedMultiValueMap;
@@ -680,6 +681,12 @@ class UriComponentsBuilderTests {
void fromHttpRequestWithEmptyScheme() {
HttpRequest request = new HttpRequest() {
@Override
public HttpMethod getMethod() {
return HttpMethod.GET;
}
@Override
@Deprecated
public String getMethodValue() {
return "GET";
}

View File

@@ -25,10 +25,6 @@ import org.springframework.core.annotation.SynthesizingMethodParameter
import org.springframework.core.convert.support.DefaultConversionService
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.web.testfixture.servlet.MockHttpServletRequest
import org.springframework.web.testfixture.servlet.MockHttpServletResponse
import org.springframework.web.testfixture.servlet.MockMultipartFile
import org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest
import org.springframework.util.ReflectionUtils
import org.springframework.web.bind.MissingServletRequestParameterException
import org.springframework.web.bind.annotation.RequestParam
@@ -39,6 +35,10 @@ import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.context.request.ServletWebRequest
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.multipart.support.MissingServletRequestPartException
import org.springframework.web.testfixture.servlet.MockHttpServletRequest
import org.springframework.web.testfixture.servlet.MockHttpServletResponse
import org.springframework.web.testfixture.servlet.MockMultipartFile
import org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest
/**
* Kotlin test fixture for [RequestParamMethodArgumentResolver].
@@ -156,7 +156,7 @@ class RequestParamMethodArgumentResolverKotlinTests {
@Test
fun resolveNullableRequiredWithoutMultipartParameter() {
request.method = HttpMethod.POST.name
request.method = HttpMethod.POST.name()
request.contentType = MediaType.MULTIPART_FORM_DATA_VALUE
var result = resolver.resolveArgument(nullableMultipartParamRequired, null, webRequest, binderFactory)
@@ -176,7 +176,7 @@ class RequestParamMethodArgumentResolverKotlinTests {
@Test
fun resolveNullableNotRequiredWithoutMultipartParameter() {
request.method = HttpMethod.POST.name
request.method = HttpMethod.POST.name()
request.contentType = MediaType.MULTIPART_FORM_DATA_VALUE
var result = resolver.resolveArgument(nullableMultipartParamNotRequired, null, webRequest, binderFactory)
@@ -196,7 +196,7 @@ class RequestParamMethodArgumentResolverKotlinTests {
@Test
fun resolveNonNullableRequiredWithoutMultipartParameter() {
request.method = HttpMethod.POST.name
request.method = HttpMethod.POST.name()
request.contentType = MediaType.MULTIPART_FORM_DATA_VALUE
assertThatExceptionOfType(MissingServletRequestPartException::class.java).isThrownBy {
@@ -217,7 +217,7 @@ class RequestParamMethodArgumentResolverKotlinTests {
@Test
fun resolveNonNullableNotRequiredWithoutMultipartParameter() {
request.method = HttpMethod.POST.name
request.method = HttpMethod.POST.name()
request.contentType = MediaType.MULTIPART_FORM_DATA_VALUE
assertThatExceptionOfType(NullPointerException::class.java).isThrownBy {

View File

@@ -96,6 +96,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest implements
}
@Override
@Deprecated
public String getMethodValue() {
return this.httpMethod.name();
}

View File

@@ -55,10 +55,7 @@ import org.springframework.web.util.UriComponentsBuilder;
*/
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
/**
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>).
*/
private final String httpMethod;
private final HttpMethod httpMethod;
private final MultiValueMap<String, HttpCookie> cookies;
@@ -73,13 +70,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final Flux<DataBuffer> body;
private MockServerHttpRequest(String httpMethod,
private MockServerHttpRequest(HttpMethod httpMethod,
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
super(uri, contextPath, headers);
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
this.httpMethod = httpMethod;
this.cookies = cookies;
this.localAddress = localAddress;
@@ -90,14 +86,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
@Nullable
public HttpMethod getMethod() {
return HttpMethod.resolve(this.httpMethod);
return this.httpMethod;
}
@Override
@Deprecated
public String getMethodValue() {
return this.httpMethod;
return this.httpMethod.name();
}
@Override
@@ -218,7 +214,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
public static BodyBuilder method(HttpMethod method, URI 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);
return new DefaultBodyBuilder(method, url);
}
/**
@@ -242,9 +238,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
* @param vars variables to expand into the template
* @return the created builder
* @since 5.2.7
* @deprecated in favor of {@link #method(HttpMethod, String, Object...)}
*/
@Deprecated
public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
return new DefaultBodyBuilder(httpMethod, toUri(uri, vars));
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
return new DefaultBodyBuilder(HttpMethod.valueOf(httpMethod), toUri(uri, vars));
}
private static URI toUri(String uri, Object[] vars) {
@@ -427,7 +426,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private static class DefaultBodyBuilder implements BodyBuilder {
private final String methodValue;
private final HttpMethod method;
private final URI url;
@@ -449,8 +448,8 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private SslInfo sslInfo;
DefaultBodyBuilder(String method, URI url) {
this.methodValue = method;
DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
this.url = url;
}
@@ -589,7 +588,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
applyCookiesIfNecessary();
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
return new MockServerHttpRequest(this.method, getUrlToUse(), this.contextPath,
this.headers, this.cookies, this.localAddress, this.remoteAddress, this.sslInfo, body);
}

View File

@@ -141,7 +141,13 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
@Override
public HttpMethod getRequestMethod() {
return HttpMethod.resolve(getMethod());
String method = getMethod();
if (method != null) {
return HttpMethod.valueOf(method);
}
else {
return null;
}
}
@Override