1   /*
2    * $Source: /usr/cvsroot/melati/melati/src/test/java/org/melati/util/test/HttpHeaderTest.java,v $
3    * $Revision: 1.6 $
4    *
5    * Copyright (C) 2003 Jim Wright
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Jim Wright <jimw@paneris.org>
42   *     Bohemian Enterprise
43   *     Predmerice nad Jizerou 77
44   *     294 74
45   *     Mlada Boleslav
46   *     Czech Republic
47   */
48  
49  package org.melati.util.test;
50  
51  import junit.framework.TestCase;
52  
53  import org.melati.util.HttpHeader;
54  
55  /**
56   * Tests the corresponding class in the superpackage.
57   *
58   * @see HttpHeader
59   * @author jimw@paneris.org
60   * @version $Version$
61   */
62  public class HttpHeaderTest extends TestCase {
63  
64    /**
65     * Constructor.
66     */
67    public HttpHeaderTest(String testCaseName) {
68      super(testCaseName);
69    }
70  
71    /**
72     * Test a parsing of headers.
73     */
74    public void testCommaSeparated() throws Exception {
75      String fields = "\"malted barley\"; q=0.5, ,wheat ,,\t oats, maize";
76      // Excuse the pun
77      HttpHeader cornFields = new HttpHeader(fields);
78  
79      HttpHeader.TokenAndQValueIterator taqvi = cornFields.tokenAndQValueIterator();
80      HttpHeader.TokenAndQValue taqv;
81      
82      assertTrue(taqvi.hasNext());
83      taqv = taqvi.nextTokenAndQValue();
84      assertEquals("malted barley", taqv.token);
85      assertEquals(0.5f, taqv.q, 0.0001f);
86  
87      assertTrue(taqvi.hasNext());
88      taqv = taqvi.nextTokenAndQValue();
89      assertEquals("wheat", taqv.token);
90      assertEquals(1.0f, taqv.q, 0.0001f);
91  
92      assertTrue(taqvi.hasNext());
93      taqv = (HttpHeader.TokenAndQValue)taqvi.nextElement();
94      assertEquals("oats", taqv.token);
95      assertEquals(1.0f, taqv.q, 0.0001f);
96      try {
97        taqvi.remove();
98        assertTrue(false);
99      }
100     catch (UnsupportedOperationException e) {
101       e = null; // shut PMD up
102     }
103 
104     assertTrue(taqvi.hasMoreElements());
105     taqv = taqvi.nextTokenAndQValue();
106     assertEquals("maize", taqv.token);
107     assertEquals(1.0f, taqv.q, 0.0001f);
108 
109     assertTrue(! taqvi.hasNext());
110     assertTrue(! taqvi.hasMoreElements());
111     assertTrue(taqvi.next() instanceof HttpHeader.HttpHeaderException);
112 
113     try {
114       cornFields = new HttpHeader("");
115       assertTrue(false);
116     }
117     catch (HttpHeader.HttpHeaderException e) {
118       e = null; // shut PMD up
119     }
120     catch (AssertionError e) {
121       // This is the current policy. "" is treated as null if
122       // assertions are switched off.
123       e = null; // shut PMD up
124     }
125       
126     try {
127       cornFields = new HttpHeader(",EmptyFirst");
128       assertTrue(false);
129     }
130     catch (HttpHeader.HttpHeaderException e) {
131       e = null; // shut PMD up
132     }
133 
134     try {
135       cornFields = new HttpHeader("First . Second");
136       taqvi = cornFields.tokenAndQValueIterator();
137       taqvi.nextTokenAndQValue();
138       assertTrue(false);
139     }
140     catch (HttpHeader.HttpHeaderException e) {
141       e = null; // shut PMD up
142     }
143 
144     try {
145       cornFields = new HttpHeader("=");
146       taqvi = cornFields.tokenAndQValueIterator();
147       taqvi.nextTokenAndQValue();
148       assertTrue(false);
149     }
150     catch (HttpHeader.HttpHeaderException e) {
151       e = null; // shut PMD up
152     }
153 
154     try {
155       cornFields = new HttpHeader("hello; = 0.1");
156       taqvi = cornFields.tokenAndQValueIterator();
157       taqvi.nextTokenAndQValue();
158       assertTrue(false);
159     }
160     catch (HttpHeader.HttpHeaderException e) {
161       e = null; // shut PMD up
162     }
163 
164     try {
165       cornFields = new HttpHeader("hello; r = 0.1");
166       taqvi = cornFields.tokenAndQValueIterator();
167       taqvi.nextTokenAndQValue();
168       assertTrue(false);
169     }
170     catch (HttpHeader.HttpHeaderException e) {
171       e = null; // shut PMD up
172     }
173       
174     try {
175       cornFields = new HttpHeader("hello hello");
176       taqvi = cornFields.tokenAndQValueIterator();
177       taqvi.nextTokenAndQValue();
178       assertTrue(false);
179     }
180     catch (HttpHeader.HttpHeaderException e) {
181       e = null; // shut PMD up
182     }
183 
184     try {
185       cornFields = new HttpHeader("hello; q = p");
186       taqvi = cornFields.tokenAndQValueIterator();
187       taqvi.nextTokenAndQValue();
188       assertTrue(false);
189     }
190     catch (HttpHeader.HttpHeaderException e) {
191       e = null; // shut PMD up
192     }
193 
194     try {
195       cornFields = new HttpHeader("hello; q - 0.0");
196       taqvi = cornFields.tokenAndQValueIterator();
197       taqvi.nextTokenAndQValue();
198       assertTrue(false);
199     }
200     catch (HttpHeader.HttpHeaderException e) {
201       e = null; // shut PMD up
202     }
203 
204 /* tokenizer is protected
205     try {
206       cornFields = new HttpHeader("hello");
207       cornFields.tokenizer.readQValue();
208       assertTrue(false);
209     }
210     catch (Exception e) {
211     }
212 
213     try {
214       cornFields = new HttpHeader("hello");
215       cornFields.tokenizer.skipCommaSeparator();
216       assertTrue(false);
217     }
218     catch (Exception e) {
219     }
220 */
221   }
222 
223 }
224