1919import static org .junit .Assert .assertNotNull ;
2020import static org .junit .Assert .assertTrue ;
2121import static org .junit .Assert .fail ;
22- import static org .junit .Assume .assumeTrue ;
22+ import static org .junit .Assume .assumeFalse ;
2323import static org .mockito .Matchers .any ;
2424import static org .mockito .Mockito .mock ;
2525import static org .mockito .Mockito .when ;
2626
2727import com .google .api .client .http .GenericUrl ;
28+ import com .google .api .client .http .HttpResponseException ;
2829import com .google .api .client .http .HttpTransport ;
2930import com .google .api .client .http .LowLevelHttpResponse ;
3031import com .google .api .client .util .ByteArrayStreamingContent ;
5455import org .apache .http .message .BasicHttpResponse ;
5556import org .apache .http .protocol .HttpContext ;
5657import org .apache .http .protocol .HttpRequestExecutor ;
58+ import org .junit .Assert ;
5759import 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 }
0 commit comments