Path: blob/master/test/jdk/java/net/httpclient/ExpectContinue.java
41149 views
/*1* Copyright (c) 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*/2223/*24* @test25* @summary Basic test for Expect 100-Continue ( HTTP/1.1 only )26* @modules java.net.http27* jdk.httpserver28* @library /test/lib29* @build jdk.test.lib.net.SimpleSSLContext30* @run testng/othervm ExpectContinue31*/3233import com.sun.net.httpserver.HttpExchange;34import com.sun.net.httpserver.HttpHandler;35import com.sun.net.httpserver.HttpServer;36import com.sun.net.httpserver.HttpsConfigurator;37import com.sun.net.httpserver.HttpsServer;38import java.io.IOException;39import java.io.InputStream;40import java.io.OutputStream;41import java.net.InetAddress;42import java.net.InetSocketAddress;43import java.net.URI;44import java.net.http.HttpClient;45import java.net.http.HttpRequest;46import java.net.http.HttpRequest.BodyPublishers;47import java.net.http.HttpResponse;48import java.net.http.HttpResponse.BodyHandlers;49import java.util.List;50import javax.net.ssl.SSLContext;51import jdk.test.lib.net.SimpleSSLContext;52import org.testng.annotations.AfterTest;53import org.testng.annotations.BeforeTest;54import org.testng.annotations.DataProvider;55import org.testng.annotations.Test;56import static java.lang.System.out;57import static org.testng.Assert.assertEquals;5859public class ExpectContinue {6061SSLContext sslContext;62HttpServer httpTestServer; // HTTP/1.1 [ 2 servers ]63HttpsServer httpsTestServer; // HTTPS/1.164String httpURI;65String httpsURI;6667@DataProvider(name = "positive")68public Object[][] positive() {69return new Object[][] {70{ httpURI, false, "Billy" },71{ httpURI, false, "Bob" },72{ httpURI, true, "Jimmy" },73{ httpsURI, true, "Jack" },74};75}7677@Test(dataProvider = "positive")78void test(String uriString, boolean expectedContinue, String data)79throws Exception80{81out.printf("test(%s, %s, %s): starting%n", uriString, expectedContinue, data);82HttpClient client = HttpClient.newBuilder()83.sslContext(sslContext)84.build();8586URI uri = URI.create(uriString);87HttpRequest request = HttpRequest.newBuilder(uri)88.expectContinue(expectedContinue)89.POST(BodyPublishers.ofString(data))90.build();9192HttpResponse<String> response = client.send(request,93BodyHandlers.ofString());94System.out.println("First response: " + response);95assertEquals(response.statusCode(), 200);96assertEquals(response.body(), data);9798// again with the same request, to ensure no Expect header duplication99response = client.send(request, BodyHandlers.ofString());100System.out.println("Second response: " + response);101assertEquals(response.statusCode(), 200);102assertEquals(response.body(), data);103}104105@Test(dataProvider = "positive")106void testAsync(String uriString, boolean expectedContinue, String data) {107out.printf("test(%s, %s, %s): starting%n", uriString, expectedContinue, data);108HttpClient client = HttpClient.newBuilder()109.sslContext(sslContext)110.build();111112URI uri = URI.create(uriString);113HttpRequest request = HttpRequest.newBuilder(uri)114.expectContinue(expectedContinue)115.POST(BodyPublishers.ofString(data))116.build();117118HttpResponse<String> response = client.sendAsync(request,119BodyHandlers.ofString()).join();120System.out.println("First response: " + response);121assertEquals(response.statusCode(), 200);122assertEquals(response.body(), data);123124// again with the same request, to ensure no Expect header duplication125response = client.sendAsync(request, BodyHandlers.ofString()).join();126System.out.println("Second response: " + response);127assertEquals(response.statusCode(), 200);128assertEquals(response.body(), data);129}130131// -- Infrastructure132133static String serverAuthority(HttpServer server) {134return InetAddress.getLoopbackAddress().getHostName() + ":"135+ server.getAddress().getPort();136}137138@BeforeTest139public void setup() throws Exception {140sslContext = new SimpleSSLContext().get();141if (sslContext == null)142throw new AssertionError("Unexpected null sslContext");143144InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);145httpTestServer = HttpServer.create(sa, 0);146httpTestServer.createContext("/http1/ec", new Http1ExpectContinueHandler());147httpURI = "http://" + serverAuthority(httpTestServer) + "/http1/ec";148149httpsTestServer = HttpsServer.create(sa, 0);150httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));151httpsTestServer.createContext("/https1/ec", new Http1ExpectContinueHandler());152httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1/ec";153154httpTestServer.start();155httpsTestServer.start();156}157158@AfterTest159public void teardown() throws Exception {160httpTestServer.stop(0);161httpsTestServer.stop(0);162}163164static class Http1ExpectContinueHandler implements HttpHandler {165@Override166public void handle(HttpExchange t) throws IOException {167try (InputStream is = t.getRequestBody();168OutputStream os = t.getResponseBody()) {169byte[] bytes = is.readAllBytes();170171List<String> expect = t.getRequestHeaders().get("Expect");172if (expect != null && expect.size() != 1) {173System.out.println("Server: Expect: " + expect);174Throwable ex = new AssertionError("Expect: " + expect);175ex.printStackTrace();176t.sendResponseHeaders(500, 0);177} else {178t.sendResponseHeaders(200, bytes.length);179os.write(bytes);180}181}182}183}184}185186187