Add URI based MockHttpServletRequestBuilder constructors
This commit adds new MockHttpServletRequestBuilder constructors with an URI parameter in addition to the URL template + URL variables existing ones. It gives more control on how the URL is built, allowing for example to use URL variables containing '/' character with proper encoding. Issue: SPR-11441
This commit is contained in:
committed by
Rossen Stoyanchev
parent
c10eeb414c
commit
0b69a0ba4b
@@ -17,6 +17,7 @@
|
||||
package org.springframework.test.web.servlet.request;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.security.Principal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -114,6 +115,7 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
||||
* <p>Although this class cannot be extended, additional ways to initialize
|
||||
* the {@code MockHttpServletRequest} can be plugged in via
|
||||
* {@link #with(RequestPostProcessor)}.
|
||||
* @param httpMethod the HTTP method (GET, POST, etc)
|
||||
* @param urlTemplate a URL template; the resulting URL will be encoded
|
||||
* @param urlVariables zero or more URL variables
|
||||
*/
|
||||
@@ -124,6 +126,23 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
||||
this.uriComponents = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVariables).encode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)}.
|
||||
* @param httpMethod the HTTP method (GET, POST, etc)
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
MockHttpServletRequestBuilder(HttpMethod httpMethod, URI url) {
|
||||
Assert.notNull(httpMethod, "httpMethod is required");
|
||||
Assert.notNull(url, "url is required");
|
||||
this.method = httpMethod;
|
||||
this.uriComponents = UriComponentsBuilder.fromUri(url).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a request parameter to the {@link MockHttpServletRequest}.
|
||||
* If called more than once, the new values are added.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.test.web.servlet.request;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.servlet.ServletContext;
|
||||
@@ -52,6 +53,20 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
|
||||
super.contentType(MediaType.MULTIPART_FORM_DATA);
|
||||
}
|
||||
|
||||
/**
|
||||
* Package-private constructor. Use static factory methods in
|
||||
* {@link MockMvcRequestBuilders}.
|
||||
* <p>For other ways to initialize a {@code MockMultipartHttpServletRequest},
|
||||
* see {@link #with(RequestPostProcessor)} and the
|
||||
* {@link RequestPostProcessor} extension point.
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
MockMultipartHttpServletRequestBuilder(URI url) {
|
||||
super(HttpMethod.POST, url);
|
||||
super.contentType(MediaType.MULTIPART_FORM_DATA);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new MockMultipartFile with the given content.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -22,6 +22,8 @@ import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.RequestBuilder;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* Static factory methods for {@link RequestBuilder}s.
|
||||
*
|
||||
@@ -31,6 +33,7 @@ import org.springframework.test.web.servlet.RequestBuilder;
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Greg Turnquist
|
||||
* @author Sebastien Deleuze
|
||||
* @since 3.2
|
||||
*/
|
||||
public abstract class MockMvcRequestBuilders {
|
||||
@@ -109,6 +112,80 @@ public abstract class MockMvcRequestBuilders {
|
||||
return new MockMultipartHttpServletRequestBuilder(urlTemplate, urlVariables);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a GET request.
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder get(URI url) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.GET, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a POST request.
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder post(URI url) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.POST, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a PUT request.
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder put(URI url) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.PUT, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder patch(URI url) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a DELETE request.
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder delete(URI url) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.DELETE, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for an OPTIONS request.
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder options(URI url) {
|
||||
return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
|
||||
* @param httpMethod the HTTP method (GET, POST, etc)
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, URI url) {
|
||||
return new MockHttpServletRequestBuilder(httpMethod, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MockHttpServletRequestBuilder} for a multipart request.
|
||||
* @param url the URL
|
||||
* @since 4.0.3
|
||||
*/
|
||||
public static MockMultipartHttpServletRequestBuilder fileUpload(URI url) {
|
||||
return new MockMultipartHttpServletRequestBuilder(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link RequestBuilder} for an async dispatch from the
|
||||
* {@link MvcResult} of the request that started async processing.
|
||||
|
||||
Reference in New Issue
Block a user