From 9a62e94261e6531c6b60d296322e0cef2a5b21e1 Mon Sep 17 00:00:00 2001 From: Kostya Date: Fri, 9 Oct 2026 12:55:16 +0300 Subject: server: add Tomcat webapp module with shared engine and CORS Exercise 3 moves the market behind a Tomcat server that the desktop and browser clients share. Add a server module, registered in the build scripts, holding the pieces every later endpoint builds on: - ServerEngine wires one in-memory market (repositories, ledger, trading pipeline, v3 upload loader) and lives in the servlet context for the webapp's lifetime. Its catalog refuses path-based loading, so events can only arrive through uploaded streams and the server never reads or writes files. - EngineBootstrap builds that engine when the webapp deploys. - CorsFilter answers preflights and echoes the caller's Origin with Allow-Credentials, which the browser client needs to send its session cookie from another origin. With usernames as the only login, echoing any origin exposes nothing a page couldn't get by logging in itself. - /api/health reports that the engine is up. Vendor tomcat-juli 10.1.26 next to the other Tomcat jars. It ships in Tomcat's bin/ rather than lib/, and embedded Tomcat cannot start without it. A test deploys the module into embedded Tomcat, so annotation scanning, the health check and CORS are checked against a real container. --- .../java/market/guess/server/ServerBootTest.java | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 server/src/test/java/market/guess/server/ServerBootTest.java (limited to 'server/src/test/java') diff --git a/server/src/test/java/market/guess/server/ServerBootTest.java b/server/src/test/java/market/guess/server/ServerBootTest.java new file mode 100644 index 0000000..6fdcdc6 --- /dev/null +++ b/server/src/test/java/market/guess/server/ServerBootTest.java @@ -0,0 +1,97 @@ +package market.guess.server; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.webresources.DirResourceSet; +import org.apache.catalina.webresources.StandardRoot; +import org.apache.tomcat.util.scan.StandardJarScanner; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class ServerBootTest { + private static final String ORIGIN = "http://localhost:5173"; + private static final HttpClient HTTP = HttpClient.newHttpClient(); + + @TempDir static Path base; + private static Tomcat tomcat; + private static String api; + + @BeforeAll + static void start() throws Exception { + tomcat = new Tomcat(); + tomcat.setBaseDir(base.toString()); + tomcat.setPort(0); + tomcat.getConnector(); + + var docBase = Files.createDirectories(base.resolve("webapp")); + var ctx = tomcat.addWebapp("/guess-market", docBase.toString()); + ctx.setParentClassLoader(ServerBootTest.class.getClassLoader()); + ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false); + var resources = new StandardRoot(ctx); + resources.addPreResources( + new DirResourceSet( + resources, + "/WEB-INF/classes", + Path.of("build/classes/server").toAbsolutePath().toString(), + "/")); + ctx.setResources(resources); + + tomcat.start(); + api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api"; + } + + @AfterAll + static void stop() throws Exception { + tomcat.stop(); + tomcat.destroy(); + } + + private static HttpResponse send(HttpRequest.Builder req) throws Exception { + return HTTP.send(req.build(), HttpResponse.BodyHandlers.ofString()); + } + + @Test + void healthReportsEmptyMarket() throws Exception { + var res = send(HttpRequest.newBuilder(URI.create(api + "/health"))); + assertEquals(200, res.statusCode()); + assertTrue(res.headers().firstValue("Content-Type").orElse("").startsWith("application/json")); + assertTrue(res.body().contains("\"status\":\"ok\""), res.body()); + assertTrue(res.body().contains("\"events\":0"), res.body()); + } + + @Test + void preflightAllowsCredentialedCrossOriginCalls() throws Exception { + var res = + send( + HttpRequest.newBuilder(URI.create(api + "/health")) + .method("OPTIONS", HttpRequest.BodyPublishers.noBody()) + .header("Origin", ORIGIN) + .header("Access-Control-Request-Method", "POST")); + assertEquals(204, res.statusCode()); + assertEquals(ORIGIN, res.headers().firstValue("Access-Control-Allow-Origin").orElse(null)); + assertEquals("true", res.headers().firstValue("Access-Control-Allow-Credentials").orElse(null)); + assertTrue(res.headers().firstValue("Access-Control-Allow-Methods").orElse("").contains("POST")); + } + + @Test + void corsHeadersOnlyForCrossOriginRequests() throws Exception { + var cross = send(HttpRequest.newBuilder(URI.create(api + "/health")).header("Origin", ORIGIN)); + assertEquals(ORIGIN, cross.headers().firstValue("Access-Control-Allow-Origin").orElse(null)); + + var same = send(HttpRequest.newBuilder(URI.create(api + "/health"))); + assertNull(same.headers().firstValue("Access-Control-Allow-Origin").orElse(null)); + assertFalse(same.body().isEmpty()); + } +} -- cgit v1.2.3