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.net.URI;
31  import java.util.List;
32  
33  import org.apache.http.HttpException;
34  import org.apache.http.HttpHost;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.HttpStatus;
38  import org.apache.http.HttpVersion;
39  import org.apache.http.client.CookieStore;
40  import org.apache.http.client.methods.CloseableHttpResponse;
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.impl.client.BasicCookieStore;
45  import org.apache.http.localserver.LocalServerTestBase;
46  import org.apache.http.message.BasicHeader;
47  import org.apache.http.protocol.HttpContext;
48  import org.apache.http.protocol.HttpRequestHandler;
49  import org.apache.http.util.EntityUtils;
50  import org.junit.Assert;
51  import org.junit.Test;
52  
53  /**
54   * This class tests cookie matching when using Virtual Host.
55   */
56  public class TestCookieVirtualHost extends LocalServerTestBase {
57  
58      @Test
59      public void testCookieMatchingWithVirtualHosts() throws Exception {
60          this.serverBootstrap.registerHandler("*", new HttpRequestHandler() {
61              @Override
62              public void handle(
63                      final HttpRequest request,
64                      final HttpResponse response,
65                      final HttpContext context) throws HttpException, IOException {
66  
67                  final int n = Integer.parseInt(request.getFirstHeader("X-Request").getValue());
68                  switch (n) {
69                  case 1:
70                      // Assert Host is forwarded from URI
71                      Assert.assertEquals("app.mydomain.fr", request
72                              .getFirstHeader("Host").getValue());
73  
74                      response.setStatusLine(HttpVersion.HTTP_1_1,
75                              HttpStatus.SC_OK);
76                      // Respond with Set-Cookie on virtual host domain. This
77                      // should be valid.
78                      response.addHeader(new BasicHeader("Set-Cookie",
79                              "name1=value1; domain=mydomain.fr; path=/"));
80                      break;
81  
82                  case 2:
83                      // Assert Host is still forwarded from URI
84                      Assert.assertEquals("app.mydomain.fr", request
85                              .getFirstHeader("Host").getValue());
86  
87                      // We should get our cookie back.
88                      Assert.assertNotNull("We must get a cookie header",
89                              request.getFirstHeader("Cookie"));
90                      response.setStatusLine(HttpVersion.HTTP_1_1,
91                              HttpStatus.SC_OK);
92                      break;
93  
94                  case 3:
95                      // Assert Host is forwarded from URI
96                      Assert.assertEquals("app.mydomain.fr", request
97                              .getFirstHeader("Host").getValue());
98  
99                      response.setStatusLine(HttpVersion.HTTP_1_1,
100                             HttpStatus.SC_OK);
101                     break;
102                 default:
103                     Assert.fail("Unexpected value: " + n);
104                     break;
105                 }
106             }
107 
108         });
109 
110         final HttpHost target = start();
111 
112         final CookieStore cookieStore = new BasicCookieStore();
113         final HttpClientContext context = HttpClientContext.create();
114         context.setCookieStore(cookieStore);
115 
116         // First request : retrieve a domain cookie from remote server.
117         URI uri = new URI("http://app.mydomain.fr");
118         HttpRequest httpRequest = new HttpGet(uri);
119         httpRequest.addHeader("X-Request", "1");
120         final CloseableHttpResponse response1 = this.httpclient.execute(target, httpRequest, context);
121         try {
122             EntityUtils.consume(response1.getEntity());
123         } finally {
124             response1.close();
125         }
126 
127         // We should have one cookie set on domain.
128         final List<Cookie> cookies = cookieStore.getCookies();
129         Assert.assertNotNull(cookies);
130         Assert.assertEquals(1, cookies.size());
131         Assert.assertEquals("name1", cookies.get(0).getName());
132 
133         // Second request : send the cookie back.
134         uri = new URI("http://app.mydomain.fr");
135         httpRequest = new HttpGet(uri);
136         httpRequest.addHeader("X-Request", "2");
137         final CloseableHttpResponse response2 = this.httpclient.execute(target, httpRequest, context);
138         try {
139             EntityUtils.consume(response2.getEntity());
140         } finally {
141             response2.close();
142         }
143 
144         // Third request : Host header
145         uri = new URI("http://app.mydomain.fr");
146         httpRequest = new HttpGet(uri);
147         httpRequest.addHeader("X-Request", "3");
148         final CloseableHttpResponse response3 = this.httpclient.execute(target, httpRequest, context);
149         try {
150             EntityUtils.consume(response3.getEntity());
151         } finally {
152             response3.close();
153         }
154     }
155 
156 }