|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* @test |
| 25 | + * @summary This test checks that a broken HttpClient is not returned from the KeepAliveCache |
| 26 | + * when the intial HttpURLConnection.setRequest method is passed a 'new String("POST")' |
| 27 | + * rather than the "POST" String literal |
| 28 | + * @bug 8274779 |
| 29 | + * @library /test/lib |
| 30 | + * @modules java.base/sun.net.www.http |
| 31 | + * java.base/sun.net.www.protocol.http |
| 32 | + * @build java.base/sun.net.www.http.HttpClientAccess |
| 33 | + * @run testng/othervm RequestMethodEquality |
| 34 | + */ |
| 35 | + |
| 36 | +importcom.sun.net.httpserver.HttpExchange; |
| 37 | +importcom.sun.net.httpserver.HttpHandler; |
| 38 | +importcom.sun.net.httpserver.HttpServer; |
| 39 | +importjdk.test.lib.net.URIBuilder; |
| 40 | +importorg.testng.Assert; |
| 41 | +importorg.testng.annotations.AfterTest; |
| 42 | +importorg.testng.annotations.BeforeTest; |
| 43 | +importorg.testng.annotations.Test; |
| 44 | +importsun.net.www.http.HttpClient; |
| 45 | +importsun.net.www.http.HttpClientAccess; |
| 46 | +importsun.net.www.http.KeepAliveCache; |
| 47 | +importsun.net.www.protocol.http.HttpURLConnection; |
| 48 | + |
| 49 | +importjava.io.IOException; |
| 50 | +importjava.net.InetAddress; |
| 51 | +importjava.net.InetSocketAddress; |
| 52 | +importjava.net.Proxy; |
| 53 | +importjava.net.URL; |
| 54 | + |
| 55 | +publicclassRequestMethodEquality { |
| 56 | +privatestaticfinalStringTEST_CONTEXT ="/reqmethodtest"; |
| 57 | +privateHttpServerserver; |
| 58 | +privateCustomHandlerhandler; |
| 59 | +privateHttpClientAccesshttpClientAccess; |
| 60 | + |
| 61 | +@BeforeTest |
| 62 | +publicvoidsetup()throwsException { |
| 63 | +handler =newCustomHandler(); |
| 64 | +server =createServer(handler); |
| 65 | +httpClientAccess =newHttpClientAccess(); |
| 66 | + } |
| 67 | + |
| 68 | +@AfterTest |
| 69 | +publicvoidtearDown()throwsException { |
| 70 | +if (server !=null) { |
| 71 | +server.stop(0); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +@Test |
| 76 | +publicvoidtestHttpClient()throwsException { |
| 77 | +HttpURLConnectionconn =null; |
| 78 | +try { |
| 79 | +URLurl =URIBuilder.newBuilder() |
| 80 | + .scheme("http") |
| 81 | + .host(server.getAddress().getAddress()) |
| 82 | + .port(server.getAddress().getPort()) |
| 83 | + .path(TEST_CONTEXT) |
| 84 | + .toURL(); |
| 85 | + |
| 86 | +conn = (HttpURLConnection)url.openConnection(); |
| 87 | +conn.setChunkedStreamingMode(8);// ensures the call to HttpURLConnection.streaming() passes |
| 88 | + |
| 89 | +intfirstConnectTimeout =1234; |
| 90 | +HttpClientfreshClient =HttpClient.New(url,Proxy.NO_PROXY,firstConnectTimeout,true,conn); |
| 91 | +freshClient.closeServer();// ensures that the call to HttpClient.available() fails |
| 92 | + |
| 93 | +httpClientAccess.setInCache(freshClient,true);// allows the assertion in HttpClient.New to pass |
| 94 | + |
| 95 | +// Injecting a mock KeepAliveCache that the HttpClient can use |
| 96 | +KeepAliveCachekac =httpClientAccess.getKeepAliveCache(); |
| 97 | +kac.put(url,null,freshClient); |
| 98 | + |
| 99 | +// The 'new' keyword is important here as the original code |
| 100 | +// used '==' rather than String.equals to compare request methods |
| 101 | +conn.setRequestMethod(newString("POST")); |
| 102 | + |
| 103 | +// Before the fix, the value returned to 'cachedClient' would have been the (broken) cached |
| 104 | +// 'freshClient' as HttpClient.available() could never be checked |
| 105 | +intsecondConnectTimeout =4321; |
| 106 | +HttpClientcachedClient =HttpClient.New(url,Proxy.NO_PROXY,secondConnectTimeout,true,conn); |
| 107 | +cachedClient.closeServer(); |
| 108 | + |
| 109 | +intoriginalConnectTimeout =freshClient.getConnectTimeout(); |
| 110 | +intcachedConnectTimeout =cachedClient.getConnectTimeout(); |
| 111 | + |
| 112 | +// If both connectTimeout values are equal, it means the test retrieved the same broken |
| 113 | +// HttpClient from the cache and is trying to re-use it. |
| 114 | +Assert.assertNotEquals(originalConnectTimeout,cachedConnectTimeout,"Both connectTimeout values are equal.\nThis means the test is reusing a broken HttpClient rather than creating a new one."); |
| 115 | + }finally { |
| 116 | +if (conn !=null) { |
| 117 | +conn.disconnect(); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +privatestaticHttpServercreateServer(finalHttpHandlerhandler)throwsIOException { |
| 123 | +finalInetSocketAddressserverAddress =newInetSocketAddress(InetAddress.getLoopbackAddress(),0); |
| 124 | +finalintbacklog = -1; |
| 125 | +finalHttpServerserver =HttpServer.create(serverAddress,backlog); |
| 126 | +server.createContext(TEST_CONTEXT,handler); |
| 127 | +server.start(); |
| 128 | +System.out.println("Server started on " +server.getAddress()); |
| 129 | +returnserver; |
| 130 | + } |
| 131 | + |
| 132 | +privatestaticclassCustomHandlerimplementsHttpHandler { |
| 133 | +@Override |
| 134 | +publicvoidhandle(HttpExchangeexchange)throwsIOException { |
| 135 | +// We'll always send 200 OK - We don't care about the server logic |
| 136 | +exchange.sendResponseHeaders(200,1); |
| 137 | +exchange.close(); |
| 138 | + } |
| 139 | + } |
| 140 | +} |