Path: blob/master/test/jdk/java/net/httpclient/AuthSchemesTest.java
41152 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 821723726* @library /test/lib27* @modules java.net.http28* @run main/othervm AuthSchemesTest29* @summary HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers30*/3132import java.io.IOException;33import java.io.InputStream;34import java.io.OutputStream;35import java.net.*;36import java.net.Authenticator;37import java.net.http.HttpClient;38import java.net.http.HttpRequest;39import java.net.http.HttpResponse;40import jdk.test.lib.net.URIBuilder;4142public class AuthSchemesTest {43static class BasicServer extends Thread {4445ServerSocket server;4647Socket s;48InputStream is;49OutputStream os;50static final String RESPONSE = "Hello world";51static final String respLength = Integer.toString(RESPONSE.length());52static final String realm = "wally world";5354String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+55"WWW-Authenticate: BarScheme\r\n" +56"WWW-Authenticate: FooScheme realm=\""+realm+"\"\r\n" +57"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n" +58"WWW-Authenticate: WoofScheme\r\n\r\n";5960String reply2 = "HTTP/1.1 200 OK\r\n"+61"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +62"Server: Apache/1.3.14 (Unix)\r\n" +63"Connection: close\r\n" +64"Content-Type: text/html; charset=iso-8859-1\r\n" +65"Content-Length: " + respLength + "\r\n\r\n";6667BasicServer(ServerSocket s) {68server = s;69}7071String response() {72return RESPONSE;73}7475void readAll(Socket s) throws IOException {76byte[] buf = new byte [128];77InputStream is = s.getInputStream();78s.setSoTimeout(1000);79try {80while (is.read(buf) > 0) ;81} catch (SocketTimeoutException x) { }82}8384public void run() {85try {86System.out.println("Server 1: accept");87s = server.accept();88System.out.println("accepted");89os = s.getOutputStream();90os.write(reply1.getBytes());91readAll(s);92s.close();9394System.out.println("Server 2: accept");95s = server.accept();96System.out.println("accepted");97os = s.getOutputStream();98os.write((reply2+RESPONSE).getBytes());99readAll(s);100s.close();101102}103catch (Exception e) {104System.out.println(e);105}106finished();107}108109boolean isfinished = false;110111public synchronized void finished() {112isfinished = true;113notifyAll();114}115116public synchronized void waitforfinish() {117while (!isfinished) {118try {119wait();120} catch (InterruptedException e) {121e.printStackTrace();122}123}124}125}126127static class Auth extends Authenticator {128protected PasswordAuthentication getPasswordAuthentication() {129return new PasswordAuthentication("user", new char[] {'a','b','c'});130}131}132133public static void main(String[] args) throws Exception {134ServerSocket serversocket = null;135BasicServer server = null;136Auth authenticator = new Auth();137138serversocket = new ServerSocket(0, 10, InetAddress.getLoopbackAddress());139int port = serversocket.getLocalPort();140server = new BasicServer(serversocket);141142HttpClient client = HttpClient.newBuilder()143.authenticator(authenticator)144.build();145server.start();146URI uri = URIBuilder.newBuilder()147.scheme("http")148.loopback()149.port(port)150.path("/foo")151.build();152System.out.println("URI: " + uri);153HttpRequest request = HttpRequest.newBuilder(uri)154.GET()155.build();156HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());157if (response.statusCode() != 200 || !response.body().equals(server.response())) {158System.out.println("Status code = " + response.statusCode());159serversocket.close();160throw new RuntimeException("Test failed");161}162serversocket.close();163server.waitforfinish();164System.out.println("OK");165}166}167168169