diff --git a/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPConfigSourceInterceptor.java b/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPConfigSourceInterceptor.java new file mode 100644 index 0000000000000..a5c059a2b39cb --- /dev/null +++ b/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPConfigSourceInterceptor.java @@ -0,0 +1,49 @@ +package io.quarkus.test.common.http; + +import static io.quarkus.test.common.http.TestHTTPConfigSourceProvider.TEST_MANAGEMENT_URL_KEY; +import static io.quarkus.test.common.http.TestHTTPConfigSourceProvider.TEST_MANAGEMENT_URL_SSL_KEY; +import static io.quarkus.test.common.http.TestHTTPConfigSourceProvider.TEST_URL_KEY; +import static io.quarkus.test.common.http.TestHTTPConfigSourceProvider.TEST_URL_SSL_KEY; + +import jakarta.annotation.Priority; + +import io.smallrye.config.ConfigSourceInterceptorContext; +import io.smallrye.config.ConfigValue; +import io.smallrye.config.ExpressionConfigSourceInterceptor; +import io.smallrye.config.Priorities; + +/** + * Override the expression expansion for test urls so they can be sanitized. A simple interceptor does not work + * because the test urls values are nested expressions, so when the default expression interceptor runs, either we get + * the full value expanded or the value unexpanded. In most cases, the test urls are used as expressions, so we need to + * intercept the expression expansion directly to rewrite what we need. + */ +@Priority(Priorities.LIBRARY + 299) +public class TestHTTPConfigSourceInterceptor extends ExpressionConfigSourceInterceptor { + @Override + public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) { + if (name.equals(TEST_URL_KEY) || + name.equals(TEST_MANAGEMENT_URL_KEY) || + name.equals(TEST_URL_SSL_KEY) || + name.equals(TEST_MANAGEMENT_URL_SSL_KEY)) { + + return sanitizeUrl(super.getValue(context, name)); + } + + return context.proceed(name); + } + + private static ConfigValue sanitizeUrl(ConfigValue configValue) { + if (configValue == null || configValue.getValue() == null) { + return configValue; + } + + String url = configValue.getValue(); + url = url.replace("0.0.0.0", "localhost"); + if (url.endsWith("/")) { + url = url.substring(0, url.length() - 1); + } + + return configValue.from().withValue(url).build(); + } +} diff --git a/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPConfigSourceProvider.java b/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPConfigSourceProvider.java index f0593f04df007..7f2e25fb6fe08 100644 --- a/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPConfigSourceProvider.java +++ b/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPConfigSourceProvider.java @@ -1,6 +1,5 @@ package io.quarkus.test.common.http; -import java.io.Serializable; import java.util.Collections; import java.util.Map; import java.util.Set; @@ -26,24 +25,18 @@ public class TestHTTPConfigSourceProvider implements ConfigSourceProvider { static final String TEST_MANAGEMENT_URL_SSL_KEY = "test.management.url.ssl"; static final Map entries = Map.of( - TEST_URL_KEY, sanitizeURL(TEST_URL_VALUE), - TEST_URL_SSL_KEY, sanitizeURL(TEST_URL_SSL_VALUE), - TEST_MANAGEMENT_URL_KEY, sanitizeURL(TEST_MANAGEMENT_URL_VALUE), - TEST_MANAGEMENT_URL_SSL_KEY, sanitizeURL(TEST_MANAGEMENT_URL_SSL_VALUE), - "%dev." + TEST_URL_KEY, sanitizeURL( - "http://${quarkus.http.host:localhost}:${quarkus.http.test-port:8080}${quarkus.http.root-path:${quarkus.servlet.context-path:}}")); - - private static String sanitizeURL(String url) { - return url.replace("0.0.0.0", "localhost"); - } + TEST_URL_KEY, TEST_URL_VALUE, + TEST_URL_SSL_KEY, TEST_URL_SSL_VALUE, + TEST_MANAGEMENT_URL_KEY, TEST_MANAGEMENT_URL_VALUE, + TEST_MANAGEMENT_URL_SSL_KEY, TEST_MANAGEMENT_URL_SSL_VALUE, + "%dev." + TEST_URL_KEY, + "http://${quarkus.http.host:localhost}:${quarkus.http.test-port:8080}${quarkus.http.root-path:${quarkus.servlet.context-path:}}"); public Iterable getConfigSources(final ClassLoader forClassLoader) { return Collections.singletonList(new TestURLConfigSource()); } - static class TestURLConfigSource implements ConfigSource, Serializable { - private static final long serialVersionUID = 4841094273900625000L; - + static class TestURLConfigSource implements ConfigSource { public Map getProperties() { return entries; } diff --git a/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPResourceManager.java b/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPResourceManager.java index 87b1e87ad91b6..af8539870aef5 100644 --- a/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPResourceManager.java +++ b/test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPResourceManager.java @@ -17,7 +17,7 @@ public class TestHTTPResourceManager { public static String getUri() { try { - return sanitizeUri(ConfigProvider.getConfig().getValue("test.url", String.class)); + return ConfigProvider.getConfig().getValue("test.url", String.class); } catch (IllegalStateException e) { //massive hack for dev mode tests, dev mode has not started yet //so we don't have any way to load this correctly from config @@ -27,7 +27,7 @@ public static String getUri() { public static String getManagementUri() { try { - return sanitizeUri(ConfigProvider.getConfig().getValue("test.management.url", String.class)); + return ConfigProvider.getConfig().getValue("test.management.url", String.class); } catch (IllegalStateException e) { //massive hack for dev mode tests, dev mode has not started yet //so we don't have any way to load this correctly from config @@ -36,26 +36,19 @@ public static String getManagementUri() { } public static String getSslUri() { - return sanitizeUri(ConfigProvider.getConfig().getValue("test.url.ssl", String.class)); + return ConfigProvider.getConfig().getValue("test.url.ssl", String.class); } public static String getManagementSslUri() { - return sanitizeUri(ConfigProvider.getConfig().getValue("test.management.url.ssl", String.class)); - } - - private static String sanitizeUri(String result) { - if ((result != null) && result.endsWith("/")) { - return result.substring(0, result.length() - 1); - } - return result; + return ConfigProvider.getConfig().getValue("test.management.url.ssl", String.class); } public static String getUri(RunningQuarkusApplication application) { - return sanitizeUri(application.getConfigValue("test.url", String.class).get()); + return application.getConfigValue("test.url", String.class).get(); } public static String getSslUri(RunningQuarkusApplication application) { - return sanitizeUri(application.getConfigValue("test.url.ssl", String.class).get()); + return application.getConfigValue("test.url.ssl", String.class).get(); } public static void inject(Object testCase) { diff --git a/test-framework/common/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceInterceptor b/test-framework/common/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceInterceptor new file mode 100644 index 0000000000000..ebc40d3847730 --- /dev/null +++ b/test-framework/common/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceInterceptor @@ -0,0 +1 @@ +io.quarkus.test.common.http.TestHTTPConfigSourceInterceptor