/* * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test * @modules java.net.http * jdk.httpserver * @run main/othervm MultiAuthTest * @summary Basic Authentication test with multiple clients issuing * multiple requests. Includes password changes * on server and client side.
*/
publicstaticvoid main(String[] args) throws Exception {
ExecutorService e = Executors.newCachedThreadPool();
ServerAuth sa = new ServerAuth("foo realm");
HttpServer server = createServer(e, sa); int port = server.getAddress().getPort();
System.out.println("Server port = " + port);
try {
URI uri = new URI("http://localhost:" + port + "/test/foo");
System.out.println("URI: " + uri);
System.out.println("\nTesting with client #1, Authenticator #1");
test(client1, ca, uri, 1, null);
System.out.println("Testing again with client #1, Authenticator #1");
test(client1, ca, uri, 1, null);
System.out.println("Testing with client #2, Authenticator #1");
test(client2, ca, uri, 2, null);
System.out.println("Testing with default client"
+ " (HttpClient.newHttpClient()), no authenticator");
test(HttpClient.newHttpClient(), ca, uri, 2, IOException.class);
int count = ca.count.get(); // depends on retry limit...
System.out.println("Testing with client #1, Authenticator #1");
test(client1, ca, uri, count+1, null);
System.out.println("Testing again with client #1, Authenticator #1");
test(client1, ca, uri, count+1, null);
System.out.println("Testing with client #2, Authenticator #1");
test(client2, ca, uri, count+2, null);
sa.passwd = ca.passwd = "changed#2";
System.out.println("\nChanged password on both sides\n");
System.out.println("Testing with client #1, Authenticator #1");
test(client1, ca, uri, count+3, null);
System.out.println("Testing again with client #1, Authenticator #1");
test(client1, ca, uri, count+3, null);
System.out.println("Testing with client #2, Authenticator #1");
test(client2, ca, uri, count+4, null);
} finally {
server.stop(0);
e.shutdownNow();
}
System.out.println("OK");
}
staticvoid test(HttpClient client,
ClientAuth ca,
URI uri, int expectCount, Class<? extends Exception> expectFailure) throws IOException, InterruptedException
{
HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
// repeat same request, should succeed but no additional authenticator calls
resp = client.send(req, BodyHandlers.ofString());
ok = resp.statusCode() == 200 &&
resp.body().equals(RESPONSE);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.