diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/DeliveryUtilities.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/DeliveryUtilities.java index 440acf5b0..1b3594fe9 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/DeliveryUtilities.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/DeliveryUtilities.java @@ -18,6 +18,7 @@ package com.djrapitops.plan.delivery; import com.djrapitops.plan.delivery.formatting.Formatters; import com.djrapitops.plan.delivery.rendering.json.graphs.Graphs; +import com.djrapitops.plan.delivery.webserver.Addresses; import com.djrapitops.plan.storage.file.PublicHtmlFiles; import dagger.Lazy; @@ -27,20 +28,27 @@ import javax.inject.Singleton; @Singleton public class DeliveryUtilities { + private final Lazy addresses; private final Lazy formatters; private final Lazy graphs; private final Lazy publicHtmlFiles; @Inject public DeliveryUtilities( + Lazy addresses, Lazy formatters, Lazy graphs, Lazy publicHtmlFiles) { + this.addresses = addresses; this.formatters = formatters; this.graphs = graphs; this.publicHtmlFiles = publicHtmlFiles; } + public Addresses getAddresses() { + return addresses.get(); + } + public Formatters getFormatters() { return formatters.get(); } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/PublicHtmlTest.java b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/PublicHtmlTest.java new file mode 100644 index 000000000..7022fa153 --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/PublicHtmlTest.java @@ -0,0 +1,90 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.webserver; + +import com.djrapitops.plan.PlanSystem; +import com.djrapitops.plan.delivery.DeliveryUtilities; +import com.djrapitops.plan.settings.config.PlanConfig; +import extension.FullSystemExtension; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import utilities.HTTPConnector; + +import java.net.HttpURLConnection; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author AuroraLS3 + */ +@ExtendWith(FullSystemExtension.class) +class PublicHtmlTest { + + private static final HTTPConnector CONNECTOR = new HTTPConnector(); + + @BeforeAll + static void setupSystem(PlanSystem system) { + system.enable(); + } + + @AfterAll + static void teardownSystem(PlanSystem system) { + system.disable(); + } + + @ParameterizedTest + @CsvSource({ + "avif", "bin", "bmp", + "css", "csv", "eot", + "gif", "html", "htm", + "ico", "ics", "js", + "jpeg", "jpg", "json", + "jsonld", "mjs", "otf", + "pdf", "php", "png", + "rtf", "svg", "tif", + "tiff", "ttf", "txt", + "woff", "woff2", "xml", + }) + void customFileIsResponded(String extension, PlanConfig config, DeliveryUtilities deliveryUtilities) throws Exception { + String fileName = "test." + extension; + Path publicHtmlDirectory = config.getResourceSettings().getPublicHtmlDirectory(); + Path file = publicHtmlDirectory.resolve(fileName); + Files.createDirectories(publicHtmlDirectory); + Files.write(file, new byte[]{1, 1, 1, 1}); + String address = deliveryUtilities.getAddresses().getAccessAddress() + .orElse(deliveryUtilities.getAddresses().getFallbackLocalhostAddress()); + + assertEquals(200, access(address + "/" + fileName)); + assertEquals(404, access(address + "/does-not-exist-" + fileName)); + } + + private int access(String address) throws Exception { + HttpURLConnection connection = null; + try { + connection = CONNECTOR.getConnection("GET", address); + + return connection.getResponseCode(); + } finally { + if (connection != null) connection.disconnect(); + } + } +} diff --git a/Plan/common/src/test/java/extension/FullSystemExtension.java b/Plan/common/src/test/java/extension/FullSystemExtension.java index 161d26bab..fb123f450 100644 --- a/Plan/common/src/test/java/extension/FullSystemExtension.java +++ b/Plan/common/src/test/java/extension/FullSystemExtension.java @@ -26,6 +26,7 @@ import com.djrapitops.plan.settings.config.paths.WebserverSettings; import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.file.PlanFiles; import com.djrapitops.plan.storage.file.PublicHtmlFiles; +import javassist.tools.web.Webserver; import org.junit.jupiter.api.extension.*; import utilities.RandomData; import utilities.dagger.PlanPluginComponent; @@ -90,6 +91,7 @@ public class FullSystemExtension implements ParameterResolver, BeforeAllCallback Database.class.equals(type) || DeliveryUtilities.class.equals(type) || PublicHtmlFiles.class.equals(type) || + Webserver.class.equals(type) || Exporter.class.equals(type); } @@ -131,6 +133,9 @@ public class FullSystemExtension implements ParameterResolver, BeforeAllCallback if (PublicHtmlFiles.class.equals(type)) { return planSystem.getDeliveryUtilities().getPublicHtmlFiles(); } + if (Webserver.class.equals(type)) { + return planSystem.getWebServerSystem().getWebServer(); + } if (Exporter.class.equals(type)) { return planSystem.getExportSystem().getExporter(); }