I'd like to propose the addition of a method to set the remote address directly in the MockHttpServletRequestBuilder. Currently, this can be achieved using with(RequestPostProcessor), but it introduces some boilerplate code that could be streamlined.

Setting restrictions on remote addresses is a common practice in Spring applications. As such, it's important to be able to easily verify these restrictions through unit tests.

Given the nature of IP restrictions (e.g., CIDR input), this often results in multiple lines of repetitive code.

I'm happy to implement this feature if the proposal is accepted. Feel free to leave any comments or suggestions.

I deeply appreciate the work and dedication of all spring framework contributors.

Current Method (Java > 1.8):

mockMvc.perform(post("/example")
                .with(request -> { request.setRemoteAddr("192.168.0.100"); return request; } )
                .content(json));

Proposed Method:

mockMvc.perform(post("/example")
                .remoteAddr("192.168.0.100")
                .content(json));

Comment From: sbrannen

Hi @leewin12,

Thanks for the proposal.

It would not be much work to introduce remoteAddr(String), but I wonder how often that use case arises.

Also, there are several additional properties of MockHttpServletRequest that cannot be configured directly via MockHttpServletRequestBuilder. So that makes me wonder if it is sensible to introduce only remoteAddr(String) or if it would be prudent to introduce support for additional properties.

@rstoyanchev, WDYT?

Comment From: sbrannen

Currently, this can be achieved using with(RequestPostProcessor), but it introduces some boilerplate code that could be streamlined.

Have you considered extracting the boilerplate code into a static utility like the following?

public static RequestPostProcessor remoteAddress(String address) {
    return request -> {
        request.setRemoteAddr(address);
        return request;
    };
}

You could then use that like this:

mockMvc.perform(post("/example")
                .with(remoteAddress("192.168.0.100"))
                .content(json));

Comment From: leewin12

Currently, this can be achieved using with(RequestPostProcessor), but it introduces some boilerplate code that could be streamlined.

Have you considered extracting the boilerplate code into a static utility like the following?

java public static RequestPostProcessor remoteAddress(String address) { return request -> { request.setRemoteAddr(address); return request; }; }

You could then use that like this:

java mockMvc.perform(post("/example") .with(remoteAddress("192.168.0.100")) .content(json));

Indeed, we frequently use the static utility method in our projects. While it works well, I believe the proposed change would benefit many developers, especially in small and medium-sized companies.

PS: This change could also benefit companies that provide dynamic IP address restrictions, like B2B API providers.

Comment From: rstoyanchev

I think it makes sense to make this more of a first-class option.

Comment From: leewin12

@sbrannen @rstoyanchev thank you for your feedback! I've created a simple PR to introduce a method for setting the remote address in MockHttpServletRequestBuilder.

Please feel free to review and provide any feedback.

Comment From: sbrannen

  • superseded by #30497