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.http.client.protocol;
28  
29  import java.util.List;
30  
31  import org.apache.http.HttpResponse;
32  import org.apache.http.HttpResponseInterceptor;
33  import org.apache.http.HttpVersion;
34  import org.apache.http.client.CookieStore;
35  import org.apache.http.cookie.Cookie;
36  import org.apache.http.cookie.CookieOrigin;
37  import org.apache.http.cookie.CookieSpec;
38  import org.apache.http.cookie.SM;
39  import org.apache.http.impl.client.BasicCookieStore;
40  import org.apache.http.impl.cookie.DefaultCookieSpec;
41  import org.apache.http.message.BasicHttpResponse;
42  import org.junit.Assert;
43  import org.junit.Before;
44  import org.junit.Test;
45  
46  public class TestResponseProcessCookies {
47  
48      private CookieOrigin cookieOrigin;
49      private CookieSpec cookieSpec;
50      private CookieStore cookieStore;
51  
52      @Before
53      public void setUp() throws Exception {
54          this.cookieOrigin = new CookieOrigin("localhost", 80, "/", false);
55          this.cookieSpec = new DefaultCookieSpec();
56          this.cookieStore = new BasicCookieStore();
57      }
58  
59      @Test(expected=IllegalArgumentException.class)
60      public void testResponseParameterCheck() throws Exception {
61          final HttpClientContext context = HttpClientContext.create();
62          final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
63          interceptor.process(null, context);
64      }
65  
66      @Test(expected=IllegalArgumentException.class)
67      public void testContextParameterCheck() throws Exception {
68          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
69          final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
70          interceptor.process(response, null);
71      }
72  
73      @Test
74      public void testParseCookies() throws Exception {
75          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
76          response.addHeader(SM.SET_COOKIE, "name1=value1");
77  
78          final HttpClientContext context = HttpClientContext.create();
79          context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
80          context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
81          context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
82  
83          final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
84          interceptor.process(response, context);
85  
86          final List<Cookie> cookies = this.cookieStore.getCookies();
87          Assert.assertNotNull(cookies);
88          Assert.assertEquals(1, cookies.size());
89          final Cookie cookie = cookies.get(0);
90          Assert.assertEquals(0, cookie.getVersion());
91          Assert.assertEquals("name1", cookie.getName());
92          Assert.assertEquals("value1", cookie.getValue());
93          Assert.assertEquals("localhost", cookie.getDomain());
94          Assert.assertEquals("/", cookie.getPath());
95      }
96  
97      @Test
98      public void testNoCookieOrigin() throws Exception {
99          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
100         response.addHeader(SM.SET_COOKIE, "name1=value1");
101 
102         final HttpClientContext context = HttpClientContext.create();
103         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, null);
104         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
105         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
106 
107         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
108         interceptor.process(response, context);
109 
110         final List<Cookie> cookies = this.cookieStore.getCookies();
111         Assert.assertNotNull(cookies);
112         Assert.assertEquals(0, cookies.size());
113     }
114 
115     @Test
116     public void testNoCookieSpec() throws Exception {
117         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
118         response.addHeader(SM.SET_COOKIE, "name1=value1");
119 
120         final HttpClientContext context = HttpClientContext.create();
121         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
122         context.setAttribute(HttpClientContext.COOKIE_SPEC, null);
123         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
124 
125         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
126         interceptor.process(response, context);
127 
128         final List<Cookie> cookies = this.cookieStore.getCookies();
129         Assert.assertNotNull(cookies);
130         Assert.assertEquals(0, cookies.size());
131     }
132 
133     @Test
134     public void testNoCookieStore() throws Exception {
135         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
136         response.addHeader(SM.SET_COOKIE, "name1=value1");
137 
138         final HttpClientContext context = HttpClientContext.create();
139         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
140         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
141         context.setAttribute(HttpClientContext.COOKIE_STORE, null);
142 
143         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
144         interceptor.process(response, context);
145 
146         final List<Cookie> cookies = this.cookieStore.getCookies();
147         Assert.assertNotNull(cookies);
148         Assert.assertEquals(0, cookies.size());
149     }
150 
151     @Test
152     public void testSetCookie2OverrideSetCookie() throws Exception {
153         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
154         response.addHeader(SM.SET_COOKIE, "name1=value1");
155         response.addHeader(SM.SET_COOKIE2, "name1=value2; Version=1");
156 
157         final HttpClientContext context = HttpClientContext.create();
158         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
159         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
160         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
161 
162         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
163         interceptor.process(response, context);
164 
165         final List<Cookie> cookies = this.cookieStore.getCookies();
166         Assert.assertNotNull(cookies);
167         Assert.assertEquals(1, cookies.size());
168         final Cookie cookie = cookies.get(0);
169         Assert.assertEquals(1, cookie.getVersion());
170         Assert.assertEquals("name1", cookie.getName());
171         Assert.assertEquals("value2", cookie.getValue());
172         Assert.assertEquals("localhost.local", cookie.getDomain());
173         Assert.assertEquals("/", cookie.getPath());
174     }
175 
176     @Test
177     public void testInvalidHeader() throws Exception {
178         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
179         response.addHeader(SM.SET_COOKIE2, "name=value; Version=crap");
180 
181         final HttpClientContext context = HttpClientContext.create();
182         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
183         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
184         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
185 
186         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
187         interceptor.process(response, context);
188 
189         final List<Cookie> cookies = this.cookieStore.getCookies();
190         Assert.assertNotNull(cookies);
191         Assert.assertTrue(cookies.isEmpty());
192     }
193 
194     @Test
195     public void testCookieRejected() throws Exception {
196         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
197         response.addHeader(SM.SET_COOKIE2, "name=value; Domain=www.somedomain.com; Version=1");
198 
199         final HttpClientContext context = HttpClientContext.create();
200         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
201         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
202         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
203 
204         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
205         interceptor.process(response, context);
206 
207         final List<Cookie> cookies = this.cookieStore.getCookies();
208         Assert.assertNotNull(cookies);
209         Assert.assertTrue(cookies.isEmpty());
210     }
211 
212 }