[Httpunit-commit] SF.net SVN: httpunit:[1047] trunk/httpunit
Brought to you by:
russgold
|
From: <wol...@us...> - 2009-08-20 12:02:34
|
Revision: 1047
http://httpunit.svn.sourceforge.net/httpunit/?rev=1047&view=rev
Author: wolfgang_fahl
Date: 2009-08-20 12:02:21 +0000 (Thu, 20 Aug 2009)
Log Message:
-----------
Patch by Antoine Vernois: it implements getServerName() and getServerPort of ServletUnitHttpRequest. ServerName and ServerPort are extracted from request's URL instead of always returning localhost and 0.
Modified Paths:
--------------
trunk/httpunit/src/com/meterware/servletunit/ServletUnitHttpRequest.java
trunk/httpunit/test/com/meterware/servletunit/HttpServletRequestTest.java
Modified: trunk/httpunit/src/com/meterware/servletunit/ServletUnitHttpRequest.java
===================================================================
--- trunk/httpunit/src/com/meterware/servletunit/ServletUnitHttpRequest.java 2009-08-20 11:31:40 UTC (rev 1046)
+++ trunk/httpunit/src/com/meterware/servletunit/ServletUnitHttpRequest.java 2009-08-20 12:02:21 UTC (rev 1047)
@@ -56,7 +56,8 @@
private boolean _gotReader;
private boolean _gotInputStream;
private BufferedReader _reader;
-
+ private int _serverPort;
+ private String _serverName;
/**
@@ -75,6 +76,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 +478,7 @@
* Returns the host name of the server that received the request.
**/
public String getServerName() {
- return "localhost";
+ return _serverName;
}
@@ -480,7 +486,7 @@
* Returns the port number on which this request was received.
**/
public int getServerPort() {
- return 0;
+ return _serverPort;
}
Modified: trunk/httpunit/test/com/meterware/servletunit/HttpServletRequestTest.java
===================================================================
--- trunk/httpunit/test/com/meterware/servletunit/HttpServletRequestTest.java 2009-08-20 11:31:40 UTC (rev 1046)
+++ trunk/httpunit/test/com/meterware/servletunit/HttpServletRequestTest.java 2009-08-20 12:02:21 UTC (rev 1047)
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000-2008 by Russell Gold
+* Copyright (c) 2000-2009 by Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -714,10 +714,9 @@
/**
* test the reader with a Specific Character set (here UTF-8)
* @throws Exception
- * TODO make work an switch back on
+ * FIXME make work an switch back on
*/
public void xtestGetReaderSpecificCharset() throws Exception {
- //String body = "東京";
String body = "\u05d0\u05d1\u05d2\u05d3";
InputStream stream = new ByteArrayInputStream( body.getBytes( "UTF-8" ) );
WebRequest wr = new PostMethodWebRequest( "http://localhost/simple", stream, "text/plain; charset=UTF-8" );
@@ -771,7 +770,40 @@
assertEquals( "param2 value", hebrewValue, request.getParameter( "param2") );
}
+ /**
+ * test for getServerPort and getServerName by Antoine Vernois
+ * @throws Exception
+ */
+ 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 );
+ }
+ /**
+ * test for getServerPort and getServerName by Antoine Vernois
+ * @throws Exception
+ */
+ 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 );
+ }
+
+ /**
+ * test for getServerPort and getServerName by Antoine Vernois
+ * @throws Exception
+ */
+ 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() {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|