Merge pull request #927 from ksokol-improvement/SPR-13719
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -60,16 +60,20 @@ import org.springframework.web.util.UriUtils;
|
||||
*
|
||||
* <p>Application tests will typically access this builder through the static factory
|
||||
* methods in {@link MockMvcRequestBuilders}.
|
||||
* <p>Although this class cannot be extended, additional ways to initialize
|
||||
* the {@code MockHttpServletRequest} can be plugged in via
|
||||
* {@link #with(RequestPostProcessor)}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Arjen Poutsma
|
||||
* @author Sam Brannen
|
||||
* @author Kamill Sokol
|
||||
* @since 3.2
|
||||
*/
|
||||
public class MockHttpServletRequestBuilder
|
||||
implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable {
|
||||
|
||||
private final HttpMethod method;
|
||||
private final String method;
|
||||
|
||||
private final URI url;
|
||||
|
||||
@@ -119,27 +123,33 @@ public class MockHttpServletRequestBuilder
|
||||
* @param vars zero or more URL variables
|
||||
*/
|
||||
MockHttpServletRequestBuilder(HttpMethod httpMethod, String url, Object... vars) {
|
||||
this(httpMethod, UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri());
|
||||
this(httpMethod.name(), UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri());
|
||||
}
|
||||
|
||||
/**
|
||||
* Package private constructor. To get an instance, use static factory
|
||||
* methods in {@link MockMvcRequestBuilders}.
|
||||
* <p>Although this class cannot be extended, additional ways to initialize
|
||||
* the {@code MockHttpServletRequest} can be plugged in via
|
||||
* {@link #with(RequestPostProcessor)}.
|
||||
* Alternative to {@link #MockHttpServletRequestBuilder(HttpMethod, String, Object...)}
|
||||
* with a pre-built URI.
|
||||
* @param httpMethod the HTTP method (GET, POST, etc)
|
||||
* @param url the URL
|
||||
* @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;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a request parameter to the {@link MockHttpServletRequest}.
|
||||
* <p>If called more than once, new values get added to existing ones.
|
||||
@@ -585,7 +595,7 @@ public class MockHttpServletRequestBuilder
|
||||
request.setServerPort(this.url.getPort());
|
||||
}
|
||||
|
||||
request.setMethod(this.method.name());
|
||||
request.setMethod(this.method);
|
||||
|
||||
for (String name : this.headers.keySet()) {
|
||||
for (Object value : this.headers.get(name)) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -42,6 +42,7 @@ import org.springframework.test.web.servlet.RequestBuilder;
|
||||
* @author Greg Turnquist
|
||||
* @author Sebastien Deleuze
|
||||
* @author Sam Brannen
|
||||
* @author Kamill Sokol
|
||||
* @since 3.2
|
||||
*/
|
||||
public abstract class MockMvcRequestBuilders {
|
||||
@@ -49,10 +50,10 @@ public abstract class MockMvcRequestBuilders {
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a GET request.
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder get(String urlTemplate, Object... urlVariables) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.GET, urlTemplate, urlVariables);
|
||||
public static MockHttpServletRequestBuilder get(String urlTemplate, Object... urlVars) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.GET, urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,10 +68,10 @@ public abstract class MockMvcRequestBuilders {
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a POST request.
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder post(String urlTemplate, Object... urlVariables) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.POST, urlTemplate, urlVariables);
|
||||
public static MockHttpServletRequestBuilder post(String urlTemplate, Object... urlVars) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.POST, urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,10 +86,10 @@ public abstract class MockMvcRequestBuilders {
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a PUT request.
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder put(String urlTemplate, Object... urlVariables) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.PUT, urlTemplate, urlVariables);
|
||||
public static MockHttpServletRequestBuilder put(String urlTemplate, Object... urlVars) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.PUT, urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,10 +104,10 @@ public abstract class MockMvcRequestBuilders {
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... urlVariables) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, urlTemplate, urlVariables);
|
||||
public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... urlVars) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,10 +122,10 @@ public abstract class MockMvcRequestBuilders {
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a DELETE request.
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder delete(String urlTemplate, Object... urlVariables) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.DELETE, urlTemplate, urlVariables);
|
||||
public static MockHttpServletRequestBuilder delete(String urlTemplate, Object... urlVars) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.DELETE, urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,10 +140,10 @@ public abstract class MockMvcRequestBuilders {
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for an OPTIONS request.
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder options(String urlTemplate, Object... urlVariables) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS, urlTemplate, urlVariables);
|
||||
public static MockHttpServletRequestBuilder options(String urlTemplate, Object... urlVars) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS, urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,11 +158,11 @@ public abstract class MockMvcRequestBuilders {
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
* @since 4.1
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder head(String urlTemplate, Object... urlVariables) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, urlTemplate, urlVariables);
|
||||
public static MockHttpServletRequestBuilder head(String urlTemplate, Object... urlVars) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,12 +176,12 @@ public abstract class MockMvcRequestBuilders {
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
|
||||
* @param httpMethod the HTTP method
|
||||
* @param method the HTTP method (GET, POST, etc)
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) {
|
||||
return new MockHttpServletRequestBuilder(httpMethod, urlTemplate, urlVariables);
|
||||
public static MockHttpServletRequestBuilder request(HttpMethod method, String urlTemplate, Object... urlVars) {
|
||||
return new MockHttpServletRequestBuilder(method, urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,13 +194,23 @@ public abstract class MockMvcRequestBuilders {
|
||||
return new MockHttpServletRequestBuilder(httpMethod, uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative factory method that allows for custom HTTP verbs (e.g. WebDAV).
|
||||
* @param httpMethod the HTTP method
|
||||
* @param uri the URL
|
||||
* @since 4.3
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder request(String httpMethod, URI uri) {
|
||||
return new MockHttpServletRequestBuilder(httpMethod, uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
* @param urlVars zero or more URL variables
|
||||
*/
|
||||
public static MockMultipartHttpServletRequestBuilder fileUpload(String urlTemplate, Object... urlVariables) {
|
||||
return new MockMultipartHttpServletRequestBuilder(urlTemplate, urlVariables);
|
||||
public static MockMultipartHttpServletRequestBuilder fileUpload(String urlTemplate, Object... urlVars) {
|
||||
return new MockMultipartHttpServletRequestBuilder(urlTemplate, urlVars);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -25,7 +25,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
@@ -43,8 +42,12 @@ import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.servlet.FlashMap;
|
||||
import org.springframework.web.servlet.support.SessionFlashMapManager;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* Unit tests for building a {@link MockHttpServletRequest} with
|
||||
@@ -429,7 +432,7 @@ public class MockHttpServletRequestBuilderTests {
|
||||
|
||||
@Test
|
||||
public void sessionAttributes() {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("foo", "bar");
|
||||
this.builder.sessionAttrs(map);
|
||||
|
||||
@@ -472,6 +475,7 @@ public class MockHttpServletRequestBuilderTests {
|
||||
}
|
||||
|
||||
// SPR-12945
|
||||
|
||||
@Test
|
||||
public void mergeInvokesDefaultRequestPostProcessorFirst() {
|
||||
final String ATTR = "ATTR";
|
||||
@@ -492,6 +496,20 @@ public class MockHttpServletRequestBuilderTests {
|
||||
assertEquals(EXEPCTED, request.getAttribute(ATTR));
|
||||
}
|
||||
|
||||
// SPR-13719
|
||||
|
||||
@Test
|
||||
public void arbitraryMethod() {
|
||||
String httpMethod = "REPort";
|
||||
URI url = UriComponentsBuilder.fromPath("/foo/{bar}").buildAndExpand(42).toUri();
|
||||
this.builder = new MockHttpServletRequestBuilder(httpMethod, url);
|
||||
|
||||
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
|
||||
|
||||
assertEquals(httpMethod, request.getMethod());
|
||||
assertEquals("/foo/42", request.getPathInfo());
|
||||
}
|
||||
|
||||
|
||||
private final class User implements Principal {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user