[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit GetMethodWebRequest.java,1.13,1.14 Parame
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-02-04 22:33:58
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv7183/src/com/meterware/httpunit
Modified Files:
GetMethodWebRequest.java ParameterHolder.java
PostMethodWebRequest.java UncheckedParameterHolder.java
WebForm.java WebLink.java WebRequest.java
WebRequestSource.java
Log Message:
Handle query strings on form actions
Index: GetMethodWebRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/GetMethodWebRequest.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- GetMethodWebRequest.java 2002/01/17 19:38:09 1.13
+++ GetMethodWebRequest.java 2002/02/04 22:33:55 1.14
@@ -20,6 +20,7 @@
*
*******************************************************************************************************************/
import java.net.URL;
+import java.io.IOException;
/**
* An HTTP request using the GET method.
@@ -59,6 +60,21 @@
}
+ /**
+ * Returns the query string defined for this request.
+ **/
+ public String getQueryString() {
+ try {
+ URLEncodedString encoder = new URLEncodedString();
+ getParameterHolder().recordPredefinedParameters( encoder );
+ getParameterHolder().recordParameters( encoder );
+ return encoder.getString();
+ } catch (IOException e) {
+ throw new RuntimeException( "Programming error: " + e ); // should never happen
+ }
+ }
+
+
//--------------------------------------- package members ---------------------------------------------
@@ -75,19 +91,6 @@
**/
GetMethodWebRequest( WebLink sourceLink ) {
super( sourceLink );
- }
-
-
-//------------------------------------- protected members ---------------------------------------------
-
-
- protected String getURLString() {
- final String parameterString = getParameterString();
- if (parameterString.length() == 0) {
- return super.getURLString();
- } else {
- return super.getURLString() + "?" + parameterString;
- }
}
Index: ParameterHolder.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ParameterHolder.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ParameterHolder.java 2002/01/17 19:38:09 1.4
+++ ParameterHolder.java 2002/02/04 22:33:55 1.5
@@ -40,6 +40,14 @@
/**
+ * Iterates through the fixed, predefined parameters in this holder, recording them in the supplied parameter processor.\
+ * These parameters always go on the URL, no matter what encoding method is used.
+ **/
+ abstract
+ void recordPredefinedParameters( ParameterProcessor processor ) throws IOException;
+
+
+ /**
* Iterates through the parameters in this holder, recording them in the supplied parameter processor.
**/
abstract
Index: PostMethodWebRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/PostMethodWebRequest.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- PostMethodWebRequest.java 2002/01/17 19:38:09 1.22
+++ PostMethodWebRequest.java 2002/02/04 22:33:55 1.23
@@ -84,6 +84,20 @@
/**
+ * Returns the query string defined for this request.
+ **/
+ public String getQueryString() {
+ try {
+ URLEncodedString encoder = new URLEncodedString();
+ getParameterHolder().recordPredefinedParameters( encoder );
+ return encoder.getString();
+ } catch (IOException e) {
+ throw new RuntimeException( "Programming error: " + e ); // should never happen
+ }
+ }
+
+
+ /**
* Returns true if selectFile may be called with this parameter.
*/
protected boolean maySelectFile( String parameterName )
@@ -155,7 +169,18 @@
* Transmits the body of this request as a sequence of bytes.
**/
void writeTo( OutputStream outputStream ) throws IOException {
- outputStream.write( getRequest().getParameterString().getBytes() );
+ outputStream.write( getParameterString().getBytes() );
+ }
+
+
+ private String getParameterString() {
+ try {
+ URLEncodedString encoder = new URLEncodedString();
+ getRequest().getParameterHolder().recordParameters( encoder );
+ return encoder.getString();
+ } catch (IOException e) {
+ throw new RuntimeException( "Programming error: " + e ); // should never happen
+ }
}
}
Index: UncheckedParameterHolder.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/UncheckedParameterHolder.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- UncheckedParameterHolder.java 2002/01/17 19:38:09 1.4
+++ UncheckedParameterHolder.java 2002/02/04 22:33:55 1.5
@@ -68,6 +68,13 @@
/**
+ * Does nothing, since unchecked requests treat all parameters the same.
+ **/
+ void recordPredefinedParameters( ParameterProcessor processor ) throws IOException {
+ }
+
+
+ /**
* Iterates through the parameters in this holder, recording them in the supplied parameter processor.
**/
void recordParameters( ParameterProcessor processor ) throws IOException {
Index: WebForm.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -r1.43 -r1.44
--- WebForm.java 2002/01/17 19:38:09 1.43
+++ WebForm.java 2002/02/04 22:33:55 1.44
@@ -21,13 +21,7 @@
*******************************************************************************************************************/
import java.net.URL;
-import java.util.Hashtable;
-import java.util.Vector;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Arrays;
-import java.util.Map;
-import java.util.Iterator;
+import java.util.*;
import java.io.IOException;
import org.w3c.dom.Element;
@@ -307,6 +301,14 @@
}
+ /**
+ * Records a parameter defined by including it in the destination URL.
+ **/
+ protected void addPresetParameter( String name, String value ) {
+ _presets.add( new PresetFormParameter( name, value ) );
+ }
+
+
//---------------------------------- ParameterHolder methods --------------------------------
@@ -319,6 +321,19 @@
/**
+ * Iterates through the fixed, predefined parameters in this holder, recording them in the supplied parameter processor.\
+ * These parameters always go on the URL, no matter what encoding method is used.
+ **/
+
+ void recordPredefinedParameters( ParameterProcessor processor ) throws IOException {
+ FormControl[] controls = getPresetParameters();
+ for (int i = 0; i < controls.length; i++) {
+ controls[i].addValues( processor, getCharacterSet() );
+ }
+ }
+
+
+ /**
* Iterates through the parameters in this holder, recording them in the supplied parameter processor.
**/
public void recordParameters( ParameterProcessor processor ) throws IOException {
@@ -392,6 +407,10 @@
private Vector _buttonVector;
+ private FormControl[] _presetParameters;
+ private ArrayList _presets;
+
+
private SubmitButton getDefaultButton() {
if (getSubmitButtons().length == 1) {
return getSubmitButtons()[0];
@@ -418,6 +437,16 @@
}
+ private FormControl[] getPresetParameters() {
+ if (_presetParameters == null) {
+ _presets = new ArrayList();
+ loadDestinationParameters();
+ _presetParameters = (FormControl[]) _presets.toArray( new FormControl[ _presets.size() ] );
+ }
+ return _presetParameters;
+ }
+
+
/**
* Returns an array of form parameter attributes for this form.
**/
@@ -446,21 +475,26 @@
private Map getFormParameters() {
if (_formParameters == null) {
_formParameters = new HashMap();
- FormControl[] controls = getFormControls();
- for (int i = 0; i < controls.length; i++) {
- if (controls[i].getName().length() == 0) continue;
- FormParameter parameter = (FormParameter) _formParameters.get( controls[i].getName() );
- if (parameter == null) {
- parameter = new FormParameter();
- _formParameters.put( controls[i].getName(), parameter );
- }
- parameter.addControl( controls[i] );
- }
+ loadFormParameters( getPresetParameters() );
+ loadFormParameters( getFormControls() );
}
return _formParameters;
}
+ private void loadFormParameters( FormControl[] controls ) {
+ for (int i = 0; i < controls.length; i++) {
+ if (controls[i].getName().length() == 0) continue;
+ FormParameter parameter = (FormParameter) _formParameters.get( controls[i].getName() );
+ if (parameter == null) {
+ parameter = new FormParameter();
+ _formParameters.put( controls[i].getName(), parameter );
+ }
+ parameter.addControl( controls[i] );
+ }
+ }
+
+
private void addFormParametersToList( NodeList children, Vector list ) {
for (int i = 0; i < children.getLength(); i++) {
addFormParametersToList( children.item(i), list );
@@ -599,8 +633,72 @@
}
}
+
+//========================================== class PresetFormParameter =================================================
+
+
+ class PresetFormParameter extends FormControl {
+
+ PresetFormParameter( String name, String value ) {
+ _name = name;
+ _value = value;
+ }
+
+
+ /**
+ * Returns the name of this control..
+ **/
+ public String getName() {
+ return _name;
+ }
+
+
+ /**
+ * Returns true if this control is read-only.
+ **/
+ public boolean isReadOnly() {
+ return true;
+ }
+
+
+ /**
+ * Returns true if this control accepts free-form text.
+ **/
+ public boolean isTextControl() {
+ return true;
+ }
+
+
+ /**
+ * Remove any required values for this control from the list, throwing an exception if they are missing.
+ **/
+ void claimRequiredValues( List values ) {
+ if (_value != null) claimValueIsRequired( values, _value );
+ }
+
+
+ /**
+ * Returns the current value(s) associated with this control. These values will be transmitted to the server
+ * if the control is 'successful'.
+ **/
+ public String[] getValues() {
+ if (_values == null) _values = new String[] { _value };
+ return _values;
+ }
+
+
+ void addValues( ParameterProcessor processor, String characterSet ) throws IOException {
+ processor.addParameter( _name, _value, characterSet );
+ }
+
+
+ private String _name;
+ private String _value;
+ private String[] _values;
+ }
+
-//================================ exception class NoSuchParameterException =========================================
+//===========================---===== exception class NoSuchParameterException =========================================
/**
Index: WebLink.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebLink.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- WebLink.java 2002/01/23 18:35:56 1.16
+++ WebLink.java 2002/02/04 22:33:55 1.17
@@ -111,6 +111,15 @@
/**
+ * Iterates through the fixed, predefined parameters in this holder, recording them in the supplied parameter processor.\
+ * These parameters always go on the URL, no matter what encoding method is used.
+ **/
+
+ void recordPredefinedParameters( ParameterProcessor processor ) throws IOException {
+ }
+
+
+ /**
* Iterates through the parameters in this holder, recording them in the supplied parameter processor.
**/
void recordParameters( ParameterProcessor processor ) throws IOException {
@@ -204,25 +213,11 @@
private static final String[] NO_VALUES = new String[0];
- private static final String PARAM_DELIM = "&";
private Map _presetParameterMap;
private ArrayList _presetParameterList;
- /**
- * Gets all parameters from a URL
- **/
- private String getParametersString() {
- final String url = getURLString();
- final int questionMarkIndex = url.indexOf("?");
- if (questionMarkIndex >= 1 && questionMarkIndex < url.length() - 1) {
- return url.substring(questionMarkIndex + 1);
- }
- return "";
- }
-
-
private Map getPresetParameterMap() {
if (_presetParameterMap == null) loadPresetParameters();
return _presetParameterMap;
@@ -238,23 +233,13 @@
private void loadPresetParameters() {
_presetParameterMap = new HashMap();
_presetParameterList = new ArrayList();
- StringTokenizer st = new StringTokenizer( getParametersString(), PARAM_DELIM );
- while (st.hasMoreTokens()) stripOneParameter( _presetParameterList, _presetParameterMap, st.nextToken() );
+ loadDestinationParameters();
}
- /**
- * add a pair key-value to the hashtable, creates an array of values if param already exists.
- **/
- private void stripOneParameter( ArrayList list, Map map, String param ) {
- final int index = param.indexOf( "=" );
- String value = ((index < 0) ? null
- : ((index == param.length() - 1)
- ? ""
- : HttpUnitUtils.decode( param.substring( index + 1 ) ).trim() ));
- String name = (index < 0) ? param.trim() : HttpUnitUtils.decode( param.substring( 0, index ) ).trim();
- map.put( name, withNewValue( (String[]) map.get( name ), value ) );
- list.add( new LinkParameter( name, value ) );
+ protected void addPresetParameter( String name, String value ) {
+ _presetParameterMap.put( name, withNewValue( (String[]) _presetParameterMap.get( name ), value ) );
+ _presetParameterList.add( new LinkParameter( name, value ) );
}
Index: WebRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequest.java,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -r1.38 -r1.39
--- WebRequest.java 2002/01/25 19:09:08 1.38
+++ WebRequest.java 2002/02/04 22:33:55 1.39
@@ -82,18 +82,19 @@
/**
- * Returns the query string defined for this request.
+ * Returns the HTTP method defined for this request.
**/
- public String getQueryString() {
- return getParameterString();
- }
+ abstract
+ public String getMethod();
/**
- * Returns the HTTP method defined for this request.
+ * Returns the query string defined for this request. The query string is sent to the HTTP server as part of
+ * the request header. This default implementation returns an empty string.
**/
- abstract
- public String getMethod();
+ public String getQueryString() {
+ return "";
+ }
//------------------------------------- ParameterCollection methods ------------------------------------
@@ -348,19 +349,15 @@
}
- protected String getURLString() {
- return _urlString;
- }
+//------------------------------------- protected members ---------------------------------------------
- final
- protected String getParameterString() {
- try {
- URLEncodedString encoder = new URLEncodedString();
- _parameterHolder.recordParameters( encoder );
- return encoder.getString();
- } catch (IOException e) {
- throw new RuntimeException( "Programming error: " + e ); // should never happen
+ protected String getURLString() {
+ final String queryString = getQueryString();
+ if (queryString.length() == 0) {
+ return _urlString;
+ } else {
+ return _urlString + "?" + queryString;
}
}
Index: WebRequestSource.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequestSource.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- WebRequestSource.java 2002/01/17 19:38:09 1.5
+++ WebRequestSource.java 2002/02/04 22:33:55 1.6
@@ -23,6 +23,7 @@
import org.w3c.dom.Node;
import java.net.URL;
+import java.util.StringTokenizer;
abstract
@@ -126,7 +127,7 @@
if (node == null) throw new IllegalArgumentException( "node must not be null" );
_node = node;
_baseURL = baseURL;
- _destination = destination;
+ _destination = destination;
_parentTarget = parentTarget;
}
@@ -148,9 +149,31 @@
return _node;
}
- //----------------------------- private members -----------------------------------------------
+ /**
+ * Extracts any parameters specified as part of the destination URL, calling addPresetParameter for each one
+ * in the order in which they are found.
+ */
+ final
+ protected void loadDestinationParameters() {
+ StringTokenizer st = new StringTokenizer( getParametersString(), PARAM_DELIM );
+ while (st.hasMoreTokens()) stripOneParameter( st.nextToken() );
+ }
+
+ /**
+ * Records a parameter defined by including it in the destination URL.
+ * The value can be null, if the parameter name was not specified with an equals sign.
+ **/
+ abstract
+ protected void addPresetParameter( String name, String value );
+
+
+//----------------------------- private members -----------------------------------------------
+
+
+ private static final String PARAM_DELIM = "&";
+
/** The target in which the parent response is to be rendered. **/
private String _parentTarget;
@@ -166,6 +189,35 @@
private String getSpecifiedTarget() {
return NodeUtils.getNodeAttribute( _node, "target" );
}
+
+
+ /**
+ * Gets all parameters from a URL
+ **/
+ private String getParametersString() {
+ final String url = trimFragment( getDestination() );
+ final int questionMarkIndex = url.indexOf("?");
+ if (questionMarkIndex >= 1 && questionMarkIndex < url.length() - 1) {
+ return url.substring( questionMarkIndex + 1 );
+ }
+ return "";
+ }
+
+
+ /**
+ * Extracts a parameter of the form <name>[=[<value>]].
+ **/
+ private void stripOneParameter( String param ) {
+ final int index = param.indexOf( "=" );
+ String value = ((index < 0)
+ ? null
+ : ((index == param.length() - 1)
+ ? ""
+ : HttpUnitUtils.decode( param.substring( index + 1 ) ).trim() ));
+ String name = (index < 0) ? param.trim() : HttpUnitUtils.decode( param.substring( 0, index ) ).trim();
+ addPresetParameter( name, value );
+ }
+
}
|