8378398: Modernize test/jdk/java/net/URLClassLoader/HttpTest.java

Reviewed-by: dfuchs
This commit is contained in:
Eirik Bjørsnøs 2026-02-26 15:12:21 +00:00
parent 82ff0255c5
commit 3b8abd459f

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2026, 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
@ -24,233 +24,298 @@
/**
* @test
* @bug 4636331
* @modules jdk.httpserver
* @library /test/lib
* @summary Check that URLClassLoader doesn't create excessive http
* connections
* @summary Check that URLClassLoader with HTTP paths lookups produce the expected http requests
* @run junit HttpTest
*/
import java.net.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import jdk.test.lib.net.URIBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class HttpTest {
/*
* Simple http server to service http requests. Auto shutdown
* if "idle" (no requests) for 10 seconds. Forks worker thread
* to service persistent connections. Work threads shutdown if
* "idle" for 5 seconds.
*/
static class HttpServer implements Runnable {
// HTTP server used to track requests
static HttpServer server;
private static HttpServer svr = null;
private static Counters cnts = null;
private static ServerSocket ss;
// RequestLog for capturing requests
static class RequestLog {
List<Request> log = new ArrayList<>();
private static Object counterLock = new Object();
private static int getCount = 0;
private static int headCount = 0;
class Worker extends Thread {
Socket s;
Worker(Socket s) {
this.s = s;
}
public void run() {
InputStream in = null;
try {
in = s.getInputStream();
for (;;) {
// read entire request from client
byte b[] = new byte[1024];
int n, total=0;
// max 5 seconds to wait for new request
s.setSoTimeout(5000);
try {
do {
n = in.read(b, total, b.length-total);
// max 0.5 seconds between each segment
// of request.
s.setSoTimeout(500);
if (n > 0) total += n;
} while (n > 0);
} catch (SocketTimeoutException e) { }
if (total == 0) {
s.close();
return;
}
boolean getRequest = false;
if (b[0] == 'G' && b[1] == 'E' && b[2] == 'T')
getRequest = true;
synchronized (counterLock) {
if (getRequest)
getCount++;
else
headCount++;
}
// response to client
PrintStream out = new PrintStream(
new BufferedOutputStream(
s.getOutputStream() ));
out.print("HTTP/1.1 200 OK\r\n");
out.print("Content-Length: 75000\r\n");
out.print("\r\n");
if (getRequest) {
for (int i=0; i<75*1000; i++) {
out.write( (byte)'.' );
}
}
out.flush();
} // for
} catch (Exception e) {
unexpected(e);
} finally {
if (in != null) { try {in.close(); } catch(IOException e) {unexpected(e);} }
}
}
// Add a request to the log
public synchronized void capture(String method, URI uri) {
log.add(new Request(method, uri));
}
HttpServer() throws Exception {
ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
// Clear requests
public synchronized void clear() {
log.clear();
}
public void run() {
try {
// shutdown if no request in 10 seconds.
ss.setSoTimeout(10000);
for (;;) {
Socket s = ss.accept();
(new Worker(s)).start();
}
} catch (Exception e) {
}
public synchronized List<Request> requests() {
return List.copyOf(log);
}
void unexpected(Exception e) {
System.out.println(e);
e.printStackTrace();
}
public static HttpServer create() throws Exception {
if (svr != null)
return svr;
cnts = new Counters();
svr = new HttpServer();
(new Thread(svr)).start();
return svr;
}
public static void shutdown() throws Exception {
if (svr != null) {
ss.close();
svr = null;
}
}
public int port() {
return ss.getLocalPort();
}
public static class Counters {
public void reset() {
synchronized (counterLock) {
getCount = 0;
headCount = 0;
}
}
public int getCount() {
synchronized (counterLock) {
return getCount;
}
}
public int headCount() {
synchronized (counterLock) {
return headCount;
}
}
public String toString() {
synchronized (counterLock) {
return "GET count: " + getCount + "; " +
"HEAD count: " + headCount;
}
}
}
public Counters counters() {
return cnts;
}
}
public static void main(String args[]) throws Exception {
boolean failed = false;
// Represents a single request
record Request(String method, URI path) {}
// create http server
HttpServer svr = HttpServer.create();
// Request log for this test
static RequestLog log = new RequestLog();
// create class loader
URL urls[] = {
URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())
.path("/dir1/").toURL(),
URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())
.path("/dir2/").toURL(),
// Handlers specific to tests
static Map<URI, HttpHandler> handlers = new ConcurrentHashMap<>();
// URLClassLoader with HTTP URL class path
private static URLClassLoader loader;
@BeforeAll
static void setup() throws Exception {
server = HttpServer.create();
server.bind(new InetSocketAddress(
InetAddress.getLoopbackAddress(), 0), 0);
server.createContext("/", e -> {
// Capture request in the log
log.capture(e.getRequestMethod(), e.getRequestURI());
// Check for custom handler
HttpHandler custom = handlers.get(e.getRequestURI());
if (custom != null) {
custom.handle(e);
} else {
// Successful responses echo the request path in the body
byte[] response = e.getRequestURI().getPath()
.getBytes(StandardCharsets.UTF_8);
e.sendResponseHeaders(200, response.length);
try (var out = e.getResponseBody()) {
out.write(response);
}
}
e.close();
});
server.start();
int port = server.getAddress().getPort();
// Create class loader with two HTTP URLs
URL[] searchPath = new URL[] {
getHttpUri("/dir1/", port),
getHttpUri("/dir2/", port)
};
URLClassLoader cl = new URLClassLoader(urls);
loader = new URLClassLoader(searchPath);
}
// Test 1 - check that getResource does single HEAD request
svr.counters().reset();
URL url = cl.getResource("foo.gif");
System.out.println(svr.counters());
// Create an HTTP URL for the given path and port using the loopback address
private static URL getHttpUri(String path, int port) throws Exception {
return URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.path(path).toURL();
}
if (svr.counters().getCount() > 0 ||
svr.counters().headCount() > 1) {
failed = true;
// Add redirect handler for a given path
private static void redirect(String path, String target) {
handlers.put(URI.create(path), e -> {
e.getResponseHeaders().set("Location", target);
e.sendResponseHeaders(301, 0);
});
}
// Return 404 not found for a given path
private static void notFound(String path) {
handlers.put(URI.create(path), e ->
e.sendResponseHeaders(404, 0));
}
@AfterAll
static void shutdown() {
server.stop(2000);
}
@BeforeEach
void reset() {
synchronized (log) {
log.clear();
}
handlers.clear();
}
// Check that getResource does single HEAD request
@Test
void getResourceSingleHead() {
URL url = loader.getResource("foo.gif");
// Expect one HEAD
assertRequests(e -> e
.request("HEAD", "/dir1/foo.gif")
);
}
// Check that getResource follows redirects
@Test
void getResourceShouldFollowRedirect() {
redirect("/dir1/foo.gif", "/dir1/target.gif");
URL url = loader.getResource("foo.gif");
// Expect extra HEAD for redirect target
assertRequests(e -> e
.request("HEAD", "/dir1/foo.gif")
.request("HEAD", "/dir1/target.gif")
);
/*
* Note: Long-standing behavior is that URLClassLoader:getResource
* returns a URL for the requested resource, not the location redirected to
*/
assertEquals("/dir1/foo.gif", url.getPath());
}
// Check that getResource treats a redirect to a not-found resource as a not-found resource
@Test
void getResourceRedirectTargetNotFound() {
redirect("/dir1/foo.gif", "/dir1/target.gif");
notFound("/dir1/target.gif");
URL url = loader.getResource("foo.gif");
// Expect extra HEAD for redirect target and next URL in search path
assertRequests(e -> e
.request("HEAD", "/dir1/foo.gif")
.request("HEAD", "/dir1/target.gif")
.request("HEAD", "/dir2/foo.gif")
);
// Should find URL for /dir2
assertEquals("/dir2/foo.gif", url.getPath());
}
// Check that getResourceAsStream does one HEAD and one GET request
@Test
void getResourceAsStreamSingleGet() throws IOException {
// Expect content from the first path
try (var in = loader.getResourceAsStream("foo2.gif")) {
assertEquals("/dir1/foo2.gif",
new String(in.readAllBytes(), StandardCharsets.UTF_8));
}
// Expect one HEAD, one GET
assertRequests( e -> e
.request("HEAD", "/dir1/foo2.gif")
.request("GET", "/dir1/foo2.gif")
);
}
// Check that getResourceAsStream follows redirects
@Test
void getResourceAsStreamFollowRedirect() throws IOException {
redirect("/dir1/foo.gif", "/dir1/target.gif");
// Expect content from the redirected location
try (var in = loader.getResourceAsStream("foo.gif")) {
assertEquals("/dir1/target.gif",
new String(in.readAllBytes(), StandardCharsets.UTF_8));
}
// Test 2 - check that getResourceAsStream does at most
// one GET request
svr.counters().reset();
InputStream in = cl.getResourceAsStream("foo2.gif");
in.close();
System.out.println(svr.counters());
if (svr.counters().getCount() > 1) {
failed = true;
/*
* Note: Long standing behavior of URLClassLoader::getResourceAsStream
* is to use HEAD during the findResource resource discovery and to not
* "remember" the HEAD redirect location when performing the GET. This
* explains why we observe two redirects here, one for HEAD, one for GET.
*/
assertRequests( e -> e
.request("HEAD", "/dir1/foo.gif")
.request("HEAD", "/dir1/target.gif")
.request("GET", "/dir1/foo.gif")
.request("GET", "/dir1/target.gif")
);
}
// getResourceAsStream on a 404 should try next path
@Test
void getResourceTryNextPath() throws IOException {
// Make the first path return 404
notFound("/dir1/foo.gif");
// Expect content from the second path
try (var in = loader.getResourceAsStream("foo.gif")) {
assertEquals("/dir2/foo.gif",
new String(in.readAllBytes(), StandardCharsets.UTF_8));
}
// Expect two HEADs, one GET
assertRequests(e -> e
.request("HEAD", "/dir1/foo.gif")
.request("HEAD", "/dir2/foo.gif")
.request("GET", "/dir2/foo.gif")
);
}
// Test 3 - check that getResources only does HEAD requests
svr.counters().reset();
Enumeration e = cl.getResources("foos.gif");
try {
for (;;) {
e.nextElement();
}
} catch (NoSuchElementException exc) { }
System.out.println(svr.counters());
if (svr.counters().getCount() > 1) {
failed = true;
}
// Check that getResources only does HEAD requests
@Test
void getResourcesOnlyHead() throws IOException {
Collections.list(loader.getResources("foos.gif"));
// Expect one HEAD for each path
assertRequests(e -> e
.request("HEAD", "/dir1/foos.gif")
.request("HEAD", "/dir2/foos.gif")
);
}
// shutdown http server
svr.shutdown();
// Check that getResources skips 404 URL
@Test
void getResourcesShouldSkipFailedHead() throws IOException {
// Make first path fail with 404
notFound("/dir1/foos.gif");
List<URL> resources = Collections.list(loader.getResources("foos.gif"));
// Expect one HEAD for each path
assertRequests(e -> e
.request("HEAD", "/dir1/foos.gif")
.request("HEAD", "/dir2/foos.gif")
);
if (failed) {
throw new Exception("Excessive http connections established - Test failed");
// Expect a single URL to be returned
assertEquals(1, resources.size());
}
// Utils for asserting requests
static class Expect {
List<Request> requests = new ArrayList<>();
Expect request(String method, String path) {
requests.add(new Request(method, URI.create(path)));
return this;
}
}
static void assertRequests(Consumer<Expect> e) {
// Collect expected requests
Expect exp = new Expect();
e.accept(exp);
List<Request> expected = exp.requests;
// Actual requests
List<Request> requests = log.requests();
// Verify expected number of requests
assertEquals(expected.size(), requests.size(), "Unexpected request count");
// Verify expected requests in order
for (int i = 0; i < expected.size(); i++) {
Request ex = expected.get(i);
Request req = requests.get(i);
// Verify method
assertEquals(ex.method, req.method,
String.format("Request %s has unexpected method %s", i, ex.method)
);
// Verify path
assertEquals(ex.path, req.path,
String.format("Request %s has unexpected request URI %s", i, ex.path)
);
}
}
}