diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml new file mode 100644 index 0000000..7c1ad6b --- /dev/null +++ b/.github/workflows/integration-tests.yml @@ -0,0 +1,63 @@ +name: Integration Tests + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + integration-tests: + name: Run Integration Tests + runs-on: ubuntu-latest + services: + httpbin: + image: kennethreitz/httpbin + ports: + - 31888:80 + steps: + + - name: Wait for httpbin to be ready + run: | + for i in {1..10}; do + if curl -sSf http://localhost:31888/get >/dev/null; then + echo "httpbin is ready!" + exit 0 + fi + echo "Waiting for httpbin..." + sleep 3 + done + echo "httpbin did not start in time" >&2 + exit 1 + + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: "17" + distribution: temurin + cache: maven + + - name: Set up Maven settings.xml + uses: s4u/maven-settings-action@v3.1.0 + with: + sonatypeSnapshots: true + repositories: >- + [ + { + "id": "central-snapshots", + "name": "Maven Repository Switchboard", + "url": "https://central.sonatype.com/repository/maven-snapshots", + "snapshots": { "enabled": true }, + "releases": { "enabled": false } + } + ] + + - name: Run Integration Tests + env: + HTTPBIN_BASE_URL: http://localhost:31888 + run: | + mvn clean -B verify -Pintegration-tests \ + -Dhttpbin.url=$HTTPBIN_BASE_URL diff --git a/pom.xml b/pom.xml index c0f115b..83935b8 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.metricshub oss-parent - 2 + 4 simple-http-java @@ -218,4 +218,61 @@ + + + integration-tests + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add-test-source + generate-test-sources + + add-test-source + + + + src/it/java + + + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + integration-test + + integration-test + verify + + + + + 1 + false + + **/*IT.java + + ${project.build.outputDirectory} + false + + --add-exports java.base/sun.net.www.protocol.http=ALL-UNNAMED + --add-exports java.base/sun.security.ssl=ALL-UNNAMED + + + + + + + + \ No newline at end of file diff --git a/src/test/java/org/metricshub/http/HttpClientTest.java b/src/it/java/org/metricshub/http/HttpClientIT.java similarity index 69% rename from src/test/java/org/metricshub/http/HttpClientTest.java rename to src/it/java/org/metricshub/http/HttpClientIT.java index e4c68a1..10d9792 100644 --- a/src/test/java/org/metricshub/http/HttpClientTest.java +++ b/src/it/java/org/metricshub/http/HttpClientIT.java @@ -16,50 +16,21 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -class HttpClientTest { +class HttpClientIT { /** - * By default, HTTPBIN_URL points to the public instance of httpbin: - * http://httpbin.org - *

- * When running tests, you can set the HTTPBIN_URL environment variable - * to specify another address where httpbin is running. - *

- *

- * Example: - *

- * HTTPBIN_URL=http://httpbin.example.org:8080 + * Get the HTTPBIN_BASE_URL provided by the system property. This must be set + * before running the tests, for example: + * -Dhttpbin.url=https://httpbin.org */ - private static final String HTTPBIN_URL; + private static final String HTTPBIN_BASE_URL; static { - String url = System.getenv("HTTPBIN_URL"); + String url = System.getProperty("httpbin.url"); if (url == null || url.isEmpty()) { - url = "http://httpbin.org"; + throw new IllegalStateException("httpbin.url system property must be set"); } - HTTPBIN_URL = url; - } - - /** - * By default, HTTPBIN_SSL_URL points to the public instance of httpbin: - * https://httpbin.org - *

- * When running tests, you can set the HTTPBIN_SSL_URL environment variable - * to specify another address where httpbin is running. - *

- *

- * Example: - *

- * HTTPBIN_URL=https://httpbin.example.org:8082 - */ - private static final String HTTPBIN_SSL_URL; - - static { - String url = System.getenv("HTTPBIN_SSL_URL"); - if (url == null || url.isEmpty()) { - url = "https://httpbin.org"; - } - HTTPBIN_SSL_URL = url; + HTTPBIN_BASE_URL = url; } @ParameterizedTest @@ -111,9 +82,9 @@ class HttpClientTest { 524 } ) - void statusCode(int status) throws Exception { + void testStatusCode(int status) throws Exception { HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/status/" + status, + HTTPBIN_BASE_URL + "/status/" + status, "GET", null, null, @@ -132,70 +103,11 @@ void statusCode(int status) throws Exception { assertEquals(status, r.getStatusCode(), "Must return status " + status); } - @Test - void https() throws Exception { - HttpResponse r = HttpClient.sendRequest( - HTTPBIN_SSL_URL + "/status/200", - "GET", - null, - null, - null, - null, - 0, - null, - null, - null, - null, - null, - 30, - null - ); - assertEquals(200, r.getStatusCode(), "Default https must work"); - - r = - HttpClient.sendRequest( - HTTPBIN_SSL_URL + "/status/200", - "GET", - new String[] { "TLSv1.2" }, - null, - null, - null, - 0, - null, - null, - null, - null, - null, - 30, - null - ); - assertEquals(200, r.getStatusCode(), "TLSv1.2 only must work"); - - r = - HttpClient.sendRequest( - HTTPBIN_SSL_URL + "/status/200", - "GET", - new String[] { "SSLv2Hello" }, - null, - null, - null, - 0, - null, - null, - null, - null, - null, - 30, - null - ); - assertEquals(200, r.getStatusCode(), "Specifying SSLv2Hello must not break communication"); - } - @ParameterizedTest @ValueSource(strings = { "GET", "DELETE", "POST", "PUT" }) - void method(String method) throws Exception { + void testMethod(String method) throws Exception { HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/" + method.toLowerCase(), + HTTPBIN_BASE_URL + "/" + method.toLowerCase(), method, null, null, @@ -215,9 +127,9 @@ void method(String method) throws Exception { } @Test - void basicAuth() throws Exception { + void testBasicAuth() throws Exception { HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/basic-auth/mypasswordis/password", + HTTPBIN_BASE_URL + "/basic-auth/mypasswordis/password", "GET", null, "mypasswordis", @@ -236,9 +148,9 @@ void basicAuth() throws Exception { } @Test - void basicAuthFail() throws Exception { + void testBasicAuthFail() throws Exception { HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/basic-auth/mypasswordis/notpassword", + HTTPBIN_BASE_URL + "/basic-auth/mypasswordis/notpassword", "GET", null, "mypasswordis", @@ -257,9 +169,9 @@ void basicAuthFail() throws Exception { } @Test - void digestAuth() throws Exception { + void testDigestAuth() throws Exception { HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/digest-auth/auth/mypasswordis/password", + HTTPBIN_BASE_URL + "/digest-auth/auth/mypasswordis/password", "GET", null, "mypasswordis", @@ -278,9 +190,9 @@ void digestAuth() throws Exception { } @Test - void userAgent() throws Exception { + void testUserAgent() throws Exception { HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/user-agent", + HTTPBIN_BASE_URL + "/user-agent", "GET", null, null, @@ -299,9 +211,9 @@ void userAgent() throws Exception { } @Test - void userAgentDefault() throws Exception { + void testUserAgentDefault() throws Exception { HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/user-agent", + HTTPBIN_BASE_URL + "/user-agent", "GET", null, null, @@ -320,12 +232,12 @@ void userAgentDefault() throws Exception { } @Test - void headers() throws Exception { + void testHeaders() throws Exception { Map headers = new HashMap(); headers.put("first", "1"); headers.put("second", "1 + 1"); HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/headers", + HTTPBIN_BASE_URL + "/headers", "GET", null, null, @@ -345,11 +257,11 @@ void headers() throws Exception { } @Test - void deflate() throws Exception { + void testDeflate() throws Exception { Map headers = new HashMap(); headers.put("Accept-Encoding", "deflate"); HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/deflate", + HTTPBIN_BASE_URL + "/deflate", "GET", null, null, @@ -372,11 +284,11 @@ void deflate() throws Exception { } @Test - void gzip() throws Exception { + void testGzip() throws Exception { Map headers = new HashMap(); headers.put("Accept-Encoding", "gzip"); HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/gzip", + HTTPBIN_BASE_URL + "/gzip", "GET", null, null, @@ -399,11 +311,11 @@ void gzip() throws Exception { } @Test - void postBody() throws Exception { + void testPostBody() throws Exception { Map headers = new HashMap(); headers.put("Content-type", "text/plain"); HttpResponse r = HttpClient.sendRequest( - HTTPBIN_URL + "/anything", + HTTPBIN_BASE_URL + "/anything", "POST", null, null, @@ -422,12 +334,12 @@ void postBody() throws Exception { } @Test - void timeoutException() throws Exception { + void testTimeoutException() throws Exception { assertThrows( IOException.class, () -> HttpClient.sendRequest( - HTTPBIN_URL + "/delay/9", + HTTPBIN_BASE_URL + "/delay/9", "GET", null, null, @@ -447,13 +359,13 @@ void timeoutException() throws Exception { } @Test - void timeout() throws Exception { + void testTimeout() throws Exception { assertTimeout( Duration.ofSeconds(5), () -> { try { HttpClient.sendRequest( - HTTPBIN_URL + "/delay/9", + HTTPBIN_BASE_URL + "/delay/9", "GET", null, null, @@ -475,7 +387,7 @@ void timeout() throws Exception { } @Test - void utf8() throws Exception { + void testUtf8() throws Exception { HttpResponse r = HttpClient.sendRequest( "https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt", "GET", @@ -496,7 +408,7 @@ void utf8() throws Exception { } @Test - void downloadTo() throws Exception { + void testDownloadTo() throws Exception { Path tempPath = Files.createTempFile("test-download", ".txt"); tempPath.toFile().deleteOnExit(); HttpResponse r = HttpClient.sendRequest( @@ -521,7 +433,7 @@ void downloadTo() throws Exception { } @Test - void downloadToDirectory() throws Exception { + void testDownloadToDirectory() throws Exception { Path tempDirPath = Files.createTempDirectory("testDownloadToDirectory"); tempDirPath.toFile().deleteOnExit(); HttpResponse r = HttpClient.sendRequest(