Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/8199849/TestHttpUnicode.java
41161 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 819984926* @library /test/lib27* @summary Checks that unicode bytes are being handled correctly28* @run main/othervm -Dfile.encoding=UTF_8 TestHttpUnicode29*/3031import com.sun.net.httpserver.*;32import jdk.test.lib.net.URIBuilder;3334import java.io.IOException;35import java.io.InputStream;36import java.net.*;3738public class TestHttpUnicode {3940private static final String TEST_USER = "Selam D\u00fcnya. Ho\u015f\u00e7akal D\u00fcnya";41private static final String TEST_PW = "Selam D\u00fcnya. Ho\u015f\u00e7akal D\u00fcnya";4243static class ClientAuthenticator extends java.net.Authenticator {44public PasswordAuthentication getPasswordAuthentication() {45return new PasswordAuthentication(TEST_USER, TEST_PW.toCharArray());46}47}4849static class Handler implements HttpHandler {50public void handle(HttpExchange t) throws IOException {51InputStream is = t.getRequestBody();52while (is.read() != -1) ;53is.close();5455HttpPrincipal p = t.getPrincipal();56if (p.getUsername().equals(TEST_USER)) {57t.sendResponseHeaders(200, -1);58}59t.close();60}61}6263public static void main(String[] args) throws Exception {64HttpServer testHttpServer = null;65try {66InetSocketAddress loopbackAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);67testHttpServer = HttpServer.create(loopbackAddress, 0);68HttpContext context = testHttpServer.createContext("/test", new Handler());69System.setProperty("http.maxRedirects", "3");7071BasicAuthenticator serverAuthenticator = new BasicAuthenticator("[email protected]") {72public boolean checkCredentials(String username, String pw) {73return username.equals(TEST_USER) && pw.equals(TEST_PW);74}75};76context.setAuthenticator(serverAuthenticator);77java.net.Authenticator.setDefault(new ClientAuthenticator());7879testHttpServer.start();80URL url = URIBuilder.newBuilder()81.scheme("http")82.loopback()83.port(testHttpServer.getAddress().getPort())84.path("/test/authCharacterSet.html")85.toURL();86HttpURLConnection testConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);8788// Authenication CHECK89if (testConnection.getResponseCode() == 401) {90throw new RuntimeException("Test Authentication failed with HTTP Status 401.");91}9293InputStream is = testConnection.getInputStream();94while (is.read() != -1) ;95} finally {96testHttpServer.stop(2);97}98}99}100101102