author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58009 | 0daf32316b47 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
41965
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
24 |
/* |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
25 |
* @test |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
26 |
* @bug 6427251 6382788 |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
27 |
* @modules jdk.httpserver |
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
28 |
* @library /test/lib |
41965
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
29 |
* @run main RetryPost |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
30 |
* @run main/othervm -Dsun.net.http.retryPost=false RetryPost noRetry |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
31 |
* @summary HttpURLConnection automatically retries non-idempotent method POST |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
32 |
*/ |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
33 |
|
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
34 |
import com.sun.net.httpserver.HttpContext; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
35 |
import com.sun.net.httpserver.HttpExchange; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
36 |
import com.sun.net.httpserver.HttpHandler; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
37 |
import java.io.IOException; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
38 |
import java.io.OutputStream; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
39 |
import java.net.HttpURLConnection; |
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
40 |
import java.net.InetAddress; |
41965
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
41 |
import java.net.InetSocketAddress; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
42 |
import java.net.Proxy; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
43 |
import java.net.SocketException; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
22625
diff
changeset
|
44 |
import java.net.URL; |
2 | 45 |
import java.util.concurrent.Executors; |
46 |
import java.util.concurrent.ExecutorService; |
|
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
47 |
import jdk.test.lib.net.URIBuilder; |
2 | 48 |
|
49 |
public class RetryPost |
|
50 |
{ |
|
51 |
static boolean shouldRetry = true; |
|
52 |
||
53 |
com.sun.net.httpserver.HttpServer httpServer; |
|
54 |
MyHandler httpHandler; |
|
55 |
ExecutorService executorService; |
|
56 |
||
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54938
diff
changeset
|
57 |
public static void main(String[] args) throws Exception { |
2 | 58 |
if (args.length == 1 && args[0].equals("noRetry")) |
59 |
shouldRetry = false; |
|
60 |
||
61 |
new RetryPost(); |
|
62 |
} |
|
63 |
||
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54938
diff
changeset
|
64 |
public RetryPost() throws Exception { |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54938
diff
changeset
|
65 |
startHttpServer(shouldRetry); |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54938
diff
changeset
|
66 |
doClient(); |
2 | 67 |
} |
68 |
||
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54938
diff
changeset
|
69 |
void doClient() throws Exception { |
2 | 70 |
try { |
71 |
InetSocketAddress address = httpServer.getAddress(); |
|
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
72 |
URL url = URIBuilder.newBuilder() |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
73 |
.scheme("http") |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
74 |
.host(address.getAddress()) |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
75 |
.port(address.getPort()) |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
76 |
.path("/test/") |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
77 |
.toURLUnchecked(); |
22625
50ee5cbd439b
7150539: HttpURLConnection.getResponseMessage() doesn't throw IOException on server error (OS X)
chegar
parents:
5506
diff
changeset
|
78 |
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); |
2 | 79 |
uc.setDoOutput(true); |
80 |
uc.setRequestMethod("POST"); |
|
81 |
uc.getResponseCode(); |
|
82 |
||
83 |
// if we reach here then we have failed |
|
84 |
throw new RuntimeException("Failed: POST request being retried"); |
|
85 |
||
86 |
} catch (SocketException se) { |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55522
diff
changeset
|
87 |
System.out.println("Got expected exception: " + se); |
2 | 88 |
// this is what we expect to happen and is OK. |
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55522
diff
changeset
|
89 |
if (shouldRetry && httpHandler.getCallCount() != 2) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55522
diff
changeset
|
90 |
se.printStackTrace(System.out); |
2 | 91 |
throw new RuntimeException("Failed: Handler should have been called twice. " + |
92 |
"It was called "+ httpHandler.getCallCount() + " times"); |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55522
diff
changeset
|
93 |
} else if (!shouldRetry && httpHandler.getCallCount() != 1) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55522
diff
changeset
|
94 |
se.printStackTrace(System.out); |
2 | 95 |
throw new RuntimeException("Failed: Handler should have only been called once" + |
96 |
"It was called "+ httpHandler.getCallCount() + " times"); |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55522
diff
changeset
|
97 |
} |
2 | 98 |
} finally { |
99 |
httpServer.stop(1); |
|
100 |
executorService.shutdown(); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Http Server |
|
106 |
*/ |
|
107 |
public void startHttpServer(boolean shouldRetry) throws IOException { |
|
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
108 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
109 |
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(loopback, 0), 0); |
2 | 110 |
httpHandler = new MyHandler(shouldRetry); |
111 |
||
112 |
HttpContext ctx = httpServer.createContext("/test/", httpHandler); |
|
113 |
||
114 |
executorService = Executors.newCachedThreadPool(); |
|
115 |
httpServer.setExecutor(executorService); |
|
116 |
httpServer.start(); |
|
117 |
} |
|
118 |
||
119 |
class MyHandler implements HttpHandler { |
|
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54938
diff
changeset
|
120 |
volatile int callCount = 0; |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54938
diff
changeset
|
121 |
final boolean shouldRetry; |
2 | 122 |
|
123 |
public MyHandler(boolean shouldRetry) { |
|
124 |
this.shouldRetry = shouldRetry; |
|
125 |
} |
|
126 |
||
127 |
public void handle(HttpExchange t) throws IOException { |
|
128 |
callCount++; |
|
129 |
||
130 |
if (callCount > 1 && !shouldRetry) { |
|
131 |
// if this bug has been fixed then this method will not be called twice |
|
132 |
// when -Dhttp.retryPost=false |
|
133 |
t.sendResponseHeaders(400, -1); // indicate failure by returning 400 |
|
134 |
} else { |
|
135 |
// simply close out the stream without sending any data. |
|
136 |
OutputStream os = t.getResponseBody(); |
|
137 |
os.close(); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
public int getCallCount() { |
|
142 |
return callCount; |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
} |