|
22 | 22 | import java.lang.annotation.RetentionPolicy;
|
23 | 23 | import java.net.InetSocketAddress;
|
24 | 24 | import java.net.Socket;
|
| 25 | +import java.net.URL; |
| 26 | +import javax.net.ssl.HttpsURLConnection; |
25 | 27 |
|
26 | 28 | import org.junit.jupiter.api.extension.ConditionEvaluationResult;
|
27 | 29 | import org.junit.jupiter.api.extension.ExecutionCondition;
|
@@ -51,14 +53,32 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
|
51 | 53 | final EnabledWhenCDNAvailable annotation = optional.get();
|
52 | 54 | final String host = annotation.hostname();
|
53 | 55 | try (Socket socket = new Socket()) {
|
54 |
| - socket.connect(new InetSocketAddress(host, 80), TIMEOUT_MS); |
55 |
| - return ConditionEvaluationResult.enabled("Resource (CDN) reachable."); |
| 56 | + // First, try to establish a socket connection to ensure the host is reachable |
| 57 | + socket.connect(new InetSocketAddress(host, 443), TIMEOUT_MS); |
| 58 | + |
| 59 | + // Then, try to check the HTTP status by making an HTTPS request |
| 60 | + final URL url = new URL("https://" + host); |
| 61 | + final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
| 62 | + connection.setConnectTimeout(TIMEOUT_MS); |
| 63 | + connection.setReadTimeout(TIMEOUT_MS); |
| 64 | + int statusCode = connection.getResponseCode(); |
| 65 | + |
| 66 | + // If the HTTP status code indicates success (2xx range), return enabled |
| 67 | + if (statusCode >= 200 && statusCode < 300) { |
| 68 | + return ConditionEvaluationResult.enabled( |
| 69 | + "Resource (CDN) reachable with status code: " + statusCode); |
| 70 | + } else { |
| 71 | + return ConditionEvaluationResult.disabled( |
| 72 | + "Resource (CDN) reachable, but HTTP status code: " + statusCode); |
| 73 | + |
| 74 | + } |
56 | 75 | } catch (IOException e) {
|
57 |
| - // Unreachable, unresolvable or timeout |
58 |
| - return ConditionEvaluationResult.disabled("Resource (CDN) unreachable."); |
| 76 | + return ConditionEvaluationResult.disabled( |
| 77 | + "Resource (CDN) unreachable."); |
59 | 78 | }
|
60 | 79 | }
|
61 |
| - return ConditionEvaluationResult.enabled("Nothing annotated with DisabledWhenOffline."); |
| 80 | + return ConditionEvaluationResult.enabled( |
| 81 | + "Nothing annotated with EnabledWhenCDNAvailable."); |
62 | 82 | }
|
63 | 83 | }
|
64 | 84 |
|
|
0 commit comments