Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ui): preserve bracketed IPv6 hosts in sidecar redirect URLs (#5538)
Liu Jie5a90be10496c
diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/UiController.java b/src/main/java/io/github/hectorvent/floci/lifecycle/UiController.javaindex c76117ffd6..c318cc5218 100644--- a/src/main/java/io/github/hectorvent/floci/lifecycle/UiController.java+++ b/src/main/java/io/github/hectorvent/floci/lifecycle/UiController.java@@ -63,6 +63,13 @@ public class UiController {private static String redirectUrl(String host, int port) {String hostname = (host == null || host.isBlank()) ? "localhost" : host;int colon = hostname.indexOf(':');+ if (hostname.startsWith("[")) {+ int closingBracket = hostname.indexOf(']');+ if (closingBracket == hostname.length() - 1+ || (closingBracket >= 0 && hostname.charAt(closingBracket + 1) == ':')) {+ colon = hostname.indexOf(':', closingBracket + 1);+ }+ }if (colon >= 0) {hostname = hostname.substring(0, colon);}diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/UiControllerTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/UiControllerTest.javanew file mode 100644index 0000000000..79907a0086--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/lifecycle/UiControllerTest.java@@ -0,0 +1,85 @@+package io.github.hectorvent.floci.lifecycle;++import io.github.hectorvent.floci.lifecycle.UiController.StatusResponse;+import io.github.hectorvent.floci.services.floci.ui.FlociUiManager;+import io.github.hectorvent.floci.services.floci.ui.FlociUiManager.UiStatus;+import io.github.hectorvent.floci.services.floci.ui.UiPages;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;+import org.junit.jupiter.params.provider.NullAndEmptySource;+import org.junit.jupiter.params.provider.ValueSource;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++class UiControllerTest {++ private FlociUiManager uiManager;+ private UiController controller;++ @BeforeEach+ void setUp() {+ uiManager = mock(FlociUiManager.class);+ controller = new UiController(uiManager, mock(UiPages.class));+ }++ @ParameterizedTest+ @CsvSource({+ "[::1]:4566, http://[::1]:4500/",+ "[::1], http://[::1]:4500/",+ "[2001:db8::1]:4566, http://[2001:db8::1]:4500/",+ "[2001:db8::1], http://[2001:db8::1]:4500/",+ "floci.example:4566, http://floci.example:4500/",+ "floci.example, http://floci.example:4500/",+ "127.0.0.1:4566, http://127.0.0.1:4500/",+ "127.0.0.1, http://127.0.0.1:4500/"+ })+ void readyStatusReplacesOnlyTheHostPort(String host, String url) {+ when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null));++ assertEquals(new StatusResponse(true, url, null), status(host));+ }++ @ParameterizedTest+ @CsvSource({+ "[::1]@evil.example:4566, http://[:4500/",+ "[::1, http://[:4500/",+ "::1, http://:4500/"+ })+ void malformedHostsKeepTheExistingBehavior(String host, String url) {+ when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null));++ assertEquals(new StatusResponse(true, url, null), status(host));+ }++ @ParameterizedTest+ @NullAndEmptySource+ @ValueSource(strings = " ")+ void missingHostDefaultsToLocalhost(String host) {+ when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null));++ assertEquals(new StatusResponse(true, "http://localhost:4500/", null), status(host));+ }++ @ParameterizedTest+ @CsvSource({"false, 4500", "true, 0", "true, -1"})+ void statusWithoutAReadyPublishedPortHasNoUrl(boolean ready, int port) {+ when(uiManager.status()).thenReturn(new UiStatus(true, ready, port, null));++ assertEquals(new StatusResponse(false, null, null), status("[::1]:4566"));+ }++ @Test+ void statusErrorTakesPrecedenceOverReadiness() {+ when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, "sidecar failed"));++ assertEquals(new StatusResponse(false, null, "sidecar failed"), status("[::1]:4566"));+ }++ private StatusResponse status(String host) {+ return (StatusResponse) controller.status(host).getEntity();+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/floci/ui/UiLandingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/floci/ui/UiLandingIntegrationTest.javaindex 265f7e0b11..39a60639b1 100644--- a/src/test/java/io/github/hectorvent/floci/services/floci/ui/UiLandingIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/floci/ui/UiLandingIntegrationTest.java@@ -1,11 +1,20 @@package io.github.hectorvent.floci.services.floci.ui;+import io.github.hectorvent.floci.services.floci.ui.FlociUiManager.UiStatus;+import io.quarkus.test.InjectMock;import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;++import java.util.List;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.containsString;import static org.hamcrest.Matchers.is;+import static org.hamcrest.Matchers.nullValue;+import static org.mockito.Mockito.when;/*** Verifies the browser landing page content-negotiation on {@code /} and the@@ -15,6 +24,43 @@ import static org.hamcrest.Matchers.is;@QuarkusTestclass UiLandingIntegrationTest {+ @InjectMock+ FlociUiManager uiManager;++ @BeforeEach+ void sidecarHasNotStarted() {+ when(uiManager.status()).thenReturn(new UiStatus(false, false, 0, null));+ }++ @ParameterizedTest+ @CsvSource({+ "[::1]:4566, http://[::1]:4500/",+ "[::1], http://[::1]:4500/",+ "[2001:db8::1]:4566, http://[2001:db8::1]:4500/",+ "[2001:db8::1], http://[2001:db8::1]:4500/",+ "floci.example:4566, http://floci.example:4500/",+ "floci.example, http://floci.example:4500/",+ "127.0.0.1:4566, http://127.0.0.1:4500/",+ "127.0.0.1, http://127.0.0.1:4500/"+ })+ void readyStatusUsesTheBrowserHostAndPublishedPort(String host, String url) {+ when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null));++ for (String prefix : List.of("_floci", "_localstack")) {+ given()+ .header("Host", host)+ .accept("application/json")+ .when()+ .get("/" + prefix + "/ui/status")+ .then()+ .statusCode(200)+ .body("ready", is(true))+ .body("url", is(url))+ .body("error", nullValue());+ }+ }++@Testvoid browserAcceptHtmlGetsLandingPage() {given()