/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Tests that all response body is delivered to the BodySubscriber
* before an abortive error terminates the flow
* @modules java.net.http/jdk.internal.net.http.common
* @library /test/lib
* @build jdk.test.lib.net.SimpleSSLContext
* @run testng/othervm ResponseBodyBeforeError
*/
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodySubscriber;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Flow;
import jdk.test.lib.net.SimpleSSLContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocketFactory;
import static java.lang.System.err;
import static java.lang.System.out;
import static java.net.http.HttpClient.Builder.NO_PROXY;
import static java.net.http.HttpResponse.BodyHandlers.ofString;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
public class ResponseBodyBeforeError {
ReplyingServer variableLengthServer;
ReplyingServer variableLengthHttpsServer;
ReplyingServer fixedLengthServer;
ReplyingServer fixedLengthHttpsServer;
String httpURIVarLen;
String httpsURIVarLen;
String httpURIFixLen;
String httpsURIFixLen;
SSLContext sslContext;
static final String EXPECTED_RESPONSE_BODY =
"
Heading
Some Text
";
@DataProvider(name = "sanity")
public Object[][] sanity() {
return new Object[][]{
{ httpURIVarLen + "?length=all" },
{ httpsURIVarLen + "?length=all" },
{ httpURIFixLen + "?length=all" },
{ httpsURIFixLen + "?length=all" },
};
}
@Test(dataProvider = "sanity")
void sanity(String url) throws Exception {
HttpClient client = HttpClient.newBuilder()
.proxy(NO_PROXY)
.sslContext(sslContext)
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
HttpResponse response = client.send(request, ofString());
String body = response.body();
assertEquals(body, EXPECTED_RESPONSE_BODY);
client.sendAsync(request, ofString())
.thenApply(resp -> resp.body())
.thenAccept(b -> assertEquals(b, EXPECTED_RESPONSE_BODY))
.join();
}
@DataProvider(name = "uris")
public Object[][] variants() {
Object[][] cases = new Object[][] {
// The length query string is the total number of response body
// bytes in the reply, before the server closes the connection. The
// second arg is a partial-expected-body that the body subscriber
// should receive before onError is invoked.
{ httpURIFixLen + "?length=0", "" },
{ httpURIFixLen + "?length=1", "<" },
{ httpURIFixLen + "?length=2", "