Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/util/TestHeaderUtils.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/util/TestHeaderUtils.java $
3    * $Revision: 411100 $
4    * $Date: 2006-06-02 11:12:04 +0200 (Fri, 02 Jun 2006) $
5    * 
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http.util;
31  
32  import java.io.IOException;
33  
34  import org.apache.http.Header;
35  import org.apache.http.HeaderElement;
36  import org.apache.http.NameValuePair;
37  import org.apache.http.ProtocolException;
38  import org.apache.http.io.HttpDataReceiver;
39  import org.apache.http.mockup.HttpDataReceiverMockup;
40  
41  import junit.framework.Test;
42  import junit.framework.TestCase;
43  import junit.framework.TestSuite;
44  
45  /**
46   * Unit tests for {@link Header}.
47   *
48   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
49   */
50  public class TestHeaderUtils extends TestCase {
51  
52      public TestHeaderUtils(String testName) {
53          super(testName);
54      }
55  
56      public static void main(String args[]) {
57          String[] testCaseName = { TestHeaderUtils.class.getName() };
58          junit.textui.TestRunner.main(testCaseName);
59      }
60  
61      public static Test suite() {
62          return new TestSuite(TestHeaderUtils.class);
63      }
64  
65      public void testInvalidInput() throws Exception {
66          try {
67              HeaderUtils.parseHeaders(null);
68              fail("IllegalArgumentException should have been thrown");
69          } catch (IllegalArgumentException ex) {
70              // expected
71          }
72      }
73      
74      public void testBasicHeaderParsing() throws Exception {
75          String s = 
76              "header1: stuff\r\n" + 
77              "header2  : stuff \r\n" + 
78              "header3: stuff\r\n" + 
79              "     and more stuff\r\n" + 
80              "\t and even more stuff\r\n" +  
81              "     \r\n" +  
82              "\r\n"; 
83          HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); 
84          Header[] headers = HeaderUtils.parseHeaders(receiver);
85          assertNotNull(headers);
86          assertEquals(3, headers.length);
87          assertEquals("header1", headers[0].getName());
88          assertEquals("stuff", headers[0].getValue());
89          assertEquals("header2", headers[1].getName());
90          assertEquals("stuff", headers[1].getValue());
91          assertEquals("header3", headers[2].getName());
92          assertEquals("stuff and more stuff and even more stuff", headers[2].getValue());
93      }
94  
95      public void testBufferedHeader() throws Exception {
96          String s = 
97              "header1  : stuff; param1 = value1; param2 = \"value 2\" \r\n" + 
98              "\r\n"; 
99          HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); 
100         Header[] headers = HeaderUtils.parseHeaders(receiver);
101         assertNotNull(headers);
102         assertEquals(1, headers.length);
103         assertEquals("header1  : stuff; param1 = value1; param2 = \"value 2\" ", headers[0].toString());
104         HeaderElement[] elements = headers[0].getElements();
105         assertNotNull(elements);
106         assertEquals(1, elements.length);
107         assertEquals("stuff", elements[0].getName());
108         assertEquals(null, elements[0].getValue());
109         NameValuePair[] params = elements[0].getParameters();
110         assertNotNull(params);
111         assertEquals(2, params.length);
112         assertEquals("param1", params[0].getName());
113         assertEquals("value1", params[0].getValue());
114         assertEquals("param2", params[1].getName());
115         assertEquals("value 2", params[1].getValue());
116     }
117 
118     public void testParsingInvalidHeaders() throws Exception {
119         String s = "    stuff\r\n" + 
120             "header1: stuff\r\n" + 
121             "\r\n"; 
122         HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII");
123         try {
124             HeaderUtils.parseHeaders(receiver);
125             fail("ProtocolException should have been thrown");
126         } catch (ProtocolException ex) {
127             // expected
128         }
129         s = "  :  stuff\r\n" + 
130             "header1: stuff\r\n" + 
131             "\r\n"; 
132         receiver = new HttpDataReceiverMockup(s, "US-ASCII");
133         try {
134             HeaderUtils.parseHeaders(receiver);
135             fail("ProtocolException should have been thrown");
136         } catch (ProtocolException ex) {
137             // expected
138         }
139     }
140     
141     public void testParsingMalformedFirstHeader() throws Exception {
142         String s = 
143             "    header1: stuff\r\n" + 
144             "header2  : stuff \r\n"; 
145         HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); 
146         Header[] headers = HeaderUtils.parseHeaders(receiver);
147         assertNotNull(headers);
148         assertEquals(2, headers.length);
149         assertEquals("header1", headers[0].getName());
150         assertEquals("stuff", headers[0].getValue());
151         assertEquals("header2", headers[1].getName());
152         assertEquals("stuff", headers[1].getValue());
153     }
154     
155     public void testEmptyDataStream() throws Exception {
156         String s = ""; 
157         HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); 
158         Header[] headers = HeaderUtils.parseHeaders(receiver);
159         assertNotNull(headers);
160         assertEquals(0, headers.length);
161     }
162 
163     public void testMaxHeaderCount() throws Exception {
164         String s = 
165             "header1: stuff\r\n" + 
166             "header2: stuff \r\n" + 
167             "header3: stuff\r\n" + 
168             "\r\n"; 
169         HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII");
170         try {
171             HeaderUtils.parseHeaders(receiver, 2);
172             fail("IOException should have been thrown");
173         } catch (IOException ex) {
174             // expected
175         }
176     }
177         
178 }
179