Skip to content

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

Closed

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

Activity

  1. spring-projects-issues commented on Aug 18, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Eujung Kim commented

    I agreed to reporter's opinion, so I improved MockHttpServletRequest.java.
    And I submitted pull request.

    #626

    Thanks.

  2. spring-projects-issues commented on Aug 18, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Completed as described in the comments for GitHub commit 110be33:

    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.

  3. spring-projects-issues commented on Aug 18, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Hendy Irawan commented

    Thank you Eujung, Sam :)

  4. added
    in: test
    type: enhancement
    in: web
    on Jan 11, 2019
  5. added this to the 4.1 GA milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testin: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions