Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/CookieHandler/EmptyCookieHeader.java
38812 views
1
/*
2
* Copyright (c) 2013, 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 8015799
27
* @summary HttpURLConnection.getHeaderFields() throws IllegalArgumentException
28
*/
29
30
import com.sun.net.httpserver.*;
31
import java.io.IOException;
32
import java.io.OutputStream;
33
import java.net.*;
34
import java.util.*;
35
36
public class EmptyCookieHeader {
37
38
public static void main(String[] args) throws Exception {
39
new EmptyCookieHeader().runTest();
40
}
41
42
public void runTest() throws Exception {
43
final CookieHandler oldHandler = CookieHandler.getDefault();
44
CookieHandler.setDefault(new TestCookieHandler());
45
HttpServer s = HttpServer.create(new InetSocketAddress(0), 0);
46
try {
47
startServer(s);
48
URL url = new URL("http://localhost:" + s.getAddress().getPort() + "/");
49
HttpURLConnection c = (HttpURLConnection)url.openConnection();
50
c.getHeaderFields();
51
} finally {
52
CookieHandler.setDefault(oldHandler);
53
s.stop(0);
54
}
55
}
56
57
static void startServer(HttpServer server) throws IOException {
58
server.createContext("/", new EmptyCookieHandler());
59
server.start();
60
}
61
62
static class EmptyCookieHandler implements HttpHandler {
63
64
@Override
65
public void handle(HttpExchange exchange) throws IOException {
66
String requestMethod = exchange.getRequestMethod();
67
if (requestMethod.equalsIgnoreCase("GET")) {
68
Headers responseHeaders = exchange.getResponseHeaders();
69
responseHeaders.set("Content-Type", "text/plain");
70
responseHeaders.set("Date", "June 13th 2012");
71
72
// No domain value set
73
responseHeaders.set("Set-Cookie2", "name=value");
74
responseHeaders.set("Set-Cookie2", "");
75
exchange.sendResponseHeaders(200, 0);
76
try (OutputStream os = exchange.getResponseBody()) {
77
String str = "This is what the server sent!";
78
os.write(str.getBytes());
79
}
80
}
81
}
82
}
83
84
class TestCookieHandler extends CookieHandler {
85
@Override
86
public Map<String,List<String>> get(URI uri,
87
Map<String,List<String>> respH) {
88
return new HashMap<>();
89
}
90
91
@Override
92
public void put(URI uri, Map<String,List<String >> respH) { }
93
}
94
}
95
96