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.net.URI;
30  import java.util.List;
31  
32  import org.apache.hc.client5.http.classic.methods.HttpGet;
33  import org.apache.hc.client5.http.cookie.BasicCookieStore;
34  import org.apache.hc.client5.http.cookie.Cookie;
35  import org.apache.hc.client5.http.cookie.CookieStore;
36  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
37  import org.apache.hc.client5.http.protocol.HttpClientContext;
38  import org.apache.hc.client5.testing.sync.extension.TestClientResources;
39  import org.apache.hc.core5.http.HttpHost;
40  import org.apache.hc.core5.http.HttpStatus;
41  import org.apache.hc.core5.http.URIScheme;
42  import org.apache.hc.core5.http.io.entity.EntityUtils;
43  import org.apache.hc.core5.http.message.BasicHeader;
44  import org.apache.hc.core5.testing.classic.ClassicTestServer;
45  import org.apache.hc.core5.util.Timeout;
46  import org.junit.jupiter.api.Assertions;
47  import org.junit.jupiter.api.Test;
48  import org.junit.jupiter.api.extension.RegisterExtension;
49  
50  /**
51   * This class tests cookie matching when using Virtual Host.
52   */
53  public class TestCookieVirtualHost {
54  
55      public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
56  
57      @RegisterExtension
58      private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
59  
60      @Test
61      public void testCookieMatchingWithVirtualHosts() throws Exception {
62          final ClassicTestServer server = testResources.startServer(null, null, null);
63          server.registerHandlerVirtual("app.mydomain.fr", "*", (request, response, context) -> {
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                  Assertions.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                  Assertions.assertEquals("app.mydomain.fr", request
82                          .getFirstHeader("Host").getValue());
83  
84                  // We should get our cookie back.
85                  Assertions.assertNotNull(request.getFirstHeader("Cookie"), "We must get a cookie header");
86                  response.setCode(HttpStatus.SC_OK);
87                  break;
88  
89              case 3:
90                  // Assert Host is forwarded from URI
91                  Assertions.assertEquals("app.mydomain.fr", request
92                          .getFirstHeader("Host").getValue());
93  
94                  response.setCode(HttpStatus.SC_OK);
95                  break;
96              default:
97                  Assertions.fail("Unexpected value: " + n);
98                  break;
99              }
100         });
101 
102         final HttpHost target = testResources.targetHost();
103 
104         final CloseableHttpClient client = testResources.startClient(b -> {});
105 
106         final CookieStore cookieStore = new BasicCookieStore();
107         final HttpClientContext context = HttpClientContext.create();
108         context.setCookieStore(cookieStore);
109 
110         // First request : retrieve a domain cookie from remote server.
111         final HttpGet request1 = new HttpGet(new URI("http://app.mydomain.fr"));
112         request1.addHeader("X-Request", "1");
113         client.execute(target, request1, context, response -> {
114             EntityUtils.consume(response.getEntity());
115             return null;
116         });
117 
118         // We should have one cookie set on domain.
119         final List<Cookie> cookies = cookieStore.getCookies();
120         Assertions.assertNotNull(cookies);
121         Assertions.assertEquals(1, cookies.size());
122         Assertions.assertEquals("name1", cookies.get(0).getName());
123 
124         // Second request : send the cookie back.
125         final HttpGet request2 = new HttpGet(new URI("http://app.mydomain.fr"));
126         request2.addHeader("X-Request", "2");
127         client.execute(target, request2, context, response -> {
128             EntityUtils.consume(response.getEntity());
129             return null;
130         });
131 
132         // Third request : Host header
133         final HttpGet request3 = new HttpGet(new URI("http://app.mydomain.fr"));
134         request3.addHeader("X-Request", "3");
135         client.execute(target, request3, context, response -> {
136             EntityUtils.consume(response.getEntity());
137             return null;
138         });
139     }
140 
141 }