2016-04-30 00:30:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, 2016, 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
|
2016-12-09 11:35:02 +00:00
|
|
|
* @bug 8087112 8159814
|
|
|
|
* @library /lib/testlibrary server
|
2016-04-30 00:30:31 +01:00
|
|
|
* @build jdk.testlibrary.SimpleSSLContext
|
2016-12-09 11:35:02 +00:00
|
|
|
* @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common
|
|
|
|
* jdk.incubator.httpclient/jdk.incubator.http.internal.frame
|
|
|
|
* jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
|
|
|
|
* @run testng/othervm -Djdk.httpclient.HttpClient.log=errors,requests,responses ServerPush
|
2016-04-30 00:30:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.*;
|
|
|
|
import java.nio.file.*;
|
|
|
|
import java.nio.file.attribute.*;
|
2016-12-09 11:35:02 +00:00
|
|
|
import jdk.incubator.http.*;
|
|
|
|
import jdk.incubator.http.HttpResponse.MultiProcessor;
|
|
|
|
import jdk.incubator.http.HttpResponse.BodyHandler;
|
2016-04-30 00:30:31 +01:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import org.testng.annotations.Test;
|
|
|
|
|
|
|
|
public class ServerPush {
|
|
|
|
|
|
|
|
static ExecutorService e = Executors.newCachedThreadPool();
|
|
|
|
|
|
|
|
static final int LOOPS = 13;
|
2016-12-09 11:35:02 +00:00
|
|
|
static final int FILE_SIZE = 512 * 1024 + 343;
|
2016-04-30 00:30:31 +01:00
|
|
|
|
|
|
|
static Path tempFile;
|
|
|
|
|
|
|
|
@Test(timeOut=30000)
|
|
|
|
public static void test() throws Exception {
|
|
|
|
Http2TestServer server = null;
|
2016-12-09 11:35:02 +00:00
|
|
|
final Path dir = Files.createTempDirectory("serverPush");
|
2016-04-30 00:30:31 +01:00
|
|
|
try {
|
2016-12-09 11:35:02 +00:00
|
|
|
server = new Http2TestServer(false, 0);
|
|
|
|
server.addHandler(new PushHandler(FILE_SIZE, LOOPS), "/");
|
2016-04-30 00:30:31 +01:00
|
|
|
tempFile = TestUtil.getAFile(FILE_SIZE);
|
|
|
|
|
|
|
|
System.err.println("Server listening on port " + server.getAddress().getPort());
|
|
|
|
server.start();
|
|
|
|
int port = server.getAddress().getPort();
|
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
// use multi-level path
|
|
|
|
URI uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c");
|
|
|
|
HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
|
2016-04-30 00:30:31 +01:00
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
CompletableFuture<MultiMapResult<Path>> cf =
|
|
|
|
HttpClient.newBuilder().version(HttpClient.Version.HTTP_2)
|
|
|
|
.executor(e).build().sendAsync(
|
|
|
|
request, MultiProcessor.asMap((req) -> {
|
|
|
|
URI u = req.uri();
|
|
|
|
Path path = Paths.get(dir.toString(), u.getPath());
|
|
|
|
try {
|
|
|
|
Files.createDirectories(path.getParent());
|
|
|
|
} catch (IOException ee) {
|
|
|
|
throw new UncheckedIOException(ee);
|
|
|
|
}
|
|
|
|
return Optional.of(BodyHandler.asFile(path));
|
|
|
|
}
|
|
|
|
));
|
|
|
|
MultiMapResult<Path> results = cf.get();
|
2016-04-30 00:30:31 +01:00
|
|
|
|
|
|
|
//HttpResponse resp = request.response();
|
|
|
|
System.err.println(results.size());
|
2016-12-09 11:35:02 +00:00
|
|
|
Set<HttpRequest> requests = results.keySet();
|
|
|
|
|
|
|
|
for (HttpRequest r : requests) {
|
|
|
|
URI u = r.uri();
|
|
|
|
Path result = results.get(r).get().body();
|
2016-04-30 00:30:31 +01:00
|
|
|
System.err.printf("%s -> %s\n", u.toString(), result.toString());
|
|
|
|
TestUtil.compareFiles(result, tempFile);
|
|
|
|
}
|
2016-12-09 11:35:02 +00:00
|
|
|
if (requests.size() != LOOPS + 1)
|
|
|
|
throw new RuntimeException("some results missing");
|
|
|
|
System.out.println("TEST OK: sleeping for 5 sec");
|
|
|
|
Thread.sleep (5 * 1000);
|
2016-04-30 00:30:31 +01:00
|
|
|
} finally {
|
|
|
|
e.shutdownNow();
|
|
|
|
server.stop();
|
|
|
|
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
|
|
|
|
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
|
|
|
|
dir.toFile().delete();
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
}
|
|
|
|
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
|
|
|
|
path.toFile().delete();
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|