[Httpunit-commit] CVS: httpunit/src/com/meterware/pseudoserver HttpUserAgentTest.java,1.3,1.4 Pseudo
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-11-15 06:51:48
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/pseudoserver
In directory usw-pr-cvs1:/tmp/cvs-serv27410/src/com/meterware/pseudoserver
Modified Files:
HttpUserAgentTest.java PseudoServer.java WebResource.java
Log Message:
support applet load, find, show-document
Index: HttpUserAgentTest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/pseudoserver/HttpUserAgentTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- HttpUserAgentTest.java 22 Oct 2002 15:12:54 -0000 1.3
+++ HttpUserAgentTest.java 15 Nov 2002 00:07:33 -0000 1.4
@@ -89,6 +89,11 @@
}
+ protected void mapToClasspath( String directory ) {
+ _server.mapToClasspath( directory );
+ }
+
+
protected String getHostPath() {
return _hostPath;
}
@@ -189,6 +194,7 @@
}
return sb.toString();
}
+
static class QuerySpec {
QuerySpec( String urlString ) {
Index: PseudoServer.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/pseudoserver/PseudoServer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- PseudoServer.java 13 Nov 2002 14:01:23 -0000 1.3
+++ PseudoServer.java 15 Nov 2002 00:07:33 -0000 1.4
@@ -19,26 +19,21 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import java.io.InterruptedIOException;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.Socket;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Vector;
+import java.util.*;
/**
* A basic simulated web-server for testing user agents without a web server.
**/
public class PseudoServer {
+ private ArrayList _classpathDirs = new ArrayList();
+
public PseudoServer() {
Thread t = new Thread() {
@@ -157,6 +152,11 @@
}
+ public void mapToClasspath( String directory ) {
+ _classpathDirs.add( directory );
+ }
+
+
//------------------------------------- private members ---------------------------------------
private Hashtable _resources = new Hashtable();
@@ -212,7 +212,7 @@
for (int i = 0; i < headers.length; i++) {
response.addHeader( headers[i] );
}
- response.write( resource.getContents() );
+ response.write( resource );
}
} catch (UnknownMethodException e) {
response.setResponse( HttpURLConnection.HTTP_BAD_METHOD, "unsupported method: " + e.getMethod() );
@@ -237,6 +237,13 @@
} else if (resource instanceof PseudoServlet) {
return getResource( (PseudoServlet) resource, request );
} else {
+ for (Iterator iterator = _classpathDirs.iterator(); iterator.hasNext();) {
+ String directory = (String) iterator.next();
+ if (request.getURI().startsWith( directory )) {
+ String resourceName = request.getURI().substring( directory.length()+1 );
+ return new WebResource( getClass().getClassLoader().getResourceAsStream( resourceName ), "application/class", 200 );
+ }
+ }
return null;
}
}
@@ -303,9 +310,9 @@
}
- void write( byte[] contents ) throws IOException {
+ void write( WebResource resource ) throws IOException {
flushHeaders();
- _stream.write( contents, 0, contents.length );
+ resource.writeTo( _stream );
}
@@ -315,7 +322,7 @@
}
- private void flushHeaders() throws IOException {
+ private void flushHeaders() {
if (!_headersWritten) {
sendResponse( _responseCode, _responseText );
for (Enumeration e = _headers.elements(); e.hasMoreElements();) {
@@ -328,18 +335,18 @@
}
- private void sendResponse( int responseCode, String responseText ) throws IOException {
+ private void sendResponse( int responseCode, String responseText ) {
sendLine( "HTTP/1.0 " + responseCode + ' ' + responseText );
}
- private void sendLine( String text ) throws IOException {
+ private void sendLine( String text ) {
sendText( text );
sendText( CRLF );
}
- private void sendText( String text ) throws IOException {
+ private void sendText( String text ) {
_pw.write( text );
}
Index: WebResource.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/pseudoserver/WebResource.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- WebResource.java 13 Nov 2002 14:01:23 -0000 1.2
+++ WebResource.java 15 Nov 2002 00:07:33 -0000 1.3
@@ -21,7 +21,9 @@
*******************************************************************************************************************/
import com.meterware.httpunit.HttpUnitUtils;
-import java.io.UnsupportedEncodingException;
+import java.io.OutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.Vector;
@@ -87,6 +89,13 @@
}
+ public WebResource( InputStream stream, String contentType, int responseCode ) {
+ _stream = stream;
+ _contentType = contentType;
+ _responseCode = responseCode;
+ }
+
+
String[] getHeaders() {
final Vector effectiveHeaders = (Vector) _headers.clone();
if (!_hasExplicitContentTypeHeader) effectiveHeaders.add( getContentTypeHeader() );
@@ -96,11 +105,18 @@
}
- byte[] getContents() throws UnsupportedEncodingException {
+ void writeTo( OutputStream outputStream ) throws IOException {
if (_string != null) {
- return _string.getBytes( getCharacterSet() );
- } else {
- return _contents;
+ outputStream.write( _string.getBytes( getCharacterSet() ) );
+ } else if (_contents != null) {
+ outputStream.write( _contents );
+ } else if (_stream != null) {
+ byte[] buffer = new byte[8 * 1024];
+ int count = 0;
+ do {
+ outputStream.write( buffer, 0, count );
+ count = _stream.read( buffer, 0, buffer.length );
+ } while (count != -1);
}
}
@@ -143,8 +159,9 @@
}
- private byte[] _contents;
- private String _string;
+ private byte[] _contents;
+ private String _string;
+ private InputStream _stream;
private int _responseCode;
private boolean _sendCharacterSet;
|