Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/BasicAuthTest.java
41149 views
1
/*
2
* Copyright (c) 2015, 2018, 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 8087112
27
* @modules java.net.http
28
* jdk.httpserver
29
* @run main/othervm BasicAuthTest
30
* @summary Basic Authentication Test
31
*/
32
33
import com.sun.net.httpserver.BasicAuthenticator;
34
import com.sun.net.httpserver.HttpContext;
35
import com.sun.net.httpserver.HttpExchange;
36
import com.sun.net.httpserver.HttpHandler;
37
import com.sun.net.httpserver.HttpServer;
38
import java.io.IOException;
39
import java.io.InputStream;
40
import java.io.OutputStream;
41
import java.net.InetAddress;
42
import java.net.InetSocketAddress;
43
import java.net.PasswordAuthentication;
44
import java.net.URI;
45
import java.net.http.HttpClient;
46
import java.net.http.HttpRequest;
47
import java.net.http.HttpRequest.BodyPublishers;
48
import java.net.http.HttpResponse;
49
import java.net.http.HttpResponse.BodyHandlers;
50
import java.util.concurrent.ExecutorService;
51
import java.util.concurrent.Executors;
52
import static java.nio.charset.StandardCharsets.US_ASCII;
53
54
public class BasicAuthTest {
55
56
static volatile boolean ok;
57
static final String RESPONSE = "Hello world";
58
static final String POST_BODY = "This is the POST body 123909090909090";
59
60
public static void main(String[] args) throws Exception {
61
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(),0);
62
HttpServer server = HttpServer.create(addr, 10);
63
ExecutorService e = Executors.newCachedThreadPool();
64
Handler h = new Handler();
65
HttpContext serverContext = server.createContext("/test", h);
66
int port = server.getAddress().getPort();
67
System.out.println("Server port = " + port);
68
69
ClientAuth ca = new ClientAuth();
70
ServerAuth sa = new ServerAuth("foo realm");
71
serverContext.setAuthenticator(sa);
72
server.setExecutor(e);
73
server.start();
74
HttpClient client = HttpClient.newBuilder()
75
.authenticator(ca)
76
.build();
77
78
try {
79
URI uri = new URI("http://localhost:" + Integer.toString(port) + "/test/foo");
80
HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
81
82
HttpResponse resp = client.send(req, BodyHandlers.ofString());
83
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);
84
85
if (!ok || ca.count != 1)
86
throw new RuntimeException("Test failed");
87
88
// repeat same request, should succeed but no additional authenticator calls
89
90
resp = client.send(req, BodyHandlers.ofString());
91
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);
92
93
if (!ok || ca.count != 1)
94
throw new RuntimeException("Test failed");
95
96
// try a POST
97
98
req = HttpRequest.newBuilder(uri)
99
.POST(BodyPublishers.ofString(POST_BODY))
100
.build();
101
resp = client.send(req, BodyHandlers.ofString());
102
ok = resp.statusCode() == 200;
103
104
if (!ok || ca.count != 1)
105
throw new RuntimeException("Test failed");
106
} finally {
107
server.stop(0);
108
e.shutdownNow();
109
}
110
System.out.println("OK");
111
}
112
113
static class ServerAuth extends BasicAuthenticator {
114
115
ServerAuth(String realm) {
116
super(realm);
117
}
118
119
@Override
120
public boolean checkCredentials(String username, String password) {
121
if (!"user".equals(username) || !"passwd".equals(password)) {
122
return false;
123
}
124
return true;
125
}
126
127
}
128
129
static class ClientAuth extends java.net.Authenticator {
130
volatile int count = 0;
131
132
@Override
133
protected PasswordAuthentication getPasswordAuthentication() {
134
count++;
135
return new PasswordAuthentication("user", "passwd".toCharArray());
136
}
137
}
138
139
static class Handler implements HttpHandler {
140
static volatile boolean ok;
141
142
@Override
143
public void handle(HttpExchange he) throws IOException {
144
String method = he.getRequestMethod();
145
InputStream is = he.getRequestBody();
146
if (method.equalsIgnoreCase("POST")) {
147
String requestBody = new String(is.readAllBytes(), US_ASCII);
148
if (!requestBody.equals(POST_BODY)) {
149
he.sendResponseHeaders(500, -1);
150
ok = false;
151
} else {
152
he.sendResponseHeaders(200, -1);
153
ok = true;
154
}
155
} else { // GET
156
he.sendResponseHeaders(200, RESPONSE.length());
157
OutputStream os = he.getResponseBody();
158
os.write(RESPONSE.getBytes(US_ASCII));
159
os.close();
160
ok = true;
161
}
162
}
163
}
164
}
165
166