author | jjiang |
Mon, 15 Oct 2018 22:47:03 +0800 | |
changeset 52121 | 934969c63223 |
parent 50681 | 4254bed3c09d |
permissions | -rw-r--r-- |
49765 | 1 |
/* |
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Test for cookie handling when redirecting |
|
27 |
* @modules java.base/sun.net.www.http |
|
28 |
* java.net.http/jdk.internal.net.http.common |
|
29 |
* java.net.http/jdk.internal.net.http.frame |
|
30 |
* java.net.http/jdk.internal.net.http.hpack |
|
31 |
* java.logging |
|
32 |
* jdk.httpserver |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
50681
diff
changeset
|
33 |
* @library /test/lib http2/server |
49765 | 34 |
* @build Http2TestServer |
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
50681
diff
changeset
|
35 |
* @build jdk.test.lib.net.SimpleSSLContext |
49765 | 36 |
* @run testng/othervm |
37 |
* -Djdk.httpclient.HttpClient.log=trace,headers,requests |
|
38 |
* RedirectWithCookie |
|
39 |
*/ |
|
40 |
||
41 |
import com.sun.net.httpserver.HttpServer; |
|
42 |
import com.sun.net.httpserver.HttpsConfigurator; |
|
43 |
import com.sun.net.httpserver.HttpsServer; |
|
44 |
import java.io.IOException; |
|
45 |
import java.io.InputStream; |
|
46 |
import java.io.OutputStream; |
|
47 |
import java.net.CookieManager; |
|
48 |
import java.net.InetAddress; |
|
49 |
import java.net.InetSocketAddress; |
|
50 |
import java.net.URI; |
|
51 |
import java.net.http.HttpClient; |
|
52 |
import java.net.http.HttpClient.Redirect; |
|
53 |
import java.net.http.HttpRequest; |
|
54 |
import java.net.http.HttpResponse; |
|
55 |
import java.net.http.HttpResponse.BodyHandlers; |
|
56 |
import java.util.List; |
|
57 |
import javax.net.ssl.SSLContext; |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
50681
diff
changeset
|
58 |
import jdk.test.lib.net.SimpleSSLContext; |
49765 | 59 |
import org.testng.annotations.AfterTest; |
60 |
import org.testng.annotations.BeforeTest; |
|
61 |
import org.testng.annotations.DataProvider; |
|
62 |
import org.testng.annotations.Test; |
|
63 |
import static java.lang.System.out; |
|
64 |
import static java.nio.charset.StandardCharsets.UTF_8; |
|
65 |
import static org.testng.Assert.assertEquals; |
|
66 |
import static org.testng.Assert.assertTrue; |
|
67 |
||
68 |
public class RedirectWithCookie implements HttpServerAdapters { |
|
69 |
||
70 |
SSLContext sslContext; |
|
71 |
HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ] |
|
72 |
HttpTestServer httpsTestServer; // HTTPS/1.1 |
|
73 |
HttpTestServer http2TestServer; // HTTP/2 ( h2c ) |
|
74 |
HttpTestServer https2TestServer; // HTTP/2 ( h2 ) |
|
75 |
String httpURI; |
|
76 |
String httpsURI; |
|
77 |
String http2URI; |
|
78 |
String https2URI; |
|
79 |
||
80 |
static final String MESSAGE = "BasicRedirectTest message body"; |
|
81 |
static final int ITERATIONS = 3; |
|
82 |
||
83 |
@DataProvider(name = "positive") |
|
84 |
public Object[][] positive() { |
|
85 |
return new Object[][] { |
|
86 |
{ httpURI, }, |
|
87 |
{ httpsURI, }, |
|
88 |
{ http2URI, }, |
|
89 |
{ https2URI, }, |
|
90 |
}; |
|
91 |
} |
|
92 |
||
93 |
@Test(dataProvider = "positive") |
|
94 |
void test(String uriString) throws Exception { |
|
95 |
out.printf("%n---- starting (%s) ----%n", uriString); |
|
96 |
HttpClient client = HttpClient.newBuilder() |
|
97 |
.followRedirects(Redirect.ALWAYS) |
|
98 |
.cookieHandler(new CookieManager()) |
|
99 |
.sslContext(sslContext) |
|
100 |
.build(); |
|
101 |
assert client.cookieHandler().isPresent(); |
|
102 |
||
103 |
URI uri = URI.create(uriString); |
|
104 |
HttpRequest request = HttpRequest.newBuilder(uri).build(); |
|
105 |
out.println("Initial request: " + request.uri()); |
|
106 |
||
107 |
for (int i=0; i< ITERATIONS; i++) { |
|
108 |
out.println("iteration: " + i); |
|
109 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
|
110 |
||
111 |
out.println(" Got response: " + response); |
|
112 |
out.println(" Got body Path: " + response.body()); |
|
113 |
||
114 |
assertEquals(response.statusCode(), 200); |
|
115 |
assertEquals(response.body(), MESSAGE); |
|
116 |
// asserts redirected URI in response.request().uri() |
|
117 |
assertTrue(response.uri().getPath().endsWith("message")); |
|
118 |
assertPreviousRedirectResponses(request, response); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
static void assertPreviousRedirectResponses(HttpRequest initialRequest, |
|
123 |
HttpResponse<?> finalResponse) { |
|
124 |
// there must be at least one previous response |
|
125 |
finalResponse.previousResponse() |
|
126 |
.orElseThrow(() -> new RuntimeException("no previous response")); |
|
127 |
||
128 |
HttpResponse<?> response = finalResponse; |
|
129 |
do { |
|
130 |
URI uri = response.uri(); |
|
131 |
response = response.previousResponse().get(); |
|
132 |
assertTrue(300 <= response.statusCode() && response.statusCode() <= 309, |
|
133 |
"Expected 300 <= code <= 309, got:" + response.statusCode()); |
|
134 |
assertEquals(response.body(), null, "Unexpected body: " + response.body()); |
|
135 |
String locationHeader = response.headers().firstValue("Location") |
|
136 |
.orElseThrow(() -> new RuntimeException("no previous Location")); |
|
137 |
assertTrue(uri.toString().endsWith(locationHeader), |
|
138 |
"URI: " + uri + ", Location: " + locationHeader); |
|
139 |
||
140 |
} while (response.previousResponse().isPresent()); |
|
141 |
||
142 |
// initial |
|
143 |
assertEquals(initialRequest, response.request(), |
|
144 |
String.format("Expected initial request [%s] to equal last prev req [%s]", |
|
145 |
initialRequest, response.request())); |
|
146 |
} |
|
147 |
||
148 |
// -- Infrastructure |
|
149 |
||
150 |
@BeforeTest |
|
151 |
public void setup() throws Exception { |
|
152 |
sslContext = new SimpleSSLContext().get(); |
|
153 |
if (sslContext == null) |
|
154 |
throw new AssertionError("Unexpected null sslContext"); |
|
155 |
||
156 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
|
157 |
||
158 |
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0)); |
|
159 |
httpTestServer.addHandler(new CookieRedirectHandler(), "/http1/cookie/"); |
|
160 |
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/cookie/redirect"; |
|
161 |
HttpsServer httpsServer = HttpsServer.create(sa, 0); |
|
162 |
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
|
163 |
httpsTestServer = HttpTestServer.of(httpsServer); |
|
164 |
httpsTestServer.addHandler(new CookieRedirectHandler(),"/https1/cookie/"); |
|
165 |
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/cookie/redirect"; |
|
166 |
||
167 |
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0)); |
|
168 |
http2TestServer.addHandler(new CookieRedirectHandler(), "/http2/cookie/"); |
|
169 |
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/cookie/redirect"; |
|
50681 | 170 |
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext)); |
49765 | 171 |
https2TestServer.addHandler(new CookieRedirectHandler(), "/https2/cookie/"); |
172 |
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/cookie/redirect"; |
|
173 |
||
174 |
httpTestServer.start(); |
|
175 |
httpsTestServer.start(); |
|
176 |
http2TestServer.start(); |
|
177 |
https2TestServer.start(); |
|
178 |
} |
|
179 |
||
180 |
@AfterTest |
|
181 |
public void teardown() throws Exception { |
|
182 |
httpTestServer.stop(); |
|
183 |
httpsTestServer.stop(); |
|
184 |
http2TestServer.stop(); |
|
185 |
https2TestServer.stop(); |
|
186 |
} |
|
187 |
||
188 |
static class CookieRedirectHandler implements HttpTestHandler { |
|
189 |
@Override |
|
190 |
public void handle(HttpTestExchange t) throws IOException { |
|
191 |
System.out.println("CookieRedirectHandler for: " + t.getRequestURI()); |
|
192 |
readAllRequestData(t); |
|
193 |
||
194 |
// redirecting |
|
195 |
if (t.getRequestURI().getPath().endsWith("redirect")) { |
|
196 |
String url = t.getRequestURI().resolve("message").toString(); |
|
197 |
t.getResponseHeaders().addHeader("Location", url); |
|
198 |
t.getResponseHeaders().addHeader("Set-Cookie", |
|
199 |
"CUSTOMER=WILE_E_COYOTE"); |
|
200 |
t.sendResponseHeaders(302, 0); |
|
201 |
return; |
|
202 |
} |
|
203 |
||
204 |
// not redirecting |
|
205 |
try (OutputStream os = t.getResponseBody()) { |
|
206 |
List<String> cookie = t.getRequestHeaders().get("Cookie"); |
|
207 |
||
208 |
if (cookie == null || cookie.size() == 0) { |
|
209 |
String msg = "No cookie header present"; |
|
210 |
(new RuntimeException(msg)).printStackTrace(); |
|
211 |
t.sendResponseHeaders(500, -1); |
|
212 |
os.write(msg.getBytes(UTF_8)); |
|
213 |
} else if (!cookie.get(0).equals("CUSTOMER=WILE_E_COYOTE")) { |
|
214 |
String msg = "Incorrect cookie header value:[" + cookie.get(0) + "]"; |
|
215 |
(new RuntimeException(msg)).printStackTrace(); |
|
216 |
t.sendResponseHeaders(500, -1); |
|
217 |
os.write(msg.getBytes(UTF_8)); |
|
218 |
} else { |
|
219 |
assert cookie.get(0).equals("CUSTOMER=WILE_E_COYOTE"); |
|
220 |
byte[] bytes = MESSAGE.getBytes(UTF_8); |
|
221 |
t.sendResponseHeaders(200, bytes.length); |
|
222 |
os.write(bytes); |
|
223 |
} |
|
224 |
} |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
static void readAllRequestData(HttpTestExchange t) throws IOException { |
|
229 |
try (InputStream is = t.getRequestBody()) { |
|
230 |
is.readAllBytes(); |
|
231 |
} |
|
232 |
} |
|
233 |
} |