Path: blob/master/test/jdk/sun/net/www/http/RequestMethodCheck/RequestMethodEquality.java
66646 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/* @test24* @summary This test checks that a broken HttpClient is not returned from the KeepAliveCache25* when the intial HttpURLConnection.setRequest method is passed a 'new String("POST")'26* rather than the "POST" String literal27* @bug 827477928* @library /test/lib29* @modules java.base/sun.net.www.http30* java.base/sun.net.www.protocol.http31* @build java.base/sun.net.www.http.HttpClientAccess32* @run testng/othervm RequestMethodEquality33*/3435import com.sun.net.httpserver.HttpExchange;36import com.sun.net.httpserver.HttpHandler;37import com.sun.net.httpserver.HttpServer;38import jdk.test.lib.net.URIBuilder;39import org.testng.Assert;40import org.testng.annotations.AfterTest;41import org.testng.annotations.BeforeTest;42import org.testng.annotations.Test;43import sun.net.www.http.HttpClient;44import sun.net.www.http.HttpClientAccess;45import sun.net.www.http.KeepAliveCache;46import sun.net.www.protocol.http.HttpURLConnection;4748import java.io.IOException;49import java.net.InetAddress;50import java.net.InetSocketAddress;51import java.net.Proxy;52import java.net.URL;5354public class RequestMethodEquality {55private static final String TEST_CONTEXT = "/reqmethodtest";56private HttpServer server;57private CustomHandler handler;58private HttpClientAccess httpClientAccess;5960@BeforeTest61public void setup() throws Exception {62handler = new CustomHandler();63server = createServer(handler);64httpClientAccess = new HttpClientAccess();65}6667@AfterTest68public void tearDown() throws Exception {69if (server != null) {70server.stop(0);71}72}7374@Test75public void testHttpClient() throws Exception {76HttpURLConnection conn = null;77try {78URL url = URIBuilder.newBuilder()79.scheme("http")80.host(server.getAddress().getAddress())81.port(server.getAddress().getPort())82.path(TEST_CONTEXT)83.toURL();8485conn = (HttpURLConnection) url.openConnection();86conn.setChunkedStreamingMode(8); // ensures the call to HttpURLConnection.streaming() passes8788int firstConnectTimeout = 1234;89HttpClient freshClient = HttpClient.New(url, Proxy.NO_PROXY, firstConnectTimeout, true, conn);90freshClient.closeServer(); // ensures that the call to HttpClient.available() fails9192httpClientAccess.setInCache(freshClient, true); // allows the assertion in HttpClient.New to pass9394// Injecting a mock KeepAliveCache that the HttpClient can use95KeepAliveCache kac = httpClientAccess.getKeepAliveCache();96kac.put(url, null, freshClient);9798// The 'new' keyword is important here as the original code99// used '==' rather than String.equals to compare request methods100conn.setRequestMethod(new String("POST"));101102// Before the fix, the value returned to 'cachedClient' would have been the (broken) cached103// 'freshClient' as HttpClient.available() could never be checked104int secondConnectTimeout = 4321;105HttpClient cachedClient = HttpClient.New(url, Proxy.NO_PROXY, secondConnectTimeout, true, conn);106cachedClient.closeServer();107108int originalConnectTimeout = freshClient.getConnectTimeout();109int cachedConnectTimeout = cachedClient.getConnectTimeout();110111// If both connectTimeout values are equal, it means the test retrieved the same broken112// HttpClient from the cache and is trying to re-use it.113Assert.assertNotEquals(originalConnectTimeout, cachedConnectTimeout, "Both connectTimeout values are equal.\nThis means the test is reusing a broken HttpClient rather than creating a new one.");114} finally {115if (conn != null) {116conn.disconnect();117}118}119}120121private static HttpServer createServer(final HttpHandler handler) throws IOException {122final InetSocketAddress serverAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);123final int backlog = -1;124final HttpServer server = HttpServer.create(serverAddress, backlog);125server.createContext(TEST_CONTEXT, handler);126server.start();127System.out.println("Server started on " + server.getAddress());128return server;129}130131private static class CustomHandler implements HttpHandler {132@Override133public void handle(HttpExchange exchange) throws IOException {134// We'll always send 200 OK - We don't care about the server logic135exchange.sendResponseHeaders(200, 1);136exchange.close();137}138}139}140141142