1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
69 Assertions.assertEquals("app.mydomain.fr", request
70 .getFirstHeader("Host").getValue());
71
72 response.setCode(HttpStatus.SC_OK);
73
74
75 response.addHeader(new BasicHeader("Set-Cookie",
76 "name1=value1; domain=mydomain.fr; path=/"));
77 break;
78
79 case 2:
80
81 Assertions.assertEquals("app.mydomain.fr", request
82 .getFirstHeader("Host").getValue());
83
84
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
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
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
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
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
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 }