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 java.util.Arrays;
31  import java.util.List;
32  
33  import org.apache.http.Header;
34  import org.apache.http.cookie.ClientCookie;
35  import org.apache.http.cookie.CommonCookieAttributeHandler;
36  import org.apache.http.cookie.Cookie;
37  import org.apache.http.cookie.CookieOrigin;
38  import org.apache.http.cookie.MalformedCookieException;
39  import org.apache.http.cookie.SetCookie;
40  import org.apache.http.message.BasicHeader;
41  import org.junit.Assert;
42  import org.junit.Test;
43  import org.mockito.Mockito;
44  
45  public class TestRFC6265CookieSpec {
46  
47      @Test
48      public void testParseCookieBasics() throws Exception {
49          final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
50          Mockito.when(h1.getAttributeName()).thenReturn("this");
51          final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
52          Mockito.when(h2.getAttributeName()).thenReturn("that");
53  
54          final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);
55  
56          final Header header = new BasicHeader("Set-Cookie", "name = value ; this = stuff;");
57          final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
58          final List<Cookie> cookies = cookiespec.parse(header, origin);
59  
60          Assert.assertEquals(1, cookies.size());
61          final Cookie cookie = cookies.get(0);
62          Assert.assertEquals("name", cookie.getName());
63          Assert.assertEquals("value", cookie.getValue());
64          Assert.assertEquals("/path", cookie.getPath());
65          Assert.assertEquals("host", cookie.getDomain());
66          Assert.assertTrue(cookie instanceof ClientCookie);
67          final ClientCookie clientCookie = (ClientCookie) cookie;
68          Assert.assertEquals("stuff", clientCookie.getAttribute("this"));
69          Assert.assertEquals(null, clientCookie.getAttribute("that"));
70  
71          Mockito.verify(h1).parse(Mockito.<SetCookie>any(), Mockito.eq("stuff"));
72          Mockito.verify(h2, Mockito.never()).parse(Mockito.<SetCookie>any(), Mockito.anyString());
73      }
74  
75      @Test
76      public void testParseCookieQuotedValue() throws Exception {
77          final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
78  
79          final Header header = new BasicHeader("Set-Cookie", "name = \" one, two, three; four \" ; this = stuff;");
80          final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
81          final List<Cookie> cookies = cookiespec.parse(header, origin);
82  
83          Assert.assertEquals(1, cookies.size());
84          final Cookie cookie = cookies.get(0);
85          Assert.assertEquals("name", cookie.getName());
86          Assert.assertEquals(" one, two, three; four ", cookie.getValue());
87          Assert.assertTrue(cookie instanceof ClientCookie);
88          final ClientCookie clientCookie = (ClientCookie) cookie;
89          Assert.assertEquals("stuff", clientCookie.getAttribute("this"));
90      }
91  
92      @Test(expected = MalformedCookieException.class)
93      public void testParseCookieWrongHeader() throws Exception {
94          final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
95  
96          final Header header = new BasicHeader("Set-Cookie2", "blah");
97          final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
98          cookiespec.parse(header, origin);
99      }
100 
101     @Test
102     public void testParseCookieMissingName() throws Exception {
103         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
104 
105         final Header header = new BasicHeader("Set-Cookie", "=blah ; this = stuff;");
106         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
107         final List<Cookie> cookies = cookiespec.parse(header, origin);
108         Assert.assertEquals(0, cookies.size());
109     }
110 
111     @Test
112     public void testParseCookieMissingValue1() throws Exception {
113         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
114 
115         final Header header = new BasicHeader("Set-Cookie", "blah");
116         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
117         final List<Cookie> cookies = cookiespec.parse(header, origin);
118         Assert.assertEquals(0, cookies.size());
119     }
120 
121     @Test(expected = MalformedCookieException.class)
122     public void testParseCookieMissingValue2() throws Exception {
123         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
124 
125         final Header header = new BasicHeader("Set-Cookie", "blah;");
126         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
127         cookiespec.parse(header, origin);
128     }
129 
130     @Test
131     public void testParseCookieEmptyValue() throws Exception {
132         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
133 
134         final Header header = new BasicHeader("Set-Cookie", "blah=;");
135         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
136         final List<Cookie> cookies = cookiespec.parse(header, origin);
137         Assert.assertEquals(1, cookies.size());
138         final Cookie cookie = cookies.get(0);
139         Assert.assertEquals("blah", cookie.getName());
140         Assert.assertEquals("", cookie.getValue());
141     }
142 
143     @Test
144     public void testParseCookieWithAttributes() throws Exception {
145         final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
146         Mockito.when(h1.getAttributeName()).thenReturn("this");
147         final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
148         Mockito.when(h2.getAttributeName()).thenReturn("that");
149 
150         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);
151 
152         final Header header = new BasicHeader("Set-Cookie", "name = value ; p1 = v ; p2 = v,0; p3 ; p4");
153         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
154         final List<Cookie> cookies = cookiespec.parse(header, origin);
155 
156         Assert.assertEquals(1, cookies.size());
157         final Cookie cookie = cookies.get(0);
158         Assert.assertEquals("name", cookie.getName());
159         Assert.assertEquals("value", cookie.getValue());
160         Assert.assertTrue(cookie instanceof ClientCookie);
161         final ClientCookie clientCookie = (ClientCookie) cookie;
162         Assert.assertEquals("v", clientCookie.getAttribute("p1"));
163         Assert.assertEquals("v,0", clientCookie.getAttribute("p2"));
164         Assert.assertTrue(clientCookie.containsAttribute("p3"));
165         Assert.assertTrue(clientCookie.containsAttribute("p4"));
166         Assert.assertFalse(clientCookie.containsAttribute("p5"));
167     }
168 
169     @Test
170     public void testParseCookieWithAttributes2() throws Exception {
171         final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
172         Mockito.when(h1.getAttributeName()).thenReturn("this");
173         final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
174         Mockito.when(h2.getAttributeName()).thenReturn("that");
175 
176         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);
177 
178         final Header header = new BasicHeader("Set-Cookie", "name = value ; p1 = v");
179         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
180         final List<Cookie> cookies = cookiespec.parse(header, origin);
181 
182         Assert.assertEquals(1, cookies.size());
183         final Cookie cookie = cookies.get(0);
184         Assert.assertEquals("name", cookie.getName());
185         Assert.assertEquals("value", cookie.getValue());
186         Assert.assertTrue(cookie instanceof ClientCookie);
187         final ClientCookie clientCookie = (ClientCookie) cookie;
188         Assert.assertEquals("v", clientCookie.getAttribute("p1"));
189     }
190 
191     @Test
192     public void testParseCookieWithAttributes3() throws Exception {
193         final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
194         Mockito.when(h1.getAttributeName()).thenReturn("this");
195         final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
196         Mockito.when(h2.getAttributeName()).thenReturn("that");
197 
198         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);
199 
200         final Header header = new BasicHeader("Set-Cookie", "name = value ; p1 =");
201         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
202         final List<Cookie> cookies = cookiespec.parse(header, origin);
203 
204         Assert.assertEquals(1, cookies.size());
205         final Cookie cookie = cookies.get(0);
206         Assert.assertEquals("name", cookie.getName());
207         Assert.assertEquals("value", cookie.getValue());
208         Assert.assertTrue(cookie instanceof ClientCookie);
209         final ClientCookie clientCookie = (ClientCookie) cookie;
210         Assert.assertEquals("", clientCookie.getAttribute("p1"));
211     }
212 
213     @Test
214     public void testValidateCookieBasics() throws Exception {
215         final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
216         Mockito.when(h1.getAttributeName()).thenReturn("this");
217         final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
218         Mockito.when(h2.getAttributeName()).thenReturn("that");
219 
220         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);
221 
222         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
223         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
224         cookiespec.validate(cookie, origin);
225 
226         Mockito.verify(h1).validate(cookie, origin);
227         Mockito.verify(h2).validate(cookie, origin);
228     }
229 
230     @Test
231     public void testMatchCookie() throws Exception {
232         final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
233         Mockito.when(h1.getAttributeName()).thenReturn("this");
234         final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
235         Mockito.when(h2.getAttributeName()).thenReturn("that");
236 
237         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);
238 
239         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
240         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
241 
242         Mockito.when(h1.match(cookie, origin)).thenReturn(true);
243         Mockito.when(h2.match(cookie, origin)).thenReturn(true);
244 
245         Assert.assertTrue(cookiespec.match(cookie, origin));
246 
247         Mockito.verify(h1).match(cookie, origin);
248         Mockito.verify(h2).match(cookie, origin);
249     }
250 
251     @Test
252     public void testMatchCookieNoMatch() throws Exception {
253         final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
254         Mockito.when(h1.getAttributeName()).thenReturn("this");
255         final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
256         Mockito.when(h2.getAttributeName()).thenReturn("that");
257 
258         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);
259 
260         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
261         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
262 
263         Mockito.when(h1.match(cookie, origin)).thenReturn(false);
264         Mockito.when(h2.match(cookie, origin)).thenReturn(false);
265 
266         Assert.assertFalse(cookiespec.match(cookie, origin));
267 
268         Mockito.verify(h1).match(cookie, origin);
269         Mockito.verify(h2, Mockito.never()).match(cookie, origin);
270     }
271 
272     @Test
273     public void testLegacy() throws Exception {
274         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
275 
276         Assert.assertEquals(0, cookiespec.getVersion());
277         Assert.assertEquals(null, cookiespec.getVersionHeader());
278     }
279 
280     @Test
281     public void testFormatCookiesBasics() throws Exception {
282         final Cookie cookie1 = new BasicClientCookie("name1", "value");
283 
284         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
285         final List<Header> headers = cookiespec.formatCookies(Arrays.asList(cookie1));
286         Assert.assertNotNull(headers);
287         Assert.assertEquals(1, headers.size());
288         final Header header = headers.get(0);
289         Assert.assertEquals("Cookie", header.getName());
290         Assert.assertEquals("name1=value", header.getValue());
291     }
292 
293     @Test
294     public void testFormatCookiesIllegalCharsInValue() throws Exception {
295         final Cookie cookie1 = new BasicClientCookie("name1", "value");
296         final Cookie cookie2 = new BasicClientCookie("name2", "some value");
297         final Cookie cookie3 = new BasicClientCookie("name3", "\"\\\"");
298         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
299         final List<Header> headers = cookiespec.formatCookies(Arrays.asList(cookie1, cookie2, cookie3));
300         Assert.assertNotNull(headers);
301         Assert.assertEquals(1, headers.size());
302         final Header header = headers.get(0);
303         Assert.assertEquals("Cookie", header.getName());
304         Assert.assertEquals("name1=value; name2=\"some value\"; name3=\"\\\"\\\\\\\"\"", header.getValue());
305     }
306 
307     @Test
308     public void testParseCookieMultipleAttributes() throws Exception {
309         final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
310         Mockito.when(h1.getAttributeName()).thenReturn("this");
311 
312         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1);
313 
314         final Header header = new BasicHeader("Set-Cookie", "name = value ; this = stuff; this = morestuff;");
315         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
316         cookiespec.parse(header, origin);
317 
318         Mockito.verify(h1).parse(Mockito.<SetCookie>any(), Mockito.eq("morestuff"));
319         Mockito.verify(h1, Mockito.times(1)).parse(Mockito.<SetCookie>any(), Mockito.anyString());
320     }
321 
322     @Test
323     public void testParseCookieMaxAgeOverExpires() throws Exception {
324         final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class);
325         Mockito.when(h1.getAttributeName()).thenReturn("Expires");
326         final CommonCookieAttributeHandler h2 = Mockito.mock(CommonCookieAttributeHandler.class);
327         Mockito.when(h2.getAttributeName()).thenReturn("Max-Age");
328 
329         final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(h1, h2);
330 
331         final Header header = new BasicHeader("Set-Cookie", "name = value ; expires = stuff; max-age = otherstuff;");
332         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
333         cookiespec.parse(header, origin);
334 
335         Mockito.verify(h1, Mockito.never()).parse(Mockito.<SetCookie>any(), Mockito.anyString());
336         Mockito.verify(h2).parse(Mockito.<SetCookie>any(), Mockito.eq("otherstuff"));
337     }
338 
339 }