SSL HttpRedirectTest.java
Interaktion und PortierbarkeitJAVA
/* * Copyright (c) 2019, 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.
*/ import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsServer; import jdk.test.lib.net.SimpleSSLContext; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; importstatic org.testng.Assert.*;
/** * @test * @bug 8232625 * @summary This test verifies that the HttpClient works correctly when redirecting a post request. * @library /test/lib http2/server * @build jdk.test.lib.net.SimpleSSLContext HttpServerAdapters DigestEchoServer HttpRedirectTest * @modules java.net.http/jdk.internal.net.http.common * java.net.http/jdk.internal.net.http.frame * java.net.http/jdk.internal.net.http.hpack * java.logging * java.base/sun.net.www.http * java.base/sun.net.www * java.base/sun.net * @run testng/othervm -Dtest.requiresHost=true * -Djdk.httpclient.HttpClient.log=headers * -Djdk.internal.httpclient.debug=false * HttpRedirectTest *
*/ publicclass HttpRedirectTest implements HttpServerAdapters { staticfinal String GET_RESPONSE_BODY = "Lorem ipsum dolor sit amet"; staticfinal String REQUEST_BODY = "Here it goes"; staticfinal SSLContext context; static { try {
context = new SimpleSSLContext().get();
SSLContext.setDefault(context);
} catch (Exception x) { thrownew ExceptionInInitializerError(x);
}
}
final AtomicLong requestCounter = new AtomicLong(); final AtomicLong responseCounter = new AtomicLong();
HttpTestServer http1Server;
HttpTestServer http2Server;
HttpTestServer https1Server;
HttpTestServer https2Server;
DigestEchoServer.TunnelingProxy proxy;
URI http1URI;
URI https1URI;
URI http2URI;
URI https2URI;
InetSocketAddress proxyAddress;
ProxySelector proxySelector;
HttpClient client;
List<CompletableFuture<?>> futures = new CopyOnWriteArrayList<>();
Set<URI> pending = new CopyOnWriteArraySet<>();
final ExecutorService executor = new ThreadPoolExecutor(12, 60, 10,
TimeUnit.SECONDS, new LinkedBlockingQueue<>()); // Shared by HTTP/1.1 servers final ExecutorService clientexec = new ThreadPoolExecutor(6, 12, 1,
TimeUnit.SECONDS, new LinkedBlockingQueue<>()); // Used by the client
privatevoid testNonIdempotent(URI u, HttpRequest request, int code, String method) throws Exception {
System.out.println("Testing with " + u);
CompletableFuture<HttpResponse<String>> respCf =
client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> resp = respCf.join(); if (method.equals("DO_NOT_FOLLOW")) {
assertEquals(resp.statusCode(), code, u + ": status code");
} else {
assertEquals(resp.statusCode(), 200, u + ": status code");
} if (method.equals("POST")) {
assertEquals(resp.body(), REQUEST_BODY, u + ": body");
} elseif (code == 304) {
assertEquals(resp.body(), "", u + ": body");
} elseif (method.equals("DO_NOT_FOLLOW")) {
assertNotEquals(resp.body(), GET_RESPONSE_BODY, u + ": body");
assertNotEquals(resp.body(), REQUEST_BODY, u + ": body");
} else {
assertEquals(resp.body(), GET_RESPONSE_BODY, u + ": body");
}
}
publicvoid testIdempotent(URI u, HttpRequest request, int code, String method) throws Exception {
CompletableFuture<HttpResponse<String>> respCf =
client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> resp = respCf.join(); if (method.equals("DO_NOT_FOLLOW")) {
assertEquals(resp.statusCode(), code, u + ": status code");
} else {
assertEquals(resp.statusCode(), 200, u + ": status code");
} if (method.equals("POST")) {
assertEquals(resp.body(), REQUEST_BODY, u + ": body");
} elseif (code == 304) {
assertEquals(resp.body(), "", u + ": body");
} elseif (method.equals("DO_NOT_FOLLOW")) {
assertNotEquals(resp.body(), GET_RESPONSE_BODY, u + ": body");
assertNotEquals(resp.body(), REQUEST_BODY, u + ": body");
} elseif (code == 303) {
assertEquals(resp.body(), GET_RESPONSE_BODY, u + ": body");
} else {
assertEquals(resp.body(), REQUEST_BODY, u + ": body");
}
}
@Test(dataProvider = "uris") publicvoid testPOST(URI uri, int code, String method) throws Exception {
URI u = uri.resolve("foo?n=" + requestCounter.incrementAndGet());
HttpRequest request = HttpRequest.newBuilder(u)
.POST(HttpRequest.BodyPublishers.ofString(REQUEST_BODY)).build(); // POST is not considered idempotent.
testNonIdempotent(u, request, code, method);
}
@Test(dataProvider = "uris") publicvoid testPUT(URI uri, int code, String method) throws Exception {
URI u = uri.resolve("foo?n=" + requestCounter.incrementAndGet());
System.out.println("Testing with " + u);
HttpRequest request = HttpRequest.newBuilder(u)
.PUT(HttpRequest.BodyPublishers.ofString(REQUEST_BODY)).build(); // PUT is considered idempotent.
testIdempotent(u, request, code, method);
}
@Test(dataProvider = "uris") publicvoid testFoo(URI uri, int code, String method) throws Exception {
URI u = uri.resolve("foo?n=" + requestCounter.incrementAndGet());
System.out.println("Testing with " + u);
HttpRequest request = HttpRequest.newBuilder(u)
.method("FOO",
HttpRequest.BodyPublishers.ofString(REQUEST_BODY)).build(); // FOO is considered idempotent.
testIdempotent(u, request, code, method);
}
@Test(dataProvider = "uris") publicvoid testGet(URI uri, int code, String method) throws Exception {
URI u = uri.resolve("foo?n=" + requestCounter.incrementAndGet());
System.out.println("Testing with " + u);
HttpRequest request = HttpRequest.newBuilder(u)
.method("GET",
HttpRequest.BodyPublishers.ofString(REQUEST_BODY)).build();
CompletableFuture<HttpResponse<String>> respCf =
client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> resp = respCf.join(); // body will be preserved except for 304 and 303: this is a GET. if (method.equals("DO_NOT_FOLLOW")) {
assertEquals(resp.statusCode(), code, u + ": status code");
} else {
assertEquals(resp.statusCode(), 200, u + ": status code");
} if (code == 304) {
assertEquals(resp.body(), "", u + ": body");
} elseif (method.equals("DO_NOT_FOLLOW")) {
assertNotEquals(resp.body(), GET_RESPONSE_BODY, u + ": body");
assertNotEquals(resp.body(), REQUEST_BODY, u + ": body");
} elseif (code == 303) {
assertEquals(resp.body(), GET_RESPONSE_BODY, u + ": body");
} else {
assertEquals(resp.body(), REQUEST_BODY, u + ": body");
}
}
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.