[Httpunit-commit] CVS: httpunit/test/com/meterware/httpunit PseudoServer.java,1.17,1.18 PseudoServer
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-03-06 04:59:55
|
Update of /cvsroot/httpunit/httpunit/test/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv9204/test/com/meterware/httpunit Modified Files: PseudoServer.java PseudoServerTest.java PseudoServlet.java WebClientTest.java Log Message: Added support for gzip-encoded responses Index: PseudoServer.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServer.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- PseudoServer.java 28 Jan 2002 05:06:37 -0000 1.17 +++ PseudoServer.java 6 Mar 2002 04:59:52 -0000 1.18 @@ -224,7 +224,7 @@ } - private WebResource getResource( PseudoServlet servlet, HttpRequestStream request ) { + private WebResource getResource( PseudoServlet servlet, HttpRequestStream request ) throws IOException { servlet.init( request ); return servlet.getResponse( request.getCommand() ); } Index: PseudoServerTest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServerTest.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- PseudoServerTest.java 6 Mar 2002 03:45:57 -0000 1.23 +++ PseudoServerTest.java 6 Mar 2002 04:59:52 -0000 1.24 @@ -4,12 +4,12 @@ * * Copyright (c) 2000-2002, 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 +* 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and * to permit persons to whom the Software is furnished to do so, subject to the following conditions: * -* The above copyright notice and this permission notice shall be included in all copies or substantial portions +* The above copyright notice and this permission notice shall be included in all copies or substantial portions * of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO @@ -34,8 +34,8 @@ public static void main(String args[]) { junit.textui.TestRunner.run( suite() ); } - - + + public static Test suite() { return new TestSuite( PseudoServerTest.class ); } @@ -164,60 +164,6 @@ } - public void testRedirect() throws Exception { - String resourceName = "something/redirected"; - String resourceValue = "the desired content"; - - String redirectName = "anOldOne"; - - defineResource( resourceName, resourceValue ); - defineResource( redirectName, "ignored content", HttpURLConnection.HTTP_MOVED_PERM ); - addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName ); - - WebConversation wc = new WebConversation(); - WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName ); - assertEquals( "requested resource", resourceValue, response.getText().trim() ); - assertEquals( "content type", "text/html", response.getContentType() ); - } - - - public void testDuplicateHeaderRedirect() throws Exception { - String resourceName = "something/redirected"; - String resourceValue = "the desired content"; - - String redirectName = "anOldOne"; - - defineResource( resourceName, resourceValue ); - defineResource( redirectName, "ignored content", HttpURLConnection.HTTP_MOVED_PERM ); - addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName ); - addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName ); - - WebConversation wc = new WebConversation(); - WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName ); - assertEquals( "requested resource", resourceValue, response.getText().trim() ); - assertEquals( "content type", "text/html", response.getContentType() ); - } - - - public void testDisabledRedirect() throws Exception { - String resourceName = "something/redirected"; - String resourceValue = "the desired content"; - - String redirectName = "anOldOne"; - String redirectValue = "old content"; - - defineResource( resourceName, resourceValue ); - defineResource( redirectName, redirectValue, HttpURLConnection.HTTP_MOVED_PERM ); - addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName ); - - HttpUnitOptions.setAutoRedirect( false ); - WebConversation wc = new WebConversation(); - WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName ); - assertEquals( "requested resource", redirectValue, response.getText().trim() ); - assertEquals( "content type", "text/html", response.getContentType() ); - } - - public void testCookies() throws Exception { String resourceName = "something/baking"; String resourceValue = "the desired content"; @@ -281,7 +227,7 @@ String resourceName = "tellMe"; String name = "Charlie"; final String prefix = "Hello there, "; - String expectedResponse = prefix + name; + String expectedResponse = prefix + name; PseudoServer ps = new PseudoServer(); int port = ps.getConnectedPort(); @@ -292,7 +238,7 @@ return new WebResource( prefix + getParameter( "name" )[0], "text/plain" ); } } ); - + WebConversation wc = new WebConversation(); WebRequest request = new PostMethodWebRequest( "http://localhost:" + port + '/' + resourceName ); request.setParameter( "name", name ); @@ -309,14 +255,14 @@ 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>" ); @@ -327,7 +273,7 @@ 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() ); @@ -336,7 +282,7 @@ 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() ); Index: PseudoServlet.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServlet.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- PseudoServlet.java 9 Nov 2001 18:35:14 -0000 1.6 +++ PseudoServlet.java 6 Mar 2002 04:59:52 -0000 1.7 @@ -20,6 +20,7 @@ * *******************************************************************************************************************/ import java.io.Reader; +import java.io.IOException; /** * A basic simulated servlet for testing the HttpUnit library. @@ -34,7 +35,7 @@ /** * Returns a resource object as a result of a get request. **/ - public WebResource getResponse( String methodType ) { + public WebResource getResponse( String methodType ) throws IOException { if (methodType.equalsIgnoreCase( "GET" )) { return getGetResponse(); } else if (methodType.equalsIgnoreCase( "PUT" )) { @@ -50,7 +51,7 @@ /** * Returns a resource object as a result of a get request. **/ - public WebResource getGetResponse() { + public WebResource getGetResponse() throws IOException { throw new RuntimeException( "get not implemented" ); } @@ -58,7 +59,7 @@ /* * Returns a resource object as a result of a post request. **/ - public WebResource getPostResponse() { + public WebResource getPostResponse() throws IOException { throw new RuntimeException( "post not implemented" ); } @@ -66,7 +67,7 @@ /* * Returns a resource object as a result of a put request. **/ - public WebResource getPutResponse() { + public WebResource getPutResponse() throws IOException { throw new RuntimeException( "put not implemented" ); } Index: WebClientTest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/WebClientTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- WebClientTest.java 28 Feb 2002 14:30:34 -0000 1.1 +++ WebClientTest.java 6 Mar 2002 04:59:52 -0000 1.2 @@ -20,13 +20,17 @@ * *******************************************************************************************************************/ -import java.util.List; -import java.util.ArrayList; -import java.net.MalformedURLException; -import java.io.IOException; - import junit.framework.TestSuite; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.GZIPOutputStream; + /** * @@ -34,7 +38,7 @@ **/ public class WebClientTest extends HttpUnitTest { - public static void main(String args[]) { + public static void main( String args[] ) { junit.textui.TestRunner.run( suite() ); } @@ -49,17 +53,58 @@ } + public void testGZIPHandling() throws Exception { + String expectedResponse = "Here is my answer"; + defineResource( "Compressed.html", new CompressedPseudoServlet( expectedResponse ) ); + + WebConversation wc = new WebConversation(); + WebResponse wr = wc.getResponse( getHostPath() + "/Compressed.html" ); + assertEquals( "Content-Encoding header", "gzip", wr.getHeaderField( "Content-encoding" ) ); + assertEquals( "Content-Type", "text/plain", wr.getContentType() ); + assertEquals( "Content", expectedResponse, wr.getText().trim() ); + } + + + private class CompressedPseudoServlet extends PseudoServlet { + + private String _responseText; + + + public CompressedPseudoServlet( String responseText ) { + _responseText = responseText; + } + + + public WebResource getGetResponse() throws IOException { + WebResource result = new WebResource( getCompressedContents(), "text/plain" ); + result.addHeader( "Content-Encoding: gzip" ); + return result; + } + + + private byte[] getCompressedContents() throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + GZIPOutputStream gzip = new GZIPOutputStream( baos ); + OutputStreamWriter out = new OutputStreamWriter( gzip ); + out.write( _responseText ); + out.flush(); + out.close(); + return baos.toByteArray(); + } + } + + public void testClientListener() throws Exception { - defineWebPage( "Target", "This is another page with <a href=Form.html target='_top'>one link</a>" ); - defineWebPage( "Form", "This is a page with a simple form: " + - "<form action=submit><input name=name><input type=submit></form>" + - "<a href=Target.html target=red>a link</a>"); + defineWebPage( "Target", "This is another page with <a href=Form.html target='_top'>one link</a>" ); + defineWebPage( "Form", "This is a page with a simple form: " + + "<form action=submit><input name=name><input type=submit></form>" + + "<a href=Target.html target=red>a link</a>" ); defineResource( "Frames.html", - "<HTML><HEAD><TITLE>Initial</TITLE></HEAD>" + - "<FRAMESET cols='20%,80%'>" + - " <FRAME src='Target.html' name='red'>" + - " <FRAME src=Form.html name=blue>" + - "</FRAMESET></HTML>" ); + "<HTML><HEAD><TITLE>Initial</TITLE></HEAD>" + + "<FRAMESET cols='20%,80%'>" + + " <FRAME src='Target.html' name='red'>" + + " <FRAME src=Form.html name=blue>" + + "</FRAMESET></HTML>" ); WebConversation wc = new WebConversation(); ArrayList messageLog = new ArrayList(); @@ -67,19 +112,19 @@ WebResponse response = wc.getResponse( getHostPath() + "/Frames.html" ); assertEquals( "Num logged items", 6, messageLog.size() ); - for (int i=0; i <3; i++) { - verifyRequestResponsePair( messageLog, 2*i ); + for (int i = 0; i < 3; i++) { + verifyRequestResponsePair( messageLog, 2 * i ); } } private void verifyRequestResponsePair( ArrayList messageLog, int i ) throws MalformedURLException { - assertTrue( "Logged item " + i + " is not a web request, but " + messageLog.get(i).getClass(), - messageLog.get(i) instanceof WebRequest ); - assertTrue( "Logged item " + (i+1) + " is not a web response, but " + messageLog.get(i+1).getClass(), - messageLog.get(i+1) instanceof WebResponse ); - assertEquals( "Response target", ((WebRequest) messageLog.get(i)).getTarget(), ((WebResponse) messageLog.get(i+1)).getTarget() ); - assertEquals( "Response URL", ((WebRequest) messageLog.get(i)).getURL(), ((WebResponse) messageLog.get(i+1)).getURL() ); + assertTrue( "Logged item " + i + " is not a web request, but " + messageLog.get( i ).getClass(), + messageLog.get( i ) instanceof WebRequest ); + assertTrue( "Logged item " + (i + 1) + " is not a web response, but " + messageLog.get( i + 1 ).getClass(), + messageLog.get( i + 1 ) instanceof WebResponse ); + assertEquals( "Response target", ((WebRequest) messageLog.get( i )).getTarget(), ((WebResponse) messageLog.get( i + 1 )).getTarget() ); + assertEquals( "Response URL", ((WebRequest) messageLog.get( i )).getURL(), ((WebResponse) messageLog.get( i + 1 )).getURL() ); } @@ -100,7 +145,61 @@ public void responseReceived( WebClient src, WebResponse resp ) { _messageLog.add( resp ); - } + } + } + + + public void testRedirect() throws Exception { + String resourceName = "something/redirected"; + String resourceValue = "the desired content"; + + String redirectName = "anOldOne"; + + defineResource( resourceName, resourceValue ); + defineResource( redirectName, "ignored content", HttpURLConnection.HTTP_MOVED_PERM ); + addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName ); + + WebConversation wc = new WebConversation(); + WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName ); + assertEquals( "requested resource", resourceValue, response.getText().trim() ); + assertEquals( "content type", "text/html", response.getContentType() ); + } + + + public void testDuplicateHeaderRedirect() throws Exception { + String resourceName = "something/redirected"; + String resourceValue = "the desired content"; + + String redirectName = "anOldOne"; + + defineResource( resourceName, resourceValue ); + defineResource( redirectName, "ignored content", HttpURLConnection.HTTP_MOVED_PERM ); + addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName ); + addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName ); + + WebConversation wc = new WebConversation(); + WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName ); + assertEquals( "requested resource", resourceValue, response.getText().trim() ); + assertEquals( "content type", "text/html", response.getContentType() ); + } + + + public void testDisabledRedirect() throws Exception { + String resourceName = "something/redirected"; + String resourceValue = "the desired content"; + + String redirectName = "anOldOne"; + String redirectValue = "old content"; + + defineResource( resourceName, resourceValue ); + defineResource( redirectName, redirectValue, HttpURLConnection.HTTP_MOVED_PERM ); + addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName ); + + HttpUnitOptions.setAutoRedirect( false ); + WebConversation wc = new WebConversation(); + WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName ); + assertEquals( "requested resource", redirectValue, response.getText().trim() ); + assertEquals( "content type", "text/html", response.getContentType() ); } |