[patch] ServletUnitHttpRequest.getServerName .getServerPort
Antoine Vernois <[email protected]> Thu, 11 Jun 2009 13:37:26 +0200
| Newsgroups | gmane.comp.web.httpunit.devel |
|---|---|
| Message-ID | <[email protected]> |
Hello, here is a small patch against rev1022 of HEAD. It implements getServerName() and getServerPort of ServletUnitHttpRequest. ServerName and ServerPort are extracted from request's URL instead of always returning localhost and 0. patch.text in attachment. -- antoine ------------------------------------------------------------------------------ Crystal Reports - New Free Runtime and 30 Day Trial Check out the new simplified licensing option that enables unlimited royalty-free distribution of the report engine for externally facing server and web deployment. http://p.sf.net/sfu/businessobjects _______________________________________________ Httpunit-develop mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/httpunit-develop
patch.txt
(text/plain, 3.2 KB)
### Eclipse Workspace Patch 1.0
#P httpunit
Index: src/com/meterware/servletunit/ServletUnitHttpRequest.java
===================================================================
--- src/com/meterware/servletunit/ServletUnitHttpRequest.java (revision 1022)
+++ src/com/meterware/servletunit/ServletUnitHttpRequest.java (working copy)
@@ -56,6 +56,8 @@
private boolean _gotReader;
private boolean _gotInputStream;
private BufferedReader _reader;
+ private int _serverPort;
+ private String _serverName;
@@ -75,6 +77,11 @@
_messageBody = messageBody;
_protocol=request.getURL().getProtocol().toLowerCase();
_secure = _protocol.endsWith("s" );
+ _serverName = request.getURL().getHost();
+ _serverPort = request.getURL().getPort();
+ if ( _serverPort == -1 ) {
+ _serverPort = request.getURL().getDefaultPort();
+ }
_requestContext = new RequestContext( request.getURL() );
String contentTypeHeader = (String) _headers.get( "Content-Type" );
@@ -472,7 +479,7 @@
* Returns the host name of the server that received the request.
**/
public String getServerName() {
- return "localhost";
+ return _serverName;
}
@@ -480,7 +487,7 @@
* Returns the port number on which this request was received.
**/
public int getServerPort() {
- return 0;
+ return _serverPort;
}
Index: test/com/meterware/servletunit/HttpServletRequestTest.java
===================================================================
--- test/com/meterware/servletunit/HttpServletRequestTest.java (revision 1022)
+++ test/com/meterware/servletunit/HttpServletRequestTest.java (working copy)
@@ -771,7 +771,27 @@
assertEquals( "param2 value", hebrewValue, request.getParameter( "param2") );
}
+ public void testDefaultHttpServerPort() throws Exception {
+ WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" );
+ HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY );
+ int serverPort = request.getServerPort();
+ assertEquals( "default http server port", serverPort, 80 );
+ }
+
+ public void testSuppliedHttpServerPort() throws Exception {
+ WebRequest wr = new GetMethodWebRequest( "http://localhost:8080/simple" );
+ HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY );
+ int serverPort = request.getServerPort();
+ assertEquals( "supplied http server port", serverPort, 8080 );
+ }
+ public void testServerName() throws Exception {
+ WebRequest wr = new GetMethodWebRequest( "http://myhost:8080/simple" );
+ HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY );
+ String serverName = request.getServerName();
+ assertEquals( "server name", serverName, "myhost" );
+ }
+
private final static byte[] NO_MESSAGE_BODY = new byte[0];
private final static ServletMetaData NULL_SERVLET_REQUEST = new ServletMetaData() {