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.impl.client.integration;
28  
29  import java.io.IOException;
30  import java.util.List;
31  
32  import org.apache.http.Header;
33  import org.apache.http.HttpEntity;
34  import org.apache.http.HttpException;
35  import org.apache.http.HttpHost;
36  import org.apache.http.HttpRequest;
37  import org.apache.http.HttpResponse;
38  import org.apache.http.HttpStatus;
39  import org.apache.http.ProtocolVersion;
40  import org.apache.http.client.CookieStore;
41  import org.apache.http.client.methods.HttpGet;
42  import org.apache.http.client.protocol.HttpClientContext;
43  import org.apache.http.cookie.Cookie;
44  import org.apache.http.cookie.SM;
45  import org.apache.http.cookie.SetCookie2;
46  import org.apache.http.entity.StringEntity;
47  import org.apache.http.impl.client.BasicCookieStore;
48  import org.apache.http.localserver.LocalServerTestBase;
49  import org.apache.http.message.BasicHeader;
50  import org.apache.http.protocol.HttpContext;
51  import org.apache.http.protocol.HttpRequestHandler;
52  import org.apache.http.util.EntityUtils;
53  import org.junit.Assert;
54  import org.junit.Test;
55  
56  /**
57   * Cookie2 support tests.
58   */
59  public class TestCookie2Support extends LocalServerTestBase {
60  
61      private static class CookieVer0Service implements HttpRequestHandler {
62  
63          @Override
64          public void handle(
65                  final HttpRequest request,
66                  final HttpResponse response,
67                  final HttpContext context) throws HttpException, IOException {
68              final ProtocolVersion httpversion = request.getRequestLine().getProtocolVersion();
69              response.setStatusLine(httpversion, HttpStatus.SC_OK);
70              response.addHeader(new BasicHeader("Set-Cookie", "name1=value1; path=/test"));
71              final StringEntity entity = new StringEntity("whatever");
72              response.setEntity(entity);
73          }
74  
75      }
76  
77      @Test
78      public void testCookieVersionSupportHeader1() throws Exception {
79          this.serverBootstrap.registerHandler("*", new CookieVer0Service());
80  
81          final HttpHost target = start();
82  
83          final CookieStore cookieStore = new BasicCookieStore();
84          final HttpClientContext context = HttpClientContext.create();
85          context.setCookieStore(cookieStore);
86  
87          final HttpGet httpget = new HttpGet("/test/");
88  
89          final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
90          final HttpEntity e1 = response1.getEntity();
91          EntityUtils.consume(e1);
92  
93          final List<Cookie> cookies = cookieStore.getCookies();
94          Assert.assertNotNull(cookies);
95          Assert.assertEquals(1, cookies.size());
96  
97          final HttpResponse response2 = this.httpclient.execute(target, httpget, context);
98          final HttpEntity e2 = response2.getEntity();
99          EntityUtils.consume(e2);
100 
101         final HttpRequest reqWrapper = context.getRequest();
102 
103         final Header cookiesupport = reqWrapper.getFirstHeader("Cookie2");
104         Assert.assertNotNull(cookiesupport);
105         Assert.assertEquals("$Version=1", cookiesupport.getValue());
106     }
107 
108     private static class CookieVer1Service implements HttpRequestHandler {
109 
110         @Override
111         public void handle(
112                 final HttpRequest request,
113                 final HttpResponse response,
114                 final HttpContext context) throws HttpException, IOException {
115             final ProtocolVersion httpversion = request.getRequestLine().getProtocolVersion();
116             response.setStatusLine(httpversion, HttpStatus.SC_OK);
117             response.addHeader(new BasicHeader("Set-Cookie", "name1=value1; Path=\"/test\"; Version=1"));
118             response.addHeader(new BasicHeader("Set-Cookie2", "name2=value2; Path=\"/test\"; Version=1"));
119             final StringEntity entity = new StringEntity("whatever");
120             response.setEntity(entity);
121         }
122 
123     }
124 
125     @Test
126     public void testCookieVersionSupportHeader2() throws Exception {
127         this.serverBootstrap.registerHandler("*", new CookieVer1Service());
128 
129         final HttpHost target = start();
130 
131         final CookieStore cookieStore = new BasicCookieStore();
132         final HttpClientContext context = HttpClientContext.create();
133         context.setCookieStore(cookieStore);
134 
135         final HttpGet httpget = new HttpGet("/test/");
136 
137         final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
138         final HttpEntity e1 = response1.getEntity();
139         EntityUtils.consume(e1);
140 
141         final List<Cookie> cookies = cookieStore.getCookies();
142         Assert.assertNotNull(cookies);
143         Assert.assertEquals(2, cookies.size());
144 
145         final HttpResponse response2 = this.httpclient.execute(target, httpget, context);
146         final HttpEntity e2 = response2.getEntity();
147         EntityUtils.consume(e2);
148 
149         final HttpRequest reqWrapper = context.getRequest();
150 
151         final Header cookiesupport = reqWrapper.getFirstHeader(SM.COOKIE2);
152         Assert.assertNotNull(cookiesupport);
153         Assert.assertEquals("$Version=1", cookiesupport.getValue());
154     }
155 
156     private static class CookieVer2Service implements HttpRequestHandler {
157 
158         @Override
159         public void handle(
160                 final HttpRequest request,
161                 final HttpResponse response,
162                 final HttpContext context) throws HttpException, IOException {
163             final ProtocolVersion httpversion = request.getRequestLine().getProtocolVersion();
164             response.setStatusLine(httpversion, HttpStatus.SC_OK);
165             response.addHeader(new BasicHeader("Set-Cookie2", "name2=value2; Path=\"/test\"; Version=2"));
166             final StringEntity entity = new StringEntity("whatever");
167             response.setEntity(entity);
168         }
169 
170     }
171 
172     @Test
173     public void testCookieVersionSupportHeader3() throws Exception {
174         this.serverBootstrap.registerHandler("*", new CookieVer2Service());
175 
176         final HttpHost target = start();
177 
178         final CookieStore cookieStore = new BasicCookieStore();
179         final HttpClientContext context = HttpClientContext.create();
180         context.setCookieStore(cookieStore);
181 
182         final HttpGet httpget = new HttpGet("/test/");
183 
184         final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
185         final HttpEntity e1 = response1.getEntity();
186         EntityUtils.consume(e1);
187 
188         final List<Cookie> cookies = cookieStore.getCookies();
189         Assert.assertNotNull(cookies);
190         Assert.assertEquals(1, cookies.size());
191 
192         final HttpResponse response2 = this.httpclient.execute(target, httpget, context);
193         final HttpEntity e2 = response2.getEntity();
194         EntityUtils.consume(e2);
195 
196         final HttpRequest reqWrapper = context.getRequest();
197 
198         final Header cookiesupport = reqWrapper.getFirstHeader("Cookie2");
199         Assert.assertNotNull(cookiesupport);
200         Assert.assertEquals("$Version=1", cookiesupport.getValue());
201     }
202 
203     private static class SetCookieVersionMixService implements HttpRequestHandler {
204 
205         @Override
206         public void handle(
207                 final HttpRequest request,
208                 final HttpResponse response,
209                 final HttpContext context) throws HttpException, IOException {
210             final ProtocolVersion httpversion = request.getRequestLine().getProtocolVersion();
211             response.setStatusLine(httpversion, HttpStatus.SC_OK);
212             response.addHeader(new BasicHeader("Set-Cookie", "name=wrong; Path=/test"));
213             response.addHeader(new BasicHeader("Set-Cookie2", "name=right; Path=\"/test\"; Version=1"));
214             final StringEntity entity = new StringEntity("whatever");
215             response.setEntity(entity);
216         }
217 
218     }
219 
220     @Test
221     public void testSetCookieVersionMix() throws Exception {
222         this.serverBootstrap.registerHandler("*", new SetCookieVersionMixService());
223 
224         final HttpHost target = start();
225 
226         final CookieStore cookieStore = new BasicCookieStore();
227         final HttpClientContext context = HttpClientContext.create();
228         context.setCookieStore(cookieStore);
229 
230         final HttpGet httpget = new HttpGet("/test/");
231 
232         final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
233         final HttpEntity e1 = response1.getEntity();
234         EntityUtils.consume(e1);
235 
236         final List<Cookie> cookies = cookieStore.getCookies();
237         Assert.assertNotNull(cookies);
238         Assert.assertEquals(1, cookies.size());
239         Assert.assertEquals("right", cookies.get(0).getValue());
240         Assert.assertTrue(cookies.get(0) instanceof SetCookie2);
241     }
242 
243 }