001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java $
003: * $Revision: 589325 $
004: * $Date: 2007-10-28 11:37:56 +0100 (Sun, 28 Oct 2007) $
005: * ====================================================================
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: * [Additional notices, if required by prior licensing conditions]
030: *
031: */
032:
033: package org.apache.http.message;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: import org.apache.http.HeaderElement;
040: import org.apache.http.NameValuePair;
041: import org.apache.http.util.CharArrayBuffer;
042:
043: /**
044: * Tests for header value parsing.
045: *
046: * @author Rodney Waldhoff
047: * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
048: * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
049: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
050: * @author and others
051: * @version $Id: TestBasicHeaderValueParser.java 589325 2007-10-28 10:37:56Z olegk $
052: */
053: public class TestBasicHeaderValueParser extends TestCase {
054:
055: // ------------------------------------------------------------ Constructor
056: public TestBasicHeaderValueParser(String testName) {
057: super (testName);
058: }
059:
060: // ------------------------------------------------------------------- Main
061: public static void main(String args[]) {
062: String[] testCaseName = { TestBasicHeaderValueParser.class
063: .getName() };
064: junit.textui.TestRunner.main(testCaseName);
065: }
066:
067: // ------------------------------------------------------- TestCase Methods
068:
069: public static Test suite() {
070: return new TestSuite(TestBasicHeaderValueParser.class);
071: }
072:
073: public void testParseHeaderElements() throws Exception {
074: String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; "
075: + "name5=value5, name6= ; name7 = value7; name8 = \" value8\"";
076: HeaderElement[] elements = BasicHeaderValueParser
077: .parseElements(headerValue, null);
078: // there are 3 elements
079: assertEquals(3, elements.length);
080: // 1st element
081: assertEquals("name1", elements[0].getName());
082: assertEquals("value1", elements[0].getValue());
083: // 1st element has 2 getParameters()
084: assertEquals(2, elements[0].getParameters().length);
085: assertEquals("name2", elements[0].getParameters()[0].getName());
086: assertEquals(null, elements[0].getParameters()[0].getValue());
087: assertEquals("name3", elements[0].getParameters()[1].getName());
088: assertEquals("value3", elements[0].getParameters()[1]
089: .getValue());
090: // 2nd element
091: assertEquals("name4", elements[1].getName());
092: assertEquals("value4", elements[1].getValue());
093: // 2nd element has 1 parameter
094: assertEquals(1, elements[1].getParameters().length);
095: assertEquals("name5", elements[1].getParameters()[0].getName());
096: assertEquals("value5", elements[1].getParameters()[0]
097: .getValue());
098: // 3rd element
099: assertEquals("name6", elements[2].getName());
100: assertEquals("", elements[2].getValue());
101: // 3rd element has 2 getParameters()
102: assertEquals(2, elements[2].getParameters().length);
103: assertEquals("name7", elements[2].getParameters()[0].getName());
104: assertEquals("value7", elements[2].getParameters()[0]
105: .getValue());
106: assertEquals("name8", elements[2].getParameters()[1].getName());
107: assertEquals(" value8", elements[2].getParameters()[1]
108: .getValue());
109: }
110:
111: public void testParseHEEscaped() {
112: String s = "test1 = \"\\\"stuff\\\"\", test2= \"\\\\\", test3 = \"stuff, stuff\"";
113: HeaderElement[] elements = BasicHeaderValueParser
114: .parseElements(s, null);
115: assertEquals(3, elements.length);
116: assertEquals("test1", elements[0].getName());
117: assertEquals("\\\"stuff\\\"", elements[0].getValue());
118: assertEquals("test2", elements[1].getName());
119: assertEquals("\\\\", elements[1].getValue());
120: assertEquals("test3", elements[2].getName());
121: assertEquals("stuff, stuff", elements[2].getValue());
122: }
123:
124: public void testHEFringeCase1() throws Exception {
125: String headerValue = "name1 = value1,";
126: HeaderElement[] elements = BasicHeaderValueParser
127: .parseElements(headerValue, null);
128: assertEquals("Number of elements", 1, elements.length);
129: }
130:
131: public void testHEFringeCase2() throws Exception {
132: String headerValue = "name1 = value1, ";
133: HeaderElement[] elements = BasicHeaderValueParser
134: .parseElements(headerValue, null);
135: assertEquals("Number of elements", 1, elements.length);
136: }
137:
138: public void testHEFringeCase3() throws Exception {
139: String headerValue = ",, ,, ,";
140: HeaderElement[] elements = BasicHeaderValueParser
141: .parseElements(headerValue, null);
142: assertEquals("Number of elements", 0, elements.length);
143: }
144:
145: public void testNVParseUsingCursor() {
146:
147: HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
148:
149: String s = "test";
150: CharArrayBuffer buffer = new CharArrayBuffer(16);
151: buffer.append(s);
152: ParserCursor cursor = new ParserCursor(0, s.length());
153:
154: NameValuePair param = parser.parseNameValuePair(buffer, cursor);
155: assertEquals("test", param.getName());
156: assertEquals(null, param.getValue());
157: assertEquals(s.length(), cursor.getPos());
158: assertTrue(cursor.atEnd());
159:
160: s = "test;";
161: buffer = new CharArrayBuffer(16);
162: buffer.append(s);
163: cursor = new ParserCursor(0, s.length());
164:
165: param = parser.parseNameValuePair(buffer, cursor);
166: assertEquals("test", param.getName());
167: assertEquals(null, param.getValue());
168: assertEquals(s.length(), cursor.getPos());
169: assertTrue(cursor.atEnd());
170:
171: s = "test ,12";
172: buffer = new CharArrayBuffer(16);
173: buffer.append(s);
174: cursor = new ParserCursor(0, s.length());
175:
176: param = parser.parseNameValuePair(buffer, cursor);
177: assertEquals("test", param.getName());
178: assertEquals(null, param.getValue());
179: assertEquals(s.length() - 2, cursor.getPos());
180: assertFalse(cursor.atEnd());
181:
182: s = "test=stuff";
183: buffer = new CharArrayBuffer(16);
184: buffer.append(s);
185: cursor = new ParserCursor(0, s.length());
186:
187: param = parser.parseNameValuePair(buffer, cursor);
188: assertEquals("test", param.getName());
189: assertEquals("stuff", param.getValue());
190: assertEquals(s.length(), cursor.getPos());
191: assertTrue(cursor.atEnd());
192:
193: s = " test = stuff ";
194: buffer = new CharArrayBuffer(16);
195: buffer.append(s);
196: cursor = new ParserCursor(0, s.length());
197:
198: param = parser.parseNameValuePair(buffer, cursor);
199: assertEquals("test", param.getName());
200: assertEquals("stuff", param.getValue());
201: assertEquals(s.length(), cursor.getPos());
202: assertTrue(cursor.atEnd());
203:
204: s = " test = stuff ;1234";
205: buffer = new CharArrayBuffer(16);
206: buffer.append(s);
207: cursor = new ParserCursor(0, s.length());
208:
209: param = parser.parseNameValuePair(buffer, cursor);
210: assertEquals("test", param.getName());
211: assertEquals("stuff", param.getValue());
212: assertEquals(s.length() - 4, cursor.getPos());
213: assertFalse(cursor.atEnd());
214:
215: s = "test = \"stuff\"";
216: buffer = new CharArrayBuffer(16);
217: buffer.append(s);
218: cursor = new ParserCursor(0, s.length());
219:
220: param = parser.parseNameValuePair(buffer, cursor);
221: assertEquals("test", param.getName());
222: assertEquals("stuff", param.getValue());
223:
224: s = "test = \" stuff\\\"\"";
225: buffer = new CharArrayBuffer(16);
226: buffer.append(s);
227: cursor = new ParserCursor(0, s.length());
228:
229: param = parser.parseNameValuePair(buffer, cursor);
230: assertEquals("test", param.getName());
231: assertEquals(" stuff\\\"", param.getValue());
232:
233: s = " test";
234: buffer = new CharArrayBuffer(16);
235: buffer.append(s);
236: cursor = new ParserCursor(0, s.length());
237:
238: param = parser.parseNameValuePair(buffer, cursor);
239: assertEquals("test", param.getName());
240: assertEquals(null, param.getValue());
241:
242: s = " ";
243: buffer = new CharArrayBuffer(16);
244: buffer.append(s);
245: cursor = new ParserCursor(0, s.length());
246:
247: param = parser.parseNameValuePair(buffer, cursor);
248: assertEquals("", param.getName());
249: assertEquals(null, param.getValue());
250:
251: s = " = stuff ";
252: buffer = new CharArrayBuffer(16);
253: buffer.append(s);
254: cursor = new ParserCursor(0, s.length());
255:
256: param = parser.parseNameValuePair(buffer, cursor);
257: assertEquals("", param.getName());
258: assertEquals("stuff", param.getValue());
259: }
260:
261: public void testNVParse() {
262: String s = "test";
263: NameValuePair param = BasicHeaderValueParser
264: .parseNameValuePair(s, null);
265: assertEquals("test", param.getName());
266: assertEquals(null, param.getValue());
267:
268: s = "test=stuff";
269: param = BasicHeaderValueParser.parseNameValuePair(s, null);
270: assertEquals("test", param.getName());
271: assertEquals("stuff", param.getValue());
272:
273: s = " test = stuff ";
274: param = BasicHeaderValueParser.parseNameValuePair(s, null);
275: assertEquals("test", param.getName());
276: assertEquals("stuff", param.getValue());
277:
278: s = "test = \"stuff\"";
279: param = BasicHeaderValueParser.parseNameValuePair(s, null);
280: assertEquals("test", param.getName());
281: assertEquals("stuff", param.getValue());
282:
283: s = "test = \" stuff\\\"\"";
284: param = BasicHeaderValueParser.parseNameValuePair(s, null);
285: assertEquals("test", param.getName());
286: assertEquals(" stuff\\\"", param.getValue());
287:
288: s = " test";
289: param = BasicHeaderValueParser.parseNameValuePair(s, null);
290: assertEquals("test", param.getName());
291: assertEquals(null, param.getValue());
292:
293: s = " ";
294: param = BasicHeaderValueParser.parseNameValuePair(s, null);
295: assertEquals("", param.getName());
296: assertEquals(null, param.getValue());
297:
298: s = " = stuff ";
299: param = BasicHeaderValueParser.parseNameValuePair(s, null);
300: assertEquals("", param.getName());
301: assertEquals("stuff", param.getValue());
302: }
303:
304: public void testNVParseAllWithCursor() {
305: HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
306:
307: String s = "test; test1 = stuff ; test2 = \"stuff; stuff\"; test3=\"stuff";
308: CharArrayBuffer buffer = new CharArrayBuffer(16);
309: buffer.append(s);
310: ParserCursor cursor = new ParserCursor(0, s.length());
311:
312: NameValuePair[] params = parser.parseParameters(buffer, cursor);
313: assertEquals("test", params[0].getName());
314: assertEquals(null, params[0].getValue());
315: assertEquals("test1", params[1].getName());
316: assertEquals("stuff", params[1].getValue());
317: assertEquals("test2", params[2].getName());
318: assertEquals("stuff; stuff", params[2].getValue());
319: assertEquals("test3", params[3].getName());
320: assertEquals("\"stuff", params[3].getValue());
321: assertEquals(s.length(), cursor.getPos());
322: assertTrue(cursor.atEnd());
323:
324: s = "test; test1 = stuff ; test2 = \"stuff; stuff\"; test3=\"stuff\",123";
325: buffer = new CharArrayBuffer(16);
326: buffer.append(s);
327: cursor = new ParserCursor(0, s.length());
328:
329: params = parser.parseParameters(buffer, cursor);
330: assertEquals("test", params[0].getName());
331: assertEquals(null, params[0].getValue());
332: assertEquals("test1", params[1].getName());
333: assertEquals("stuff", params[1].getValue());
334: assertEquals("test2", params[2].getName());
335: assertEquals("stuff; stuff", params[2].getValue());
336: assertEquals("test3", params[3].getName());
337: assertEquals("stuff", params[3].getValue());
338: assertEquals(s.length() - 3, cursor.getPos());
339: assertFalse(cursor.atEnd());
340:
341: s = " ";
342: buffer = new CharArrayBuffer(16);
343: buffer.append(s);
344: cursor = new ParserCursor(0, s.length());
345: params = parser.parseParameters(buffer, cursor);
346: assertEquals(0, params.length);
347: }
348:
349: public void testNVParseAll() {
350: String s = "test; test1 = stuff ; test2 = \"stuff; stuff\"; test3=\"stuff";
351: NameValuePair[] params = BasicHeaderValueParser
352: .parseParameters(s, null);
353: assertEquals("test", params[0].getName());
354: assertEquals(null, params[0].getValue());
355: assertEquals("test1", params[1].getName());
356: assertEquals("stuff", params[1].getValue());
357: assertEquals("test2", params[2].getName());
358: assertEquals("stuff; stuff", params[2].getValue());
359: assertEquals("test3", params[3].getName());
360: assertEquals("\"stuff", params[3].getValue());
361:
362: s = " ";
363: params = BasicHeaderValueParser.parseParameters(s, null);
364: assertEquals(0, params.length);
365: }
366:
367: public void testNVParseEscaped() {
368: String s = "test1 = \"\\\"stuff\\\"\"; test2= \"\\\\\"; test3 = \"stuff; stuff\"";
369: NameValuePair[] params = BasicHeaderValueParser
370: .parseParameters(s, null);
371: assertEquals(3, params.length);
372: assertEquals("test1", params[0].getName());
373: assertEquals("\\\"stuff\\\"", params[0].getValue());
374: assertEquals("test2", params[1].getName());
375: assertEquals("\\\\", params[1].getValue());
376: assertEquals("test3", params[2].getName());
377: assertEquals("stuff; stuff", params[2].getValue());
378: }
379:
380: }
|