You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MockHttpServletRequest should honor "Host" header in getServerName() and getServerPort() [SPR-12088] #16704
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
@Testpublicvoidregister() throwsException {
ResultActionsactions = 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));
MvcResultresult = 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.
Honor Host header for server name/port in MckHtSrvRq
Prior to this commit, the getServerName() and getServerPort() methods
in MockHttpServletRequest simply returned the 'mocked' serverName and
serverPort but ignored the 'Host' header entirely. Per the Servlet
specification, however, these methods must parse the server name or
port from the 'Host' header if it is present and otherwise fall back to
the resolved server name or port.
This commit fixes this by ensuring that getServerName() and
getServerPort() properly parse the server's name or port from the
'Host' header if it is present in the request. If the 'Host' header is
not present, MockHttpServletRequest falls back to returning the
'mocked' serverName and serverPort.
Activity
spring-projects-issues commentedon Aug 18, 2014
Eujung Kim commented
I agreed to reporter's opinion, so I improved MockHttpServletRequest.java.
And I submitted pull request.
#626
Thanks.
spring-projects-issues commentedon Aug 18, 2014
Sam Brannen commented
Completed as described in the comments for GitHub commit 110be33:
spring-projects-issues commentedon Aug 18, 2014
Hendy Irawan commented
Thank you Eujung, Sam :)
Retain brackets for IPV6 address in MockHttpServletRequest
Retain brackets for IPV6 address in MockHttpServletRequest
Retain brackets for IPV6 address in MockHttpServletRequest
Retain brackets for IPV6 address in MockHttpServletRequest
Retain brackets for IPV6 address in MockHttpServletRequest