author | jboes |
Fri, 08 Nov 2019 11:15:16 +0000 | |
changeset 59029 | 3786a0962570 |
parent 49765 | ee6f7a61f3a5 |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
36131 | 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 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
7 |
* published by the Free Software Foundation. |
36131 | 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 |
|
40684
2e37c119dc2a
8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents:
38883
diff
changeset
|
21 |
* questions. |
36131 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 8087112 |
|
49765 | 27 |
* @modules java.net.http |
38883 | 28 |
* jdk.httpserver |
36131 | 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; |
|
49765 | 41 |
import java.net.InetAddress; |
36131 | 42 |
import java.net.InetSocketAddress; |
43 |
import java.net.PasswordAuthentication; |
|
44 |
import java.net.URI; |
|
49765 | 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; |
|
36131 | 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 { |
|
49765 | 61 |
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(),0); |
62 |
HttpServer server = HttpServer.create(addr, 10); |
|
36131 | 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(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
74 |
HttpClient client = HttpClient.newBuilder() |
36131 | 75 |
.authenticator(ca) |
76 |
.build(); |
|
77 |
||
78 |
try { |
|
49765 | 79 |
URI uri = new URI("http://localhost:" + Integer.toString(port) + "/test/foo"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
80 |
HttpRequest req = HttpRequest.newBuilder(uri).GET().build(); |
36131 | 81 |
|
49765 | 82 |
HttpResponse resp = client.send(req, BodyHandlers.ofString()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
83 |
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE); |
36131 | 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 |
||
49765 | 90 |
resp = client.send(req, BodyHandlers.ofString()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
91 |
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE); |
36131 | 92 |
|
93 |
if (!ok || ca.count != 1) |
|
94 |
throw new RuntimeException("Test failed"); |
|
95 |
||
96 |
// try a POST |
|
97 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
98 |
req = HttpRequest.newBuilder(uri) |
49765 | 99 |
.POST(BodyPublishers.ofString(POST_BODY)) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
100 |
.build(); |
49765 | 101 |
resp = client.send(req, BodyHandlers.ofString()); |
36131 | 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 |
} |