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

NullPointerException in MockHttpServletRequestBuilder for URI string with no protocol #24556

Closed
dFarras opened this issue Feb 19, 2020 · 2 comments
Assignees
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@dFarras
Copy link

dFarras commented Feb 19, 2020

Affects: spring-boot-starter-test 2.1.6.RELEASE

Use Case: When performing an spring integration test over a get controller we add an url as a String to MockHttpServletRequestBuilder to perform such action.

Issue: When we add that url String without the protocol (for instance: "localhost:port...") the request returns a NullPointerException.

Code failing:

MockHttpServletResponse response = 
    mockMvc.perform(MockMvcRequestBuilders.get("localhost:port/some domain/etc...")).andReturn().getResponse();

Code working:

MockHttpServletResponse response = 
    mockMvc.perform(MockMvcRequestBuilders.get("http://localhost:port/some domain/etc...")).andReturn().getResponse();

Just to clarify the only difference is that the second url contains the http protcol explicitly declared

Expectation:
Since http is the default protocol in many other frameworks I expect it to be inferred as url protocl if no other is defined explicitly.

Cause:
Inside that MockMvcRequestBuilders.get("uri") method there is a call to a private constructor who converts that String into an actual URI

MockHttpServletRequestBuilder(HttpMethod httpMethod, String url, Object... vars) {
	this(httpMethod.name(), UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri());
}

But it does not work properly since this.utl.getRawPath() returns null which later causes updatePathRequestProperties to throw mentioned NullPointerException
image

Thanks in advance!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 19, 2020
@rstoyanchev rstoyanchev self-assigned this Feb 20, 2020
@rstoyanchev rstoyanchev added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 20, 2020
@rstoyanchev rstoyanchev added this to the 5.2.4 milestone Feb 20, 2020
@rstoyanchev
Copy link
Contributor

MockMvcRequestBuilders uses UriComponentsBuilder.fromUriString, which can't parse the given input correctly because "localhost:" looks like a scheme.

Switching to UriComponentsBuilder.fromHttpUrl provides a more helpful error:

java.lang.IllegalArgumentException: [localhost:8080/path] is not a valid HTTP URL

I don't we should go further and fill in the protocol since we don't know if that should be "http:" or "https:" and that could lead to surprises, and possibly even possible contradictions (e.g. "localhost:443").

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Feb 20, 2020

Actually it looks like fromUriString is quite useful because it allows to provide a path only. We need to keep that so it comes down to, either provide a URI with a path only (i.e. no scheme, host, port), or otherwise a proper HTTP URL.

If you want shorter, why not skip the host and port too?

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) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants