001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java $
003: * $Revision: 571954 $
004: * $Date: 2007-09-02 13:05:21 +0200 (Sun, 02 Sep 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:
042: /**
043: * Tests for header value formatting.
044: *
045: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
046: * @author and others
047: *
048: * @version $Revision: 571954 $
049: */
050: public class TestBasicHeaderValueFormatter extends TestCase {
051:
052: // ------------------------------------------------------------ Constructor
053: public TestBasicHeaderValueFormatter(String testName) {
054: super (testName);
055: }
056:
057: // ------------------------------------------------------------------- Main
058: public static void main(String args[]) {
059: String[] testCaseName = { TestBasicHeaderValueFormatter.class
060: .getName() };
061: junit.textui.TestRunner.main(testCaseName);
062: }
063:
064: // ------------------------------------------------------- TestCase Methods
065:
066: public static Test suite() {
067: return new TestSuite(TestBasicHeaderValueFormatter.class);
068: }
069:
070: public void testNVPFormatting() throws Exception {
071: NameValuePair param1 = new BasicNameValuePair("param",
072: "regular_stuff");
073: NameValuePair param2 = new BasicNameValuePair("param",
074: "this\\that");
075: NameValuePair param3 = new BasicNameValuePair("param",
076: "this,that");
077: NameValuePair param4 = new BasicNameValuePair("param",
078: "quote marks (\") must be escaped");
079: NameValuePair param5 = new BasicNameValuePair("param",
080: "back slash (\\) must be escaped too");
081: NameValuePair param6 = new BasicNameValuePair("param",
082: "values with\tblanks must always be quoted");
083: NameValuePair param7 = new BasicNameValuePair("param", null);
084:
085: assertEquals("param=regular_stuff", BasicHeaderValueFormatter
086: .formatNameValuePair(param1, false, null));
087: assertEquals("param=\"this\\\\that\"",
088: BasicHeaderValueFormatter.formatNameValuePair(param2,
089: false, null));
090: assertEquals("param=\"this,that\"", BasicHeaderValueFormatter
091: .formatNameValuePair(param3, false, null));
092: assertEquals("param=\"quote marks (\\\") must be escaped\"",
093: BasicHeaderValueFormatter.formatNameValuePair(param4,
094: false, null));
095: assertEquals("param=\"back slash (\\\\) must be escaped too\"",
096: BasicHeaderValueFormatter.formatNameValuePair(param5,
097: false, null));
098: assertEquals(
099: "param=\"values with\tblanks must always be quoted\"",
100: BasicHeaderValueFormatter.formatNameValuePair(param6,
101: false, null));
102: assertEquals("param", BasicHeaderValueFormatter
103: .formatNameValuePair(param7, false, null));
104:
105: assertEquals("param=\"regular_stuff\"",
106: BasicHeaderValueFormatter.formatNameValuePair(param1,
107: true, null));
108: assertEquals("param=\"this\\\\that\"",
109: BasicHeaderValueFormatter.formatNameValuePair(param2,
110: true, null));
111: assertEquals("param=\"this,that\"", BasicHeaderValueFormatter
112: .formatNameValuePair(param3, true, null));
113: assertEquals("param=\"quote marks (\\\") must be escaped\"",
114: BasicHeaderValueFormatter.formatNameValuePair(param4,
115: true, null));
116: assertEquals("param=\"back slash (\\\\) must be escaped too\"",
117: BasicHeaderValueFormatter.formatNameValuePair(param5,
118: true, null));
119: assertEquals(
120: "param=\"values with\tblanks must always be quoted\"",
121: BasicHeaderValueFormatter.formatNameValuePair(param6,
122: true, null));
123: assertEquals("param", BasicHeaderValueFormatter
124: .formatNameValuePair(param7, false, null));
125: }
126:
127: public void testParamsFormatting() throws Exception {
128: NameValuePair param1 = new BasicNameValuePair("param",
129: "regular_stuff");
130: NameValuePair param2 = new BasicNameValuePair("param",
131: "this\\that");
132: NameValuePair param3 = new BasicNameValuePair("param",
133: "this,that");
134: NameValuePair[] params = new NameValuePair[] { param1, param2,
135: param3 };
136: assertEquals(
137: "param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"",
138: BasicHeaderValueFormatter.formatParameters(params,
139: false, null));
140: assertEquals(
141: "param=\"regular_stuff\"; param=\"this\\\\that\"; param=\"this,that\"",
142: BasicHeaderValueFormatter.formatParameters(params,
143: true, null));
144: }
145:
146: public void testHEFormatting() throws Exception {
147: NameValuePair param1 = new BasicNameValuePair("param",
148: "regular_stuff");
149: NameValuePair param2 = new BasicNameValuePair("param",
150: "this\\that");
151: NameValuePair param3 = new BasicNameValuePair("param",
152: "this,that");
153: NameValuePair param4 = new BasicNameValuePair("param", null);
154: NameValuePair[] params = new NameValuePair[] { param1, param2,
155: param3, param4 };
156: HeaderElement element = new BasicHeaderElement("name", "value",
157: params);
158:
159: assertEquals(
160: "name=value; param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"; param",
161: BasicHeaderValueFormatter.formatHeaderElement(element,
162: false, null));
163: }
164:
165: public void testElementsFormatting() throws Exception {
166: NameValuePair param1 = new BasicNameValuePair("param",
167: "regular_stuff");
168: NameValuePair param2 = new BasicNameValuePair("param",
169: "this\\that");
170: NameValuePair param3 = new BasicNameValuePair("param",
171: "this,that");
172: NameValuePair param4 = new BasicNameValuePair("param", null);
173: HeaderElement element1 = new BasicHeaderElement("name1",
174: "value1", new NameValuePair[] { param1 });
175: HeaderElement element2 = new BasicHeaderElement("name2",
176: "value2", new NameValuePair[] { param2 });
177: HeaderElement element3 = new BasicHeaderElement("name3",
178: "value3", new NameValuePair[] { param3 });
179: HeaderElement element4 = new BasicHeaderElement("name4",
180: "value4", new NameValuePair[] { param4 });
181: HeaderElement element5 = new BasicHeaderElement("name5", null);
182: HeaderElement[] elements = new HeaderElement[] { element1,
183: element2, element3, element4, element5 };
184:
185: assertEquals(
186: "name1=value1; param=regular_stuff, name2=value2; "
187: + "param=\"this\\\\that\", name3=value3; param=\"this,that\", "
188: + "name4=value4; param, name5",
189: BasicHeaderValueFormatter.formatElements(elements,
190: false, null));
191: }
192:
193: public void testInvalidHEArguments() throws Exception {
194: try {
195: BasicHeaderValueFormatter.formatHeaderElement(
196: (HeaderElement) null, false,
197: BasicHeaderValueFormatter.DEFAULT);
198: fail("IllegalArgumentException should habe been thrown");
199: } catch (IllegalArgumentException ex) {
200: // expected
201: }
202:
203: try {
204: BasicHeaderValueFormatter.formatElements(
205: (HeaderElement[]) null, false,
206: BasicHeaderValueFormatter.DEFAULT);
207: fail("IllegalArgumentException should habe been thrown");
208: } catch (IllegalArgumentException ex) {
209: // expected
210: }
211: }
212:
213: public void testInvalidNVArguments() throws Exception {
214:
215: try {
216: BasicHeaderValueFormatter.formatNameValuePair(
217: (NameValuePair) null, true, null);
218: fail("IllegalArgumentException should habe been thrown");
219: } catch (IllegalArgumentException ex) {
220: // expected
221: }
222:
223: try {
224: BasicHeaderValueFormatter.formatParameters(
225: (NameValuePair[]) null, true,
226: BasicHeaderValueFormatter.DEFAULT);
227: fail("IllegalArgumentException should habe been thrown");
228: } catch (IllegalArgumentException ex) {
229: // expected
230: }
231: }
232:
233: }
|