Description
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:
- MockHttpServletRequest with Host: set builds wrong getRequestURL() [SPR-16138] #20686 MockHttpServletRequest with Host: set builds wrong getRequestURL()
Referenced from: commits 110be33