|
| 1 | +package io.quarkus.test.common; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 7 | + |
| 8 | +import io.restassured.RestAssured; |
| 9 | +import io.restassured.config.HttpClientConfig; |
| 10 | +import io.restassured.config.RestAssuredConfig; |
| 11 | +import io.restassured.internal.path.json.ConfigurableJsonSlurper; |
| 12 | +import io.restassured.path.json.JsonPath; |
| 13 | +import io.restassured.specification.RequestSpecification; |
| 14 | + |
| 15 | +/** |
| 16 | + * Utility class that sets the rest assured port to the default test port and meaningful timeouts. |
| 17 | + * <p> |
| 18 | + * This class works whether RestAssured is on the classpath or not - if it is not, invoking the methods of the class are |
| 19 | + * essentially NO-OPs |
| 20 | + * <p> |
| 21 | + * TODO: do we actually want this here, or should it be in a different module? |
| 22 | + */ |
| 23 | +public class RestAssuredStateManager { |
| 24 | + |
| 25 | + private static final int DEFAULT_HTTP_PORT = 8081; |
| 26 | + private static final int DEFAULT_HTTPS_PORT = 8444; |
| 27 | + |
| 28 | + private static int oldPort; |
| 29 | + private static String oldBaseURI; |
| 30 | + private static String oldBasePath; |
| 31 | + private static Object oldRestAssuredConfig; // we can't declare the type here as that would prevent this class for being loaded if RestAssured is not present |
| 32 | + private static Object oldRequestSpecification; |
| 33 | + |
| 34 | + private static final boolean REST_ASSURED_PRESENT; |
| 35 | + |
| 36 | + static { |
| 37 | + boolean present = false; |
| 38 | + try { |
| 39 | + Class.forName("io.restassured.RestAssured"); |
| 40 | + present = true; |
| 41 | + } catch (ClassNotFoundException ignored) { |
| 42 | + } |
| 43 | + REST_ASSURED_PRESENT = present; |
| 44 | + } |
| 45 | + |
| 46 | + private RestAssuredStateManager() { |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + private static int getPortFromConfig(int defaultValue, String... keys) { |
| 51 | + for (String key : keys) { |
| 52 | + Optional<Integer> port = ConfigProvider.getConfig().getOptionalValue(key, Integer.class); |
| 53 | + if (port.isPresent()) |
| 54 | + return port.get(); |
| 55 | + } |
| 56 | + return defaultValue; |
| 57 | + } |
| 58 | + |
| 59 | + public static void setURL(boolean useSecureConnection) { |
| 60 | + setURL(useSecureConnection, null, null); |
| 61 | + } |
| 62 | + |
| 63 | + public static void setURL(boolean useSecureConnection, String additionalPath) { |
| 64 | + setURL(useSecureConnection, null, additionalPath); |
| 65 | + } |
| 66 | + |
| 67 | + public static void setURL(boolean useSecureConnection, Integer port) { |
| 68 | + setURL(useSecureConnection, port, null); |
| 69 | + } |
| 70 | + |
| 71 | + public static void setURL(boolean useSecureConnection, Integer port, String additionalPath) { |
| 72 | + if (!REST_ASSURED_PRESENT) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + oldPort = RestAssured.port; |
| 77 | + if (port == null) { |
| 78 | + port = useSecureConnection ? getPortFromConfig(DEFAULT_HTTPS_PORT, "quarkus.http.test-ssl-port") |
| 79 | + : getPortFromConfig(DEFAULT_HTTP_PORT, "quarkus.lambda.mock-event-server.test-port", |
| 80 | + "quarkus.http.test-port"); |
| 81 | + } |
| 82 | + RestAssured.port = port; |
| 83 | + |
| 84 | + oldBaseURI = RestAssured.baseURI; |
| 85 | + final String protocol = useSecureConnection ? "https://" : "http://"; |
| 86 | + String host = ConfigProvider.getConfig().getOptionalValue("quarkus.http.host", String.class) |
| 87 | + .orElse("localhost"); |
| 88 | + if (host.equals("0.0.0.0")) { |
| 89 | + host = "localhost"; |
| 90 | + } |
| 91 | + RestAssured.baseURI = protocol + host; |
| 92 | + |
| 93 | + oldBasePath = RestAssured.basePath; |
| 94 | + Optional<String> basePath = ConfigProvider.getConfig().getOptionalValue("quarkus.http.root-path", |
| 95 | + String.class); |
| 96 | + if (basePath.isPresent() || additionalPath != null) { |
| 97 | + StringBuilder bp = new StringBuilder(); |
| 98 | + if (basePath.isPresent()) { |
| 99 | + if (basePath.get().startsWith("/")) { |
| 100 | + bp.append(basePath.get().substring(1)); |
| 101 | + } else { |
| 102 | + bp.append(basePath.get()); |
| 103 | + } |
| 104 | + if (bp.toString().endsWith("/")) { |
| 105 | + bp.setLength(bp.length() - 1); |
| 106 | + } |
| 107 | + } |
| 108 | + if (additionalPath != null) { |
| 109 | + if (!additionalPath.startsWith("/")) { |
| 110 | + bp.append("/"); |
| 111 | + } |
| 112 | + bp.append(additionalPath); |
| 113 | + if (bp.toString().endsWith("/")) { |
| 114 | + bp.setLength(bp.length() - 1); |
| 115 | + } |
| 116 | + } |
| 117 | + RestAssured.basePath = bp.toString(); |
| 118 | + } |
| 119 | + |
| 120 | + oldRestAssuredConfig = RestAssured.config(); |
| 121 | + |
| 122 | + Duration timeout = ConfigProvider.getConfig() |
| 123 | + .getOptionalValue("quarkus.http.test-timeout", Duration.class).orElse(Duration.ofSeconds(30)); |
| 124 | + configureTimeouts(timeout); |
| 125 | + |
| 126 | + oldRequestSpecification = RestAssured.requestSpecification; |
| 127 | + if (ConfigProvider.getConfig() |
| 128 | + .getOptionalValue("quarkus.test.rest-assured.enable-logging-on-failure", Boolean.class).orElse(true)) { |
| 129 | + RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static void configureTimeouts(Duration d) { |
| 134 | + RestAssured.config = RestAssured.config().httpClient(new HttpClientConfig() |
| 135 | + .setParam("http.conn-manager.timeout", d.toMillis()) // this needs to be long |
| 136 | + .setParam("http.connection.timeout", (int) d.toMillis()) // this needs to be int |
| 137 | + .setParam("http.socket.timeout", (int) d.toMillis())); // same here |
| 138 | + } |
| 139 | + |
| 140 | + public static void clearState() { |
| 141 | + if (!REST_ASSURED_PRESENT) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + clearURL(); |
| 146 | + |
| 147 | + JsonPath.config = null; |
| 148 | + // Reset the numberReturnType ThreadLocal in ConfigurableJsonSlurper as it is causing class loader leaks |
| 149 | + try { |
| 150 | + java.lang.reflect.Field numberReturnTypeField = ConfigurableJsonSlurper.class.getDeclaredField("numberReturnType"); |
| 151 | + numberReturnTypeField.setAccessible(true); |
| 152 | + ThreadLocal<?> threadLocal = (ThreadLocal<?>) numberReturnTypeField.get(null); |
| 153 | + if (threadLocal != null) { |
| 154 | + threadLocal.remove(); |
| 155 | + } |
| 156 | + } catch (Exception e) { |
| 157 | + // Ignore if the field doesn't exist or can't be accessed |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @deprecated most probably, you want to call {@link #clearState()}. If it's not the case, please let us know so that we |
| 163 | + * can reconsider the deprecation. |
| 164 | + * Note: when removing, please move the code to {@link #clearState()}. |
| 165 | + */ |
| 166 | + @Deprecated(forRemoval = true, since = "3.31") |
| 167 | + static void clearURL() { |
| 168 | + if (!REST_ASSURED_PRESENT) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + RestAssured.port = oldPort; |
| 173 | + RestAssured.baseURI = oldBaseURI; |
| 174 | + RestAssured.basePath = oldBasePath; |
| 175 | + RestAssured.config = (RestAssuredConfig) oldRestAssuredConfig; |
| 176 | + RestAssured.requestSpecification = (RequestSpecification) oldRequestSpecification; |
| 177 | + } |
| 178 | +} |
0 commit comments