Skip to content

NullPointerException in MockHttpServletRequestBuilder for URI string with no protocol #24556

Closed
@dFarras

Description

@dFarras

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!

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions