/*
* Copyright (c) 2018, 2020, 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
* @bug 8216498
* @summary Tests Exception detail message when too few response bytes are
* received before a socket exception or eof.
* @library /test/lib
* @build jdk.test.lib.net.SimpleSSLContext
* @run testng/othervm
* -Djdk.httpclient.HttpClient.log=headers,errors,channel
* ShortResponseBody
*/
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.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
import jdk.test.lib.net.SimpleSSLContext;
import org.testng.ITestContext;
import org.testng.annotations.BeforeMethod;
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.SSLHandshakeException;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
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.util.stream.Collectors.toList;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
public class ShortResponseBody {
Server closeImmediatelyServer;
Server closeImmediatelyHttpsServer;
Server variableLengthServer;
Server variableLengthHttpsServer;
Server fixedLengthServer;
String httpURIClsImed;
String httpsURIClsImed;
String httpURIVarLen;
String httpsURIVarLen;
String httpURIFixLen;
SSLContext sslContext;
SSLParameters sslParameters;
static final String EXPECTED_RESPONSE_BODY =
"
Heading
Some Text
";
final static AtomicLong ids = new AtomicLong();
final ThreadFactory factory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "HttpClient-Worker-" + ids.incrementAndGet());
thread.setDaemon(true);
return thread;
}
};
final ExecutorService service = Executors.newCachedThreadPool(factory);
@BeforeMethod
void beforeMethod(ITestContext context) {
if (context.getFailedTests().size() > 0) {
throw new RuntimeException("some tests failed");
}
}
@DataProvider(name = "sanity")
public Object[][] sanity() {
return new Object[][]{
{ httpURIVarLen + "?length=all" },
{ httpsURIVarLen + "?length=all" },
{ httpURIFixLen + "?length=all" },
};
}
@Test(dataProvider = "sanity")
void sanity(String url) throws Exception {
HttpClient client = newHttpClient();
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(ITestContext context) {
String[][] cases = new String[][] {
// The length query string is the total number of bytes in the reply,
// including headers, before the server closes the connection. The
// second arg is a partial-expected-detail message in the exception.
{ httpURIVarLen + "?length=0", "no bytes" }, // EOF without receiving anything
{ httpURIVarLen + "?length=1", "status line" }, // EOF during status-line
{ httpURIVarLen + "?length=2", "status line" },
{ httpURIVarLen + "?length=10", "status line" },
{ httpURIVarLen + "?length=19", "header" }, // EOF during Content-Type header
{ httpURIVarLen + "?length=30", "header" },
{ httpURIVarLen + "?length=45", "header" },
{ httpURIVarLen + "?length=48", "header" },
{ httpURIVarLen + "?length=51", "header" },
{ httpURIVarLen + "?length=98", "header" }, // EOF during Connection header
{ httpURIVarLen + "?length=100", "header" },
{ httpURIVarLen + "?length=101", "header" },
{ httpURIVarLen + "?length=104", "header" },
{ httpURIVarLen + "?length=106", "chunked transfer encoding" }, // EOF during chunk header ( length )
{ httpURIVarLen + "?length=110", "chunked transfer encoding" }, // EOF during chunk response body data
{ httpsURIVarLen + "?length=0", "no bytes" },
{ httpsURIVarLen + "?length=1", "status line" },
{ httpsURIVarLen + "?length=2", "status line" },
{ httpsURIVarLen + "?length=10", "status line" },
{ httpsURIVarLen + "?length=19", "header" },
{ httpsURIVarLen + "?length=30", "header" },
{ httpsURIVarLen + "?length=45", "header" },
{ httpsURIVarLen + "?length=48", "header" },
{ httpsURIVarLen + "?length=51", "header" },
{ httpsURIVarLen + "?length=98", "header" },
{ httpsURIVarLen + "?length=100", "header" },
{ httpsURIVarLen + "?length=101", "header" },
{ httpsURIVarLen + "?length=104", "header" },
{ httpsURIVarLen + "?length=106", "chunked transfer encoding" },
{ httpsURIVarLen + "?length=110", "chunked transfer encoding" },
{ httpURIFixLen + "?length=0", "no bytes" }, // EOF without receiving anything
{ httpURIFixLen + "?length=1", "status line" }, // EOF during status-line
{ httpURIFixLen + "?length=2", "status line" },
{ httpURIFixLen + "?length=10", "status line" },
{ httpURIFixLen + "?length=19", "header" }, // EOF during Content-Type header
{ httpURIFixLen + "?length=30", "header" },
{ httpURIFixLen + "?length=45", "header" },
{ httpURIFixLen + "?length=48", "header" },
{ httpURIFixLen + "?length=51", "header" },
{ httpURIFixLen + "?length=78", "header" }, // EOF during Connection header
{ httpURIFixLen + "?length=79", "header" },
{ httpURIFixLen + "?length=86", "header" },
{ httpURIFixLen + "?length=104", "fixed content-length" }, // EOF during body
{ httpURIFixLen + "?length=106", "fixed content-length" },
{ httpURIFixLen + "?length=110", "fixed content-length" },
// ## ADD https fixed
{ httpURIClsImed, "no bytes"},
{ httpsURIClsImed, "no bytes"},
};
if (context.getFailedTests().size() > 0) {
// Shorten the log output by preventing useless
// skip traces to be printed for subsequent methods
// if one of the previous @Test method has failed.
return new Object[0][];
}
List