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.jupiter.api.Assertions;
41  import org.junit.jupiter.api.BeforeEach;
42  import org.junit.jupiter.api.Test;
43  
44  public class TestResponseProcessCookies {
45  
46      private CookieOrigin cookieOrigin;
47      private CookieSpec cookieSpec;
48      private CookieStore cookieStore;
49  
50      @BeforeEach
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
58      public void testResponseParameterCheck() throws Exception {
59          final HttpClientContext context = HttpClientContext.create();
60          final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE;
61          Assertions.assertThrows(NullPointerException.class, () ->
62                  interceptor.process(null, null, context));
63      }
64  
65      @Test
66      public void testContextParameterCheck() throws Exception {
67          final HttpResponse response = new BasicHttpResponse(200, "OK");
68          final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE;
69          Assertions.assertThrows(NullPointerException.class, () ->
70                  interceptor.process(response, null, null));
71      }
72  
73      @Test
74      public void testParseCookies() throws Exception {
75          final HttpResponse response = new BasicHttpResponse(200, "OK");
76          response.addHeader("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 = ResponseProcessCookies.INSTANCE;
84          interceptor.process(response, null, context);
85  
86          final List<Cookie> cookies = this.cookieStore.getCookies();
87          Assertions.assertNotNull(cookies);
88          Assertions.assertEquals(1, cookies.size());
89          final Cookie cookie = cookies.get(0);
90          Assertions.assertEquals("name1", cookie.getName());
91          Assertions.assertEquals("value1", cookie.getValue());
92          Assertions.assertEquals("localhost", cookie.getDomain());
93          Assertions.assertEquals("/", cookie.getPath());
94      }
95  
96      @Test
97      public void testNoCookieOrigin() throws Exception {
98          final HttpResponse response = new BasicHttpResponse(200, "OK");
99          response.addHeader("Set-Cookie", "name1=value1");
100 
101         final HttpClientContext context = HttpClientContext.create();
102         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, null);
103         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
104         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
105 
106         final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE;
107         interceptor.process(response, null, context);
108 
109         final List<Cookie> cookies = this.cookieStore.getCookies();
110         Assertions.assertNotNull(cookies);
111         Assertions.assertEquals(0, cookies.size());
112     }
113 
114     @Test
115     public void testNoCookieSpec() throws Exception {
116         final HttpResponse response = new BasicHttpResponse(200, "OK");
117         response.addHeader("Set-Cookie", "name1=value1");
118 
119         final HttpClientContext context = HttpClientContext.create();
120         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
121         context.setAttribute(HttpClientContext.COOKIE_SPEC, null);
122         context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
123 
124         final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE;
125         interceptor.process(response, null, context);
126 
127         final List<Cookie> cookies = this.cookieStore.getCookies();
128         Assertions.assertNotNull(cookies);
129         Assertions.assertEquals(0, cookies.size());
130     }
131 
132     @Test
133     public void testNoCookieStore() throws Exception {
134         final HttpResponse response = new BasicHttpResponse(200, "OK");
135         response.addHeader("Set-Cookie", "name1=value1");
136 
137         final HttpClientContext context = HttpClientContext.create();
138         context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
139         context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
140         context.setAttribute(HttpClientContext.COOKIE_STORE, null);
141 
142         final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE;
143         interceptor.process(response, null, context);
144 
145         final List<Cookie> cookies = this.cookieStore.getCookies();
146         Assertions.assertNotNull(cookies);
147         Assertions.assertEquals(0, cookies.size());
148     }
149 
150 }