diff options
| author | Kostya <mail@sartin.in> | 2026-10-09 12:55:16 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-09 12:55:40 +0300 |
| commit | 9a62e94261e6531c6b60d296322e0cef2a5b21e1 (patch) | |
| tree | f684d5b0f350161b331e53149e7c8dabb9e1c885 | |
| parent | 246c1ab5ffce1a1bb3c0534868b29f6e4c862a62 (diff) | |
| download | guess-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.
| -rw-r--r-- | lib/tomcat/tomcat-juli.jar | bin | 0 -> 47595 bytes | |||
| -rw-r--r-- | server/.classpath | 91 | ||||
| -rw-r--r-- | server/.project | 29 | ||||
| -rw-r--r-- | server/src/main/java/market/guess/server/CorsFilter.java | 33 | ||||
| -rw-r--r-- | server/src/main/java/market/guess/server/EngineBootstrap.java | 14 | ||||
| -rw-r--r-- | server/src/main/java/market/guess/server/HealthServlet.java | 32 | ||||
| -rw-r--r-- | server/src/main/java/market/guess/server/ServerEngine.java | 99 | ||||
| -rw-r--r-- | server/src/test/java/market/guess/server/ServerBootTest.java | 97 | ||||
| -rw-r--r-- | tools/_util.sh | 3 |
9 files changed, 397 insertions, 1 deletions
diff --git a/lib/tomcat/tomcat-juli.jar b/lib/tomcat/tomcat-juli.jar new file mode 100644 index 0000000..b68f5f7 --- /dev/null +++ b/lib/tomcat/tomcat-juli.jar | |||
| Binary files differ | |||
diff --git a/server/.classpath b/server/.classpath new file mode 100644 index 0000000..90c57ca --- /dev/null +++ b/server/.classpath | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <classpath> | ||
| 3 | <classpathentry kind="src" output="bin/classes" path="src/main/java"/> | ||
| 4 | <classpathentry kind="src" output="bin/test-classes" path="src/test/java"> | ||
| 5 | <attributes> | ||
| 6 | <attribute name="test" value="true"/> | ||
| 7 | </attributes> | ||
| 8 | </classpathentry> | ||
| 9 | <classpathentry kind="src" path="/guess-market-api"/> | ||
| 10 | <classpathentry kind="src" path="/guess-market-service"/> | ||
| 11 | <classpathentry kind="lib" path="../lib/tomcat/servlet-api.jar"/> | ||
| 12 | <classpathentry kind="lib" path="../lib/gson/gson-2.11.0.jar" sourcepath="../lib/gson/gson-2.11.0-sources.jar"/> | ||
| 13 | <classpathentry kind="lib" path="../lib/jaxb/angus-activation.jar"/> | ||
| 14 | <classpathentry kind="lib" path="../lib/jaxb/jakarta.activation-api.jar"/> | ||
| 15 | <classpathentry kind="lib" path="../lib/jaxb/jaxb-core.jar"/> | ||
| 16 | <classpathentry kind="lib" path="../lib/jaxb/jaxb-impl.jar"/> | ||
| 17 | <classpathentry kind="lib" path="../lib/jaxb/jakarta.xml.bind-api.jar"/> | ||
| 18 | <classpathentry kind="lib" path="../lib/tomcat/annotations-api.jar"> | ||
| 19 | <attributes> | ||
| 20 | <attribute name="test" value="true"/> | ||
| 21 | </attributes> | ||
| 22 | </classpathentry> | ||
| 23 | <classpathentry kind="lib" path="../lib/tomcat/catalina.jar"> | ||
| 24 | <attributes> | ||
| 25 | <attribute name="test" value="true"/> | ||
| 26 | </attributes> | ||
| 27 | </classpathentry> | ||
| 28 | <classpathentry kind="lib" path="../lib/tomcat/el-api.jar"> | ||
| 29 | <attributes> | ||
| 30 | <attribute name="test" value="true"/> | ||
| 31 | </attributes> | ||
| 32 | </classpathentry> | ||
| 33 | <classpathentry kind="lib" path="../lib/tomcat/jasper.jar"> | ||
| 34 | <attributes> | ||
| 35 | <attribute name="test" value="true"/> | ||
| 36 | </attributes> | ||
| 37 | </classpathentry> | ||
| 38 | <classpathentry kind="lib" path="../lib/tomcat/jasper-el.jar"> | ||
| 39 | <attributes> | ||
| 40 | <attribute name="test" value="true"/> | ||
| 41 | </attributes> | ||
| 42 | </classpathentry> | ||
| 43 | <classpathentry kind="lib" path="../lib/tomcat/jaspic-api.jar"> | ||
| 44 | <attributes> | ||
| 45 | <attribute name="test" value="true"/> | ||
| 46 | </attributes> | ||
| 47 | </classpathentry> | ||
| 48 | <classpathentry kind="lib" path="../lib/tomcat/jsp-api.jar"> | ||
| 49 | <attributes> | ||
| 50 | <attribute name="test" value="true"/> | ||
| 51 | </attributes> | ||
| 52 | </classpathentry> | ||
| 53 | <classpathentry kind="lib" path="../lib/tomcat/tomcat-api.jar"> | ||
| 54 | <attributes> | ||
| 55 | <attribute name="test" value="true"/> | ||
| 56 | </attributes> | ||
| 57 | </classpathentry> | ||
| 58 | <classpathentry kind="lib" path="../lib/tomcat/tomcat-coyote.jar"> | ||
| 59 | <attributes> | ||
| 60 | <attribute name="test" value="true"/> | ||
| 61 | </attributes> | ||
| 62 | </classpathentry> | ||
| 63 | <classpathentry kind="lib" path="../lib/tomcat/tomcat-juli.jar"> | ||
| 64 | <attributes> | ||
| 65 | <attribute name="test" value="true"/> | ||
| 66 | </attributes> | ||
| 67 | </classpathentry> | ||
| 68 | <classpathentry kind="lib" path="../lib/tomcat/tomcat-util.jar"> | ||
| 69 | <attributes> | ||
| 70 | <attribute name="test" value="true"/> | ||
| 71 | </attributes> | ||
| 72 | </classpathentry> | ||
| 73 | <classpathentry kind="lib" path="../lib/tomcat/tomcat-util-scan.jar"> | ||
| 74 | <attributes> | ||
| 75 | <attribute name="test" value="true"/> | ||
| 76 | </attributes> | ||
| 77 | </classpathentry> | ||
| 78 | <classpathentry kind="lib" path="../lib/tomcat/tomcat-websocket.jar"> | ||
| 79 | <attributes> | ||
| 80 | <attribute name="test" value="true"/> | ||
| 81 | </attributes> | ||
| 82 | </classpathentry> | ||
| 83 | <classpathentry kind="lib" path="../lib/tomcat/websocket-api.jar"> | ||
| 84 | <attributes> | ||
| 85 | <attribute name="test" value="true"/> | ||
| 86 | </attributes> | ||
| 87 | </classpathentry> | ||
| 88 | <classpathentry kind="lib" path="../lib/junit/junit-platform-console-standalone-6.1.1.jar"/> | ||
| 89 | <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
| 90 | <classpathentry kind="output" path="bin"/> | ||
| 91 | </classpath> | ||
diff --git a/server/.project b/server/.project new file mode 100644 index 0000000..f807d88 --- /dev/null +++ b/server/.project | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <projectDescription> | ||
| 3 | <name>guess-market-server</name> | ||
| 4 | <comment></comment> | ||
| 5 | <projects> | ||
| 6 | <project>guess-market-api</project> | ||
| 7 | <project>guess-market-service</project> | ||
| 8 | </projects> | ||
| 9 | <buildSpec> | ||
| 10 | <buildCommand> | ||
| 11 | <name>org.eclipse.jdt.core.javabuilder</name> | ||
| 12 | <arguments></arguments> | ||
| 13 | </buildCommand> | ||
| 14 | </buildSpec> | ||
| 15 | <natures> | ||
| 16 | <nature>org.eclipse.jdt.core.javanature</nature> | ||
| 17 | </natures> | ||
| 18 | <filteredResources> | ||
| 19 | <filter> | ||
| 20 | <id>1791557442636</id> | ||
| 21 | <name></name> | ||
| 22 | <type>30</type> | ||
| 23 | <matcher> | ||
| 24 | <id>org.eclipse.core.resources.regexFilterMatcher</id> | ||
| 25 | <arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments> | ||
| 26 | </matcher> | ||
| 27 | </filter> | ||
| 28 | </filteredResources> | ||
| 29 | </projectDescription> | ||
diff --git a/server/src/main/java/market/guess/server/CorsFilter.java b/server/src/main/java/market/guess/server/CorsFilter.java new file mode 100644 index 0000000..f8ebfa8 --- /dev/null +++ b/server/src/main/java/market/guess/server/CorsFilter.java | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import jakarta.servlet.FilterChain; | ||
| 4 | import jakarta.servlet.ServletException; | ||
| 5 | import jakarta.servlet.annotation.WebFilter; | ||
| 6 | import jakarta.servlet.http.HttpFilter; | ||
| 7 | import jakarta.servlet.http.HttpServletRequest; | ||
| 8 | import jakarta.servlet.http.HttpServletResponse; | ||
| 9 | import java.io.IOException; | ||
| 10 | |||
| 11 | @WebFilter("/api/*") | ||
| 12 | public final class CorsFilter extends HttpFilter { | ||
| 13 | @Override | ||
| 14 | protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) | ||
| 15 | throws IOException, ServletException { | ||
| 16 | var origin = req.getHeader("Origin"); | ||
| 17 | if (origin != null) { | ||
| 18 | res.setHeader("Access-Control-Allow-Origin", origin); | ||
| 19 | res.setHeader("Access-Control-Allow-Credentials", "true"); | ||
| 20 | res.addHeader("Vary", "Origin"); | ||
| 21 | } | ||
| 22 | |||
| 23 | if ("OPTIONS".equals(req.getMethod())) { | ||
| 24 | res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); | ||
| 25 | res.setHeader("Access-Control-Allow-Headers", "Content-Type"); | ||
| 26 | res.setHeader("Access-Control-Max-Age", "600"); | ||
| 27 | res.setStatus(HttpServletResponse.SC_NO_CONTENT); | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | |||
| 31 | chain.doFilter(req, res); | ||
| 32 | } | ||
| 33 | } | ||
diff --git a/server/src/main/java/market/guess/server/EngineBootstrap.java b/server/src/main/java/market/guess/server/EngineBootstrap.java new file mode 100644 index 0000000..5a06a31 --- /dev/null +++ b/server/src/main/java/market/guess/server/EngineBootstrap.java | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import jakarta.servlet.ServletContextEvent; | ||
| 4 | import jakarta.servlet.ServletContextListener; | ||
| 5 | import jakarta.servlet.annotation.WebListener; | ||
| 6 | import java.time.Clock; | ||
| 7 | |||
| 8 | @WebListener | ||
| 9 | public final class EngineBootstrap implements ServletContextListener { | ||
| 10 | @Override | ||
| 11 | public void contextInitialized(ServletContextEvent sce) { | ||
| 12 | ServerEngine.install(sce.getServletContext(), new ServerEngine(Clock.systemUTC())); | ||
| 13 | } | ||
| 14 | } | ||
diff --git a/server/src/main/java/market/guess/server/HealthServlet.java b/server/src/main/java/market/guess/server/HealthServlet.java new file mode 100644 index 0000000..124bf8d --- /dev/null +++ b/server/src/main/java/market/guess/server/HealthServlet.java | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import com.google.gson.Gson; | ||
| 4 | import jakarta.servlet.annotation.WebServlet; | ||
| 5 | import jakarta.servlet.http.HttpServlet; | ||
| 6 | import jakarta.servlet.http.HttpServletRequest; | ||
| 7 | import jakarta.servlet.http.HttpServletResponse; | ||
| 8 | import java.io.IOException; | ||
| 9 | import java.util.Map; | ||
| 10 | |||
| 11 | @WebServlet("/api/health") | ||
| 12 | public final class HealthServlet extends HttpServlet { | ||
| 13 | private static final Gson GSON = new Gson(); | ||
| 14 | |||
| 15 | @Override | ||
| 16 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { | ||
| 17 | var engine = ServerEngine.from(getServletContext()); | ||
| 18 | res.setContentType("application/json"); | ||
| 19 | res.setCharacterEncoding("UTF-8"); | ||
| 20 | if (engine == null) { | ||
| 21 | res.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); | ||
| 22 | GSON.toJson(Map.of("status", "starting"), res.getWriter()); | ||
| 23 | return; | ||
| 24 | } | ||
| 25 | GSON.toJson( | ||
| 26 | Map.of( | ||
| 27 | "status", "ok", | ||
| 28 | "events", engine.events().getAll().size(), | ||
| 29 | "users", engine.users().getAll().size()), | ||
| 30 | res.getWriter()); | ||
| 31 | } | ||
| 32 | } | ||
diff --git a/server/src/main/java/market/guess/server/ServerEngine.java b/server/src/main/java/market/guess/server/ServerEngine.java new file mode 100644 index 0000000..f9413a8 --- /dev/null +++ b/server/src/main/java/market/guess/server/ServerEngine.java | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import jakarta.servlet.ServletContext; | ||
| 4 | import java.nio.file.Path; | ||
| 5 | import java.time.Clock; | ||
| 6 | import market.guess.api.AccountContext; | ||
| 7 | import market.guess.api.CatalogContext; | ||
| 8 | import market.guess.api.GuessMarketContext; | ||
| 9 | import market.guess.api.LoadResult; | ||
| 10 | import market.guess.api.Result; | ||
| 11 | import market.guess.service.LocalGuessMarketContext; | ||
| 12 | import market.guess.service.catalog.LocalCatalogContext; | ||
| 13 | import market.guess.service.catalog.infrastructure.MarketContext; | ||
| 14 | import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; | ||
| 15 | import market.guess.service.catalog.infrastructure.provider.Loader; | ||
| 16 | import market.guess.service.catalog.infrastructure.provider.v3.XMLLoaderV3; | ||
| 17 | import market.guess.service.catalog.infrastructure.provider.v3.XMLValidatorV3; | ||
| 18 | import market.guess.service.catalog.infrastructure.repository.EventRepository; | ||
| 19 | import market.guess.service.catalog.infrastructure.repository.InMemoryEventRepository; | ||
| 20 | import market.guess.service.catalog.infrastructure.repository.InMemoryUserRepository; | ||
| 21 | import market.guess.service.catalog.infrastructure.repository.UserRepository; | ||
| 22 | import market.guess.service.fulfillment.LocalFulfillmentContext; | ||
| 23 | import market.guess.service.ledger.LedgerContext; | ||
| 24 | import market.guess.service.ledger.LocalAccountContext; | ||
| 25 | import market.guess.service.matching.LocalMatchingEngine; | ||
| 26 | import market.guess.service.risk.LocalRiskEngine; | ||
| 27 | import market.guess.service.settlement.LocalSettlementContext; | ||
| 28 | |||
| 29 | public final class ServerEngine { | ||
| 30 | private static final String ATTRIBUTE = ServerEngine.class.getName(); | ||
| 31 | |||
| 32 | private final EventRepository events = new InMemoryEventRepository(); | ||
| 33 | private final UserRepository users = new InMemoryUserRepository(); | ||
| 34 | private final XMLLoaderV3 uploads; | ||
| 35 | private final CatalogContext catalog; | ||
| 36 | private final AccountContext accounts; | ||
| 37 | private final GuessMarketContext market; | ||
| 38 | |||
| 39 | public ServerEngine(Clock clock) { | ||
| 40 | var marketInfra = new MarketContext(events, users); | ||
| 41 | var ledger = new LedgerContext(users, clock); | ||
| 42 | |||
| 43 | this.uploads = new XMLLoaderV3(events, new EventMapperV3(), new XMLValidatorV3()); | ||
| 44 | this.catalog = new LocalCatalogContext(NO_DISK, marketInfra, events); | ||
| 45 | this.accounts = new LocalAccountContext(users, ledger); | ||
| 46 | this.market = | ||
| 47 | new LocalGuessMarketContext( | ||
| 48 | marketInfra, | ||
| 49 | new LocalRiskEngine(events), | ||
| 50 | new LocalMatchingEngine(clock), | ||
| 51 | new LocalFulfillmentContext(ledger), | ||
| 52 | new LocalSettlementContext(ledger)); | ||
| 53 | } | ||
| 54 | |||
| 55 | public static void install(ServletContext ctx, ServerEngine engine) { | ||
| 56 | ctx.setAttribute(ATTRIBUTE, engine); | ||
| 57 | } | ||
| 58 | |||
| 59 | public static ServerEngine from(ServletContext ctx) { | ||
| 60 | return (ServerEngine) ctx.getAttribute(ATTRIBUTE); | ||
| 61 | } | ||
| 62 | |||
| 63 | public EventRepository events() { | ||
| 64 | return events; | ||
| 65 | } | ||
| 66 | |||
| 67 | public UserRepository users() { | ||
| 68 | return users; | ||
| 69 | } | ||
| 70 | |||
| 71 | public XMLLoaderV3 uploads() { | ||
| 72 | return uploads; | ||
| 73 | } | ||
| 74 | |||
| 75 | public CatalogContext catalog() { | ||
| 76 | return catalog; | ||
| 77 | } | ||
| 78 | |||
| 79 | public AccountContext accounts() { | ||
| 80 | return accounts; | ||
| 81 | } | ||
| 82 | |||
| 83 | public GuessMarketContext market() { | ||
| 84 | return market; | ||
| 85 | } | ||
| 86 | |||
| 87 | private static final Loader NO_DISK = | ||
| 88 | new Loader() { | ||
| 89 | @Override | ||
| 90 | public Result<LoadResult> load(Path path) { | ||
| 91 | return Result.error("UNSUPPORTED", "The server only accepts uploaded files."); | ||
| 92 | } | ||
| 93 | |||
| 94 | @Override | ||
| 95 | public Result<LoadResult> seed(CatalogContext context) { | ||
| 96 | return Result.error("UNSUPPORTED", "The server only accepts uploaded files."); | ||
| 97 | } | ||
| 98 | }; | ||
| 99 | } | ||
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 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 4 | import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| 5 | import static org.junit.jupiter.api.Assertions.assertNull; | ||
| 6 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 7 | |||
| 8 | import java.net.URI; | ||
| 9 | import java.net.http.HttpClient; | ||
| 10 | import java.net.http.HttpRequest; | ||
| 11 | import java.net.http.HttpResponse; | ||
| 12 | import java.nio.file.Files; | ||
| 13 | import java.nio.file.Path; | ||
| 14 | import org.apache.catalina.startup.Tomcat; | ||
| 15 | import org.apache.catalina.webresources.DirResourceSet; | ||
| 16 | import org.apache.catalina.webresources.StandardRoot; | ||
| 17 | import org.apache.tomcat.util.scan.StandardJarScanner; | ||
| 18 | import org.junit.jupiter.api.AfterAll; | ||
| 19 | import org.junit.jupiter.api.BeforeAll; | ||
| 20 | import org.junit.jupiter.api.Test; | ||
| 21 | import org.junit.jupiter.api.io.TempDir; | ||
| 22 | |||
| 23 | class 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 | } | ||
diff --git a/tools/_util.sh b/tools/_util.sh index d6e8db5..3a29ecb 100644 --- a/tools/_util.sh +++ b/tools/_util.sh | |||
| @@ -5,10 +5,11 @@ case "$(uname -s)" in | |||
| 5 | *) SEP=":" ;; | 5 | *) SEP=":" ;; |
| 6 | esac | 6 | esac |
| 7 | 7 | ||
| 8 | MODULES="api service ui-console ui-desktop" | 8 | MODULES="api service server ui-console ui-desktop" |
| 9 | 9 | ||
| 10 | DEPS_api="" | 10 | DEPS_api="" |
| 11 | DEPS_service="api" | 11 | DEPS_service="api" |
| 12 | DEPS_server="api service" | ||
| 12 | DEPS_ui_console="api service" | 13 | DEPS_ui_console="api service" |
| 13 | DEPS_ui_desktop="api service" | 14 | DEPS_ui_desktop="api service" |
| 14 | 15 | ||