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 Method change during redirection
|
|
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 |
* jdk.httpserver
|
|
32 |
* @library /lib/testlibrary /test/lib http2/server
|
|
33 |
* @build Http2TestServer
|
|
34 |
* @build jdk.testlibrary.SimpleSSLContext
|
|
35 |
* @run testng/othervm RedirectMethodChange
|
|
36 |
*/
|
|
37 |
|
|
38 |
import javax.net.ssl.SSLContext;
|
|
39 |
import java.io.IOException;
|
|
40 |
import java.io.InputStream;
|
|
41 |
import java.io.OutputStream;
|
|
42 |
import java.net.InetAddress;
|
|
43 |
import java.net.InetSocketAddress;
|
|
44 |
import java.net.URI;
|
|
45 |
import java.net.http.HttpClient;
|
|
46 |
import java.net.http.HttpRequest;
|
|
47 |
import java.net.http.HttpRequest.BodyPublishers;
|
|
48 |
import java.net.http.HttpResponse;
|
|
49 |
import java.net.http.HttpResponse.BodyHandlers;
|
|
50 |
import com.sun.net.httpserver.HttpServer;
|
|
51 |
import com.sun.net.httpserver.HttpsConfigurator;
|
|
52 |
import com.sun.net.httpserver.HttpsServer;
|
|
53 |
import jdk.testlibrary.SimpleSSLContext;
|
|
54 |
import org.testng.annotations.AfterTest;
|
|
55 |
import org.testng.annotations.BeforeTest;
|
|
56 |
import org.testng.annotations.DataProvider;
|
|
57 |
import org.testng.annotations.Test;
|
|
58 |
import static java.nio.charset.StandardCharsets.US_ASCII;
|
|
59 |
import static org.testng.Assert.assertEquals;
|
|
60 |
|
|
61 |
public class RedirectMethodChange implements HttpServerAdapters {
|
|
62 |
|
|
63 |
SSLContext sslContext;
|
|
64 |
HttpClient client;
|
|
65 |
|
|
66 |
HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]
|
|
67 |
HttpTestServer httpsTestServer; // HTTPS/1.1
|
|
68 |
HttpTestServer http2TestServer; // HTTP/2 ( h2c )
|
|
69 |
HttpTestServer https2TestServer; // HTTP/2 ( h2 )
|
|
70 |
String httpURI;
|
|
71 |
String httpsURI;
|
|
72 |
String http2URI;
|
|
73 |
String https2URI;
|
|
74 |
|
|
75 |
static final String RESPONSE = "Hello world";
|
|
76 |
static final String POST_BODY = "This is the POST body 123909090909090";
|
|
77 |
|
|
78 |
static HttpRequest.BodyPublisher getRequestBodyFor(String method) {
|
|
79 |
switch (method) {
|
|
80 |
case "GET":
|
|
81 |
case "DELETE":
|
|
82 |
case "HEAD":
|
|
83 |
return BodyPublishers.noBody();
|
|
84 |
case "POST":
|
|
85 |
case "PUT":
|
|
86 |
return BodyPublishers.ofString(POST_BODY);
|
|
87 |
default:
|
|
88 |
throw new AssertionError("Unknown method:" + method);
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
@DataProvider(name = "variants")
|
|
93 |
public Object[][] variants() {
|
|
94 |
return new Object[][] {
|
|
95 |
{ httpURI, "GET", 301, "GET" },
|
|
96 |
{ httpURI, "GET", 302, "GET" },
|
|
97 |
{ httpURI, "GET", 303, "GET" },
|
|
98 |
{ httpURI, "GET", 307, "GET" },
|
|
99 |
{ httpURI, "GET", 308, "GET" },
|
|
100 |
{ httpURI, "POST", 301, "GET" },
|
|
101 |
{ httpURI, "POST", 302, "GET" },
|
|
102 |
{ httpURI, "POST", 303, "GET" },
|
|
103 |
{ httpURI, "POST", 307, "POST" },
|
|
104 |
{ httpURI, "POST", 308, "POST" },
|
|
105 |
{ httpURI, "PUT", 301, "PUT" },
|
|
106 |
{ httpURI, "PUT", 302, "PUT" },
|
|
107 |
{ httpURI, "PUT", 303, "GET" },
|
|
108 |
{ httpURI, "PUT", 307, "PUT" },
|
|
109 |
{ httpURI, "PUT", 308, "PUT" },
|
|
110 |
|
|
111 |
{ httpsURI, "GET", 301, "GET" },
|
|
112 |
{ httpsURI, "GET", 302, "GET" },
|
|
113 |
{ httpsURI, "GET", 303, "GET" },
|
|
114 |
{ httpsURI, "GET", 307, "GET" },
|
|
115 |
{ httpsURI, "GET", 308, "GET" },
|
|
116 |
{ httpsURI, "POST", 301, "GET" },
|
|
117 |
{ httpsURI, "POST", 302, "GET" },
|
|
118 |
{ httpsURI, "POST", 303, "GET" },
|
|
119 |
{ httpsURI, "POST", 307, "POST" },
|
|
120 |
{ httpsURI, "POST", 308, "POST" },
|
|
121 |
{ httpsURI, "PUT", 301, "PUT" },
|
|
122 |
{ httpsURI, "PUT", 302, "PUT" },
|
|
123 |
{ httpsURI, "PUT", 303, "GET" },
|
|
124 |
{ httpsURI, "PUT", 307, "PUT" },
|
|
125 |
{ httpsURI, "PUT", 308, "PUT" },
|
|
126 |
|
|
127 |
{ http2URI, "GET", 301, "GET" },
|
|
128 |
{ http2URI, "GET", 302, "GET" },
|
|
129 |
{ http2URI, "GET", 303, "GET" },
|
|
130 |
{ http2URI, "GET", 307, "GET" },
|
|
131 |
{ http2URI, "GET", 308, "GET" },
|
|
132 |
{ http2URI, "POST", 301, "GET" },
|
|
133 |
{ http2URI, "POST", 302, "GET" },
|
|
134 |
{ http2URI, "POST", 303, "GET" },
|
|
135 |
{ http2URI, "POST", 307, "POST" },
|
|
136 |
{ http2URI, "POST", 308, "POST" },
|
|
137 |
{ http2URI, "PUT", 301, "PUT" },
|
|
138 |
{ http2URI, "PUT", 302, "PUT" },
|
|
139 |
{ http2URI, "PUT", 303, "GET" },
|
|
140 |
{ http2URI, "PUT", 307, "PUT" },
|
|
141 |
{ http2URI, "PUT", 308, "PUT" },
|
|
142 |
|
|
143 |
{ https2URI, "GET", 301, "GET" },
|
|
144 |
{ https2URI, "GET", 302, "GET" },
|
|
145 |
{ https2URI, "GET", 303, "GET" },
|
|
146 |
{ https2URI, "GET", 307, "GET" },
|
|
147 |
{ https2URI, "GET", 308, "GET" },
|
|
148 |
{ https2URI, "POST", 301, "GET" },
|
|
149 |
{ https2URI, "POST", 302, "GET" },
|
|
150 |
{ https2URI, "POST", 303, "GET" },
|
|
151 |
{ https2URI, "POST", 307, "POST" },
|
|
152 |
{ https2URI, "POST", 308, "POST" },
|
|
153 |
{ https2URI, "PUT", 301, "PUT" },
|
|
154 |
{ https2URI, "PUT", 302, "PUT" },
|
|
155 |
{ https2URI, "PUT", 303, "GET" },
|
|
156 |
{ https2URI, "PUT", 307, "PUT" },
|
|
157 |
{ https2URI, "PUT", 308, "PUT" },
|
|
158 |
};
|
|
159 |
}
|
|
160 |
|
|
161 |
@Test(dataProvider = "variants")
|
|
162 |
public void test(String uriString,
|
|
163 |
String method,
|
|
164 |
int redirectCode,
|
|
165 |
String expectedMethod)
|
|
166 |
throws Exception
|
|
167 |
{
|
|
168 |
HttpRequest req = HttpRequest.newBuilder(URI.create(uriString))
|
|
169 |
.method(method, getRequestBodyFor(method))
|
|
170 |
.header("X-Redirect-Code", Integer.toString(redirectCode))
|
|
171 |
.header("X-Expect-Method", expectedMethod)
|
|
172 |
.build();
|
|
173 |
HttpResponse<String> resp = client.send(req, BodyHandlers.ofString());
|
|
174 |
|
|
175 |
System.out.println("Response: " + resp + ", body: " + resp.body());
|
|
176 |
assertEquals(resp.statusCode(), 200);
|
|
177 |
assertEquals(resp.body(), RESPONSE);
|
|
178 |
}
|
|
179 |
|
|
180 |
// -- Infrastructure
|
|
181 |
|
|
182 |
@BeforeTest
|
|
183 |
public void setup() throws Exception {
|
|
184 |
sslContext = new SimpleSSLContext().get();
|
|
185 |
if (sslContext == null)
|
|
186 |
throw new AssertionError("Unexpected null sslContext");
|
|
187 |
|
|
188 |
client = HttpClient.newBuilder()
|
|
189 |
.followRedirects(HttpClient.Redirect.NORMAL)
|
|
190 |
.sslContext(sslContext)
|
|
191 |
.build();
|
|
192 |
|
|
193 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
|
|
194 |
|
|
195 |
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
|
|
196 |
String targetURI = "http://" + httpTestServer.serverAuthority() + "/http1/redirect/rmt";
|
|
197 |
RedirMethodChgeHandler handler = new RedirMethodChgeHandler(targetURI);
|
|
198 |
httpTestServer.addHandler(handler, "/http1/");
|
|
199 |
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/test/rmt";
|
|
200 |
|
|
201 |
HttpsServer httpsServer = HttpsServer.create(sa, 0);
|
|
202 |
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
|
203 |
httpsTestServer = HttpTestServer.of(httpsServer);
|
|
204 |
targetURI = "https://" + httpsTestServer.serverAuthority() + "/https1/redirect/rmt";
|
|
205 |
handler = new RedirMethodChgeHandler(targetURI);
|
|
206 |
httpsTestServer.addHandler(handler,"/https1/");
|
|
207 |
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/test/rmt";
|
|
208 |
|
|
209 |
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
|
|
210 |
targetURI = "http://" + http2TestServer.serverAuthority() + "/http2/redirect/rmt";
|
|
211 |
handler = new RedirMethodChgeHandler(targetURI);
|
|
212 |
http2TestServer.addHandler(handler, "/http2/");
|
|
213 |
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/test/rmt";
|
|
214 |
|
56771
|
215 |
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
|
49765
|
216 |
targetURI = "https://" + https2TestServer.serverAuthority() + "/https2/redirect/rmt";
|
|
217 |
handler = new RedirMethodChgeHandler(targetURI);
|
|
218 |
https2TestServer.addHandler(handler, "/https2/");
|
|
219 |
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/test/rmt";
|
|
220 |
|
|
221 |
httpTestServer.start();
|
|
222 |
httpsTestServer.start();
|
|
223 |
http2TestServer.start();
|
|
224 |
https2TestServer.start();
|
|
225 |
}
|
|
226 |
|
|
227 |
@AfterTest
|
|
228 |
public void teardown() throws Exception {
|
|
229 |
httpTestServer.stop();
|
|
230 |
httpsTestServer.stop();
|
|
231 |
http2TestServer.stop();
|
|
232 |
https2TestServer.stop();
|
|
233 |
}
|
|
234 |
|
|
235 |
/**
|
|
236 |
* Stateful handler.
|
|
237 |
*
|
|
238 |
* Request to "<protocol>/test/rmt" is first, with the following checked
|
|
239 |
* headers:
|
|
240 |
* X-Redirect-Code: nnn <the redirect code to send back>
|
|
241 |
* X-Expect-Method: the method that the client should use for the next request
|
|
242 |
*
|
|
243 |
* The following request should be to "<protocol>/redirect/rmt" and should
|
|
244 |
* use the method indicated previously. If all ok, return a 200 response.
|
|
245 |
* Otherwise 50X error.
|
|
246 |
*/
|
|
247 |
static class RedirMethodChgeHandler implements HttpTestHandler {
|
|
248 |
|
|
249 |
boolean inTest;
|
|
250 |
String expectedMethod;
|
|
251 |
|
|
252 |
final String targetURL;
|
|
253 |
RedirMethodChgeHandler(String targetURL) {
|
|
254 |
this.targetURL = targetURL;
|
|
255 |
}
|
|
256 |
|
|
257 |
boolean readAndCheckBody(HttpTestExchange e) throws IOException {
|
|
258 |
String method = e.getRequestMethod();
|
|
259 |
String requestBody;
|
|
260 |
try (InputStream is = e.getRequestBody()) {
|
|
261 |
requestBody = new String(is.readAllBytes(), US_ASCII);
|
|
262 |
}
|
|
263 |
if ((method.equals("POST") || method.equals("PUT"))
|
|
264 |
&& !requestBody.equals(POST_BODY)) {
|
|
265 |
Throwable ex = new RuntimeException("Unexpected request body for "
|
|
266 |
+ method + ": [" + requestBody +"]");
|
|
267 |
ex.printStackTrace();
|
|
268 |
e.sendResponseHeaders(503, 0);
|
|
269 |
return false;
|
|
270 |
}
|
|
271 |
return true;
|
|
272 |
}
|
|
273 |
|
|
274 |
@Override
|
|
275 |
public synchronized void handle(HttpTestExchange he) throws IOException {
|
|
276 |
boolean newtest = he.getRequestURI().getPath().endsWith("/test/rmt");
|
|
277 |
if ((newtest && inTest) || (!newtest && !inTest)) {
|
|
278 |
Throwable ex = new RuntimeException("Unexpected newtest:" + newtest
|
|
279 |
+ ", inTest:" + inTest + ", for " + he.getRequestURI());
|
|
280 |
ex.printStackTrace();
|
|
281 |
he.sendResponseHeaders(500, 0);
|
|
282 |
return;
|
|
283 |
}
|
|
284 |
|
|
285 |
if (newtest) {
|
56619
|
286 |
HttpTestRequestHeaders hdrs = he.getRequestHeaders();
|
49765
|
287 |
String value = hdrs.firstValue("X-Redirect-Code").get();
|
|
288 |
int redirectCode = Integer.parseInt(value);
|
|
289 |
expectedMethod = hdrs.firstValue("X-Expect-Method").get();
|
|
290 |
if (!readAndCheckBody(he))
|
|
291 |
return;
|
56619
|
292 |
HttpTestResponseHeaders headersbuilder = he.getResponseHeaders();
|
|
293 |
headersbuilder.addHeader("Location", targetURL);
|
49765
|
294 |
he.sendResponseHeaders(redirectCode, 0);
|
|
295 |
inTest = true;
|
|
296 |
} else {
|
|
297 |
// should be the redirect
|
|
298 |
if (!he.getRequestURI().getPath().endsWith("/redirect/rmt")) {
|
|
299 |
Throwable ex = new RuntimeException("Unexpected redirected request, got:"
|
|
300 |
+ he.getRequestURI());
|
|
301 |
ex.printStackTrace();
|
|
302 |
he.sendResponseHeaders(501, 0);
|
|
303 |
} else if (!he.getRequestMethod().equals(expectedMethod)) {
|
|
304 |
Throwable ex = new RuntimeException("Expected: " + expectedMethod
|
|
305 |
+ " Got: " + he.getRequestMethod());
|
|
306 |
ex.printStackTrace();
|
|
307 |
he.sendResponseHeaders(504, 0);
|
|
308 |
} else {
|
|
309 |
if (!readAndCheckBody(he))
|
|
310 |
return;
|
|
311 |
he.sendResponseHeaders(200, RESPONSE.length());
|
|
312 |
try (OutputStream os = he.getResponseBody()) {
|
|
313 |
os.write(RESPONSE.getBytes(US_ASCII));
|
|
314 |
}
|
|
315 |
}
|
|
316 |
inTest = false;
|
|
317 |
}
|
|
318 |
}
|
|
319 |
}
|
|
320 |
}
|