author | chegar |
Wed, 07 Feb 2018 14:17:24 +0000 | |
branch | http-client-branch |
changeset 56089 | 42208b2f224e |
parent 56082 | 1da51fab3032 |
child 56167 | 96fa4f49a9ff |
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.Headers; |
|
32 |
import com.sun.net.httpserver.HttpContext; |
|
33 |
import com.sun.net.httpserver.HttpExchange; |
|
34 |
import com.sun.net.httpserver.HttpHandler; |
|
35 |
import com.sun.net.httpserver.HttpServer; |
|
36 |
import java.io.IOException; |
|
37 |
import java.io.OutputStream; |
|
38 |
import java.net.URI; |
|
39 |
import java.util.concurrent.Executors; |
|
40 |
import java.util.concurrent.ExecutorService; |
|
41 |
import java.net.InetSocketAddress; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
42 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
43 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
44 |
import java.net.http.HttpResponse; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
45 |
import static java.net.http.HttpRequest.BodyPublisher.fromString; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
46 |
import static java.net.http.HttpResponse.BodyHandler.asString; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
47 |
import static java.net.http.HttpResponse.BodyHandler.discard; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
48 |
import static java.net.http.HttpClient.Version.HTTP_1_1; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
49 |
import static java.net.http.HttpClient.Version.HTTP_2; |
48083 | 50 |
|
51 |
/** |
|
52 |
*/ |
|
53 |
public class ZeroRedirects { |
|
54 |
static HttpServer s1 ; |
|
55 |
static ExecutorService executor; |
|
56 |
static int port; |
|
57 |
static HttpClient client; |
|
58 |
static URI uri; |
|
59 |
||
60 |
public static void main(String[] args) throws Exception { |
|
61 |
initServer(); |
|
62 |
||
63 |
client = HttpClient.newBuilder() |
|
64 |
.executor(executor) |
|
65 |
.followRedirects(HttpClient.Redirect.ALWAYS) |
|
66 |
.build(); |
|
67 |
try { |
|
68 |
test(); |
|
69 |
} finally { |
|
70 |
s1.stop(0); |
|
71 |
executor.shutdownNow(); |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
public static void test() throws Exception { |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
76 |
System.setProperty("java.net.http.redirects.retrylimit", "0"); |
48083 | 77 |
HttpRequest r = HttpRequest.newBuilder(uri) |
78 |
.GET() |
|
79 |
.build(); |
|
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
48083
diff
changeset
|
80 |
HttpResponse<Void> resp = client.send(r, discard()); |
48083 | 81 |
System.out.printf("Client: response is %d\n", resp.statusCode()); |
82 |
if (resp.statusCode() != 200) |
|
83 |
throw new RuntimeException(); |
|
84 |
} |
|
85 |
||
86 |
static void initServer() throws Exception { |
|
87 |
InetSocketAddress addr = new InetSocketAddress (0); |
|
88 |
s1 = HttpServer.create (addr, 0); |
|
89 |
HttpHandler h = new Handler(); |
|
90 |
||
91 |
HttpContext c1 = s1.createContext("/", h); |
|
92 |
||
93 |
executor = Executors.newCachedThreadPool(); |
|
94 |
s1.setExecutor(executor); |
|
95 |
s1.start(); |
|
96 |
||
97 |
port = s1.getAddress().getPort(); |
|
98 |
uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/foo"); |
|
99 |
System.out.println("HTTP server port = " + port); |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
class Handler implements HttpHandler { |
|
104 |
||
105 |
@Override |
|
106 |
public synchronized void handle(HttpExchange t) |
|
107 |
throws IOException |
|
108 |
{ |
|
109 |
String reply = "Hello world"; |
|
110 |
int len = reply.length(); |
|
111 |
System.out.printf("Sending response 200\n"); |
|
112 |
t.sendResponseHeaders(200, len); |
|
113 |
OutputStream o = t.getResponseBody(); |
|
114 |
o.write(reply.getBytes()); |
|
115 |
t.close(); |
|
116 |
} |
|
117 |
} |