Skip to content

MockHttpServletRequest should honor "Host" header in getServerName() and getServerPort() [SPR-12088] #16704

Closed
@spring-projects-issues

Description

@spring-projects-issues

Hendy Irawan opened SPR-12088 and commented

Background

Javadoc for ServletRequest.getServerName()

Returns the host name of the server to which the request was sent.

It is the value of the part before ":" in the Host header value, if any, or the resolved server name, or the server IP address.

Javadoc for ServletRequest.getServerPort()

Returns the port number to which the request was sent.

It is the value of the part after ":" in the Host header value, if any, or the server port where the client connection was accepted on.


Status Quo

Neither getServerName() nor getServletPort() in Spring's MockHttpServletRequest honor the Host header as defined in the Servlet spec.

Example Mock MVC Unit test
@Test
public void register() throws Exception {
	ResultActions actions = mockMvc.perform(post("/frequency/portaluser")
			.header("Host", "acme") // this should be returned by HttpServletRequest.getServerName()
			.param("clientHashCode", "123")
			.param("email", "[email protected]")
			.accept(MediaType.TEXT_PLAIN));
	MvcResult result = actions.andReturn();
	log.info("Status: {}", result.getResponse().getStatus());
	log.info("Type: {}", result.getResponse().getContentType());
	log.info("Content: {}", result.getResponse().getContentAsString());
	actions.andExpect(status().isOk())
		.andExpect(content().string("18978932619,0\n"));
}

When observed from the other end, getting getServerName() (implemented by MockHttpServletRequest) currently returns localhost, not acme as expected. The same applies to the getServletPort() method in MockHttpServletRequest.


Affects: 3.0 GA

Issue Links:

Referenced from: commits 110be33

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions