nastra commented on code in PR #10052: URL: https://github.com/apache/iceberg/pull/10052#discussion_r1560558272
########## core/src/test/java/org/apache/iceberg/rest/TestHTTPClient.java: ########## @@ -121,6 +128,108 @@ public void testHeadFailure() throws JsonProcessingException { testHttpMethodOnFailure(HttpMethod.HEAD); } + @Test + public void testProxyServer() throws IOException { + int proxyPort = 1070; + try (ClientAndServer proxyServer = startClientAndServer(proxyPort); + RESTClient clientWithProxy = + HTTPClient.builder(ImmutableMap.of()) + .uri(URI) + .withProxy("localhost", proxyPort) + .build()) { + String path = "v1/config"; + + HttpRequest mockRequest = + request("/" + path).withMethod(HttpMethod.HEAD.name().toUpperCase(Locale.ROOT)); + + HttpResponse mockResponse = response().withStatusCode(200); + + mockServer.when(mockRequest).respond(mockResponse); + proxyServer.when(mockRequest).respond(mockResponse); + + clientWithProxy.head(path, ImmutableMap.of(), (onError) -> {}); + proxyServer.verify(mockRequest, VerificationTimes.exactly(1)); + } + } + + @Test + public void testProxyCredentialProviderWithoutProxyServer() { + HttpHost proxy = new HttpHost("localhost", 1070); + BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials( + new AuthScope(proxy), + new UsernamePasswordCredentials("test-username", "test-password".toCharArray())); + Assertions.assertThatThrownBy( + () -> { + HTTPClient.builder(ImmutableMap.of()) + .uri(URI) + .withProxyCredentialsProvider(credentialsProvider) + .build(); + }) + .isInstanceOf(NullPointerException.class) + .hasMessage("Invalid http client proxy for proxy credentials provider: null"); + } + + @Test + public void testProxyServerWithNullHostname() { + Assertions.assertThatThrownBy( + () -> { + HTTPClient.builder(ImmutableMap.of()).uri(URI).withProxy(null, 1070).build(); + }) + .isInstanceOf(NullPointerException.class) + .hasMessage("Invalid hostname for http client proxy: null"); + } + + @Test + public void testProxyAuthenticationFailure() throws IOException { + int proxyPort = 1070; + String proxyHostName = "localhost"; + String authorizedUsername = "test-username"; + String authorizedPassword = "test-password"; + String invalidPassword = "invalid-password"; + + HttpHost proxy = new HttpHost(proxyHostName, proxyPort); + BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials( + new AuthScope(proxy), + new UsernamePasswordCredentials(authorizedUsername, invalidPassword.toCharArray())); + + try (ClientAndServer proxyServer = + startClientAndServer( + new Configuration() + .proxyAuthenticationUsername(authorizedUsername) + .proxyAuthenticationPassword(authorizedPassword), + proxyPort); + RESTClient clientWithProxy = + HTTPClient.builder(ImmutableMap.of()) + .uri(URI) + .withProxy(proxyHostName, proxyPort) + .withProxyCredentialsProvider(credentialsProvider) + .build()) { + + ErrorHandler onError = + new ErrorHandler() { + @Override + public ErrorResponse parseResponse(int code, String responseBody) { + return null; + } + + @Override + public void accept(ErrorResponse errorResponse) { + throw new RuntimeException(errorResponse.message() + " - " + errorResponse.code()); + } + }; + + int expectedErrorCode = HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED; Review Comment: I think it's fine to inline the error code and the msg -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org