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.hc.core5.http.message;
28  
29  import org.apache.hc.core5.http.HeaderElement;
30  import org.apache.hc.core5.http.NameValuePair;
31  import org.apache.hc.core5.util.CharArrayBuffer;
32  import org.junit.Assert;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  /**
37   * Tests for header value formatting.
38   *
39   *
40   */
41  public class TestBasicHeaderValueFormatter {
42  
43      private BasicHeaderValueFormatter formatter;
44  
45      @Before
46      public void setup() {
47          this.formatter = BasicHeaderValueFormatter.INSTANCE;
48      }
49  
50      @Test
51      public void testNVPFormatting() throws Exception {
52          final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
53          final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
54          final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
55          final NameValuePair param4 = new BasicNameValuePair("param", "quote marks (\") must be escaped");
56          final NameValuePair param5 = new BasicNameValuePair("param", "back slash (\\) must be escaped too");
57          final NameValuePair param6 = new BasicNameValuePair("param", "values with\tblanks must always be quoted");
58          final NameValuePair param7 = new BasicNameValuePair("param", null);
59  
60          final CharArrayBuffer buf = new CharArrayBuffer(64);
61  
62          buf.clear();
63          this.formatter.formatNameValuePair(buf, param1, false);
64          Assert.assertEquals("param=regular_stuff", buf.toString());
65          buf.clear();
66          this.formatter.formatNameValuePair(buf, param2, false);
67          Assert.assertEquals("param=\"this\\\\that\"", buf.toString());
68          buf.clear();
69          this.formatter.formatNameValuePair(buf, param3, false);
70          Assert.assertEquals("param=\"this,that\"", buf.toString());
71          buf.clear();
72          this.formatter.formatNameValuePair(buf, param4, false);
73          Assert.assertEquals("param=\"quote marks (\\\") must be escaped\"", buf.toString());
74          buf.clear();
75          this.formatter.formatNameValuePair(buf, param5, false);
76          Assert.assertEquals("param=\"back slash (\\\\) must be escaped too\"", buf.toString());
77          buf.clear();
78          this.formatter.formatNameValuePair(buf, param6, false);
79          Assert.assertEquals("param=\"values with\tblanks must always be quoted\"", buf.toString());
80          buf.clear();
81          this.formatter.formatNameValuePair(buf, param7, false);
82          Assert.assertEquals("param", buf.toString());
83  
84          buf.clear();
85          this.formatter.formatNameValuePair(buf, param1, true);
86          Assert.assertEquals("param=\"regular_stuff\"", buf.toString());
87          buf.clear();
88          this.formatter.formatNameValuePair(buf, param2, true);
89          Assert.assertEquals("param=\"this\\\\that\"", buf.toString());
90          buf.clear();
91          this.formatter.formatNameValuePair(buf, param3, true);
92          Assert.assertEquals("param=\"this,that\"", buf.toString());
93          buf.clear();
94          this.formatter.formatNameValuePair(buf, param4, true);
95          Assert.assertEquals("param=\"quote marks (\\\") must be escaped\"", buf.toString());
96          buf.clear();
97          this.formatter.formatNameValuePair(buf, param5, true);
98          Assert.assertEquals("param=\"back slash (\\\\) must be escaped too\"", buf.toString());
99          buf.clear();
100         this.formatter.formatNameValuePair(buf, param6, true);
101         Assert.assertEquals("param=\"values with\tblanks must always be quoted\"", buf.toString());
102         buf.clear();
103         this.formatter.formatNameValuePair(buf, param7, true);
104         Assert.assertEquals("param", buf.toString());
105     }
106 
107     @Test
108     public void testParamsFormatting() throws Exception {
109         final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
110         final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
111         final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
112         final NameValuePair[] params = new NameValuePair[] {param1, param2, param3};
113 
114         final CharArrayBuffer buf = new CharArrayBuffer(64);
115 
116         buf.clear();
117         this.formatter.formatParameters(buf, params, false);
118         Assert.assertEquals("param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"",
119                 buf.toString());
120         buf.clear();
121         this.formatter.formatParameters(buf, params, true);
122         Assert.assertEquals("param=\"regular_stuff\"; param=\"this\\\\that\"; param=\"this,that\"",
123                 buf.toString());
124     }
125 
126     @Test
127     public void testHEFormatting() throws Exception {
128         final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
129         final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
130         final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
131         final NameValuePair param4 = new BasicNameValuePair("param", null);
132         final NameValuePair[] params = new NameValuePair[] {param1, param2, param3, param4};
133         final HeaderElement element = new BasicHeaderElement("name", "value", params);
134 
135         final CharArrayBuffer buf = new CharArrayBuffer(64);
136 
137         this.formatter.formatHeaderElement(buf, element, false);
138         Assert.assertEquals("name=value; param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"; param",
139                 buf.toString());
140     }
141 
142     @Test
143     public void testElementsFormatting() throws Exception {
144         final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
145         final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
146         final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
147         final NameValuePair param4 = new BasicNameValuePair("param", null);
148         final HeaderElement element1 = new BasicHeaderElement("name1", "value1", new NameValuePair[] {param1});
149         final HeaderElement element2 = new BasicHeaderElement("name2", "value2", new NameValuePair[] {param2});
150         final HeaderElement element3 = new BasicHeaderElement("name3", "value3", new NameValuePair[] {param3});
151         final HeaderElement element4 = new BasicHeaderElement("name4", "value4", new NameValuePair[] {param4});
152         final HeaderElement element5 = new BasicHeaderElement("name5", null);
153         final HeaderElement[] elements = new HeaderElement[] {element1, element2, element3, element4, element5};
154 
155         final CharArrayBuffer buf = new CharArrayBuffer(64);
156 
157         this.formatter.formatElements(buf, elements, false);
158         Assert.assertEquals("name1=value1; param=regular_stuff, name2=value2; " +
159              "param=\"this\\\\that\", name3=value3; param=\"this,that\", " +
160              "name4=value4; param, name5", buf.toString());
161     }
162 
163 
164     @Test
165     public void testInvalidArguments() throws Exception {
166         final CharArrayBuffer buf = new CharArrayBuffer(64);
167         final NameValuePair param = new BasicNameValuePair("param", "regular_stuff");
168         final NameValuePair[] params = new NameValuePair[] {param};
169         final HeaderElement element = new BasicHeaderElement("name1", "value1", null);
170         final HeaderElement[] elements = new HeaderElement[] {element};
171 
172         try {
173             this.formatter.formatNameValuePair(null, param, false);
174             Assert.fail("IllegalArgumentException should habe been thrown");
175         } catch (final IllegalArgumentException ex) {
176             // expected
177         }
178         try {
179             this.formatter.formatNameValuePair(buf, null, false);
180             Assert.fail("IllegalArgumentException should habe been thrown");
181         } catch (final IllegalArgumentException ex) {
182             // expected
183         }
184         try {
185             this.formatter.formatParameters(null, params, false);
186             Assert.fail("IllegalArgumentException should habe been thrown");
187         } catch (final IllegalArgumentException ex) {
188             // expected
189         }
190         try {
191             this.formatter.formatParameters(buf, null, false);
192             Assert.fail("IllegalArgumentException should habe been thrown");
193         } catch (final IllegalArgumentException ex) {
194             // expected
195         }
196 
197         try {
198             this.formatter.formatHeaderElement(null, element, false);
199             Assert.fail("IllegalArgumentException should habe been thrown");
200         } catch (final IllegalArgumentException ex) {
201             // expected
202         }
203         try {
204             this.formatter.formatHeaderElement(buf, null, false);
205             Assert.fail("IllegalArgumentException should habe been thrown");
206         } catch (final IllegalArgumentException ex) {
207             // expected
208         }
209         try {
210             this.formatter.formatElements(null, elements, false);
211             Assert.fail("IllegalArgumentException should habe been thrown");
212         } catch (final IllegalArgumentException ex) {
213             // expected
214         }
215         try {
216             this.formatter.formatElements(buf, null, false);
217             Assert.fail("IllegalArgumentException should habe been thrown");
218         } catch (final IllegalArgumentException ex) {
219             // expected
220         }
221     }
222 
223 }