author | dfuchs |
Wed, 23 Oct 2019 15:54:39 +0100 | |
changeset 58758 | 2b13d126a2d8 |
parent 52121 | 934969c63223 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
52121
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
* @test |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
* @bug 8156514 |
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
27 |
* @library /test/lib server |
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
28 |
* @build jdk.test.lib.net.SimpleSSLContext |
48083 | 29 |
* @modules java.base/sun.net.www.http |
49765 | 30 |
* java.net.http/jdk.internal.net.http.common |
31 |
* java.net.http/jdk.internal.net.http.frame |
|
32 |
* java.net.http/jdk.internal.net.http.hpack |
|
33 |
* @run testng/othervm |
|
34 |
* -Djdk.httpclient.HttpClient.log=frames,ssl,requests,responses,errors |
|
35 |
* -Djdk.internal.httpclient.debug=true |
|
36 |
* RedirectTest |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
|
49765 | 39 |
import java.net.InetSocketAddress; |
40 |
import java.net.URI; |
|
41 |
import java.net.http.HttpClient; |
|
42 |
import java.net.http.HttpRequest; |
|
43 |
import java.net.http.HttpRequest.BodyPublishers; |
|
44 |
import java.net.http.HttpResponse; |
|
45 |
import java.net.http.HttpResponse.BodyHandlers; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
import java.util.concurrent.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.util.function.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.util.Arrays; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
import java.util.Iterator; |
48083 | 50 |
import org.testng.annotations.Test; |
49765 | 51 |
import static java.net.http.HttpClient.Version.HTTP_2; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
public class RedirectTest { |
48083 | 54 |
static int httpPort; |
55 |
static Http2TestServer httpServer; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
static HttpClient client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
static String httpURIString, altURIString1, altURIString2; |
48083 | 59 |
static URI httpURI, altURI1, altURI2; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
static Supplier<String> sup(String... args) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
Iterator<String> i = Arrays.asList(args).iterator(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
// need to know when to stop calling it. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
return () -> i.next(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
|
48083 | 67 |
static class Redirector extends Http2RedirectHandler { |
68 |
private InetSocketAddress remoteAddr; |
|
69 |
private boolean error = false; |
|
70 |
||
71 |
Redirector(Supplier<String> supplier) { |
|
72 |
super(supplier); |
|
73 |
} |
|
74 |
||
75 |
protected synchronized void examineExchange(Http2TestExchange ex) { |
|
76 |
InetSocketAddress addr = ex.getRemoteAddress(); |
|
77 |
if (remoteAddr == null) { |
|
78 |
remoteAddr = addr; |
|
79 |
return; |
|
80 |
} |
|
81 |
// check that the client addr/port stays the same, proving |
|
82 |
// that the connection didn't get dropped. |
|
83 |
if (!remoteAddr.equals(addr)) { |
|
84 |
System.err.printf("Error %s/%s\n", remoteAddr.toString(), |
|
85 |
addr.toString()); |
|
86 |
error = true; |
|
87 |
} |
|
88 |
} |
|
89 |
||
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
52121
diff
changeset
|
90 |
@Override |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
52121
diff
changeset
|
91 |
protected int redirectCode() { |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
52121
diff
changeset
|
92 |
return 308; // we need to use a code that preserves the body |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
52121
diff
changeset
|
93 |
} |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
52121
diff
changeset
|
94 |
|
48083 | 95 |
public synchronized boolean error() { |
96 |
return error; |
|
97 |
} |
|
98 |
} |
|
99 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
100 |
static void initialize() throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
101 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
client = getClient(); |
48083 | 103 |
httpServer = new Http2TestServer(false, 0, null, null); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
httpPort = httpServer.getAddress().getPort(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
105 |
|
48083 | 106 |
// urls are accessed in sequence below. The first two are on |
107 |
// different servers. Third on same server as second. So, the |
|
108 |
// client should use the same http connection. |
|
49765 | 109 |
httpURIString = "http://localhost:" + httpPort + "/foo/"; |
48083 | 110 |
httpURI = URI.create(httpURIString); |
49765 | 111 |
altURIString1 = "http://localhost:" + httpPort + "/redir"; |
48083 | 112 |
altURI1 = URI.create(altURIString1); |
49765 | 113 |
altURIString2 = "http://localhost:" + httpPort + "/redir_again"; |
48083 | 114 |
altURI2 = URI.create(altURIString2); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
|
48083 | 116 |
Redirector r = new Redirector(sup(altURIString1, altURIString2)); |
117 |
httpServer.addHandler(r, "/foo"); |
|
118 |
httpServer.addHandler(r, "/redir"); |
|
119 |
httpServer.addHandler(new Http2EchoHandler(), "/redir_again"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
httpServer.start(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
} catch (Throwable e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
System.err.println("Throwing now"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
e.printStackTrace(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
throw e; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
|
48083 | 129 |
@Test |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
public static void test() throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
initialize(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
simpleTest(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
httpServer.stop(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
static HttpClient getClient() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
if (client == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
client = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
.followRedirects(HttpClient.Redirect.ALWAYS) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
.version(HTTP_2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
return client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
148 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
static URI getURI() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
return URI.create(httpURIString); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
static void checkStatus(int expected, int found) throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
if (expected != found) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
System.err.printf ("Test failed: wrong status code %d/%d\n", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
expected, found); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
throw new RuntimeException("Test failed"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
|
48083 | 161 |
static void checkURIs(URI expected, URI found) throws Exception { |
162 |
System.out.printf ("Expected: %s, Found: %s\n", expected.toString(), found.toString()); |
|
163 |
if (!expected.equals(found)) { |
|
164 |
System.err.printf ("Test failed: wrong URI %s/%s\n", |
|
165 |
expected.toString(), found.toString()); |
|
166 |
throw new RuntimeException("Test failed"); |
|
167 |
} |
|
168 |
} |
|
169 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
static void checkStrings(String expected, String found) throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
if (!expected.equals(found)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
System.err.printf ("Test failed: wrong string %s/%s\n", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
expected, found); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
throw new RuntimeException("Test failed"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
|
48083 | 178 |
static void check(boolean cond, Object... msg) { |
179 |
if (cond) |
|
180 |
return; |
|
181 |
StringBuilder sb = new StringBuilder(); |
|
182 |
for (Object o : msg) |
|
183 |
sb.append(o); |
|
184 |
throw new RuntimeException(sb.toString()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
static final String SIMPLE_STRING = "Hello world Goodbye world"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
static void simpleTest() throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
URI uri = getURI(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
191 |
System.err.println("Request to " + uri); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
HttpClient client = getClient(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
HttpRequest req = HttpRequest.newBuilder(uri) |
49765 | 195 |
.POST(BodyPublishers.ofString(SIMPLE_STRING)) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
.build(); |
49765 | 197 |
CompletableFuture<HttpResponse<String>> cf = client.sendAsync(req, BodyHandlers.ofString()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
HttpResponse<String> response = cf.join(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
checkStatus(200, response.statusCode()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
201 |
String responseBody = response.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
202 |
checkStrings(SIMPLE_STRING, responseBody); |
48083 | 203 |
checkURIs(response.uri(), altURI2); |
204 |
||
205 |
// check two previous responses |
|
206 |
HttpResponse<String> prev = response.previousResponse() |
|
207 |
.orElseThrow(() -> new RuntimeException("no previous response")); |
|
208 |
checkURIs(prev.uri(), altURI1); |
|
209 |
||
210 |
prev = prev.previousResponse() |
|
211 |
.orElseThrow(() -> new RuntimeException("no previous response")); |
|
212 |
checkURIs(prev.uri(), httpURI); |
|
213 |
||
214 |
checkPreviousRedirectResponses(req, response); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
System.err.println("DONE"); |
48083 | 217 |
} |
218 |
||
219 |
static void checkPreviousRedirectResponses(HttpRequest initialRequest, |
|
220 |
HttpResponse<?> finalResponse) { |
|
221 |
// there must be at least one previous response |
|
222 |
finalResponse.previousResponse() |
|
223 |
.orElseThrow(() -> new RuntimeException("no previous response")); |
|
224 |
||
225 |
HttpResponse<?> response = finalResponse; |
|
226 |
do { |
|
227 |
URI uri = response.uri(); |
|
228 |
response = response.previousResponse().get(); |
|
229 |
check(300 <= response.statusCode() && response.statusCode() <= 309, |
|
230 |
"Expected 300 <= code <= 309, got:" + response.statusCode()); |
|
231 |
check(response.body() == null, "Unexpected body: " + response.body()); |
|
232 |
String locationHeader = response.headers().firstValue("Location") |
|
233 |
.orElseThrow(() -> new RuntimeException("no previous Location")); |
|
234 |
check(uri.toString().endsWith(locationHeader), |
|
235 |
"URI: " + uri + ", Location: " + locationHeader); |
|
236 |
} while (response.previousResponse().isPresent()); |
|
237 |
||
238 |
// initial |
|
239 |
check(initialRequest.equals(response.request()), |
|
240 |
"Expected initial request [%s] to equal last prev req [%s]", |
|
241 |
initialRequest, response.request()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
} |