Skip to content

Commit

Permalink
Consistent test urls
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jul 25, 2024
1 parent 4f31e0d commit 044af63
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,24 +25,18 @@ public class TestHTTPConfigSourceProvider implements ConfigSourceProvider {
static final String TEST_MANAGEMENT_URL_SSL_KEY = "test.management.url.ssl";

static final Map<String, String> 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<ConfigSource> 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<String, String> getProperties() {
return entries;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.test.common.http.TestHTTPConfigSourceInterceptor

0 comments on commit 044af63

Please sign in to comment.