author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58365 | 73950479184b |
permissions | -rw-r--r-- |
18174 | 1 |
/* |
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
55399
diff
changeset
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
18174 | 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 |
import com.sun.net.httpserver.*; |
|
26 |
import java.io.IOException; |
|
27 |
import java.io.OutputStream; |
|
28 |
import java.net.*; |
|
29 |
import java.util.List; |
|
30 |
import java.util.Map; |
|
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
55399
diff
changeset
|
31 |
import static java.net.Proxy.NO_PROXY; |
18174 | 32 |
|
33 |
/* |
|
34 |
* @test |
|
35 |
* @bug 7169142 |
|
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
36 |
* @key intermittent |
38883 | 37 |
* @modules jdk.httpserver |
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
38 |
* @summary CookieHandler does not work with localhost. This requires |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
39 |
* binding to the wildcard address and might fail intermittently |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
40 |
* due to port reuse issues. |
18174 | 41 |
* @run main/othervm LocalHostCookie |
42 |
*/ |
|
43 |
public class LocalHostCookie { |
|
44 |
||
45 |
public static void main(String[] args) throws Exception { |
|
46 |
new LocalHostCookie().runTest(); |
|
47 |
} |
|
48 |
||
49 |
public void runTest() throws Exception { |
|
50 |
Server s = null; |
|
51 |
try { |
|
52 |
s = new Server(); |
|
53 |
s.startServer(); |
|
54 |
URL url = new URL("http","localhost", s.getPort(), "/"); |
|
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
55399
diff
changeset
|
55 |
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(NO_PROXY); |
18174 | 56 |
urlConnection.setRequestMethod("GET"); |
57 |
urlConnection.setDoOutput(true); |
|
58 |
urlConnection.connect(); |
|
59 |
urlConnection.getInputStream(); |
|
60 |
||
61 |
CookieHandler cookieHandler = CookieHandler.getDefault(); |
|
62 |
if (cookieHandler == null) { |
|
63 |
cookieHandler = new java.net.CookieManager(); |
|
64 |
CookieHandler.setDefault(cookieHandler); |
|
65 |
} |
|
66 |
cookieHandler.put(urlConnection.getURL().toURI(), |
|
67 |
urlConnection.getHeaderFields()); |
|
68 |
Map<String, List<String>> map = |
|
69 |
cookieHandler.get(urlConnection.getURL().toURI(), |
|
70 |
urlConnection.getHeaderFields()); |
|
71 |
if (map.containsKey("Cookie")) { |
|
72 |
List<String> list = map.get("Cookie"); |
|
73 |
// name-value list will be empty if ".local" is not appended |
|
74 |
if (list == null || list.size() == 0) { |
|
75 |
throw new RuntimeException("Test failed!"); |
|
76 |
} |
|
77 |
} |
|
78 |
} finally { |
|
19826
460110102fd4
8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents:
18174
diff
changeset
|
79 |
if (s != null) { |
460110102fd4
8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents:
18174
diff
changeset
|
80 |
s.stopServer(); |
460110102fd4
8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents:
18174
diff
changeset
|
81 |
} |
18174 | 82 |
} |
83 |
} |
|
84 |
||
85 |
class Server { |
|
86 |
HttpServer server; |
|
87 |
||
88 |
public void startServer() { |
|
89 |
InetSocketAddress addr = new InetSocketAddress(0); |
|
90 |
try { |
|
91 |
server = HttpServer.create(addr, 0); |
|
92 |
} catch (IOException ioe) { |
|
93 |
throw new RuntimeException("Server could not be created"); |
|
94 |
} |
|
95 |
||
96 |
server.createContext("/", new MyCookieHandler()); |
|
97 |
server.start(); |
|
98 |
} |
|
99 |
||
100 |
public int getPort() { |
|
101 |
return server.getAddress().getPort(); |
|
102 |
} |
|
103 |
||
104 |
public void stopServer() { |
|
19826
460110102fd4
8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents:
18174
diff
changeset
|
105 |
if (server != null) { |
460110102fd4
8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents:
18174
diff
changeset
|
106 |
server.stop(0); |
460110102fd4
8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents:
18174
diff
changeset
|
107 |
} |
18174 | 108 |
} |
109 |
} |
|
110 |
||
111 |
class MyCookieHandler implements HttpHandler { |
|
112 |
||
113 |
@Override |
|
114 |
public void handle(HttpExchange exchange) throws IOException { |
|
115 |
String requestMethod = exchange.getRequestMethod(); |
|
116 |
if (requestMethod.equalsIgnoreCase("GET")){ |
|
117 |
Headers responseHeaders = exchange.getResponseHeaders(); |
|
118 |
responseHeaders.set("Content-Type", "text/plain"); |
|
119 |
responseHeaders.set("Date", "June 13th 2012"); |
|
120 |
// No domain value set |
|
121 |
responseHeaders.set("Set-Cookie2", "name=value"); |
|
122 |
exchange.sendResponseHeaders(200, 0); |
|
123 |
OutputStream os = exchange.getResponseBody(); |
|
124 |
String str = "This is what the server sent!"; |
|
125 |
os.write(str.getBytes()); |
|
126 |
os.flush(); |
|
127 |
os.close(); |
|
128 |
} |
|
129 |
} |
|
130 |
} |
|
131 |
} |