KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > unit > TestHttpHeaders


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.sample.servlet.unit;
21
22 import org.apache.cactus.ServletTestCase;
23 import org.apache.cactus.WebRequest;
24 import org.apache.cactus.WebResponse;
25
26 /**
27  * Tests manipulating HTTP headers.
28  *
29  * @version $Id: TestHttpHeaders.java,v 1.3 2004/02/29 16:36:44 vmassol Exp $
30  */

31 public class TestHttpHeaders extends ServletTestCase
32 {
33     /**
34      * Verify that we can simulate several HTTP header values with the same
35      * header name.
36      *
37      * @param theRequest the request object that serves to initialize the
38      * HTTP connection to the server redirector.
39      */

40     public void beginSendMultivaluedHeader(WebRequest theRequest)
41     {
42         theRequest.addHeader("testheader", "value1");
43         theRequest.addHeader("testheader", "value2");
44     }
45
46     /**
47      * Verify that we can simulate several HTTP header values with the same
48      * header name.
49      */

50     public void testSendMultivaluedHeader()
51     {
52         // Note: I am not sure how to retrieve multi valued headers. The
53
// problem is that I use
54
// URLConnection.setRequestProperty("testheader", "value1,value2") in
55
// JdkConnectionHelper to send the headers but request.getHeaders() does
56
// not seem to separate the different header values.
57
// The RFC 2616 says :
58
// message-header = field-name ":" [ field-value ]
59
// field-name = token
60
// field-value = *( field-content | LWS )
61
// field-content = <the OCTETs making up the field-value
62
// and consisting of either *TEXT or combinations
63
// of token, separators, and quoted-string>
64
// [...]
65
// Multiple message-header fields with the same field-name MAY be
66
// present in a message if and only if the entire field-value for that
67
// header field is defined as a comma-separated list [i.e., #(values)].
68
// It MUST be possible to combine the multiple header fields into one
69
// "field-name: field-value" pair, without changing the semantics of
70
// the message, by appending each subsequent field-value to the first,
71
// each separated by a comma. The order in which header fields with the
72
// same field-name are received is therefore significant to the
73
// interpretation of the combined field value, and thus a proxy MUST
74
// NOT change the order of these field values when a message is
75
// forwarded.
76
// ... so it should be ok ...
77
assertEquals("value1,value2", request.getHeader("testheader"));
78
79         // Here is commented out what I would have thought I should have
80
// written to verify this test but it does not seem to work this way ...
81

82         /*
83         Enumeration values = request.getHeaders("testheader");
84         int count = 0;
85         while (values.hasMoreElements()) {
86             String value = (String)values.nextElement();
87             if (!(value.equals("value1") || value.equals("value2"))) {
88                 fail("unknown value [" + value + "] for header [testheader]");
89             }
90             count++;
91         }
92         assertEquals("Should have received 2 values for header [testheader]",
93             2, count);
94         */

95     }
96
97     //-------------------------------------------------------------------------
98

99     /**
100      * Verify we can set the content type by setting an HTTP header.
101      *
102      * @param theRequest the request object that serves to initialize the
103      * HTTP connection to the server redirector.
104      */

105     public void beginSetContentTypeHeader(WebRequest theRequest)
106     {
107         theRequest.addHeader("Content-type", "text/xml");
108     }
109
110     /**
111      * Verify we can set the content type by setting an HTTP header.
112      */

113     public void testSetContentTypeHeader()
114     {
115         assertEquals("text/xml", request.getContentType());
116     }
117
118     //-------------------------------------------------------------------------
119

120     /**
121      * Verify that we can set several headers in the response and
122      * assert them in endXXX().
123      */

124     public void testResponseAddHeaders()
125     {
126         response.addHeader("X-Test-Header1", "value1");
127         response.addHeader("X-Test-Header2", "value2");
128     }
129
130     /**
131      * Verify that we can set several headers in the response and
132      * assert them in endXXX().
133      *
134      * @param theResponse the response from the server side.
135      */

136     public void endResponseAddHeaders(WebResponse theResponse)
137     {
138         String JavaDoc value1 =
139             theResponse.getConnection().getHeaderField("X-Test-Header1");
140         String JavaDoc value2 =
141             theResponse.getConnection().getHeaderField("X-Test-Header2");
142
143         assertEquals("value1", value1);
144         assertEquals("value2", value2);
145     }
146
147 }
148
Popular Tags