/* * Copyright (c) 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.
*/
@Override publicvoid handle(final HttpTestExchange exchange) throws IOException { // we only send informational response and then return for (int i = 0; i < 5; i++) {
exchange.sendResponseHeaders(102, -1);
System.out.println("Sent 102 response code from H2 server"); // wait for a while before sending again try { Thread.sleep(2000);
} catch (InterruptedException e) { // just return
System.err.println("Handler thread interrupted");
}
}
}
}
/** * Tests that when a HTTP/1.1 server sends intermediate 1xx response codes and then the final * response, the client (internally) will ignore those intermediate informational response codes * and only return the final response to the application
*/
@Test publicvoid test1xxForHTTP11() throws Exception { final HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.proxy(HttpClient.Builder.NO_PROXY).build();
TRACKER.track(client); final URI[] requestURIs = new URI[]{ new URI(http1RequestURIBase + "/test/foo"), new URI(http1RequestURIBase + "/test/bar"), new URI(http1RequestURIBase + "/test/hello")}; for (final URI requestURI : requestURIs) { final HttpRequest request = HttpRequest.newBuilder(requestURI).build();
System.out.println("Issuing request to " + requestURI); final HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); Assert.assertEquals(response.version(), HttpClient.Version.HTTP_1_1, "Unexpected HTTP version in response"); Assert.assertEquals(response.statusCode(), 200, "Unexpected response code"); Assert.assertEquals(response.body(), EXPECTED_RSP_BODY, "Unexpected response body");
}
}
/** * Tests that when a HTTP2 server sends intermediate 1xx response codes and then the final * response, the client (internally) will ignore those intermediate informational response codes * and only return the final response to the application
*/
@Test publicvoid test1xxForHTTP2() throws Exception { final HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.proxy(HttpClient.Builder.NO_PROXY).build();
TRACKER.track(client); final URI[] requestURIs = new URI[]{ new URI(http2RequestURIBase + "/102"), new URI(http2RequestURIBase + "/103"), new URI(http2RequestURIBase + "/100")}; for (final URI requestURI : requestURIs) { final HttpRequest request = HttpRequest.newBuilder(requestURI).build();
System.out.println("Issuing request to " + requestURI); final HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); Assert.assertEquals(response.version(), HttpClient.Version.HTTP_2, "Unexpected HTTP version in response"); Assert.assertEquals(response.statusCode(), 200, "Unexpected response code"); Assert.assertEquals(response.body(), EXPECTED_RSP_BODY, "Unexpected response body");
}
}
/** * Tests that when a request is issued with a specific request timeout and the server * responds with intermediate 1xx response code but doesn't respond with a final response within * the timeout duration, then the application fails with a request timeout
*/
@Test publicvoid test1xxRequestTimeout() throws Exception { final HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.proxy(HttpClient.Builder.NO_PROXY).build();
TRACKER.track(client); final URI requestURI = new URI(http2RequestURIBase + "/only-informational"); final Duration requestTimeout = Duration.ofSeconds(2); final HttpRequest request = HttpRequest.newBuilder(requestURI).timeout(requestTimeout)
.build();
System.out.println("Issuing request to " + requestURI); // we expect the request to timeout Assert.assertThrows(HttpTimeoutException.class, () -> {
client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
});
}
/** * Tests that when the HTTP/1.1 server sends a 101 response when the request hasn't asked * for an "Upgrade" then the request fails.
*/
@Test publicvoid testHTTP11Unexpected101() throws Exception { final HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.proxy(HttpClient.Builder.NO_PROXY).build();
TRACKER.track(client); final URI requestURI = new URI(http1RequestURIBase + "/test/bye"); final HttpRequest request = HttpRequest.newBuilder(requestURI).build();
System.out.println("Issuing request to " + requestURI); // we expect the request to fail because the server sent an unexpected 101 Assert.assertThrows(ProtocolException.class,
() -> client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)));
}
/** * Tests that when the HTTP2 server (over HTTPS) sends a 101 response when the request * hasn't asked for an "Upgrade" then the request fails.
*/
@Test publicvoid testSecureHTTP2Unexpected101() throws Exception { final HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.sslContext(sslContext)
.proxy(HttpClient.Builder.NO_PROXY).build();
TRACKER.track(client); final URI requestURI = new URI(https2RequestURIBase + "/101"); final HttpRequest request = HttpRequest.newBuilder(requestURI).build();
System.out.println("Issuing request to " + requestURI); // we expect the request to fail because the server sent an unexpected 101 Assert.assertThrows(ProtocolException.class,
() -> client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)));
}
/** * Tests that when the HTTP2 server (over plain HTTP) sends a 101 response when the request * hasn't asked for an "Upgrade" then the request fails.
*/
@Test publicvoid testPlainHTTP2Unexpected101() throws Exception { final HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.proxy(HttpClient.Builder.NO_PROXY).build();
TRACKER.track(client); // when using HTTP2 version against a "http://" (non-secure) URI // the HTTP client (implementation) internally initiates a HTTP/1.1 connection // and then does an "Upgrade:" to "h2c". This it does when there isn't already a // H2 connection against the target/destination server. So here we initiate a dummy request // using the client instance against the same target server and just expect it to return // back successfully. Once that connection is established (and internally pooled), the client // will then reuse that connection and won't issue an "Upgrade:" and thus we can then // start our testing
warmupH2Client(client); // start the actual testing final URI requestURI = new URI(http2RequestURIBase + "/101"); final HttpRequest request = HttpRequest.newBuilder(requestURI).build();
System.out.println("Issuing request to " + requestURI); // we expect the request to fail because the server sent an unexpected 101 Assert.assertThrows(ProtocolException.class,
() -> client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)));
}
// sends a request and expects a 200 response back privatevoid warmupH2Client(final HttpClient client) throws Exception { final URI requestURI = new URI(http2RequestURIBase + "/200"); final HttpRequest request = HttpRequest.newBuilder(requestURI).build();
System.out.println("Issuing (warmup) request to " + requestURI); final HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding()); Assert.assertEquals(response.statusCode(), 200, "Unexpected response code");
}
// verifies that the HttpClient being tracked has no outstanding operations privatevoid assertNoOutstandingClientOps() throws AssertionError {
System.gc(); final AssertionError refCheckFailure = TRACKER.check(1000); if (refCheckFailure != null) { throw refCheckFailure;
} // successful test completion
}
}
¤ Dauer der Verarbeitung: 0.23 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.