diff options
Diffstat (limited to 'server/src/test')
| -rw-r--r-- | server/src/test/java/market/guess/server/ServerBootTest.java | 97 |
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 @@ | |||
| 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 | } | ||