Path: blob/master/test/jdk/java/net/httpclient/EmptyAuthenticate.java
41149 views
/*1* Copyright (c) 2021, 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 826389926* @summary HttpClient throws NPE in AuthenticationFilter when parsing www-authenticate head27*28* @run main/othervm EmptyAuthenticate29*/30import com.sun.net.httpserver.HttpServer;31import java.io.IOException;32import java.io.OutputStream;33import java.net.InetSocketAddress;34import java.net.URI;35import java.net.URISyntaxException;36import java.net.http.HttpClient;37import java.net.http.HttpRequest;38import java.net.http.HttpResponse;3940public class EmptyAuthenticate {4142public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {43int port = 0;4445//start server:46HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);47port = server.getAddress().getPort();48server.createContext("/", exchange -> {49String response = "test body";50//this empty header will make the HttpClient throw NPE51exchange.getResponseHeaders().add("www-authenticate", "");52exchange.sendResponseHeaders(401, response.length());53OutputStream os = exchange.getResponseBody();54os.write(response.getBytes());55os.close();56});57server.start();5859HttpResponse<String> response = null;60//run client:61try {62HttpClient httpClient = HttpClient.newHttpClient();63HttpRequest request = HttpRequest.newBuilder(new URI("http://localhost:" + port + "/")).GET().build();64//this line will throw NPE (wrapped by IOException) when parsing empty www-authenticate response header in AuthenticationFilter:65response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());66boolean ok = !response.headers().firstValue("WWW-Authenticate").isEmpty();67if (!ok) {68throw new RuntimeException("WWW-Authenicate missing");69}70} catch (IOException e) {71e.printStackTrace();72throw new RuntimeException("Test failed");73} finally {74server.stop(0);75}76}77}787980