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  
28  package org.apache.http.message;
29  
30  import org.apache.http.util.CharArrayBuffer;
31  import org.junit.Assert;
32  import org.junit.Before;
33  import org.junit.Test;
34  
35  public class TestTokenParser {
36  
37      private TokenParser parser;
38  
39      @Before
40      public void setUp() throws Exception {
41          parser = new TokenParser();
42      }
43  
44      private static CharArrayBuffer createBuffer(final String value) {
45          if (value == null) {
46              return null;
47          }
48          final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
49          buffer.append(value);
50          return buffer;
51      }
52  
53      @Test
54      public void testBasicTokenParsing() throws Exception {
55          final String s = "   raw: \" some stuff \"";
56          final CharArrayBuffer raw = createBuffer(s);
57          final ParserCursor cursor = new ParserCursor(0, s.length());
58  
59          parser.skipWhiteSpace(raw, cursor);
60  
61          Assert.assertFalse(cursor.atEnd());
62          Assert.assertEquals(3, cursor.getPos());
63  
64          final StringBuilder strbuf1 = new StringBuilder();
65          parser.copyContent(raw, cursor, TokenParser.INIT_BITSET(':'), strbuf1);
66  
67          Assert.assertFalse(cursor.atEnd());
68          Assert.assertEquals(6, cursor.getPos());
69          Assert.assertEquals("raw", strbuf1.toString());
70          Assert.assertEquals(':', raw.charAt(cursor.getPos()));
71          cursor.updatePos(cursor.getPos() + 1);
72  
73          parser.skipWhiteSpace(raw, cursor);
74  
75          Assert.assertFalse(cursor.atEnd());
76          Assert.assertEquals(8, cursor.getPos());
77  
78          final StringBuilder strbuf2 = new StringBuilder();
79          parser.copyQuotedContent(raw, cursor, strbuf2);
80  
81          Assert.assertTrue(cursor.atEnd());
82          Assert.assertEquals(" some stuff ", strbuf2.toString());
83  
84          parser.copyQuotedContent(raw, cursor, strbuf2);
85          Assert.assertTrue(cursor.atEnd());
86  
87          parser.skipWhiteSpace(raw, cursor);
88          Assert.assertTrue(cursor.atEnd());
89      }
90  
91      @Test
92      public void testTokenParsingWithQuotedPairs() throws Exception {
93          final String s = "raw: \"\\\"some\\stuff\\\\\"";
94          final CharArrayBuffer raw = createBuffer(s);
95          final ParserCursor cursor = new ParserCursor(0, s.length());
96  
97          parser.skipWhiteSpace(raw, cursor);
98  
99          Assert.assertFalse(cursor.atEnd());
100         Assert.assertEquals(0, cursor.getPos());
101 
102         final StringBuilder strbuf1 = new StringBuilder();
103         parser.copyContent(raw, cursor, TokenParser.INIT_BITSET(':'), strbuf1);
104 
105         Assert.assertFalse(cursor.atEnd());
106         Assert.assertEquals("raw", strbuf1.toString());
107         Assert.assertEquals(':', raw.charAt(cursor.getPos()));
108         cursor.updatePos(cursor.getPos() + 1);
109 
110         parser.skipWhiteSpace(raw, cursor);
111 
112         Assert.assertFalse(cursor.atEnd());
113 
114         final StringBuilder strbuf2 = new StringBuilder();
115         parser.copyQuotedContent(raw, cursor, strbuf2);
116 
117         Assert.assertTrue(cursor.atEnd());
118         Assert.assertEquals("\"some\\stuff\\", strbuf2.toString());
119     }
120 
121     @Test
122     public void testTokenParsingIncompleteQuote() throws Exception {
123         final String s = "\"stuff and more stuff  ";
124         final CharArrayBuffer raw = createBuffer(s);
125         final ParserCursor cursor = new ParserCursor(0, s.length());
126         final StringBuilder strbuf1 = new StringBuilder();
127         parser.copyQuotedContent(raw, cursor, strbuf1);
128         Assert.assertEquals("stuff and more stuff  ", strbuf1.toString());
129     }
130 
131     @Test
132     public void testTokenParsingTokensWithUnquotedBlanks() throws Exception {
133         final String s = "  stuff and   \tsome\tmore  stuff  ;";
134         final CharArrayBuffer raw = createBuffer(s);
135         final ParserCursor cursor = new ParserCursor(0, s.length());
136         final String result = parser.parseToken(raw, cursor, TokenParser.INIT_BITSET(';'));
137         Assert.assertEquals("stuff and some more stuff", result);
138     }
139 
140     @Test
141     public void testTokenParsingMixedValuesAndQuotedValues() throws Exception {
142         final String s = "  stuff and    \" some more \"   \"stuff  ;";
143         final CharArrayBuffer raw = createBuffer(s);
144         final ParserCursor cursor = new ParserCursor(0, s.length());
145         final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
146         Assert.assertEquals("stuff and  some more  stuff  ;", result);
147     }
148 
149     @Test
150     public void testTokenParsingMixedValuesAndQuotedValues2() throws Exception {
151         final String s = "stuff\"more\"stuff;";
152         final CharArrayBuffer raw = createBuffer(s);
153         final ParserCursor cursor = new ParserCursor(0, s.length());
154         final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
155         Assert.assertEquals("stuffmorestuff", result);
156     }
157 
158     @Test
159     public void testTokenParsingEscapedQuotes() throws Exception {
160         final String s = "stuff\"\\\"more\\\"\"stuff;";
161         final CharArrayBuffer raw = createBuffer(s);
162         final ParserCursor cursor = new ParserCursor(0, s.length());
163         final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
164         Assert.assertEquals("stuff\"more\"stuff", result);
165     }
166 
167     @Test
168     public void testTokenParsingEscapedDelimiter() throws Exception {
169         final String s = "stuff\"\\\"more\\\";\"stuff;";
170         final CharArrayBuffer raw = createBuffer(s);
171         final ParserCursor cursor = new ParserCursor(0, s.length());
172         final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
173         Assert.assertEquals("stuff\"more\";stuff", result);
174     }
175 
176     @Test
177     public void testTokenParsingEscapedSlash() throws Exception {
178         final String s = "stuff\"\\\"more\\\";\\\\\"stuff;";
179         final CharArrayBuffer raw = createBuffer(s);
180         final ParserCursor cursor = new ParserCursor(0, s.length());
181         final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
182         Assert.assertEquals("stuff\"more\";\\stuff", result);
183     }
184 
185     @Test
186     public void testTokenParsingSlashOutsideQuotes() throws Exception {
187         final String s = "stuff\\; more stuff;";
188         final CharArrayBuffer raw = createBuffer(s);
189         final ParserCursor cursor = new ParserCursor(0, s.length());
190         final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
191         Assert.assertEquals("stuff\\", result);
192     }
193 }