|
| 1 | +package com.getindata.connectors.http.internal.table.lookup; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.http.HttpRequest; |
| 6 | +import java.time.Duration; |
| 7 | + |
| 8 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 9 | +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
| 10 | +import com.github.tomakehurst.wiremock.stubbing.StubMapping; |
| 11 | +import org.apache.flink.api.common.RuntimeExecutionMode; |
| 12 | +import org.apache.flink.api.common.restartstrategy.RestartStrategies; |
| 13 | +import org.apache.flink.configuration.Configuration; |
| 14 | +import org.apache.flink.configuration.ExecutionOptions; |
| 15 | +import org.apache.flink.streaming.api.CheckpointingMode; |
| 16 | +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +import com.getindata.connectors.http.internal.HeaderPreprocessor; |
| 24 | +import com.getindata.connectors.http.internal.utils.HttpHeaderUtils; |
| 25 | +import static com.getindata.connectors.http.TestHelper.readTestFile; |
| 26 | +import static com.getindata.connectors.http.internal.table.lookup.HttpLookupConnectorOptions.*; |
| 27 | + |
| 28 | +public class JavaNetHttpPollingClientWithWireTest { |
| 29 | + private static final String BASE_URL = "http://localhost.com"; |
| 30 | + |
| 31 | + private static final String SAMPLES_FOLDER = "/auth/"; |
| 32 | + private static final int SERVER_PORT = 9090; |
| 33 | + |
| 34 | + private static final int HTTPS_SERVER_PORT = 8443; |
| 35 | + |
| 36 | + private static final String SERVER_KEYSTORE_PATH = |
| 37 | + "src/test/resources/security/certs/serverKeyStore.jks"; |
| 38 | + |
| 39 | + private static final String SERVER_TRUSTSTORE_PATH = |
| 40 | + "src/test/resources/security/certs/serverTrustStore.jks"; |
| 41 | + |
| 42 | + private static final String ENDPOINT = "/auth"; |
| 43 | + private static final String BEARER_REQUEST = "Bearer Dummy"; |
| 44 | + |
| 45 | + private WireMockServer wireMockServer; |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + @BeforeEach |
| 48 | + public void setup() { |
| 49 | + |
| 50 | + File keyStoreFile = new File(SERVER_KEYSTORE_PATH); |
| 51 | + File trustStoreFile = new File(SERVER_TRUSTSTORE_PATH); |
| 52 | + |
| 53 | + wireMockServer = new WireMockServer( |
| 54 | + WireMockConfiguration.wireMockConfig() |
| 55 | + .port(SERVER_PORT) |
| 56 | + .httpsPort(HTTPS_SERVER_PORT) |
| 57 | + .keystorePath(keyStoreFile.getAbsolutePath()) |
| 58 | + .keystorePassword("password") |
| 59 | + .keyManagerPassword("password") |
| 60 | + .needClientAuth(true) |
| 61 | + .trustStorePath(trustStoreFile.getAbsolutePath()) |
| 62 | + .trustStorePassword("password") |
| 63 | + .extensions(JsonTransform.class) |
| 64 | + ); |
| 65 | + wireMockServer.start(); |
| 66 | + |
| 67 | + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); |
| 68 | + env.setRestartStrategy(RestartStrategies.noRestart()); |
| 69 | + Configuration config = new Configuration(); |
| 70 | + config.set(ExecutionOptions.RUNTIME_MODE, RuntimeExecutionMode.STREAMING); |
| 71 | + env.configure(config, getClass().getClassLoader()); |
| 72 | + env.enableCheckpointing(1000, CheckpointingMode.EXACTLY_ONCE); |
| 73 | + } |
| 74 | + |
| 75 | + @AfterEach |
| 76 | + public void tearDown() { |
| 77 | + wireMockServer.stop(); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + @Test |
| 82 | + public void shouldUpdateHttpRequestIfRequiredGet() { |
| 83 | + HttpRequest httpRequest = HttpRequest.newBuilder() |
| 84 | + .GET() |
| 85 | + .uri(URI.create(BASE_URL)) |
| 86 | + .timeout(Duration.ofSeconds(1)) |
| 87 | + .setHeader("Origin","*") |
| 88 | + .setHeader("X-Content-Type-Options","nosniff") |
| 89 | + .setHeader("Content-Type","application/json") |
| 90 | + .build(); |
| 91 | + shouldUpdateHttpRequestIfRequired(httpRequest); |
| 92 | + } |
| 93 | + @Test |
| 94 | + public void shouldUpdateHttpRequestIfRequiredPut() { |
| 95 | + HttpRequest httpRequest = HttpRequest.newBuilder() |
| 96 | + .PUT( HttpRequest.BodyPublishers.ofString("foo")) |
| 97 | + .uri(URI.create(BASE_URL)) |
| 98 | + .timeout(Duration.ofSeconds(1)) |
| 99 | + .setHeader("Origin","*") |
| 100 | + .setHeader("X-Content-Type-Options","nosniff") |
| 101 | + .setHeader("Content-Type","application/json") |
| 102 | + .build(); |
| 103 | + shouldUpdateHttpRequestIfRequired(httpRequest); |
| 104 | + } |
| 105 | + private void shouldUpdateHttpRequestIfRequired(HttpRequest httpRequest) { |
| 106 | + setUpServerBodyStub(); |
| 107 | + JavaNetHttpPollingClient client = new JavaNetHttpPollingClient(null, |
| 108 | + null, |
| 109 | + HttpLookupConfig.builder().url(BASE_URL).build(), |
| 110 | + null); |
| 111 | + LookupQueryInfo lookupQueryInfo = null; |
| 112 | + HttpLookupSourceRequestEntry request = |
| 113 | + new HttpLookupSourceRequestEntry(httpRequest, lookupQueryInfo); |
| 114 | + |
| 115 | + Configuration configuration = new Configuration(); |
| 116 | + HeaderPreprocessor oidcHeaderPreProcessor = |
| 117 | + HttpHeaderUtils.createOIDCHeaderPreprocessor(configuration); |
| 118 | + HttpRequest newHttpRequest = client.updateHttpRequestIfRequired(request, |
| 119 | + oidcHeaderPreProcessor); |
| 120 | + assertThat(httpRequest).isEqualTo(newHttpRequest); |
| 121 | + configuration.setString(SOURCE_LOOKUP_OIDC_AUTH_TOKEN_ENDPOINT_URL.key(),"http://localhost:9090/auth"); |
| 122 | + configuration.setString(SOURCE_LOOKUP_OIDC_AUTH_TOKEN_REQUEST, BEARER_REQUEST); |
| 123 | + configuration.set(SOURCE_LOOKUP_OIDC_AUTH_TOKEN_EXPIRY_REDUCTION, |
| 124 | + Duration.ofSeconds(1L)); |
| 125 | + client = new JavaNetHttpPollingClient(null, |
| 126 | + null, |
| 127 | + HttpLookupConfig.builder().url(BASE_URL).readableConfig(configuration).build(), |
| 128 | + null); |
| 129 | + oidcHeaderPreProcessor = |
| 130 | + HttpHeaderUtils.createOIDCHeaderPreprocessor(configuration); |
| 131 | + // change oidcHeaderPreProcessor to use the mock http client for the authentication flow |
| 132 | + newHttpRequest = client.updateHttpRequestIfRequired(request, |
| 133 | + oidcHeaderPreProcessor); |
| 134 | + assertThat(httpRequest).isNotEqualTo(newHttpRequest); |
| 135 | + assertThat(httpRequest.headers().map().keySet().size()).isEqualTo(3); |
| 136 | + assertThat(newHttpRequest.headers().map().keySet().size()).isEqualTo(4); |
| 137 | + assertThat(httpRequest.headers().map().get("Content-Type")) |
| 138 | + .isEqualTo(newHttpRequest.headers().map().get("Content-Type")); |
| 139 | + } |
| 140 | + |
| 141 | + private StubMapping setUpServerBodyStub() { |
| 142 | + return wireMockServer.stubFor( |
| 143 | + post(urlEqualTo(ENDPOINT)) |
| 144 | + .withHeader("Content-Type", equalTo("application/x-www-form-urlencoded")) |
| 145 | + .withRequestBody(equalTo(BEARER_REQUEST)) |
| 146 | + .willReturn( |
| 147 | + aResponse() |
| 148 | + .withStatus(200) |
| 149 | + .withBody(readTestFile(SAMPLES_FOLDER + "AuthResult.json")) |
| 150 | + ) |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments