Re: [patch] ServletUnitHttpRequest.getServerName .getServerPort

"Wolfgang Fahl" <[email protected]> Thu, 20 Aug 2009 14:04:58 +0200
Newsgroups gmane.comp.web.httpunit.devel
Organization BITPlan GmbH
Message-ID <[email protected]>
Dear Antoine,

thank you for your patch.
Sorry for the delay in responding.
http://httpunit.svn.sourceforge.net/viewvc/httpunit?view=rev&revision=1047

has it and it will make it to the next release.

Keep up the good work! This was a very good example on 
how a patch should be supplied:
### 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() {



Yours
  Wolfgang
> 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
> 



BITPlan - smart solutions
Pater-Delp-Str. 1, D-47877 Willich Schiefbahn
Tel. +49 2154 811-480, Fax +49 2154 811-481
Web: http://www.bitplan.de
bitplan GmbH, Willich - HRB 6820 Krefeld, VAT-ID: 10258040548, 
Geschäftsführer: Wolfgang Fahl

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july

_______________________________________________
Httpunit-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/httpunit-develop