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