Expose the uriTemplate used to build a MockHttpServletRequest
This commit exposes the unexpanded URI template used to build a MockHttpServletRequest. This allows MockMvc users to retrieve that information, in consistency with ExchangeResult in WebTestClient. Closes gh-33509
This commit is contained in:
@@ -256,6 +256,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
@Nullable
|
||||
private String requestedSessionId;
|
||||
|
||||
@Nullable
|
||||
private String uriTemplate;
|
||||
|
||||
@Nullable
|
||||
private String requestURI;
|
||||
|
||||
@@ -1284,6 +1287,24 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
return this.requestedSessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the original URI template used to prepare the request, if any.
|
||||
* @param uriTemplate the URI template used to set up the request, if any
|
||||
* @since 6.2
|
||||
*/
|
||||
public void setUriTemplate(@Nullable String uriTemplate) {
|
||||
this.uriTemplate = uriTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the original URI template used to prepare the request, if any.
|
||||
* @since 6.2
|
||||
*/
|
||||
@Nullable
|
||||
public String getUriTemplate() {
|
||||
return this.uriTemplate;
|
||||
}
|
||||
|
||||
public void setRequestURI(@Nullable String requestURI) {
|
||||
this.requestURI = requestURI;
|
||||
}
|
||||
|
||||
@@ -82,6 +82,9 @@ public abstract class AbstractMockHttpServletRequestBuilder<B extends AbstractMo
|
||||
|
||||
private final HttpMethod method;
|
||||
|
||||
@Nullable
|
||||
private String uriTemplate;
|
||||
|
||||
@Nullable
|
||||
private URI uri;
|
||||
|
||||
@@ -152,15 +155,20 @@ public abstract class AbstractMockHttpServletRequestBuilder<B extends AbstractMo
|
||||
* Specify the URI using an absolute, fully constructed {@link java.net.URI}.
|
||||
*/
|
||||
public B uri(URI uri) {
|
||||
this.uri = uri;
|
||||
return self();
|
||||
return updateUri(uri, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the URI for the request using a URI template and URI variables.
|
||||
*/
|
||||
public B uri(String uriTemplate, Object... uriVariables) {
|
||||
return uri(initUri(uriTemplate, uriVariables));
|
||||
return updateUri(initUri(uriTemplate, uriVariables), uriTemplate);
|
||||
}
|
||||
|
||||
private B updateUri(URI uri, @Nullable String uriTemplate) {
|
||||
this.uri = uri;
|
||||
this.uriTemplate = uriTemplate;
|
||||
return self();
|
||||
}
|
||||
|
||||
private static URI initUri(String uri, Object[] vars) {
|
||||
@@ -594,6 +602,7 @@ public abstract class AbstractMockHttpServletRequestBuilder<B extends AbstractMo
|
||||
}
|
||||
if (this.uri == null) {
|
||||
this.uri = parentBuilder.uri;
|
||||
this.uriTemplate = parentBuilder.uriTemplate;
|
||||
}
|
||||
if (!StringUtils.hasText(this.contextPath)) {
|
||||
this.contextPath = parentBuilder.contextPath;
|
||||
@@ -707,6 +716,8 @@ public abstract class AbstractMockHttpServletRequestBuilder<B extends AbstractMo
|
||||
request.setAsyncSupported(true);
|
||||
request.setMethod(this.method.name());
|
||||
|
||||
request.setUriTemplate(this.uriTemplate);
|
||||
|
||||
String requestUri = this.uri.getRawPath();
|
||||
request.setRequestURI(requestUri);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user