Browse Source

Add merged RequestPostProcessor to front on merge

Previously MockHttpServletRequestBuilder merge method would append the
parent's (default) RequestPostProcessor implementations to the end. This
means that the default RequestPostProcessor implementations would override
values set by previous RequestPostProcessor implementations.

This commit ensures that the default RequestPostProcessor are preformed
first so that additional RequestPostProcessor implementations override
the defaults.

Issue: SPR-12945
pull/903/head
Rob Winch 10 years ago committed by Rossen Stoyanchev
parent
commit
1a6aeb17e1
  1. 2
      spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
  2. 48
      spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

2
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

@ -537,7 +537,7 @@ public class MockHttpServletRequestBuilder @@ -537,7 +537,7 @@ public class MockHttpServletRequestBuilder
this.pathInfo = parentBuilder.pathInfo;
}
this.postProcessors.addAll(parentBuilder.postProcessors);
this.postProcessors.addAll(0, parentBuilder.postProcessors);
return this;
}

48
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

@ -23,12 +23,12 @@ import java.util.HashMap; @@ -23,12 +23,12 @@ 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;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
@ -434,6 +434,27 @@ public class MockHttpServletRequestBuilderTests { @@ -434,6 +434,27 @@ public class MockHttpServletRequestBuilderTests {
assertEquals(user, request.getUserPrincipal());
}
// SPR-12945
@Test
public void mergeInvokesDefaultRequestPostProcessorFirst() {
final String ATTR = "ATTR";
final String EXEPCTED = "override";
MockHttpServletRequestBuilder defaultBuilder =
new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo/bar")
.with(requestAttr(ATTR).value("default"));
builder
.with(requestAttr(ATTR).value(EXEPCTED));
builder.merge(defaultBuilder);
MockHttpServletRequest request = builder.buildRequest(servletContext);
request = builder.postProcessRequest(request);
assertEquals(EXEPCTED, request.getAttribute(ATTR));
}
private final class User implements Principal {
@ -443,4 +464,29 @@ public class MockHttpServletRequestBuilderTests { @@ -443,4 +464,29 @@ public class MockHttpServletRequestBuilderTests {
}
}
private static RequestAttributePostProcessor requestAttr(String attrName) {
return new RequestAttributePostProcessor().attr(attrName);
}
private static class RequestAttributePostProcessor implements RequestPostProcessor {
String attr;
String value;
public RequestAttributePostProcessor attr(String attr) {
this.attr = attr;
return this;
}
public RequestAttributePostProcessor value(String value) {
this.value = value;
return this;
}
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
request.setAttribute(attr, value);
return request;
}
}
}

Loading…
Cancel
Save