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.impl.cookie;
29  
30  import org.apache.http.HeaderElement;
31  import org.apache.http.NameValuePair;
32  import org.apache.http.message.ParserCursor;
33  import org.apache.http.util.CharArrayBuffer;
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  /**
38   * Unit tests for {@link NetscapeDraftHeaderParser}.
39   */
40  public class TestNetscapeDraftHeaderParser {
41  
42      @Test
43      public void testNetscapeCookieParsing() throws Exception {
44          final NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
45  
46          String s = "name  = value; test; test1 =  stuff,with,commas   ;" +
47                  " test2 =  \"stuff, stuff\"; test3=\"stuff";
48          CharArrayBuffer buffer = new CharArrayBuffer(16);
49          buffer.append(s);
50          ParserCursor cursor = new ParserCursor(0, s.length());
51  
52          HeaderElement he = parser.parseHeader(buffer, cursor);
53          Assert.assertEquals("name", he.getName());
54          Assert.assertEquals("value", he.getValue());
55          final NameValuePair[] params = he.getParameters();
56          Assert.assertEquals("test", params[0].getName());
57          Assert.assertEquals(null, params[0].getValue());
58          Assert.assertEquals("test1", params[1].getName());
59          Assert.assertEquals("stuff,with,commas", params[1].getValue());
60          Assert.assertEquals("test2", params[2].getName());
61          Assert.assertEquals("\"stuff, stuff\"", params[2].getValue());
62          Assert.assertEquals("test3", params[3].getName());
63          Assert.assertEquals("\"stuff", params[3].getValue());
64          Assert.assertEquals(s.length(), cursor.getPos());
65          Assert.assertTrue(cursor.atEnd());
66  
67          s = "  ";
68          buffer = new CharArrayBuffer(16);
69          buffer.append(s);
70          cursor = new ParserCursor(0, s.length());
71          he = parser.parseHeader(buffer, cursor);
72          Assert.assertEquals("", he.getName());
73          Assert.assertEquals(null, he.getValue());
74      }
75  
76  }