From a83c43fa86966ca1be625086a211211e3861f7b1 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Mon, 19 Apr 2021 14:50:29 +0000 Subject: [PATCH] fix: test error responses such as 403 (#1345) * test error responses such as 403 * format * sort imports * sort imports --- .../apache/v2/ApacheHttpTransportTest.java | 64 +++++++++++++++++-- .../google/api/client/http/HttpResponse.java | 6 +- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java index a0349ad8d..f6800ff47 100644 --- a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java +++ b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java @@ -19,12 +19,13 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; +import static org.junit.Assume.assumeFalse; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.google.api.client.http.GenericUrl; +import com.google.api.client.http.HttpResponseException; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.LowLevelHttpResponse; import com.google.api.client.util.ByteArrayStreamingContent; @@ -54,6 +55,7 @@ import org.apache.http.message.BasicHttpResponse; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestExecutor; +import org.junit.Assert; import org.junit.Test; /** @@ -201,7 +203,7 @@ public void process(HttpRequest request, HttpContext context) @Test(timeout = 10_000L) public void testConnectTimeout() { // Apache HttpClient doesn't appear to behave correctly on windows - assumeTrue(!isWindows()); + assumeFalse(isWindows()); HttpTransport httpTransport = new ApacheHttpTransport(); GenericUrl url = new GenericUrl("http://google.com:81"); @@ -215,11 +217,11 @@ public void testConnectTimeout() { } } - static class FakeServer implements AutoCloseable { + private static class FakeServer implements AutoCloseable { private final HttpServer server; private final ExecutorService executorService; - public FakeServer(HttpHandler httpHandler) throws IOException { + FakeServer(HttpHandler httpHandler) throws IOException { this.server = HttpServer.create(new InetSocketAddress(0), 0); this.executorService = Executors.newFixedThreadPool(1); server.setExecutor(this.executorService); @@ -262,6 +264,60 @@ public void handle(HttpExchange httpExchange) throws IOException { } } + @Test + public void testReadErrorStream() throws IOException { + final HttpHandler handler = + new HttpHandler() { + @Override + public void handle(HttpExchange httpExchange) throws IOException { + byte[] response = "Forbidden".getBytes(StandardCharsets.UTF_8); + httpExchange.sendResponseHeaders(403, response.length); + try (OutputStream out = httpExchange.getResponseBody()) { + out.write(response); + } + } + }; + try (FakeServer server = new FakeServer(handler)) { + HttpTransport transport = new ApacheHttpTransport(); + GenericUrl testUrl = new GenericUrl("http://localhost/foo//bar"); + testUrl.setPort(server.getPort()); + com.google.api.client.http.HttpRequest getRequest = + transport.createRequestFactory().buildGetRequest(testUrl); + getRequest.setThrowExceptionOnExecuteError(false); + com.google.api.client.http.HttpResponse response = getRequest.execute(); + assertEquals(403, response.getStatusCode()); + assertEquals("Forbidden", response.parseAsString()); + } + } + + @Test + public void testReadErrorStream_withException() throws IOException { + final HttpHandler handler = + new HttpHandler() { + @Override + public void handle(HttpExchange httpExchange) throws IOException { + byte[] response = "Forbidden".getBytes(StandardCharsets.UTF_8); + httpExchange.sendResponseHeaders(403, response.length); + try (OutputStream out = httpExchange.getResponseBody()) { + out.write(response); + } + } + }; + try (FakeServer server = new FakeServer(handler)) { + HttpTransport transport = new ApacheHttpTransport(); + GenericUrl testUrl = new GenericUrl("http://localhost/foo//bar"); + testUrl.setPort(server.getPort()); + com.google.api.client.http.HttpRequest getRequest = + transport.createRequestFactory().buildGetRequest(testUrl); + try { + getRequest.execute(); + Assert.fail(); + } catch (HttpResponseException ex) { + assertEquals("Forbidden", ex.getContent()); + } + } + } + private boolean isWindows() { return System.getProperty("os.name").startsWith("Windows"); } diff --git a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java index 68d8850c8..77497a2a4 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java +++ b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java @@ -429,14 +429,14 @@ public void ignore() throws IOException { } /** - * Close the HTTP response content using {@link #ignore}, and disconnect using {@link - * LowLevelHttpResponse#disconnect()}. + * Disconnect using {@link LowLevelHttpResponse#disconnect()}, then close the HTTP response + * content using {@link #ignore}. * * @since 1.4 */ public void disconnect() throws IOException { // Close the connection before trying to close the InputStream content. If you are trying to - // disconnect, we shouldn't need to try to read any further content. + // disconnect, we shouldn't need to read any further content. response.disconnect(); ignore(); }