Introduce method in MockHttpServletRequestBuilder to set remote address
Co-authored-by: Sam Brannen <sam@sambrannen.com> See gh-30510 Closes gh-30497
This commit is contained in:
@@ -103,6 +103,9 @@ public class MockHttpServletRequestBuilder
|
|||||||
@Nullable
|
@Nullable
|
||||||
private MockHttpSession session;
|
private MockHttpSession session;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String remoteAddress;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private String characterEncoding;
|
private String characterEncoding;
|
||||||
|
|
||||||
@@ -526,6 +529,17 @@ public class MockHttpServletRequestBuilder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the remote address of the request.
|
||||||
|
* @param remoteAddress the remote address (IP)
|
||||||
|
* @since 6.0.10
|
||||||
|
*/
|
||||||
|
public MockHttpServletRequestBuilder remoteAddress(String remoteAddress) {
|
||||||
|
Assert.hasText(remoteAddress, "'remoteAddress' must not be null or blank");
|
||||||
|
this.remoteAddress = remoteAddress;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An extension point for further initialization of {@link MockHttpServletRequest}
|
* An extension point for further initialization of {@link MockHttpServletRequest}
|
||||||
* in ways not built directly into the {@code MockHttpServletRequestBuilder}.
|
* in ways not built directly into the {@code MockHttpServletRequestBuilder}.
|
||||||
@@ -583,6 +597,9 @@ public class MockHttpServletRequestBuilder
|
|||||||
if (this.session == null) {
|
if (this.session == null) {
|
||||||
this.session = parentBuilder.session;
|
this.session = parentBuilder.session;
|
||||||
}
|
}
|
||||||
|
if (this.remoteAddress == null) {
|
||||||
|
this.remoteAddress = parentBuilder.remoteAddress;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.characterEncoding == null) {
|
if (this.characterEncoding == null) {
|
||||||
this.characterEncoding = parentBuilder.characterEncoding;
|
this.characterEncoding = parentBuilder.characterEncoding;
|
||||||
@@ -687,6 +704,9 @@ public class MockHttpServletRequestBuilder
|
|||||||
if (this.principal != null) {
|
if (this.principal != null) {
|
||||||
request.setUserPrincipal(this.principal);
|
request.setUserPrincipal(this.principal);
|
||||||
}
|
}
|
||||||
|
if (this.remoteAddress != null) {
|
||||||
|
request.setRemoteAddr(this.remoteAddress);
|
||||||
|
}
|
||||||
if (this.session != null) {
|
if (this.session != null) {
|
||||||
request.setSession(this.session);
|
request.setSession(this.session);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -559,6 +559,15 @@ class MockHttpServletRequestBuilderTests {
|
|||||||
assertThat(request.getUserPrincipal()).isEqualTo(user);
|
assertThat(request.getUserPrincipal()).isEqualTo(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void remoteAddress() {
|
||||||
|
String ip = "10.0.0.1";
|
||||||
|
this.builder.remoteAddress(ip);
|
||||||
|
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
|
||||||
|
|
||||||
|
assertThat(request.getRemoteAddr()).isEqualTo(ip);
|
||||||
|
}
|
||||||
|
|
||||||
@Test // SPR-12945
|
@Test // SPR-12945
|
||||||
void mergeInvokesDefaultRequestPostProcessorFirst() {
|
void mergeInvokesDefaultRequestPostProcessorFirst() {
|
||||||
final String ATTR = "ATTR";
|
final String ATTR = "ATTR";
|
||||||
|
|||||||
Reference in New Issue
Block a user