Deprecate use of raw String for HttpMethod in MockMvc

Closes gh-32914
This commit is contained in:
Stéphane Nicoll
2024-05-28 09:24:08 +02:00
parent 8871d67298
commit f6504c5f91
3 changed files with 9 additions and 16 deletions

View File

@@ -86,7 +86,7 @@ import org.springframework.web.util.UrlPathHelper;
public class MockHttpServletRequestBuilder
implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable {
private final String method;
private final HttpMethod method;
private final URI url;
@@ -150,7 +150,7 @@ public class MockHttpServletRequestBuilder
* @param vars zero or more URI variables
*/
MockHttpServletRequestBuilder(HttpMethod httpMethod, String url, Object... vars) {
this(httpMethod.name(), initUri(url, vars));
this(httpMethod, initUri(url, vars));
}
private static URI initUri(String url, Object[] vars) {
@@ -169,16 +169,6 @@ public class MockHttpServletRequestBuilder
* @since 4.0.3
*/
MockHttpServletRequestBuilder(HttpMethod httpMethod, URI url) {
this(httpMethod.name(), url);
}
/**
* Alternative constructor for custom HTTP methods.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url the URL
* @since 4.3
*/
MockHttpServletRequestBuilder(String httpMethod, URI url) {
Assert.notNull(httpMethod, "'httpMethod' is required");
Assert.notNull(url, "'url' is required");
this.method = httpMethod;
@@ -716,7 +706,7 @@ public class MockHttpServletRequestBuilder
MockHttpServletRequest request = createServletRequest(servletContext);
request.setAsyncSupported(true);
request.setMethod(this.method);
request.setMethod(this.method.name());
String requestUri = this.url.getRawPath();
request.setRequestURI(requestUri);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
@@ -199,9 +199,11 @@ public abstract class MockMvcRequestBuilders {
* @param httpMethod the HTTP method
* @param uri the URL
* @since 4.3
* @deprecated in favor of {@link #request(HttpMethod, URI)}
*/
@Deprecated(since = "6.2")
public static MockHttpServletRequestBuilder request(String httpMethod, URI uri) {
return new MockHttpServletRequestBuilder(httpMethod, uri);
return request(HttpMethod.valueOf(httpMethod), uri);
}
/**