2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. |
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
8 * |
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 * version 2 for more details (a copy is included in the LICENSE file that |
12 * version 2 for more details (a copy is included in the LICENSE file that |
41 import java.io.InputStream; |
39 import java.io.InputStream; |
42 import java.io.OutputStream; |
40 import java.io.OutputStream; |
43 import java.net.InetSocketAddress; |
41 import java.net.InetSocketAddress; |
44 import java.net.PasswordAuthentication; |
42 import java.net.PasswordAuthentication; |
45 import java.net.URI; |
43 import java.net.URI; |
46 import java.net.http.*; |
44 import jdk.incubator.http.*; |
47 import java.util.concurrent.ExecutorService; |
45 import java.util.concurrent.ExecutorService; |
48 import java.util.concurrent.Executors; |
46 import java.util.concurrent.Executors; |
49 import static java.nio.charset.StandardCharsets.US_ASCII; |
47 import static java.nio.charset.StandardCharsets.US_ASCII; |
|
48 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString; |
|
49 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
50 |
50 |
51 public class BasicAuthTest { |
51 public class BasicAuthTest { |
52 |
52 |
53 static volatile boolean ok; |
53 static volatile boolean ok; |
54 static final String RESPONSE = "Hello world"; |
54 static final String RESPONSE = "Hello world"; |
65 ClientAuth ca = new ClientAuth(); |
65 ClientAuth ca = new ClientAuth(); |
66 ServerAuth sa = new ServerAuth("foo realm"); |
66 ServerAuth sa = new ServerAuth("foo realm"); |
67 serverContext.setAuthenticator(sa); |
67 serverContext.setAuthenticator(sa); |
68 server.setExecutor(e); |
68 server.setExecutor(e); |
69 server.start(); |
69 server.start(); |
70 HttpClient client = HttpClient.create() |
70 HttpClient client = HttpClient.newBuilder() |
71 .authenticator(ca) |
71 .authenticator(ca) |
72 .build(); |
72 .build(); |
73 |
73 |
74 try { |
74 try { |
75 URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo"); |
75 URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo"); |
76 HttpRequest req = client.request(uri).GET(); |
76 HttpRequest req = HttpRequest.newBuilder(uri).GET().build(); |
77 |
77 |
78 HttpResponse resp = req.response(); |
78 HttpResponse resp = client.send(req, asString()); |
79 ok = resp.statusCode() == 200 && |
79 ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE); |
80 resp.body(HttpResponse.asString()).equals(RESPONSE); |
|
81 |
80 |
82 if (!ok || ca.count != 1) |
81 if (!ok || ca.count != 1) |
83 throw new RuntimeException("Test failed"); |
82 throw new RuntimeException("Test failed"); |
84 |
83 |
85 // repeat same request, should succeed but no additional authenticator calls |
84 // repeat same request, should succeed but no additional authenticator calls |
86 |
85 |
87 req = client.request(uri).GET(); |
86 resp = client.send(req, asString()); |
88 resp = req.response(); |
87 ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE); |
89 ok = resp.statusCode() == 200 && |
|
90 resp.body(HttpResponse.asString()).equals(RESPONSE); |
|
91 |
88 |
92 if (!ok || ca.count != 1) |
89 if (!ok || ca.count != 1) |
93 throw new RuntimeException("Test failed"); |
90 throw new RuntimeException("Test failed"); |
94 |
91 |
95 // try a POST |
92 // try a POST |
96 |
93 |
97 req = client.request(uri) |
94 req = HttpRequest.newBuilder(uri) |
98 .body(HttpRequest.fromString(POST_BODY)) |
95 .POST(fromString(POST_BODY)) |
99 .POST(); |
96 .build(); |
100 resp = req.response(); |
97 resp = client.send(req, asString()); |
101 ok = resp.statusCode() == 200; |
98 ok = resp.statusCode() == 200; |
102 |
99 |
103 if (!ok || ca.count != 1) |
100 if (!ok || ca.count != 1) |
104 throw new RuntimeException("Test failed"); |
101 throw new RuntimeException("Test failed"); |
105 } finally { |
102 } finally { |
106 client.executorService().shutdownNow(); |
|
107 server.stop(0); |
103 server.stop(0); |
108 e.shutdownNow(); |
104 e.shutdownNow(); |
109 } |
105 } |
110 System.out.println("OK"); |
106 System.out.println("OK"); |
111 } |
107 } |