author | chegar |
Thu, 08 Mar 2018 17:42:16 +0000 | |
branch | http-client-branch |
changeset 56265 | ec34ae013fbe |
parent 56167 | 96fa4f49a9ff |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
2 |
* Copyright (c) 2017, 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 |
* @bug 8164941 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
27 |
* @modules java.net.http java.logging jdk.httpserver |
48083 | 28 |
* @run main/othervm ZeroRedirects |
29 |
*/ |
|
30 |
||
31 |
import com.sun.net.httpserver.HttpContext; |
|
32 |
import com.sun.net.httpserver.HttpExchange; |
|
33 |
import com.sun.net.httpserver.HttpHandler; |
|
34 |
import com.sun.net.httpserver.HttpServer; |
|
35 |
import java.io.IOException; |
|
36 |
import java.io.OutputStream; |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56167
diff
changeset
|
37 |
import java.net.InetAddress; |
48083 | 38 |
import java.net.URI; |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
39 |
import java.net.http.HttpResponse.BodyHandlers; |
48083 | 40 |
import java.util.concurrent.Executors; |
41 |
import java.util.concurrent.ExecutorService; |
|
42 |
import java.net.InetSocketAddress; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
43 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
44 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
45 |
import java.net.http.HttpResponse; |
48083 | 46 |
|
47 |
public class ZeroRedirects { |
|
48 |
static HttpServer s1 ; |
|
49 |
static ExecutorService executor; |
|
50 |
static int port; |
|
51 |
static HttpClient client; |
|
52 |
static URI uri; |
|
53 |
||
54 |
public static void main(String[] args) throws Exception { |
|
55 |
initServer(); |
|
56 |
||
57 |
client = HttpClient.newBuilder() |
|
58 |
.executor(executor) |
|
59 |
.followRedirects(HttpClient.Redirect.ALWAYS) |
|
60 |
.build(); |
|
61 |
try { |
|
62 |
test(); |
|
63 |
} finally { |
|
64 |
s1.stop(0); |
|
65 |
executor.shutdownNow(); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
public static void test() throws Exception { |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
70 |
System.setProperty("java.net.http.redirects.retrylimit", "0"); |
48083 | 71 |
HttpRequest r = HttpRequest.newBuilder(uri) |
72 |
.GET() |
|
73 |
.build(); |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
74 |
HttpResponse<Void> resp = client.send(r, BodyHandlers.discarding()); |
48083 | 75 |
System.out.printf("Client: response is %d\n", resp.statusCode()); |
76 |
if (resp.statusCode() != 200) |
|
77 |
throw new RuntimeException(); |
|
78 |
} |
|
79 |
||
80 |
static void initServer() throws Exception { |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56167
diff
changeset
|
81 |
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56167
diff
changeset
|
82 |
s1 = HttpServer.create(addr, 0); |
48083 | 83 |
HttpHandler h = new Handler(); |
84 |
||
85 |
HttpContext c1 = s1.createContext("/", h); |
|
86 |
||
87 |
executor = Executors.newCachedThreadPool(); |
|
88 |
s1.setExecutor(executor); |
|
89 |
s1.start(); |
|
90 |
||
91 |
port = s1.getAddress().getPort(); |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56167
diff
changeset
|
92 |
uri = new URI("http://localhost:" + port + "/foo"); |
48083 | 93 |
System.out.println("HTTP server port = " + port); |
94 |
} |
|
95 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
96 |
static class Handler implements HttpHandler { |
48083 | 97 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
98 |
@Override |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
99 |
public synchronized void handle(HttpExchange t) throws IOException { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
100 |
String reply = "Hello world"; |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
101 |
int len = reply.length(); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
102 |
System.out.printf("Sending response 200\n"); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
103 |
t.sendResponseHeaders(200, len); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
104 |
OutputStream o = t.getResponseBody(); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
105 |
o.write(reply.getBytes()); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
106 |
t.close(); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
107 |
} |
48083 | 108 |
} |
109 |
} |