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.hc.client5.http.protocol;
28  
29  import java.util.List;
30  
31  import org.apache.hc.client5.http.cookie.BasicCookieStore;
32  import org.apache.hc.client5.http.cookie.Cookie;
33  import org.apache.hc.client5.http.cookie.CookieOrigin;
34  import org.apache.hc.client5.http.cookie.CookieSpec;
35  import org.apache.hc.client5.http.cookie.CookieStore;
36  import org.apache.hc.client5.http.impl.cookie.RFC6265LaxSpec;
37  import org.apache.hc.core5.http.HttpResponse;
38  import org.apache.hc.core5.http.HttpResponseInterceptor;
39  import org.apache.hc.core5.http.message.BasicHttpResponse;
40  import org.junit.Assert;
41  import org.junit.Before;
42  import org.junit.Test;
43  
44  public class TestResponseProcessCookies {
45  
46      private CookieOrigin cookieOrigin;
47      private CookieSpec cookieSpec;
48      private CookieStore cookieStore;
49  
50      @Before
51      public void setUp() throws Exception {
52          this.cookieOrigin = new CookieOrigin("localhost", 80, "/", false);
53          this.cookieSpec = new RFC6265LaxSpec();
54          this.cookieStore = new BasicCookieStore();
55      }
56  
57      @Test(expected=NullPointerException.class)
58      public void testResponseParameterCheck() throws Exception {
59          final HttpClientContext context = HttpClientContext.create();
60          final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
61          interceptor.process(null, null, context);
62      }
63  
64      @Test(expected=NullPointerException.class)
65      public void testContextParameterCheck() throws Exception {
66          final HttpResponse response = new BasicHttpResponse(200, "OK");
67          final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
68          interceptor.process(response, null, null);
69      }
70  
71      @Test
72      public void testParseCookies() throws Exception {
73          final HttpResponse response = new BasicHttpResponse(200, "OK");
74          response.addHeader("Set-Cookie", "name1=value1");
75  
76          final HttpClientContext context = HttpClientContext.create();
77          context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
78          context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
79          context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
80  
81          final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
82          interceptor.process(response, null, context);
83  
84          final List<Cookie> cookies = this.cookieStore.getCookies();
85          Assert.assertNotNull(cookies);
86          Assert.assertEquals(1, cookies.size());
87          final Cookie cookie = cookies.get(0);
88          Assert.assertEquals("name1", cookie.getName());
89          Assert.assertEquals("value1", cookie.getValue());
90          Assert.assertEquals("localhost", cookie.getDomain());
91          Assert.assertEquals("/", cookie.getPath());
92      }
93  
94      @Test
95      public void testNoCookieOrigin() throws Exception {
96          final HttpResponse response = new BasicHttpResponse(200, "OK");
97          response.addHeader("Set-Cookie", "name1=value1");
98  
99          final HttpClientContext context = HttpClientContext.create();
100         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, null);
101         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
102         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
103 
104         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
105         interceptor.process(response, null, context);
106 
107         final List<Cookie> cookies = this.cookieStore.getCookies();
108         Assert.assertNotNull(cookies);
109         Assert.assertEquals(0, cookies.size());
110     }
111 
112     @Test
113     public void testNoCookieSpec() throws Exception {
114         final HttpResponse response = new BasicHttpResponse(200, "OK");
115         response.addHeader("Set-Cookie", "name1=value1");
116 
117         final HttpClientContext context = HttpClientContext.create();
118         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
119         context.setAttribute(HttpClientContext.COOKIE_SPEC, null);
120         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
121 
122         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
123         interceptor.process(response, null, context);
124 
125         final List<Cookie> cookies = this.cookieStore.getCookies();
126         Assert.assertNotNull(cookies);
127         Assert.assertEquals(0, cookies.size());
128     }
129 
130     @Test
131     public void testNoCookieStore() throws Exception {
132         final HttpResponse response = new BasicHttpResponse(200, "OK");
133         response.addHeader("Set-Cookie", "name1=value1");
134 
135         final HttpClientContext context = HttpClientContext.create();
136         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
137         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
138         context.setAttribute(HttpClientContext.COOKIE_STORE, null);
139 
140         final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
141         interceptor.process(response, null, context);
142 
143         final List<Cookie> cookies = this.cookieStore.getCookies();
144         Assert.assertNotNull(cookies);
145         Assert.assertEquals(0, cookies.size());
146     }
147 
148 }