Reputation: 961
I am trying to test a servlet filter for a Tomcat application. To do so, I am using the MockHttpServletRequest provided by Spring.
I set it up like this:
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setRemoteHost("mycompany.com");
request.setRequestURI("/myapp.php");
request.setSecure(true);
but when I the following:
System.out.println(request.getRequestURL());
produces: http://localhost/myapp.php
. On the other hand, if I explicitly request one of the fields I set, like:
System.out.println(request.getRemoteHost());
I get: mycompany.com
What's going on here? How can I get getRequestURL
to produce what I'm really after: https://mycompany.com/myapp.php
Upvotes: 0
Views: 462
Reputation: 31197
Sotirios was correct regarding the serverName
vs. remoteHost
; however, that change only gets you partially there.
The following will achieve your goal:
MockHttpServletRequest request = new MockHttpServletRequest();
request.setScheme("https");
request.setServerName("mycompany.com");
request.setServerPort(443);
request.setRequestURI("/myapp.php");
System.out.println(request.getRequestURL());
// Prints: https://mycompany.com/myapp.php
Regards,
Sam
Upvotes: 2
Reputation: 279960
You're creating a MockHttpServletRequest
which represents a request which a servlet, running on a server, received.
The javadoc of MockHttpServletRequest#getRemoteHost()
(really of ServletRequest
) states
Returns the fully qualified name of the client or the last proxy that sent the request.
So what you're setting with setRemoteHost
is the hostname/ip of the client making the request, not the hostname of the server receiving the request.
You'll want MockHttpServletRequest#setServerName(String)
request.setServerName("mycompany.com");
Upvotes: 1