vmassol 01/04/14 11:27:26
Modified: cactus/docs/framework/xdocs changes.xml todo.xml
cactus/src/framework/share/org/apache/commons/cactus
ServletTestRequest.java
cactus/src/framework/share/org/apache/commons/cactus/client
HttpClientHelper.java
cactus/src/sample/share/org/apache/commons/cactus/sample/unit
TestServletTestCase2.java
Log:
added handling of multivalued HTTP headers
Revision Changes Path
1.7 +4 -0 jakarta-commons/cactus/docs/framework/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/cactus/docs/framework/xdocs/changes.xml,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- changes.xml 2001/04/14 15:48:07 1.6
+++ changes.xml 2001/04/14 18:27:25 1.7
@@ -10,6 +10,10 @@
</devs>
<release version="1.0b2 in CVS" date="">
+ <action dev="VMA" type="add" due-to="Hoani Cross"
due-to-email="[EMAIL PROTECTED]">
+ Add handling of multivalued HTTP headers (same header name with different
+ values) in <code>ServletTestRequest</code>.
+ </action>
<action dev="VMA" type="update">
Made the methods <code>setUp()</code> and <code>tearDown()</code>
protected on the server side so that it is consistent with JUnit.
1.7 +0 -3 jakarta-commons/cactus/docs/framework/xdocs/todo.xml
Index: todo.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/cactus/docs/framework/xdocs/todo.xml,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- todo.xml 2001/04/14 15:48:07 1.6
+++ todo.xml 2001/04/14 18:27:25 1.7
@@ -30,9 +30,6 @@
</actions>
<actions priority="low">
- <action context="code">
- Manage multivalued HTTP headers in <code>ServletTestRequest</code>.
- </action>
<action context="docs">
Write a Cactus integration tutorial for JBuilder 4.
</action>
1.2 +52 -5
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/ServletTestRequest.java
Index: ServletTestRequest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/ServletTestRequest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ServletTestRequest.java 2001/04/09 11:52:36 1.1
+++ ServletTestRequest.java 2001/04/14 18:27:25 1.2
@@ -319,14 +319,26 @@
}
/**
- * Adds a header to the request.
+ * Adds a header to the request. Supports adding several values for the
+ * same header name.
*
* @param theName the header's name
* @param theValue the header's value
*/
public void addHeader(String theName, String theValue)
{
- m_Headers.put(theName, theValue);
+ // If there is already a header of the same name, add the
+ // new header to the Vector. If not, create a Vector an add it to the
+ // hashtable
+
+ if (m_Headers.containsKey(theName)) {
+ Vector v = (Vector)m_Headers.get(theName);
+ v.addElement(theValue);
+ } else {
+ Vector v = new Vector();
+ v.addElement(theValue);
+ m_Headers.put(theName, v);
+ }
}
/**
@@ -338,13 +350,48 @@
}
/**
+ * Returns the first value corresponding to this header's name.
+ *
* @param theName the header's name
- * @return the value corresponding to this header's name or null if not
+ * @return the first value corresponding to this header's name or null if not
* found
*/
- public String getHeaderValue(String theName)
+ public String getHeader(String theName)
{
- return (String)m_Headers.get(theName);
+ String[] values = getHeaderValues(theName);
+
+ if (values != null) {
+ return values[0];
+ }
+
+ return null;
+ }
+
+ /**
+ * Returns all the values associated with this header's name.
+ *
+ * @param theName the header's name
+ * @return the values corresponding to this header's name or null if not
+ * found
+ */
+ public String[] getHeaderValues(String theName)
+ {
+ if (m_Headers.containsKey(theName)) {
+
+ Vector v = (Vector)m_Headers.get(theName);
+
+ Object[] objs = new Object[v.size()];
+ v.copyInto(objs);
+
+ String[] result = new String[objs.length];
+ for (int i = 0; i < objs.length; i++) {
+ result[i] = (String)objs[i];
+ }
+
+ return result;
+ }
+
+ return null;
}
}
1.2 +15 -2
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/client/HttpClientHelper.java
Index: HttpClientHelper.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/client/HttpClientHelper.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HttpClientHelper.java 2001/04/09 11:52:37 1.1
+++ HttpClientHelper.java 2001/04/14 18:27:26 1.2
@@ -259,8 +259,21 @@
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
- String value = (String)theRequest.getHeaderValue(key);
- theConnection.setRequestProperty(key, value);
+ String[] values = theRequest.getHeaderValues(key);
+
+ // As the URLConnection.setRequestProperty will overwrite any
+ // property already set we have to regroup the multi valued
+ // headers into a single header name entry.
+ // Question: Is this an implementation bug ? It seems because
+ // on the server side, I cannot use the request.getHeaders() (it
+ // only returns a single header).
+
+ StringBuffer fullHeaderValue = new StringBuffer(values[0]);
+ for (int i = 1; i < values.length; i++) {
+ fullHeaderValue.append("," + values[i]);
+ }
+ theConnection.setRequestProperty(key, fullHeaderValue.toString());
+
}
}
1.2 +69 -0
jakarta-commons/cactus/src/sample/share/org/apache/commons/cactus/sample/unit/TestServletTestCase2.java
Index: TestServletTestCase2.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cactus/src/sample/share/org/apache/commons/cactus/sample/unit/TestServletTestCase2.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestServletTestCase2.java 2001/04/09 11:52:39 1.1
+++ TestServletTestCase2.java 2001/04/14 18:27:26 1.2
@@ -222,4 +222,73 @@
assertEquals("MyServlet", config.getServletName());
}
+ //-------------------------------------------------------------------------
+
+ /**
+ * Verify that we can simulate several HTTP header values with the same
+ * header name.
+ *
+ * @param theRequest the request object that serves to initialize the
+ * HTTP connection to the server redirector.
+ */
+ public void beginSendMultivaluedHeader(ServletTestRequest theRequest)
+ {
+ theRequest.addHeader("testheader", "value1");
+ theRequest.addHeader("testheader", "value2");
+ }
+
+ /**
+ * Verify that we can simulate several HTTP header values with the same
+ * header name.
+ */
+ public void testSendMultivaluedHeader()
+ {
+ // Note: I am not sure how to retrieve multi valued headers. The
+ // problem is that I use
+ // URLConnection.setRequestProperty("testheader", "value1,value2") in
+ // HttpClientHelper to send the headers but request.getHeaders() does
+ // not seem to separate the different header values.
+
+ // The RFC 2616 says :
+
+ // message-header = field-name ":" [ field-value ]
+ // field-name = token
+ // field-value = *( field-content | LWS )
+ // field-content = <the OCTETs making up the field-value
+ // and consisting of either *TEXT or combinations
+ // of token, separators, and quoted-string>
+ // [...]
+ // Multiple message-header fields with the same field-name MAY be
+ // present in a message if and only if the entire field-value for that
+ // header field is defined as a comma-separated list [i.e., #(values)].
+ // It MUST be possible to combine the multiple header fields into one
+ // "field-name: field-value" pair, without changing the semantics of
+ // the message, by appending each subsequent field-value to the first,
+ // each separated by a comma. The order in which header fields with the
+ // same field-name are received is therefore significant to the
+ // interpretation of the combined field value, and thus a proxy MUST
+ // NOT change the order of these field values when a message is
+ // forwarded.
+
+ // ... so it should be ok ...
+
+ assertEquals("value1,value2", request.getHeader("testheader"));
+
+ // Here is commented out what I would have thought I should have
+ // written to verify this test but it does not seem to work this way ...
+
+ /*
+ Enumeration values = request.getHeaders("testheader");
+ int count = 0;
+ while (values.hasMoreElements()) {
+ String value = (String)values.nextElement();
+ if (!(value.equals("value1") || value.equals("value2"))) {
+ fail("unknown value [" + value + "] for header [testheader]");
+ }
+ count++;
+ }
+ assertEquals("Should have received 2 values for header [testheader]", 2,
count);
+ */
+ }
+
}