[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit PostMethodWebRequest.java,1.16,1.17 WebRe
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2001-10-19 18:26:32
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv7181/src/com/meterware/httpunit
Modified Files:
PostMethodWebRequest.java WebRequest.java
Log Message:
Allow file uploads in POST w/o form
Index: PostMethodWebRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/PostMethodWebRequest.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- PostMethodWebRequest.java 2001/08/21 19:56:22 1.16
+++ PostMethodWebRequest.java 2001/10/19 18:26:29 1.17
@@ -69,6 +69,18 @@
/**
+ * Selects whether MIME-encoding will be used for this request. MIME-encoding changes the way the request is sent
+ * and is required for requests which include file parameters. This method may only be called for a request which
+ * was not created from a form.
+ **/
+ public void setMimeEncoded( boolean mimeEncoded )
+ {
+ if (isFormBased()) throw new IllegalStateException( "Encoding is defined by the form from which this request is derived." );
+ _mimeEncoded = mimeEncoded;
+ }
+
+
+ /**
* Returns the HTTP method defined for this request.
**/
public String getMethod() {
@@ -106,6 +118,23 @@
}
+ /**
+ * Returns true if selectFile may be called with this parameter.
+ */
+ protected boolean maySelectFile( String parameterName )
+ {
+ return !isFormBased() || super.isFileParameter( parameterName );
+ }
+
+
+ /**
+ * Returns true if this request is to be MIME-encoded.
+ **/
+ protected boolean isMimeEncoded() {
+ return isFormBased() ? super.isMimeEncoded() : _mimeEncoded;
+ }
+
+
//----------------------------- MessageBodyWebRequest methods ---------------------------
@@ -146,6 +175,8 @@
private InputStream _source;
private MessageBody _body;
+ /** If true, non-form-based request will be MIME-encoded. **/
+ private boolean _mimeEncoded;
}
Index: WebRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequest.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- WebRequest.java 2001/10/16 16:21:25 1.24
+++ WebRequest.java 2001/10/19 18:26:29 1.25
@@ -64,9 +64,7 @@
* Sets the file for a parameter upload in a web request.
**/
public void selectFile( String parameterName, File file ) {
- if (_sourceForm == null || !_sourceForm.isFileParameter( parameterName )) {
- throw new IllegalNonFileParameterException( parameterName );
- }
+ assertFileParameter( parameterName );
if (!isMimeEncoded()) throw new MultipartFormRequiredException();
}
@@ -75,9 +73,7 @@
* Sets the file for a parameter upload in a web request.
**/
public void selectFile( String parameterName, File file, String contentType ) {
- if (_sourceForm == null || !_sourceForm.isFileParameter( parameterName )) {
- throw new IllegalNonFileParameterException( parameterName );
- }
+ assertFileParameter( parameterName );
if (!isMimeEncoded()) throw new MultipartFormRequiredException();
}
@@ -86,9 +82,16 @@
* Sets the file for a parameter upload in a web request.
**/
public void selectFile( String parameterName, String fileName, InputStream inputStream, String contentType ) {
- if (_sourceForm == null || !_sourceForm.isFileParameter( parameterName )) {
- throw new IllegalNonFileParameterException( parameterName );
- }
+ assertFileParameter( parameterName );
+ }
+
+
+ /**
+ * Throws an exception if the specified parameter may not be set as a file.
+ */
+ private void assertFileParameter( String parameterName )
+ {
+ if (!maySelectFile( parameterName )) throw new IllegalNonFileParameterException( parameterName );
if (!isMimeEncoded()) throw new MultipartFormRequiredException();
}
@@ -233,9 +236,26 @@
/**
- * Returns true if this request is to be MIME-encoded.
+ * Returns true if this request is based on a web form.
**/
final
+ protected boolean isFormBased() {
+ return _sourceForm != null;
+ }
+
+
+ /**
+ * Returns true if selectFile may be called with this parameter.
+ */
+ protected boolean maySelectFile( String parameterName )
+ {
+ return isFileParameter( parameterName );
+ }
+
+
+ /**
+ * Returns true if this request is to be MIME-encoded.
+ **/
protected boolean isMimeEncoded() {
return _sourceForm != null && _sourceForm.isSubmitAsMime();
}
|