[Httpunit-commit] CVS: httpunit/test/com/meterware/httpunit HttpUnitSuite.java,1.13,1.14 PseudoServe
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2001-06-04 23:42:42
|
Update of /cvsroot/httpunit/httpunit/test/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv26791/test/com/meterware/httpunit Modified Files: HttpUnitSuite.java PseudoServer.java PseudoServerTest.java PseudoServlet.java Log Message: Added PUT and generic (non-form) POST methods Index: HttpUnitSuite.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/HttpUnitSuite.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- HttpUnitSuite.java 2001/04/03 16:49:08 1.13 +++ HttpUnitSuite.java 2001/06/04 20:45:33 1.14 @@ -46,6 +46,7 @@ suite.addTest( FormSubmitTest.suite() ); suite.addTest( Base64Test.suite() ); suite.addTest( PseudoServerTest.suite() ); + suite.addTest( MessageBodyRequestTest.suite() ); addOptionalTestCase( suite, "com.meterware.httpunit.XMLPageTest" ); addOptionalTestCase( suite, "com.meterware.httpunit.FileUploadTest" ); return suite; Index: PseudoServer.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServer.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- PseudoServer.java 2001/05/16 21:03:00 1.12 +++ PseudoServer.java 2001/06/04 20:45:33 1.13 @@ -48,7 +48,7 @@ _active = false; } } - try { + try { if (_serverSocket != null) _serverSocket.close(); _serverSocket = null; } catch (IOException e) { @@ -176,7 +176,7 @@ String uri = st.nextToken(); String protocol = st.nextToken(); - if (!command.equals( "GET" ) && !command.equals( "POST" )) { + if (!command.equals( "GET" ) && !command.equals( "POST" ) && !command.equals( "PUT" )) { sendResponse( pw, HttpURLConnection.HTTP_BAD_METHOD, "unsupported method: " + command ); } else { try { @@ -186,8 +186,8 @@ } else if (resource.getResponseCode() != HttpURLConnection.HTTP_OK) { sendResponse( pw, resource.getResponseCode(), resource.getContents() ); sendLine( pw, "" ); - pw.flush(); - } else { + pw.flush(); + } else { sendResponse( pw, HttpURLConnection.HTTP_OK, "OK" ); sendLine( pw, "Content-type: " + resource.getContentType() + resource.getCharacterSetParameter() ); String[] headers = resource.getHeaders(); @@ -205,8 +205,8 @@ socket.close(); throw e; } catch (Throwable t) { - System.out.println( "Internal error: " + t ); - t.printStackTrace(); + System.out.println( "Internal error: " + t ); + t.printStackTrace(); sendResponse( pw, HttpURLConnection.HTTP_INTERNAL_ERROR, t.toString() ); } } @@ -220,12 +220,9 @@ Object resource = _resources.get( uri ); if (command.equals( "GET" ) && resource instanceof WebResource) { return (WebResource) resource; - } else if (command.equals( "POST" ) && resource instanceof PseudoServlet) { + } else if (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 ); + return ((PseudoServlet) resource).getResponse( command, getParameters( (String) requestData.get( PseudoServlet.CONTENTS ) ), requestData ); } else { return null; } Index: PseudoServerTest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/test/com/meterware/httpunit/PseudoServerTest.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- PseudoServerTest.java 2001/05/31 16:44:15 1.11 +++ PseudoServerTest.java 2001/06/04 20:45:33 1.12 @@ -232,17 +232,17 @@ public void testRefererHeader() throws Exception { String resourceName = "tellMe"; String linkSource = "fromLink"; - String formSource = "fromForm"; + 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; + 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>" ); + ps.setResource( formSource, "<html><body><form action=\"tellMe\"><input type=submit></form></body></html>" ); try { ps.setResource( resourceName, new PseudoServlet() { @@ -257,13 +257,13 @@ 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() ); + 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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- PseudoServlet.java 2001/03/23 20:50:41 1.3 +++ PseudoServlet.java 2001/06/04 20:45:33 1.4 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * -* Copyright (c) 2000, Russell Gold +* Copyright (c) 2000-2001, 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 @@ -37,6 +37,25 @@ * @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 getResponse( String methodType, Dictionary parameters, Dictionary headers ) { + if (methodType.equalsIgnoreCase( "GET" )) { + return getGetResponse( parameters, headers ); + } else if (methodType.equalsIgnoreCase( "PUT" )) { + return getPutResponse( parameters, headers ); + } else if (methodType.equalsIgnoreCase( "POST" )) { + return getPostResponse( parameters, headers ); + } else { + throw new RuntimeException( methodType + " not implemented" ); + } + } + + + /** + * 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" ); } @@ -52,6 +71,16 @@ throw new RuntimeException( "post not implemented" ); } + + /* + * Returns a resource object as a result of a put 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 getPutResponse( Dictionary parameters, Dictionary headers ) { + throw new RuntimeException( "put not implemented" ); + } } |