diff --git a/connectors-e2e-test/connectors-e2e-test-http/src/test/java/io/camunda/connector/e2e/HttpTests.java b/connectors-e2e-test/connectors-e2e-test-http/src/test/java/io/camunda/connector/e2e/HttpTests.java index a9ee91013e..3b4f6ca738 100644 --- a/connectors-e2e-test/connectors-e2e-test-http/src/test/java/io/camunda/connector/e2e/HttpTests.java +++ b/connectors-e2e-test/connectors-e2e-test-http/src/test/java/io/camunda/connector/e2e/HttpTests.java @@ -123,6 +123,7 @@ void basicAuth() { .property("authentication.type", BasicAuthentication.TYPE) .property("authentication.username", "username") .property("authentication.password", "password") + .property("body", "={\"order\": {\"status\": \"processing\", \"id\": string(42+3)}}") .property("resultExpression", "={orderStatus: response.body.order.status}") .writeTo(new File(tempDir, "template.json")); diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/ProxyConfiguration.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/ProxyConfiguration.java deleted file mode 100644 index 685d8dc5bc..0000000000 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/ProxyConfiguration.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH - * under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright - * ownership. Camunda licenses this file to you under the Apache License, - * Version 2.0; you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.camunda.connector.http.base.client; - -import java.util.Optional; - -public class ProxyConfiguration { - private static final String HTTP_PROXY_URL = "HTTP_PROXY_URL"; - private final String httpProxyUrl = System.getenv(HTTP_PROXY_URL); - - public Optional getHttpProxyUrl() { - return Optional.ofNullable(httpProxyUrl); - } -} diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java index dc6e997adb..d9afdf5907 100644 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java +++ b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java @@ -17,21 +17,17 @@ package io.camunda.connector.http.base.client.apache; import io.camunda.connector.api.error.ConnectorException; -import io.camunda.connector.api.error.ConnectorExceptionBuilder; import io.camunda.connector.http.base.client.HttpClient; import io.camunda.connector.http.base.client.HttpStatusHelper; -import io.camunda.connector.http.base.client.ProxyConfiguration; import io.camunda.connector.http.base.exception.ConnectorExceptionMapper; import io.camunda.connector.http.base.model.HttpCommonRequest; import io.camunda.connector.http.base.model.HttpCommonResult; import java.io.IOException; -import java.net.URISyntaxException; import org.apache.hc.client5.http.ClientProtocolException; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; -import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.io.SocketConfig; import org.apache.hc.core5.util.Timeout; @@ -104,7 +100,9 @@ public HttpCommonResult execute(HttpCommonRequest request, boolean remoteExecuti var result = httpClientBuilder .setDefaultRequestConfig(getRequestConfig(request)) - .setProxy(getProxyConfig()) + // Will allow customers to use system properties for proxy configuration + // (http.proxyHost, http.proxyPort, etc) + .useSystemProperties() .build() .execute(apacheRequest, new HttpCommonResultResponseHandler(remoteExecutionEnabled)); if (HttpStatusHelper.isError(result.status())) { @@ -124,24 +122,6 @@ public HttpCommonResult execute(HttpCommonRequest request, boolean remoteExecuti } } - HttpHost getProxyConfig() { - ProxyConfiguration proxyConfiguration = new ProxyConfiguration(); - return proxyConfiguration - .getHttpProxyUrl() - .map( - s -> { - try { - return HttpHost.create(s); - } catch (URISyntaxException e) { - throw new ConnectorExceptionBuilder() - .message("Cannot create host from URL:" + s) - .cause(e) - .build(); - } - }) - .orElse(null); - } - private RequestConfig getRequestConfig(HttpCommonRequest request) { return RequestConfig.custom() .setConnectionRequestTimeout(Timeout.ofSeconds(request.getConnectionTimeoutInSeconds())) diff --git a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java index 517b8b13de..7f064ef9ef 100644 --- a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java +++ b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java @@ -17,7 +17,6 @@ package io.camunda.connector.http.base.client.apache; import static com.github.tomakehurst.wiremock.client.WireMock.aMultipart; -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.and; import static com.github.tomakehurst.wiremock.client.WireMock.any; import static com.github.tomakehurst.wiremock.client.WireMock.containing; @@ -38,13 +37,10 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.unauthorized; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.verify; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.WireMockServer; @@ -62,13 +58,12 @@ import io.camunda.connector.http.base.model.auth.BasicAuthentication; import io.camunda.connector.http.base.model.auth.BearerAuthentication; import io.camunda.connector.http.base.model.auth.OAuthAuthentication; -import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; import org.apache.commons.text.StringEscapeUtils; +import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpHeaders; -import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpStatus; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -80,6 +75,10 @@ import org.junit.jupiter.params.provider.ValueSource; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; +import org.testcontainers.Testcontainers; +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.utility.DockerImageName; import wiremock.com.fasterxml.jackson.databind.node.JsonNodeFactory; import wiremock.com.fasterxml.jackson.databind.node.POJONode; @@ -89,22 +88,46 @@ public class CustomApacheHttpClientTest { private final CustomApacheHttpClient customApacheHttpClient = CustomApacheHttpClient.getDefault(); private final ObjectMapper objectMapper = ConnectorsObjectMapperSupplier.DEFAULT_MAPPER; - private String getHostAndPort(WireMockRuntimeInfo wmRuntimeInfo) { - return "http://localhost:" + wmRuntimeInfo.getHttpPort(); - } - @Nested class ProxyTests { - private static final WireMockServer proxy = new WireMockServer(8090); + private static final WireMockServer proxy = new WireMockServer(); + private static CustomApacheHttpClient proxiedApacheHttpClient; + private static GenericContainer proxyContainer; @BeforeAll - public static void startProxy() { + public static void setUp() { proxy.start(); + proxyContainer = + new GenericContainer<>(DockerImageName.parse("sameersbn/squid:3.5.27-2")) + .withExposedPorts(3128) + .withClasspathResourceMapping( + "squid.conf", "/etc/squid/squid.conf", BindMode.READ_ONLY) + .waitingFor(org.testcontainers.containers.wait.strategy.Wait.forListeningPort()); + Testcontainers.exposeHostPorts(proxy.port()); + proxyContainer.withAccessToHost(true); + proxyContainer.start(); + // Set up the HttpClient to use the proxy + String proxyHost = proxyContainer.getHost(); + Integer proxyPort = proxyContainer.getMappedPort(3128); + System.setProperty("http.proxyHost", proxyHost); + System.setProperty("http.proxyPort", proxyPort.toString()); + System.setProperty("http.nonProxyHosts", ""); + System.setProperty("https.proxyHost", proxyHost); + System.setProperty("https.proxyPort", proxyPort.toString()); + System.setProperty("https.nonProxyHosts", ""); + proxiedApacheHttpClient = CustomApacheHttpClient.create(HttpClients.custom()); } @AfterAll - public static void stopProxy() { + public static void tearDown() { + proxyContainer.stop(); + System.setProperty("http.proxyHost", ""); + System.setProperty("http.proxyPort", ""); + System.setProperty("http.nonProxyHosts", ""); + System.setProperty("https.proxyHost", ""); + System.setProperty("https.proxyPort", ""); + System.setProperty("https.nonProxyHosts", ""); proxy.stop(); } @@ -114,88 +137,69 @@ public void resetProxy() { } @Test - public void shouldReturn200_whenGetAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) - throws Exception { - stubFor(get("/path").willReturn(ok().withBody("Hello, world!"))); - var spied = spy(customApacheHttpClient); - when(spied.getProxyConfig()).thenReturn(HttpHost.create("http://localhost:8090")); - proxy.stubFor( - get(urlMatching(".*")) - .willReturn( - aResponse().proxiedFrom("http://localhost:" + wmRuntimeInfo.getHttpPort()))); + public void shouldReturn200_whenGetAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) { + proxy.stubFor(get("/path").willReturn(ok().withBody("Hello, world!"))); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); - HttpCommonResult result = spied.execute(request); + request.setUrl(getWireMockBaseUrlWithPath(wmRuntimeInfo, "/path")); + HttpCommonResult result = proxiedApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); assertThat(result.body()).isEqualTo("Hello, world!"); + assertThat(result.headers().get("Via")).asString().contains("squid"); proxy.verify(getRequestedFor(urlEqualTo("/path"))); } @Test - public void shouldReturn200_whenPostAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) - throws URISyntaxException { - stubFor( - post("/path").willReturn(created().withJsonBody(new POJONode(Map.of("key1", "value1"))))); - var spied = spy(customApacheHttpClient); - when(spied.getProxyConfig()).thenReturn(HttpHost.create("http://localhost:8090")); + public void shouldReturn200_whenPostAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) { proxy.stubFor( - post(urlMatching(".*")) - .willReturn( - aResponse().proxiedFrom("http://localhost:" + wmRuntimeInfo.getHttpPort()))); + post("/path").willReturn(created().withJsonBody(new POJONode(Map.of("key1", "value1"))))); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.POST); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); - HttpCommonResult result = spied.execute(request); + request.setUrl(getWireMockBaseUrlWithPath(wmRuntimeInfo, "/path")); + HttpCommonResult result = proxiedApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); assertThat(result.body()).isEqualTo(Map.of("key1", "value1")); + assertThat(result.headers().get("Via")).asString().contains("squid"); proxy.verify(postRequestedFor(urlEqualTo("/path"))); } @Test - public void shouldReturn200_whenPutAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) - throws URISyntaxException { - stubFor(put("/path").willReturn(ok().withJsonBody(new POJONode(Map.of("key1", "value1"))))); - var spied = spy(customApacheHttpClient); - when(spied.getProxyConfig()).thenReturn(HttpHost.create("http://localhost:8090")); + public void shouldReturn200_whenPutAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) { proxy.stubFor( - put(urlMatching(".*")) - .willReturn( - aResponse().proxiedFrom("http://localhost:" + wmRuntimeInfo.getHttpPort()))); + put("/path").willReturn(ok().withJsonBody(new POJONode(Map.of("key1", "value1"))))); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.PUT); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); - HttpCommonResult result = spied.execute(request); + request.setUrl(getWireMockBaseUrlWithPath(wmRuntimeInfo, "/path")); + HttpCommonResult result = proxiedApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); assertThat(result.body()).isEqualTo(Map.of("key1", "value1")); + assertThat(result.headers().get("Via")).asString().contains("squid"); proxy.verify(putRequestedFor(urlEqualTo("/path"))); } @Test - public void shouldReturn200_whenDeleteAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) - throws URISyntaxException { - stubFor(delete("/path").willReturn(noContent())); - var spied = spy(customApacheHttpClient); - when(spied.getProxyConfig()).thenReturn(HttpHost.create("http://localhost:8090")); - proxy.stubFor( - delete(urlMatching(".*")) - .willReturn( - aResponse().proxiedFrom("http://localhost:" + wmRuntimeInfo.getHttpPort()))); + public void shouldReturn200_whenDeleteAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) { + proxy.stubFor(delete("/path").willReturn(noContent())); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.DELETE); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); - HttpCommonResult result = spied.execute(request); + request.setUrl(getWireMockBaseUrlWithPath(wmRuntimeInfo, "/path")); + HttpCommonResult result = proxiedApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(204); + assertThat(result.headers().get("Via")).asString().contains("squid"); proxy.verify(deleteRequestedFor(urlEqualTo("/path"))); } + + private String getWireMockBaseUrlWithPath(WireMockRuntimeInfo wmRuntimeInfo, String path) { + return "http://host.testcontainers.internal:" + proxy.port() + path; + } } @Nested @@ -205,11 +209,12 @@ class EscapeTests { @EnumSource(HttpMethod.class) public void shouldReturn200_whenSpaceInPathAndQueryParameters( HttpMethod method, WireMockRuntimeInfo wmRuntimeInfo) { + stubFor(any(urlEqualTo("/path%20with%20spaces?andQuery=S%C3%A3o%20Paulo")).willReturn(ok())); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(method); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path with spaces"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path with spaces"); request.setQueryParameters(Map.of("andQuery", "São Paulo")); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); @@ -227,7 +232,8 @@ public void shouldReturn200_whenSpaceInPathAndQueryParametersInPath( HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(method); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path with spaces?andQuery=Param with space"); + request.setUrl( + wmRuntimeInfo.getHttpBaseUrl() + "/path with spaces?andQuery=Param with space"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -245,7 +251,7 @@ public void shouldReturn200_whenEscapedSpaceInPathAndQueryParametersInPath( HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(method); request.setUrl( - getHostAndPort(wmRuntimeInfo) + "/path%20with%20spaces?andQuery=Param with space"); + wmRuntimeInfo.getHttpBaseUrl() + "/path%20with%20spaces?andQuery=Param with space"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -263,7 +269,7 @@ public void shouldReturn200_whenNullHeaders(WireMockRuntimeInfo wmRuntimeInfo) { var headers = new HashMap(); headers.put("Content-Type", null); request.setHeaders(headers); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -276,7 +282,7 @@ public void shouldReturn200_whenNoTimeouts(WireMockRuntimeInfo wmRuntimeInfo) { request.setMethod(HttpMethod.GET); request.setConnectionTimeoutInSeconds(null); request.setReadTimeoutInSeconds(null); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -289,7 +295,7 @@ public void shouldReturn200WithoutBody_whenEmptyGet(WireMockRuntimeInfo wmRuntim HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -302,7 +308,7 @@ public void shouldReturn200WithBody_whenGetWithBody(WireMockRuntimeInfo wmRuntim HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -325,7 +331,7 @@ public void shouldReturn200WithBody_whenGetWithBodyJSON(WireMockRuntimeInfo wmRu HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); request.setHeaders(Map.of("Accept", "application/json")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -354,7 +360,7 @@ public void shouldReturn200WithBody_whenGetWithBodyXML( request.setMethod(method); request.setQueryParameters(Map.of("format", "xml")); request.setHeaders(Map.of("Accept", "application/xml")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -373,7 +379,7 @@ public void shouldReturn500_whenGetWithInvalidBody(WireMockRuntimeInfo wmRuntime stubFor(get("/path").willReturn(serverError().withStatusMessage("Invalid JSON"))); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); ConnectorException e = assertThrows(ConnectorException.class, () -> customApacheHttpClient.execute(request)); assertThat(e.getErrorCode()).isEqualTo("500"); @@ -386,7 +392,7 @@ public void shouldReturn404_whenGetWithNonExistingPath(WireMockRuntimeInfo wmRun HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); ConnectorException e = assertThrows(ConnectorException.class, () -> customApacheHttpClient.execute(request)); assertThat(e.getErrorCode()).isEqualTo("404"); @@ -401,7 +407,7 @@ public void shouldReturn408_whenGetWithTimeout(WireMockRuntimeInfo wmRuntimeInfo HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); request.setReadTimeoutInSeconds(1); ConnectorException e = assertThrows(ConnectorException.class, () -> customApacheHttpClient.execute(request)); @@ -421,7 +427,7 @@ public void shouldReturn201WithoutBody_whenEmptyPost(WireMockRuntimeInfo wmRunti HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.POST); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); @@ -436,7 +442,7 @@ public void shouldReturn201WithBody_whenPostBody(WireMockRuntimeInfo wmRuntimeIn request.setMethod(HttpMethod.POST); request.setHeaders(Map.of("header", "headerValue")); request.setBody(Map.of("key1", "value1")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); @@ -458,7 +464,7 @@ public void shouldReturn201WithBody_whenPostBodyURLEncoded(WireMockRuntimeInfo w request.setHeaders( Map.of(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType())); request.setBody(Map.of("key1", "value1", "key2", "value2")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); @@ -479,7 +485,7 @@ public void shouldReturn201WithBody_whenPostBodyMultiPart(WireMockRuntimeInfo wm request.setHeaders( Map.of(HttpHeaders.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.getMimeType())); request.setBody(Map.of("key1", "value1", "key2", "value2")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); @@ -512,7 +518,7 @@ public void shouldReturn201WithBody_whenPostBodyMultiPartWithBoundaryProvided( HttpHeaders.CONTENT_TYPE, "multipart/form-data; charset=ISO-8859-1; boundary=g7wNbtOKHnEq4vnSoWdDYS88OICfGHzBA68DqmJS")); request.setBody(Map.of("key1", "value1", "key2", "value2")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); @@ -545,7 +551,7 @@ public void shouldReturn201WithBody_whenPostBodyTextPlainWithStringBody( request.setMethod(HttpMethod.POST); request.setHeaders(Map.of(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType())); request.setBody("Hello, world!"); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); @@ -565,7 +571,7 @@ public void shouldReturn201WithBody_whenPostBodyTextPlainWithIntegerBody( request.setMethod(HttpMethod.POST); request.setHeaders(Map.of(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType())); request.setBody(123); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); @@ -585,7 +591,7 @@ public void shouldReturn200WithBody_whenPostBodyTextPlainWithBooleanBody( request.setMethod(HttpMethod.POST); request.setHeaders(Map.of(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType())); request.setBody(true); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(201); @@ -606,7 +612,7 @@ public void shouldReturn204WithoutBody_whenDelete(WireMockRuntimeInfo wmRuntimeI HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.DELETE); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path/id"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path/id"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.body()).isNull(); @@ -623,7 +629,7 @@ public void shouldReturn200WithoutBody_whenEmptyPut(WireMockRuntimeInfo wmRuntim HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.PUT); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -638,7 +644,7 @@ public void shouldReturn200WithBody_whenPutBody(WireMockRuntimeInfo wmRuntimeInf request.setMethod(HttpMethod.PUT); request.setHeaders(Map.of("header", "headerValue")); request.setBody(Map.of("key1", "value1")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -660,7 +666,7 @@ public void shouldReturn200WithBody_whenPutBodyURLEncoded(WireMockRuntimeInfo wm request.setHeaders( Map.of(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType())); request.setBody(Map.of("key1", "value1", "key2", "value2")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -681,7 +687,7 @@ public void shouldReturn200WithBody_whenPutBodyTextPlainWithStringBody( request.setMethod(HttpMethod.PUT); request.setHeaders(Map.of(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType())); request.setBody("Hello, world!"); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -697,7 +703,7 @@ public void shouldReturn200WithBody_whenPutBodyTextPlainWithIntegerBody( request.setMethod(HttpMethod.PUT); request.setHeaders(Map.of(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType())); request.setBody(123); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -725,7 +731,7 @@ public void shouldReturn200WithBody_whenGetWithBasicAuth(WireMockRuntimeInfo wmR HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); request.setHeaders(Map.of("Accept", "application/json")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); request.setAuthentication(new BasicAuthentication("user", "password")); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); @@ -745,7 +751,7 @@ public void shouldReturn401_whenGetWithWrongBasicAuth(WireMockRuntimeInfo wmRunt HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); request.setAuthentication(new BasicAuthentication("user", "password")); ConnectorException e = assertThrows(ConnectorException.class, () -> customApacheHttpClient.execute(request)); @@ -770,7 +776,7 @@ public void shouldReturn200WithBody_whenGetWithBearerAuth(WireMockRuntimeInfo wm HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); request.setAuthentication(new BearerAuthentication("token")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -798,7 +804,7 @@ public void shouldReturn200WithBody_whenGetWithApiKeyAuthInHeaders( request.setMethod(HttpMethod.GET); request.setAuthentication( new ApiKeyAuthentication(ApiKeyLocation.HEADERS, "theName", "theValue")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -826,7 +832,7 @@ public void shouldReturn200WithBody_whenGetWithApiKeyAuthInQueryParams( request.setMethod(HttpMethod.GET); request.setAuthentication( new ApiKeyAuthentication(ApiKeyLocation.QUERY, "theName", "theValue")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -856,13 +862,13 @@ public void shouldReturn200WithBody_whenGetWithOAuthAndCredentialsInBody( request.setMethod(HttpMethod.GET); request.setAuthentication( new OAuthAuthentication( - getHostAndPort(wmRuntimeInfo) + "/oauth", + wmRuntimeInfo.getHttpBaseUrl() + "/oauth", "clientId", "clientSecret", "theAudience", credentialsLocation, "read:resource")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); HttpCommonResult result = customApacheHttpClient.execute(request); assertThat(result).isNotNull(); assertThat(result.status()).isEqualTo(200); @@ -892,13 +898,13 @@ public void shouldReturn401_whenGetWithOAuthReturns401( request.setMethod(HttpMethod.GET); request.setAuthentication( new OAuthAuthentication( - getHostAndPort(wmRuntimeInfo) + "/oauth", + wmRuntimeInfo.getHttpBaseUrl() + "/oauth", "clientId", "clientSecret", "theAudience", credentialsLocation, "read:resource")); - request.setUrl(getHostAndPort(wmRuntimeInfo) + "/path"); + request.setUrl(wmRuntimeInfo.getHttpBaseUrl() + "/path"); var e = assertThrows(ConnectorException.class, () -> customApacheHttpClient.execute(request)); assertThat(e).isNotNull(); assertThat(e.getErrorCode()).isEqualTo("401"); diff --git a/connectors/http/http-base/src/test/resources/squid.conf b/connectors/http/http-base/src/test/resources/squid.conf new file mode 100644 index 0000000000..5e2071a1da --- /dev/null +++ b/connectors/http/http-base/src/test/resources/squid.conf @@ -0,0 +1,11 @@ +http_port 3128 + +# Allow SSL/HTTPS traffic on port 443 +acl SSL_ports port 443 +acl Safe_ports port 80 +acl Safe_ports port 443 +acl CONNECT method CONNECT + +# Allow all outgoing HTTP/HTTPS traffic +http_access allow all +