Introduce method in MockHttpServletRequestBuilder to set remote address
Co-authored-by: Sam Brannen <sam@sambrannen.com> Closes gh-30497
This commit is contained in:
@@ -103,6 +103,9 @@ public class MockHttpServletRequestBuilder
|
||||
@Nullable
|
||||
private MockHttpSession session;
|
||||
|
||||
@Nullable
|
||||
private String remoteAddress;
|
||||
|
||||
@Nullable
|
||||
private String characterEncoding;
|
||||
|
||||
@@ -526,6 +529,17 @@ public class MockHttpServletRequestBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the remote address of the request.
|
||||
* @param remoteAddress the remote address (IP)
|
||||
* @since 6.1
|
||||
*/
|
||||
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}
|
||||
* in ways not built directly into the {@code MockHttpServletRequestBuilder}.
|
||||
@@ -583,6 +597,9 @@ public class MockHttpServletRequestBuilder
|
||||
if (this.session == null) {
|
||||
this.session = parentBuilder.session;
|
||||
}
|
||||
if (this.remoteAddress == null) {
|
||||
this.remoteAddress = parentBuilder.remoteAddress;
|
||||
}
|
||||
|
||||
if (this.characterEncoding == null) {
|
||||
this.characterEncoding = parentBuilder.characterEncoding;
|
||||
@@ -687,6 +704,9 @@ public class MockHttpServletRequestBuilder
|
||||
if (this.principal != null) {
|
||||
request.setUserPrincipal(this.principal);
|
||||
}
|
||||
if (this.remoteAddress != null) {
|
||||
request.setRemoteAddr(this.remoteAddress);
|
||||
}
|
||||
if (this.session != null) {
|
||||
request.setSession(this.session);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user