From 87e7c0589e186fc1e87bb8595034183dfd8fe21a Mon Sep 17 00:00:00 2001 From: Adam Kolodziejczyk Date: Tue, 10 Dec 2024 15:47:31 +0100 Subject: [PATCH] use different http client for token --- .../client/authentication/OauthLatestIT.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/test/java/net/snowflake/client/authentication/OauthLatestIT.java b/src/test/java/net/snowflake/client/authentication/OauthLatestIT.java index 7c0a594e0..59f074a03 100644 --- a/src/test/java/net/snowflake/client/authentication/OauthLatestIT.java +++ b/src/test/java/net/snowflake/client/authentication/OauthLatestIT.java @@ -1,14 +1,16 @@ package net.snowflake.client.authentication; import static net.snowflake.client.authentication.AuthConnectionParameters.getOauthConnectionParameters; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.DataOutputStream; import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.List; @@ -58,7 +60,7 @@ void shouldThrowErrorForMismatchedOauthUsername() throws IOException, Interrupte "The user you were trying to authenticate as differs from the user tied to the access token."); } - private String getToken() throws IOException, InterruptedException { + private String getToken() throws IOException { List data = Stream.of( "username=" + System.getenv("SNOWFLAKE_AUTH_TEST_OKTA_USER"), @@ -73,23 +75,27 @@ private String getToken() throws IOException, InterruptedException { + System.getenv("SNOWFLAKE_AUTH_TEST_OAUTH_CLIENT_SECRET"); String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8)); - HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest request = - HttpRequest.newBuilder() - .uri(URI.create(System.getenv("SNOWFLAKE_AUTH_TEST_OAUTH_URL"))) - .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8") - .header("Authorization", "Basic " + encodedAuth) - .POST(HttpRequest.BodyPublishers.ofString(String.join("&", data))) - .build(); + URL url = new URL(System.getenv("SNOWFLAKE_AUTH_TEST_OAUTH_URL")); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty( + "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); + connection.setRequestProperty("Authorization", "Basic " + encodedAuth); + connection.setDoOutput(true); - HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); - - if (response.statusCode() != 200) { - throw new IOException("Failed to get access token, response code: " + response.statusCode()); + try (DataOutputStream out = new DataOutputStream(connection.getOutputStream())) { + out.writeBytes(String.join("&", data)); + out.flush(); } + int responseCode = connection.getResponseCode(); + assertThat("Failed to get access token, response code: " + responseCode, responseCode, is(200)); + ObjectMapper mapper = new ObjectMapper(); - JsonNode jsonNode = mapper.readTree(response.body()); + JsonNode jsonNode; + try (InputStream inputStream = connection.getInputStream()) { + jsonNode = mapper.readTree(inputStream); + } return jsonNode.get("access_token").asText(); } }