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