[Httpunit-commit] CVS: httpunit/test/com/meterware/httpunit PseudoServer.java,1.9,1.10 PseudoServerT
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2001-03-23 20:50:44
|
Update of /cvsroot/httpunit/httpunit/test/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv8865/test/com/meterware/httpunit
Modified Files:
PseudoServer.java PseudoServerTest.java PseudoServlet.java
Log Message:
Added referer header support
Index: PseudoServer.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- PseudoServer.java 2001/03/20 23:35:43 1.9
+++ PseudoServer.java 2001/03/23 20:50:41 1.10
@@ -203,6 +203,8 @@
socket.close();
throw e;
} catch (Throwable t) {
+ System.out.println( "Internal error: " + t );
+ t.printStackTrace();
sendResponse( pw, HttpURLConnection.HTTP_INTERNAL_ERROR, t.toString() );
}
}
@@ -219,6 +221,9 @@
} else if (command.equals( "POST" ) && resource instanceof PseudoServlet) {
Dictionary requestData = readRequest( br );
return ((PseudoServlet) resource).getPostResponse( getParameters( (String) requestData.get( PseudoServlet.CONTENTS ) ), requestData );
+ } else if (command.equals( "GET" ) && resource instanceof PseudoServlet) {
+ Dictionary requestData = readRequest( br );
+ return ((PseudoServlet) resource).getGetResponse( getParameters( (String) requestData.get( PseudoServlet.CONTENTS ) ), requestData );
} else {
return null;
}
@@ -258,8 +263,10 @@
}
private Dictionary getParameters( String content ) throws IOException {
- StringTokenizer st = new StringTokenizer( content, "&=" );
Hashtable parameters = new Hashtable();
+ if (content == null || content.trim().length() == 0) return parameters;
+
+ StringTokenizer st = new StringTokenizer( content, "&=" );
while (st.hasMoreTokens()) {
String name = st.nextToken();
if (st.hasMoreTokens()) {
Index: PseudoServerTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServerTest.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- PseudoServerTest.java 2001/03/23 19:13:27 1.9
+++ PseudoServerTest.java 2001/03/23 20:50:41 1.10
@@ -190,6 +190,7 @@
}
+
public void testPseudoServlet() throws Exception {
String resourceName = "tellMe";
String name = "Charlie";
@@ -212,6 +213,47 @@
WebResponse response = wc.getResponse( request );
assertEquals( "Content type", "text/plain", response.getContentType() );
assertEquals( "Response", expectedResponse, response.getText().trim() );
+ } finally {
+ ps.shutDown();
+ }
+ }
+
+
+ public void testRefererHeader() throws Exception {
+ String resourceName = "tellMe";
+ String linkSource = "fromLink";
+ String formSource = "fromForm";
+
+ PseudoServer ps = new PseudoServer();
+ int port = ps.getConnectedPort();
+
+ String page0 = "http://localhost:" + port + '/' + resourceName;
+ String page1 = "http://localhost:" + port + '/' + linkSource;
+ String page2 = "http://localhost:" + port + '/' + formSource;
+
+ ps.setResource( linkSource, "<html><head></head><body><a href=\"tellMe\">Go</a></body></html>" );
+ ps.setResource( formSource, "<html><body><form action=\"tellMe\"><input type=submit></form></body></html>" );
+
+ try {
+ ps.setResource( resourceName, new PseudoServlet() {
+ public WebResource getGetResponse( Dictionary parameters, Dictionary headers ) {
+ String referer = (String) headers.get( "REFERER" );
+ return new WebResource( referer == null ? "null" : referer, "text/plain" );
+ }
+ } );
+
+ WebConversation wc = new WebConversation();
+ WebResponse response = wc.getResponse( page0 );
+ assertEquals( "Content type", "text/plain", response.getContentType() );
+ assertEquals( "Default Referer header", "null", response.getText().trim() );
+
+ response = wc.getResponse( page1 );
+ response = wc.getResponse( response.getLinks()[0].getRequest() );
+ assertEquals( "Link Referer header", page1, response.getText().trim() );
+
+ response = wc.getResponse( page2 );
+ response = wc.getResponse( response.getForms()[0].getRequest() );
+ assertEquals( "Form Referer header", page2, response.getText().trim() );
} finally {
ps.shutDown();
}
Index: PseudoServlet.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServlet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- PseudoServlet.java 2001/01/05 14:00:36 1.2
+++ PseudoServlet.java 2001/03/23 20:50:41 1.3
@@ -32,13 +32,26 @@
/**
+ * Returns a resource object as a result of a get request.
+ * @param parameters a mapping of parameter names to arrays of value string.
+ * @param headers a mapping of header names to header contents. Also contains a special 'header' named CONTENTS
+ * which is the raw bytes of the request contents stored in a string.
+ **/
+ public WebResource getGetResponse( Dictionary parameters, Dictionary headers ) {
+ throw new RuntimeException( "get not implemented" );
+ }
+
+
+ /*
* Returns a resource object as a result of a post request.
* @param parameters a mapping of parameter names to arrays of value string.
* @param headers a mapping of header names to header contents. Also contains a special 'header' named CONTENTS
* which is the raw bytes of the request contents stored in a string.
**/
- abstract
- public WebResource getPostResponse( Dictionary parameters, Dictionary headers );
+ public WebResource getPostResponse( Dictionary parameters, Dictionary headers ) {
+ throw new RuntimeException( "post not implemented" );
+ }
+
}
|