001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.sample.servlet.unit;
021:
022: import org.apache.cactus.ServletTestCase;
023: import org.apache.cactus.WebRequest;
024: import org.apache.cactus.WebResponse;
025:
026: /**
027: * Tests manipulating HTTP headers.
028: *
029: * @version $Id: TestHttpHeaders.java 238816 2004-02-29 16:36:46Z vmassol $
030: */
031: public class TestHttpHeaders extends ServletTestCase {
032: /**
033: * Verify that we can simulate several HTTP header values with the same
034: * header name.
035: *
036: * @param theRequest the request object that serves to initialize the
037: * HTTP connection to the server redirector.
038: */
039: public void beginSendMultivaluedHeader(WebRequest theRequest) {
040: theRequest.addHeader("testheader", "value1");
041: theRequest.addHeader("testheader", "value2");
042: }
043:
044: /**
045: * Verify that we can simulate several HTTP header values with the same
046: * header name.
047: */
048: public void testSendMultivaluedHeader() {
049: // Note: I am not sure how to retrieve multi valued headers. The
050: // problem is that I use
051: // URLConnection.setRequestProperty("testheader", "value1,value2") in
052: // JdkConnectionHelper to send the headers but request.getHeaders() does
053: // not seem to separate the different header values.
054: // The RFC 2616 says :
055: // message-header = field-name ":" [ field-value ]
056: // field-name = token
057: // field-value = *( field-content | LWS )
058: // field-content = <the OCTETs making up the field-value
059: // and consisting of either *TEXT or combinations
060: // of token, separators, and quoted-string>
061: // [...]
062: // Multiple message-header fields with the same field-name MAY be
063: // present in a message if and only if the entire field-value for that
064: // header field is defined as a comma-separated list [i.e., #(values)].
065: // It MUST be possible to combine the multiple header fields into one
066: // "field-name: field-value" pair, without changing the semantics of
067: // the message, by appending each subsequent field-value to the first,
068: // each separated by a comma. The order in which header fields with the
069: // same field-name are received is therefore significant to the
070: // interpretation of the combined field value, and thus a proxy MUST
071: // NOT change the order of these field values when a message is
072: // forwarded.
073: // ... so it should be ok ...
074: assertEquals("value1,value2", request.getHeader("testheader"));
075:
076: // Here is commented out what I would have thought I should have
077: // written to verify this test but it does not seem to work this way ...
078:
079: /*
080: Enumeration values = request.getHeaders("testheader");
081: int count = 0;
082: while (values.hasMoreElements()) {
083: String value = (String)values.nextElement();
084: if (!(value.equals("value1") || value.equals("value2"))) {
085: fail("unknown value [" + value + "] for header [testheader]");
086: }
087: count++;
088: }
089: assertEquals("Should have received 2 values for header [testheader]",
090: 2, count);
091: */
092: }
093:
094: //-------------------------------------------------------------------------
095:
096: /**
097: * Verify we can set the content type by setting an HTTP header.
098: *
099: * @param theRequest the request object that serves to initialize the
100: * HTTP connection to the server redirector.
101: */
102: public void beginSetContentTypeHeader(WebRequest theRequest) {
103: theRequest.addHeader("Content-type", "text/xml");
104: }
105:
106: /**
107: * Verify we can set the content type by setting an HTTP header.
108: */
109: public void testSetContentTypeHeader() {
110: assertEquals("text/xml", request.getContentType());
111: }
112:
113: //-------------------------------------------------------------------------
114:
115: /**
116: * Verify that we can set several headers in the response and
117: * assert them in endXXX().
118: */
119: public void testResponseAddHeaders() {
120: response.addHeader("X-Test-Header1", "value1");
121: response.addHeader("X-Test-Header2", "value2");
122: }
123:
124: /**
125: * Verify that we can set several headers in the response and
126: * assert them in endXXX().
127: *
128: * @param theResponse the response from the server side.
129: */
130: public void endResponseAddHeaders(WebResponse theResponse) {
131: String value1 = theResponse.getConnection().getHeaderField(
132: "X-Test-Header1");
133: String value2 = theResponse.getConnection().getHeaderField(
134: "X-Test-Header2");
135:
136: assertEquals("value1", value1);
137: assertEquals("value2", value2);
138: }
139:
140: }
|