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.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
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
69 Assert.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 Assert.assertEquals("app.mydomain.fr", request
82 .getFirstHeader("Host").getValue());
83
84
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
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
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
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
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
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 }