[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit MessageBodyWebRequest.java,NONE,1.1 PutMe
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2001-06-04 23:00:09
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv26791/src/com/meterware/httpunit Modified Files: MessageBody.java MimeEncodedMessageBody.java PostMethodWebRequest.java WebRequest.java Added Files: MessageBodyWebRequest.java PutMethodWebRequest.java Log Message: Added PUT and generic (non-form) POST methods ***** Error reading new file[Errno 2] No such file or directory: 'MessageBodyWebRequest.java' --- NEW FILE --- package com.meterware.httpunit; /******************************************************************************************************************** * Copyright (c) 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 * 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 * of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * *******************************************************************************************************************/ import java.io.InputStream; /** * A web request using the PUT protocol. * * The objectives of this class are to suport an HTTP PUT petition * so we can test this HTTP requests. * * <B>Documentation</B> See the HTTP 1.1 [<a href="http://www.w3.org/Protocols/HTTP/">spec</a>] * * @author Tom Watkins * @author Deepa Dihr * @author Marcos Tarruella * @author Russell Gold * **/ public class PutMethodWebRequest extends MessageBodyWebRequest { public PutMethodWebRequest( String url, InputStream source ) { super( url ); _source = source; } /** * Returns a message body based on the input stream. **/ protected MessageBody newMessageBody() { return new InputStreamMessageBody( this, _source ); } /** * Returns 'PUT' to indicate the method. **/ public String getMethod() { return "PUT"; } private InputStream _source; } Index: MessageBody.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/MessageBody.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MessageBody.java 2001/01/05 14:00:36 1.1 +++ MessageBody.java 2001/06/04 20:45:33 1.2 @@ -26,13 +26,13 @@ /** - * An abstract class representing the body of a POST method request. + * An abstract class representing the body of a web request. **/ abstract class MessageBody { - MessageBody( PostMethodWebRequest request ) { + MessageBody( MessageBodyWebRequest request ) { _request = request; } @@ -51,11 +51,11 @@ void writeTo( OutputStream outputStream ) throws IOException; - protected PostMethodWebRequest getRequest() { + protected MessageBodyWebRequest getRequest() { return _request; } - private PostMethodWebRequest _request; + private MessageBodyWebRequest _request; } Index: MimeEncodedMessageBody.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/MimeEncodedMessageBody.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- MimeEncodedMessageBody.java 2001/05/16 21:05:30 1.5 +++ MimeEncodedMessageBody.java 2001/06/04 20:45:33 1.6 @@ -49,6 +49,14 @@ /** + * Returns the request associated with this message body, cast to a POST request. + **/ + PostMethodWebRequest getPostRequest() { + return (PostMethodWebRequest) getRequest(); + } + + + /** * Transmits the body of this request as a sequence of bytes. **/ void writeTo( OutputStream outputStream ) throws IOException { @@ -64,7 +72,7 @@ } } - Dictionary files = getRequest().getSelectedFiles(); + Dictionary files = getPostRequest().getSelectedFiles(); for (Enumeration e = files.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); WebRequest.UploadFileSpec spec = (WebRequest.UploadFileSpec) files.get( name ); Index: PostMethodWebRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/PostMethodWebRequest.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- PostMethodWebRequest.java 2001/05/16 21:05:30 1.12 +++ PostMethodWebRequest.java 2001/06/04 20:45:33 1.13 @@ -20,6 +20,7 @@ * *******************************************************************************************************************/ import java.io.File; +import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; @@ -36,7 +37,7 @@ /** * An HTTP request using the POST method. **/ -public class PostMethodWebRequest extends WebRequest { +public class PostMethodWebRequest extends MessageBodyWebRequest { /** @@ -48,6 +49,15 @@ /** + * Constructs a web request using a specific absolute url string and input stream. + **/ + public PostMethodWebRequest( String urlString, InputStream source ) { + super( urlString ); + _source = source; + } + + + /** * Constructs a web request with a specific target. **/ public PostMethodWebRequest( URL urlBase, String urlString, String target ) { @@ -83,18 +93,17 @@ } -//---------------------------------- WebRequest methods -------------------------------- +//----------------------------- MessageBodyWebRequest methods --------------------------- - protected void completeRequest( URLConnection connection ) throws IOException { - MessageBody mb = newMessageBody(); - mb.updateHeaders( connection ); - connection.setDoInput( true ); - connection.setDoOutput( true ); - OutputStream stream = connection.getOutputStream(); - mb.writeTo( stream ); - stream.flush(); - stream.close(); + protected MessageBody newMessageBody() { + if (_source != null) { + return new InputStreamMessageBody( this, _source ); + } else if (isMimeEncoded()) { + return new MimeEncodedMessageBody( this ); + } else { + return new URLEncodedMessageBody( this ); + } } @@ -121,17 +130,12 @@ //---------------------------------- private members ------------------------------------- private Hashtable _files = new Hashtable(); + private InputStream _source; - private MessageBody newMessageBody() { - if (isMimeEncoded()) { - return new MimeEncodedMessageBody( this ); - } else { - return new URLEncodedMessageBody( this ); - } - } - } + + //============================= class URLEncodedMessageBody ====================================== Index: WebRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequest.java,v retrieving revision 1.20 retrieving revision 1.21 diff -u -r1.20 -r1.21 --- WebRequest.java 2001/05/18 21:02:01 1.20 +++ WebRequest.java 2001/06/04 20:45:33 1.21 @@ -436,7 +436,7 @@ if (_sourceForm.isTextParameter( name )) return; if (_sourceForm.isFileParameter( name )) throw new IllegalFileParameterException( name ); if (!inArray( name, _sourceForm.getParameterNames() )) throw new NoSuchParameterException( name ); - if (!inArray( value, _sourceForm.getOptionValues( name ) )) throw new IllegalParameterValueException( name, value ); + if (!inArray( value, _sourceForm.getOptionValues( name ) )) throw new IllegalParameterValueException( name, value, _sourceForm.getOptionValues( name ) ); } @@ -561,17 +561,29 @@ class IllegalParameterValueException extends IllegalRequestParameterException { - IllegalParameterValueException( String parameterName, String badValue ) { + IllegalParameterValueException( String parameterName, String badValue, String[] allowed ) { _parameterName = parameterName; _badValue = badValue; + _allowedValues = allowed; } + public String getMessage() { - return "May not set parameter '" + _parameterName + "' to '" + _badValue + "'"; + StringBuffer sb = new StringBuffer(); + sb.append( "May not set parameter '" ).append( _parameterName ).append( "' to '" ); + sb.append( _badValue ).append( "'. Value must be one of: { " ); + for (int i = 0; i < _allowedValues.length; i++) { + if (i != 0) sb.append( ", " ); + sb.append( _allowedValues[i] ); + } + sb.append( " }" ); + return sb.toString(); } - private String _parameterName; - private String _badValue; + + private String _parameterName; + private String _badValue; + private String[] _allowedValues; } |