Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: test error responses such as 403 (#1345)
* test error responses such as 403

* format

* sort imports

* sort imports
  • Loading branch information
elharo committed Apr 19, 2021
1 parent 2570889 commit a83c43f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down
Expand Up @@ -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();
}
Expand Down

0 comments on commit a83c43f

Please sign in to comment.