mirror of
https://github.com/google/nomulus.git
synced 2025-06-27 06:44:51 +02:00
Add filter support to the test server
The lack of ObjectifyFilter means that in any tests using RegistryTestServer the Objectify session cache persists between "requests" in the same test method. This is wrong but had not caused any failures because we didn't assert anything that mattered. However, a CL I'm working on asserts that there is a creationTime on a created resource, which is set automatically on Datastore save, and therefore is still null in the session cache's version of the resource if you don't clear it before the next command. Fixing it here separately from that CL. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=141939330
This commit is contained in:
parent
c281f54def
commit
5b9219fbdd
4 changed files with 29 additions and 9 deletions
|
@ -46,9 +46,9 @@ public final class NetworkUtils {
|
|||
/**
|
||||
* Returns random unused local port that can be used for TCP listening server.
|
||||
*
|
||||
* @throws IOException if failed to find free port after {@value #PICK_ATTEMPTS} attempts
|
||||
* @throws RuntimeException if failed to find free port after {@value #PICK_ATTEMPTS} attempts
|
||||
*/
|
||||
public static int pickUnusedPort() throws IOException {
|
||||
public static int pickUnusedPort() {
|
||||
// In an ideal world, we would just listen on port 0 and use whatever random port the kernel
|
||||
// assigns us. But our CI testing system reports there are rare circumstances in which this
|
||||
// doesn't work.
|
||||
|
@ -58,7 +58,7 @@ public final class NetworkUtils {
|
|||
return serverSocket.getLocalPort();
|
||||
} catch (IOException e) {
|
||||
if (!ports.hasNext()) {
|
||||
throw new IOException("Failed to acquire random port", e);
|
||||
throw new RuntimeException("Failed to acquire random port", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,10 +54,11 @@ java_library(
|
|||
":TestServer",
|
||||
"//java/com/google/common/collect",
|
||||
"//java/com/google/common/net",
|
||||
"//java/google/registry/model",
|
||||
"//java/google/registry/module/backend",
|
||||
"//java/google/registry/module/frontend",
|
||||
"//java/google/registry/ui/server/registrar",
|
||||
"//third_party/java/jsr305_annotations",
|
||||
"//third_party/java/objectify/v4_1:v4_1_3",
|
||||
"//third_party/java/servlet/servlet_api",
|
||||
],
|
||||
)
|
||||
|
|
|
@ -19,9 +19,12 @@ import static google.registry.server.Route.route;
|
|||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.net.HostAndPort;
|
||||
import com.googlecode.objectify.ObjectifyFilter;
|
||||
import google.registry.model.ofy.OfyFilter;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import javax.servlet.Filter;
|
||||
|
||||
/** Lightweight HTTP server for testing the Nomulus Admin and Registrar consoles. */
|
||||
public final class RegistryTestServer {
|
||||
|
@ -90,11 +93,15 @@ public final class RegistryTestServer {
|
|||
route("/registrar-payment-setup",
|
||||
google.registry.module.frontend.FrontendServlet.class));
|
||||
|
||||
private static final ImmutableList<Class<? extends Filter>> FILTERS = ImmutableList.of(
|
||||
ObjectifyFilter.class,
|
||||
OfyFilter.class);
|
||||
|
||||
private final TestServer server;
|
||||
|
||||
/** @see TestServer#TestServer(HostAndPort, java.util.Map, Iterable) */
|
||||
/** @see TestServer#TestServer(HostAndPort, java.util.Map, Iterable, Iterable) */
|
||||
public RegistryTestServer(HostAndPort address) {
|
||||
server = new TestServer(address, RUNFILES, ROUTES);
|
||||
server = new TestServer(address, RUNFILES, ROUTES, FILTERS);
|
||||
}
|
||||
|
||||
/** @see TestServer#start() */
|
||||
|
|
|
@ -31,8 +31,10 @@ import java.util.concurrent.FutureTask;
|
|||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import org.mortbay.jetty.Connector;
|
||||
import org.mortbay.jetty.Handler;
|
||||
import org.mortbay.jetty.Server;
|
||||
import org.mortbay.jetty.bio.SocketConnector;
|
||||
import org.mortbay.jetty.servlet.Context;
|
||||
|
@ -80,10 +82,14 @@ public final class TestServer {
|
|||
* @param runfiles map of server paths to local directories or files, to be served statically
|
||||
* @param routes list of servlet endpoints
|
||||
*/
|
||||
public TestServer(HostAndPort address, Map<String, Path> runfiles, Iterable<Route> routes) {
|
||||
public TestServer(
|
||||
HostAndPort address,
|
||||
Map<String, Path> runfiles,
|
||||
Iterable<Route> routes,
|
||||
Iterable<Class<? extends Filter>> filters) {
|
||||
urlAddress = createUrlAddress(address);
|
||||
server.addConnector(createConnector(address));
|
||||
server.addHandler(createHandler(runfiles, routes));
|
||||
server.addHandler(createHandler(runfiles, routes, filters));
|
||||
}
|
||||
|
||||
/** Starts the HTTP server in a new thread and returns once it's online. */
|
||||
|
@ -143,7 +149,10 @@ public final class TestServer {
|
|||
}
|
||||
}
|
||||
|
||||
private Context createHandler(Map<String, Path> runfiles, Iterable<Route> routes) {
|
||||
private Context createHandler(
|
||||
Map<String, Path> runfiles,
|
||||
Iterable<Route> routes,
|
||||
Iterable<Class<? extends Filter>> filters) {
|
||||
Context context = new Context(server, CONTEXT_PATH, Context.SESSIONS);
|
||||
context.addServlet(new ServletHolder(HealthzServlet.class), "/healthz");
|
||||
for (Map.Entry<String, Path> runfile : runfiles.entrySet()) {
|
||||
|
@ -154,6 +163,9 @@ public final class TestServer {
|
|||
for (Route route : routes) {
|
||||
context.addServlet(new ServletHolder(wrapServlet(route.servletClass())), route.path());
|
||||
}
|
||||
for (Class<? extends Filter> filter : filters) {
|
||||
context.addFilter(filter, "/*", Handler.REQUEST);
|
||||
}
|
||||
ServletHolder holder = new ServletHolder(DefaultServlet.class);
|
||||
holder.setInitParameter("aliases", "1");
|
||||
context.addServlet(holder, "/*");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue