Path: blob/master/test/jdk/java/net/httpclient/AbstractNoBody.java
41149 views
/*1* Copyright (c) 2015, 2018, 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*/2223import java.io.IOException;24import java.io.InputStream;25import java.net.InetAddress;26import java.net.InetSocketAddress;27import java.util.concurrent.Executor;28import java.util.concurrent.ExecutorService;29import java.util.concurrent.Executors;30import com.sun.net.httpserver.HttpExchange;31import com.sun.net.httpserver.HttpHandler;32import com.sun.net.httpserver.HttpServer;33import com.sun.net.httpserver.HttpsConfigurator;34import com.sun.net.httpserver.HttpsServer;35import java.net.http.HttpClient;36import javax.net.ssl.SSLContext;37import jdk.test.lib.net.SimpleSSLContext;38import org.testng.annotations.AfterTest;39import org.testng.annotations.BeforeTest;40import org.testng.annotations.DataProvider;4142public abstract class AbstractNoBody {4344SSLContext sslContext;45HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ]46HttpsServer httpsTestServer; // HTTPS/1.147Http2TestServer http2TestServer; // HTTP/2 ( h2c )48Http2TestServer https2TestServer; // HTTP/2 ( h2 )49String httpURI_fixed;50String httpURI_chunk;51String httpsURI_fixed;52String httpsURI_chunk;53String http2URI_fixed;54String http2URI_chunk;55String https2URI_fixed;56String https2URI_chunk;5758static final String SIMPLE_STRING = "Hello world. Goodbye world";59static final int ITERATION_COUNT = 3;60// a shared executor helps reduce the amount of threads created by the test61static final Executor executor = Executors.newFixedThreadPool(ITERATION_COUNT * 2);62static final ExecutorService serverExecutor = Executors.newFixedThreadPool(ITERATION_COUNT * 4);6364@DataProvider(name = "variants")65public Object[][] variants() {66return new Object[][]{67{ httpURI_fixed, false },68{ httpURI_chunk, false },69{ httpsURI_fixed, false },70{ httpsURI_chunk, false },71{ http2URI_fixed, false },72{ http2URI_chunk, false },73{ https2URI_fixed, false,},74{ https2URI_chunk, false },7576{ httpURI_fixed, true },77{ httpURI_chunk, true },78{ httpsURI_fixed, true },79{ httpsURI_chunk, true },80{ http2URI_fixed, true },81{ http2URI_chunk, true },82{ https2URI_fixed, true,},83{ https2URI_chunk, true },84};85}8687HttpClient newHttpClient() {88return HttpClient.newBuilder()89.executor(executor)90.sslContext(sslContext)91.build();92}9394static String serverAuthority(HttpServer server) {95return InetAddress.getLoopbackAddress().getHostName() + ":"96+ server.getAddress().getPort();97}9899@BeforeTest100public void setup() throws Exception {101printStamp(START, "setup");102sslContext = new SimpleSSLContext().get();103if (sslContext == null)104throw new AssertionError("Unexpected null sslContext");105106// HTTP/1.1107HttpHandler h1_fixedLengthNoBodyHandler = new HTTP1_FixedLengthNoBodyHandler();108HttpHandler h1_chunkNoBodyHandler = new HTTP1_ChunkedNoBodyHandler();109InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);110httpTestServer = HttpServer.create(sa, 0);111httpTestServer.setExecutor(serverExecutor);112httpTestServer.createContext("/http1/noBodyFixed", h1_fixedLengthNoBodyHandler);113httpTestServer.createContext("/http1/noBodyChunk", h1_chunkNoBodyHandler);114httpURI_fixed = "http://" + serverAuthority(httpTestServer) + "/http1/noBodyFixed";115httpURI_chunk = "http://" + serverAuthority(httpTestServer) + "/http1/noBodyChunk";116117httpsTestServer = HttpsServer.create(sa, 0);118httpsTestServer.setExecutor(serverExecutor);119httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));120httpsTestServer.createContext("/https1/noBodyFixed", h1_fixedLengthNoBodyHandler);121httpsTestServer.createContext("/https1/noBodyChunk", h1_chunkNoBodyHandler);122httpsURI_fixed = "https://" + serverAuthority(httpsTestServer) + "/https1/noBodyFixed";123httpsURI_chunk = "https://" + serverAuthority(httpsTestServer) + "/https1/noBodyChunk";124125// HTTP/2126Http2Handler h2_fixedLengthNoBodyHandler = new HTTP2_FixedLengthNoBodyHandler();127Http2Handler h2_chunkedNoBodyHandler = new HTTP2_ChunkedNoBodyHandler();128129http2TestServer = new Http2TestServer("localhost", false, 0, serverExecutor, null);130http2TestServer.addHandler(h2_fixedLengthNoBodyHandler, "/http2/noBodyFixed");131http2TestServer.addHandler(h2_chunkedNoBodyHandler, "/http2/noBodyChunk");132http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/noBodyFixed";133http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/noBodyChunk";134135https2TestServer = new Http2TestServer("localhost", true, 0, serverExecutor, sslContext);136https2TestServer.addHandler(h2_fixedLengthNoBodyHandler, "/https2/noBodyFixed");137https2TestServer.addHandler(h2_chunkedNoBodyHandler, "/https2/noBodyChunk");138https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/noBodyFixed";139https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/noBodyChunk";140141httpTestServer.start();142httpsTestServer.start();143http2TestServer.start();144https2TestServer.start();145printStamp(END,"setup");146}147148@AfterTest149public void teardown() throws Exception {150printStamp(START, "teardown");151httpTestServer.stop(0);152httpsTestServer.stop(0);153http2TestServer.stop();154https2TestServer.stop();155printStamp(END, "teardown");156}157158static final long start = System.nanoTime();159static final String START = "start";160static final String END = "end ";161static long elapsed() { return (System.nanoTime() - start)/1000_000;}162void printStamp(String what, String fmt, Object... args) {163long elapsed = elapsed();164long sec = elapsed/1000;165long ms = elapsed % 1000;166String time = sec > 0 ? sec + "sec " : "";167time = time + ms + "ms";168System.out.printf("%s: %s \t [%s]\t %s%n",169getClass().getSimpleName(), what, time, String.format(fmt,args));170}171172173static class HTTP1_FixedLengthNoBodyHandler implements HttpHandler {174@Override175public void handle(HttpExchange t) throws IOException {176//out.println("NoBodyHandler received request to " + t.getRequestURI());177try (InputStream is = t.getRequestBody()) {178is.readAllBytes();179}180t.sendResponseHeaders(200, -1); // no body181}182}183184static class HTTP1_ChunkedNoBodyHandler implements HttpHandler {185@Override186public void handle(HttpExchange t) throws IOException {187//out.println("NoBodyHandler received request to " + t.getRequestURI());188try (InputStream is = t.getRequestBody()) {189is.readAllBytes();190}191t.sendResponseHeaders(200, 0); // chunked192t.getResponseBody().close(); // write nothing193}194}195196static class HTTP2_FixedLengthNoBodyHandler implements Http2Handler {197@Override198public void handle(Http2TestExchange t) throws IOException {199//out.println("NoBodyHandler received request to " + t.getRequestURI());200try (InputStream is = t.getRequestBody()) {201is.readAllBytes();202}203t.sendResponseHeaders(200, 0);204}205}206207static class HTTP2_ChunkedNoBodyHandler implements Http2Handler {208@Override209public void handle(Http2TestExchange t) throws IOException {210//out.println("NoBodyHandler received request to " + t.getRequestURI());211try (InputStream is = t.getRequestBody()) {212is.readAllBytes();213}214t.sendResponseHeaders(200, -1);215t.getResponseBody().close(); // write nothing216}217}218}219220221