Skip to content

Commit a83c43f

Browse files
authored
fix: test error responses such as 403 (#1345)
* test error responses such as 403 * format * sort imports * sort imports
1 parent 2570889 commit a83c43f

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
import static org.junit.Assert.assertNotNull;
2020
import static org.junit.Assert.assertTrue;
2121
import static org.junit.Assert.fail;
22-
import static org.junit.Assume.assumeTrue;
22+
import static org.junit.Assume.assumeFalse;
2323
import static org.mockito.Matchers.any;
2424
import static org.mockito.Mockito.mock;
2525
import static org.mockito.Mockito.when;
2626

2727
import com.google.api.client.http.GenericUrl;
28+
import com.google.api.client.http.HttpResponseException;
2829
import com.google.api.client.http.HttpTransport;
2930
import com.google.api.client.http.LowLevelHttpResponse;
3031
import com.google.api.client.util.ByteArrayStreamingContent;
@@ -54,6 +55,7 @@
5455
import org.apache.http.message.BasicHttpResponse;
5556
import org.apache.http.protocol.HttpContext;
5657
import org.apache.http.protocol.HttpRequestExecutor;
58+
import org.junit.Assert;
5759
import org.junit.Test;
5860

5961
/**
@@ -201,7 +203,7 @@ public void process(HttpRequest request, HttpContext context)
201203
@Test(timeout = 10_000L)
202204
public void testConnectTimeout() {
203205
// Apache HttpClient doesn't appear to behave correctly on windows
204-
assumeTrue(!isWindows());
206+
assumeFalse(isWindows());
205207

206208
HttpTransport httpTransport = new ApacheHttpTransport();
207209
GenericUrl url = new GenericUrl("http://google.com:81");
@@ -215,11 +217,11 @@ public void testConnectTimeout() {
215217
}
216218
}
217219

218-
static class FakeServer implements AutoCloseable {
220+
private static class FakeServer implements AutoCloseable {
219221
private final HttpServer server;
220222
private final ExecutorService executorService;
221223

222-
public FakeServer(HttpHandler httpHandler) throws IOException {
224+
FakeServer(HttpHandler httpHandler) throws IOException {
223225
this.server = HttpServer.create(new InetSocketAddress(0), 0);
224226
this.executorService = Executors.newFixedThreadPool(1);
225227
server.setExecutor(this.executorService);
@@ -262,6 +264,60 @@ public void handle(HttpExchange httpExchange) throws IOException {
262264
}
263265
}
264266

267+
@Test
268+
public void testReadErrorStream() throws IOException {
269+
final HttpHandler handler =
270+
new HttpHandler() {
271+
@Override
272+
public void handle(HttpExchange httpExchange) throws IOException {
273+
byte[] response = "Forbidden".getBytes(StandardCharsets.UTF_8);
274+
httpExchange.sendResponseHeaders(403, response.length);
275+
try (OutputStream out = httpExchange.getResponseBody()) {
276+
out.write(response);
277+
}
278+
}
279+
};
280+
try (FakeServer server = new FakeServer(handler)) {
281+
HttpTransport transport = new ApacheHttpTransport();
282+
GenericUrl testUrl = new GenericUrl("http://localhost/foo//bar");
283+
testUrl.setPort(server.getPort());
284+
com.google.api.client.http.HttpRequest getRequest =
285+
transport.createRequestFactory().buildGetRequest(testUrl);
286+
getRequest.setThrowExceptionOnExecuteError(false);
287+
com.google.api.client.http.HttpResponse response = getRequest.execute();
288+
assertEquals(403, response.getStatusCode());
289+
assertEquals("Forbidden", response.parseAsString());
290+
}
291+
}
292+
293+
@Test
294+
public void testReadErrorStream_withException() throws IOException {
295+
final HttpHandler handler =
296+
new HttpHandler() {
297+
@Override
298+
public void handle(HttpExchange httpExchange) throws IOException {
299+
byte[] response = "Forbidden".getBytes(StandardCharsets.UTF_8);
300+
httpExchange.sendResponseHeaders(403, response.length);
301+
try (OutputStream out = httpExchange.getResponseBody()) {
302+
out.write(response);
303+
}
304+
}
305+
};
306+
try (FakeServer server = new FakeServer(handler)) {
307+
HttpTransport transport = new ApacheHttpTransport();
308+
GenericUrl testUrl = new GenericUrl("http://localhost/foo//bar");
309+
testUrl.setPort(server.getPort());
310+
com.google.api.client.http.HttpRequest getRequest =
311+
transport.createRequestFactory().buildGetRequest(testUrl);
312+
try {
313+
getRequest.execute();
314+
Assert.fail();
315+
} catch (HttpResponseException ex) {
316+
assertEquals("Forbidden", ex.getContent());
317+
}
318+
}
319+
}
320+
265321
private boolean isWindows() {
266322
return System.getProperty("os.name").startsWith("Windows");
267323
}

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,14 @@ public void ignore() throws IOException {
429429
}
430430

431431
/**
432-
* Close the HTTP response content using {@link #ignore}, and disconnect using {@link
433-
* LowLevelHttpResponse#disconnect()}.
432+
* Disconnect using {@link LowLevelHttpResponse#disconnect()}, then close the HTTP response
433+
* content using {@link #ignore}.
434434
*
435435
* @since 1.4
436436
*/
437437
public void disconnect() throws IOException {
438438
// Close the connection before trying to close the InputStream content. If you are trying to
439-
// disconnect, we shouldn't need to try to read any further content.
439+
// disconnect, we shouldn't need to read any further content.
440440
response.disconnect();
441441
ignore();
442442
}

0 commit comments

Comments
 (0)