Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce method in MockHttpServletRequestBuilder to set remote address #30484

Closed
leewin12 opened this issue May 13, 2023 · 6 comments
Closed
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) status: ideal-for-contribution An issue that a contributor can help us with status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Comments

@leewin12
Copy link
Contributor

leewin12 commented May 13, 2023

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));
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 13, 2023
@sbrannen sbrannen changed the title Proposal to Add a Method for Setting Remote Address in MockHttpServletRequestBuilder Introduce remoteAddr() in MockHttpServletRequestBuilder to set remote address May 15, 2023
@sbrannen sbrannen added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels May 15, 2023
@sbrannen
Copy link
Member

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?

@sbrannen
Copy link
Member

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));

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label May 15, 2023
@leewin12
Copy link
Contributor Author

leewin12 commented May 15, 2023

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));

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.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 15, 2023
@sbrannen sbrannen removed the status: feedback-provided Feedback has been provided label May 15, 2023
@sbrannen sbrannen changed the title Introduce remoteAddr() in MockHttpServletRequestBuilder to set remote address Introduce method in MockHttpServletRequestBuilder to set remote address May 15, 2023
@rstoyanchev
Copy link
Contributor

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

@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged or decided on label May 16, 2023
@rstoyanchev rstoyanchev added this to the 6.1.0-M1 milestone May 16, 2023
@rstoyanchev rstoyanchev added the status: ideal-for-contribution An issue that a contributor can help us with label May 16, 2023
@sbrannen sbrannen added the status: superseded An issue that has been superseded by another label May 16, 2023
@leewin12
Copy link
Contributor Author

leewin12 commented May 16, 2023

@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.

@sbrannen
Copy link
Member

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2023
@sbrannen sbrannen removed this from the 6.1.0-M1 milestone May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) status: ideal-for-contribution An issue that a contributor can help us with status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants