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.impl.classic.HttpClientBuilder;
38  import org.apache.hc.client5.http.protocol.HttpClientContext;
39  import org.apache.hc.core5.http.HttpHost;
40  import org.apache.hc.core5.http.HttpStatus;
41  import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
42  import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
43  import org.apache.hc.core5.http.io.entity.EntityUtils;
44  import org.apache.hc.core5.http.message.BasicHeader;
45  import org.apache.hc.core5.io.CloseMode;
46  import org.junit.jupiter.api.AfterEach;
47  import org.junit.jupiter.api.Assertions;
48  import org.junit.jupiter.api.Test;
49  
50  /**
51   * This class tests cookie matching when using Virtual Host.
52   */
53  public class TestCookieVirtualHost {
54  
55      private HttpServer server;
56  
57      @AfterEach
58      public void shutDown() throws Exception {
59          if (this.server != null) {
60              this.server.close(CloseMode.GRACEFUL);
61          }
62      }
63  
64      @Test
65      public void testCookieMatchingWithVirtualHosts() throws Exception {
66          server = ServerBootstrap.bootstrap()
67                  .register("app.mydomain.fr", "*", (request, response, context) -> {
68  
69                      final int n = Integer.parseInt(request.getFirstHeader("X-Request").getValue());
70                      switch (n) {
71                          case 1:
72                              // Assert Host is forwarded from URI
73                              Assertions.assertEquals("app.mydomain.fr", request
74                                      .getFirstHeader("Host").getValue());
75  
76                              response.setCode(HttpStatus.SC_OK);
77                              // Respond with Set-Cookie on virtual host domain. This
78                              // should be valid.
79                              response.addHeader(new BasicHeader("Set-Cookie",
80                                      "name1=value1; domain=mydomain.fr; path=/"));
81                              break;
82  
83                          case 2:
84                              // Assert Host is still forwarded from URI
85                              Assertions.assertEquals("app.mydomain.fr", request
86                                      .getFirstHeader("Host").getValue());
87  
88                              // We should get our cookie back.
89                              Assertions.assertNotNull(request.getFirstHeader("Cookie"), "We must get a cookie header");
90                              response.setCode(HttpStatus.SC_OK);
91                              break;
92  
93                          case 3:
94                              // Assert Host is forwarded from URI
95                              Assertions.assertEquals("app.mydomain.fr", request
96                                      .getFirstHeader("Host").getValue());
97  
98                              response.setCode(HttpStatus.SC_OK);
99                              break;
100                         default:
101                             Assertions.fail("Unexpected value: " + n);
102                             break;
103                     }
104                 })
105                 .create();
106         server.start();
107 
108         final HttpHost target = new HttpHost("localhost", server.getLocalPort());
109         try (final CloseableHttpClient client = HttpClientBuilder.create().build()) {
110             final CookieStore cookieStore = new BasicCookieStore();
111             final HttpClientContext context = HttpClientContext.create();
112             context.setCookieStore(cookieStore);
113 
114             // First request : retrieve a domain cookie from remote server.
115             final HttpGet request1 = new HttpGet(new URI("http://app.mydomain.fr"));
116             request1.addHeader("X-Request", "1");
117             client.execute(target, request1, context, response -> {
118                 Assertions.assertEquals(200, response.getCode());
119                 EntityUtils.consume(response.getEntity());
120                 return null;
121             });
122 
123             // We should have one cookie set on domain.
124             final List<Cookie> cookies = cookieStore.getCookies();
125             Assertions.assertNotNull(cookies);
126             Assertions.assertEquals(1, cookies.size());
127             Assertions.assertEquals("name1", cookies.get(0).getName());
128 
129             // Second request : send the cookie back.
130             final HttpGet request2 = new HttpGet(new URI("http://app.mydomain.fr"));
131             request2.addHeader("X-Request", "2");
132             client.execute(target, request2, context, response -> {
133                 Assertions.assertEquals(200, response.getCode());
134                 EntityUtils.consume(response.getEntity());
135                 return null;
136             });
137 
138             // Third request : Host header
139             final HttpGet request3 = new HttpGet(new URI("http://app.mydomain.fr"));
140             request3.addHeader("X-Request", "3");
141             client.execute(target, request3, context, response -> {
142                 Assertions.assertEquals(200, response.getCode());
143                 EntityUtils.consume(response.getEntity());
144                 return null;
145             });
146         }
147     }
148 
149 }