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:
greg.lee
2023-05-16 23:51:36 +09:00
committed by Sam Brannen
parent 424daede2f
commit fa85657801
2 changed files with 29 additions and 0 deletions

View File

@@ -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.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}
* 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);
}