View Javadoc
1   /*
2    * $Source$
3    * $Revision$
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       
114     try {
115       cornFields = new HttpHeader(",EmptyFirst");
116       assertTrue(false);
117     }
118     catch (HttpHeader.HttpHeaderException e) {
119       e = null; // shut PMD up
120     }
121 
122     try {
123       cornFields = new HttpHeader("First . Second");
124       taqvi = cornFields.tokenAndQValueIterator();
125       taqvi.nextTokenAndQValue();
126       assertTrue(false);
127     }
128     catch (HttpHeader.HttpHeaderException e) {
129       e = null; // shut PMD up
130     }
131 
132     try {
133       cornFields = new HttpHeader("=");
134       taqvi = cornFields.tokenAndQValueIterator();
135       taqvi.nextTokenAndQValue();
136       assertTrue(false);
137     }
138     catch (HttpHeader.HttpHeaderException e) {
139       e = null; // shut PMD up
140     }
141 
142     try {
143       cornFields = new HttpHeader("hello; = 0.1");
144       taqvi = cornFields.tokenAndQValueIterator();
145       taqvi.nextTokenAndQValue();
146       assertTrue(false);
147     }
148     catch (HttpHeader.HttpHeaderException e) {
149       e = null; // shut PMD up
150     }
151 
152     try {
153       cornFields = new HttpHeader("hello; r = 0.1");
154       taqvi = cornFields.tokenAndQValueIterator();
155       taqvi.nextTokenAndQValue();
156       assertTrue(false);
157     }
158     catch (HttpHeader.HttpHeaderException e) {
159       e = null; // shut PMD up
160     }
161       
162     try {
163       cornFields = new HttpHeader("hello hello");
164       taqvi = cornFields.tokenAndQValueIterator();
165       taqvi.nextTokenAndQValue();
166       assertTrue(false);
167     }
168     catch (HttpHeader.HttpHeaderException e) {
169       e = null; // shut PMD up
170     }
171 
172     try {
173       cornFields = new HttpHeader("hello; q = p");
174       taqvi = cornFields.tokenAndQValueIterator();
175       taqvi.nextTokenAndQValue();
176       assertTrue(false);
177     }
178     catch (HttpHeader.HttpHeaderException e) {
179       e = null; // shut PMD up
180     }
181 
182     try {
183       cornFields = new HttpHeader("hello; q - 0.0");
184       taqvi = cornFields.tokenAndQValueIterator();
185       taqvi.nextTokenAndQValue();
186       assertTrue(false);
187     }
188     catch (HttpHeader.HttpHeaderException e) {
189       e = null; // shut PMD up
190     }
191 
192 /* tokenizer is protected
193     try {
194       cornFields = new HttpHeader("hello");
195       cornFields.tokenizer.readQValue();
196       assertTrue(false);
197     }
198     catch (Exception e) {
199     }
200 
201     try {
202       cornFields = new HttpHeader("hello");
203       cornFields.tokenizer.skipCommaSeparator();
204       assertTrue(false);
205     }
206     catch (Exception e) {
207     }
208 */
209   }
210 
211 }
212