aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/market
diff options
context:
space:
mode:
authorKostya <mail@sartin.in>2026-10-09 12:55:16 +0300
committerKostya <mail@sartin.in>2026-10-09 12:55:40 +0300
commit9a62e94261e6531c6b60d296322e0cef2a5b21e1 (patch)
treef684d5b0f350161b331e53149e7c8dabb9e1c885 /server/src/test/java/market
parent246c1ab5ffce1a1bb3c0534868b29f6e4c862a62 (diff)
downloadguess-market-9a62e94261e6531c6b60d296322e0cef2a5b21e1.tar.gz
guess-market-9a62e94261e6531c6b60d296322e0cef2a5b21e1.tar.xz
guess-market-9a62e94261e6531c6b60d296322e0cef2a5b21e1.zip
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.
Diffstat (limited to 'server/src/test/java/market')
-rw-r--r--server/src/test/java/market/guess/server/ServerBootTest.java97
1 files changed, 97 insertions, 0 deletions
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 @@
1package market.guess.server;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertFalse;
5import static org.junit.jupiter.api.Assertions.assertNull;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7
8import java.net.URI;
9import java.net.http.HttpClient;
10import java.net.http.HttpRequest;
11import java.net.http.HttpResponse;
12import java.nio.file.Files;
13import java.nio.file.Path;
14import org.apache.catalina.startup.Tomcat;
15import org.apache.catalina.webresources.DirResourceSet;
16import org.apache.catalina.webresources.StandardRoot;
17import org.apache.tomcat.util.scan.StandardJarScanner;
18import org.junit.jupiter.api.AfterAll;
19import org.junit.jupiter.api.BeforeAll;
20import org.junit.jupiter.api.Test;
21import org.junit.jupiter.api.io.TempDir;
22
23class ServerBootTest {
24 private static final String ORIGIN = "http://localhost:5173";
25 private static final HttpClient HTTP = HttpClient.newHttpClient();
26
27 @TempDir static Path base;
28 private static Tomcat tomcat;
29 private static String api;
30
31 @BeforeAll
32 static void start() throws Exception {
33 tomcat = new Tomcat();
34 tomcat.setBaseDir(base.toString());
35 tomcat.setPort(0);
36 tomcat.getConnector();
37
38 var docBase = Files.createDirectories(base.resolve("webapp"));
39 var ctx = tomcat.addWebapp("/guess-market", docBase.toString());
40 ctx.setParentClassLoader(ServerBootTest.class.getClassLoader());
41 ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false);
42 var resources = new StandardRoot(ctx);
43 resources.addPreResources(
44 new DirResourceSet(
45 resources,
46 "/WEB-INF/classes",
47 Path.of("build/classes/server").toAbsolutePath().toString(),
48 "/"));
49 ctx.setResources(resources);
50
51 tomcat.start();
52 api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api";
53 }
54
55 @AfterAll
56 static void stop() throws Exception {
57 tomcat.stop();
58 tomcat.destroy();
59 }
60
61 private static HttpResponse<String> send(HttpRequest.Builder req) throws Exception {
62 return HTTP.send(req.build(), HttpResponse.BodyHandlers.ofString());
63 }
64
65 @Test
66 void healthReportsEmptyMarket() throws Exception {
67 var res = send(HttpRequest.newBuilder(URI.create(api + "/health")));
68 assertEquals(200, res.statusCode());
69 assertTrue(res.headers().firstValue("Content-Type").orElse("").startsWith("application/json"));
70 assertTrue(res.body().contains("\"status\":\"ok\""), res.body());
71 assertTrue(res.body().contains("\"events\":0"), res.body());
72 }
73
74 @Test
75 void preflightAllowsCredentialedCrossOriginCalls() throws Exception {
76 var res =
77 send(
78 HttpRequest.newBuilder(URI.create(api + "/health"))
79 .method("OPTIONS", HttpRequest.BodyPublishers.noBody())
80 .header("Origin", ORIGIN)
81 .header("Access-Control-Request-Method", "POST"));
82 assertEquals(204, res.statusCode());
83 assertEquals(ORIGIN, res.headers().firstValue("Access-Control-Allow-Origin").orElse(null));
84 assertEquals("true", res.headers().firstValue("Access-Control-Allow-Credentials").orElse(null));
85 assertTrue(res.headers().firstValue("Access-Control-Allow-Methods").orElse("").contains("POST"));
86 }
87
88 @Test
89 void corsHeadersOnlyForCrossOriginRequests() throws Exception {
90 var cross = send(HttpRequest.newBuilder(URI.create(api + "/health")).header("Origin", ORIGIN));
91 assertEquals(ORIGIN, cross.headers().firstValue("Access-Control-Allow-Origin").orElse(null));
92
93 var same = send(HttpRequest.newBuilder(URI.create(api + "/health")));
94 assertNull(same.headers().firstValue("Access-Control-Allow-Origin").orElse(null));
95 assertFalse(same.body().isEmpty());
96 }
97}