View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.message;
28  
29  import org.apache.http.HeaderElement;
30  import org.apache.http.NameValuePair;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  /**
35   * Tests for header value formatting.
36   *
37   *
38   */
39  public class TestBasicHeaderValueFormatter {
40  
41      @Test
42      public void testNVPFormatting() throws Exception {
43          final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
44          final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
45          final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
46          final NameValuePair param4 = new BasicNameValuePair("param", "quote marks (\") must be escaped");
47          final NameValuePair param5 = new BasicNameValuePair("param", "back slash (\\) must be escaped too");
48          final NameValuePair param6 = new BasicNameValuePair("param", "values with\tblanks must always be quoted");
49          final NameValuePair param7 = new BasicNameValuePair("param", null);
50  
51  
52          Assert.assertEquals("param=regular_stuff",
53                       BasicHeaderValueFormatter.formatNameValuePair
54                       (param1, false, null));
55          Assert.assertEquals("param=\"this\\\\that\"",
56                       BasicHeaderValueFormatter.formatNameValuePair
57                       (param2, false, null));
58          Assert.assertEquals("param=\"this,that\"",
59                       BasicHeaderValueFormatter.formatNameValuePair
60                       (param3, false, null));
61          Assert.assertEquals("param=\"quote marks (\\\") must be escaped\"",
62                       BasicHeaderValueFormatter.formatNameValuePair
63                       (param4, false, null));
64          Assert.assertEquals("param=\"back slash (\\\\) must be escaped too\"",
65                       BasicHeaderValueFormatter.formatNameValuePair
66                       (param5, false, null));
67          Assert.assertEquals("param=\"values with\tblanks must always be quoted\"",
68                       BasicHeaderValueFormatter.formatNameValuePair
69                       (param6, false, null));
70          Assert.assertEquals("param", BasicHeaderValueFormatter.formatNameValuePair
71                       (param7, false, null));
72  
73          Assert.assertEquals("param=\"regular_stuff\"",
74                       BasicHeaderValueFormatter.formatNameValuePair
75                       (param1, true, null));
76          Assert.assertEquals("param=\"this\\\\that\"",
77                       BasicHeaderValueFormatter.formatNameValuePair
78                       (param2, true, null));
79          Assert.assertEquals("param=\"this,that\"",
80                       BasicHeaderValueFormatter.formatNameValuePair
81                       (param3, true, null));
82          Assert.assertEquals("param=\"quote marks (\\\") must be escaped\"",
83                       BasicHeaderValueFormatter.formatNameValuePair
84                       (param4, true, null));
85          Assert.assertEquals("param=\"back slash (\\\\) must be escaped too\"",
86                       BasicHeaderValueFormatter.formatNameValuePair
87                       (param5, true, null));
88          Assert.assertEquals("param=\"values with\tblanks must always be quoted\"",
89                       BasicHeaderValueFormatter.formatNameValuePair
90                       (param6, true, null));
91          Assert.assertEquals("param",
92                       BasicHeaderValueFormatter.formatNameValuePair
93                       (param7, false, null));
94      }
95  
96  
97  
98      @Test
99      public void testParamsFormatting() throws Exception {
100         final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
101         final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
102         final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
103         final NameValuePair[] params = new NameValuePair[] {param1, param2, param3};
104         Assert.assertEquals("param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"",
105                      BasicHeaderValueFormatter.formatParameters(params, false, null));
106         Assert.assertEquals("param=\"regular_stuff\"; param=\"this\\\\that\"; param=\"this,that\"",
107                      BasicHeaderValueFormatter.formatParameters(params, true, null));
108     }
109 
110 
111 
112     @Test
113     public void testHEFormatting() throws Exception {
114         final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
115         final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
116         final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
117         final NameValuePair param4 = new BasicNameValuePair("param", null);
118         final NameValuePair[] params = new NameValuePair[] {param1, param2, param3, param4};
119         final HeaderElement element = new BasicHeaderElement("name", "value", params);
120 
121         Assert.assertEquals("name=value; param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"; param",
122                      BasicHeaderValueFormatter.formatHeaderElement(element, false, null));
123     }
124 
125     @Test
126     public void testElementsFormatting() throws Exception {
127         final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
128         final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
129         final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
130         final NameValuePair param4 = new BasicNameValuePair("param", null);
131         final HeaderElement element1 = new BasicHeaderElement("name1", "value1", new NameValuePair[] {param1});
132         final HeaderElement element2 = new BasicHeaderElement("name2", "value2", new NameValuePair[] {param2});
133         final HeaderElement element3 = new BasicHeaderElement("name3", "value3", new NameValuePair[] {param3});
134         final HeaderElement element4 = new BasicHeaderElement("name4", "value4", new NameValuePair[] {param4});
135         final HeaderElement element5 = new BasicHeaderElement("name5", null);
136         final HeaderElement[] elements = new HeaderElement[] {element1, element2, element3, element4, element5};
137 
138         Assert.assertEquals
139             ("name1=value1; param=regular_stuff, name2=value2; " +
140              "param=\"this\\\\that\", name3=value3; param=\"this,that\", " +
141              "name4=value4; param, name5",
142              BasicHeaderValueFormatter.formatElements(elements, false, null));
143     }
144 
145 
146     @Test
147     public void testInvalidHEArguments() throws Exception {
148         try {
149             BasicHeaderValueFormatter.formatHeaderElement
150                 ((HeaderElement) null, false,
151                  BasicHeaderValueFormatter.INSTANCE);
152             Assert.fail("IllegalArgumentException should habe been thrown");
153         } catch (final IllegalArgumentException ex) {
154             // expected
155         }
156 
157         try {
158             BasicHeaderValueFormatter.formatElements
159                 ((HeaderElement[]) null, false,
160                  BasicHeaderValueFormatter.INSTANCE);
161             Assert.fail("IllegalArgumentException should habe been thrown");
162         } catch (final IllegalArgumentException ex) {
163             // expected
164         }
165     }
166 
167 
168     @Test
169     public void testInvalidNVArguments() throws Exception {
170 
171         try {
172             BasicHeaderValueFormatter.formatNameValuePair
173                 ((NameValuePair) null, true, null);
174             Assert.fail("IllegalArgumentException should habe been thrown");
175         } catch (final IllegalArgumentException ex) {
176             // expected
177         }
178 
179         try {
180             BasicHeaderValueFormatter.formatParameters
181                 ((NameValuePair[]) null, true,
182                  BasicHeaderValueFormatter.INSTANCE);
183             Assert.fail("IllegalArgumentException should habe been thrown");
184         } catch (final IllegalArgumentException ex) {
185             // expected
186         }
187     }
188 
189 
190 }