19
19
import static org .junit .Assert .assertNotNull ;
20
20
import static org .junit .Assert .assertTrue ;
21
21
import static org .junit .Assert .fail ;
22
- import static org .junit .Assume .assumeTrue ;
22
+ import static org .junit .Assume .assumeFalse ;
23
23
import static org .mockito .Matchers .any ;
24
24
import static org .mockito .Mockito .mock ;
25
25
import static org .mockito .Mockito .when ;
26
26
27
27
import com .google .api .client .http .GenericUrl ;
28
+ import com .google .api .client .http .HttpResponseException ;
28
29
import com .google .api .client .http .HttpTransport ;
29
30
import com .google .api .client .http .LowLevelHttpResponse ;
30
31
import com .google .api .client .util .ByteArrayStreamingContent ;
54
55
import org .apache .http .message .BasicHttpResponse ;
55
56
import org .apache .http .protocol .HttpContext ;
56
57
import org .apache .http .protocol .HttpRequestExecutor ;
58
+ import org .junit .Assert ;
57
59
import org .junit .Test ;
58
60
59
61
/**
@@ -201,7 +203,7 @@ public void process(HttpRequest request, HttpContext context)
201
203
@ Test (timeout = 10_000L )
202
204
public void testConnectTimeout () {
203
205
// Apache HttpClient doesn't appear to behave correctly on windows
204
- assumeTrue (! isWindows ());
206
+ assumeFalse ( isWindows ());
205
207
206
208
HttpTransport httpTransport = new ApacheHttpTransport ();
207
209
GenericUrl url = new GenericUrl ("http://google.com:81" );
@@ -215,11 +217,11 @@ public void testConnectTimeout() {
215
217
}
216
218
}
217
219
218
- static class FakeServer implements AutoCloseable {
220
+ private static class FakeServer implements AutoCloseable {
219
221
private final HttpServer server ;
220
222
private final ExecutorService executorService ;
221
223
222
- public FakeServer (HttpHandler httpHandler ) throws IOException {
224
+ FakeServer (HttpHandler httpHandler ) throws IOException {
223
225
this .server = HttpServer .create (new InetSocketAddress (0 ), 0 );
224
226
this .executorService = Executors .newFixedThreadPool (1 );
225
227
server .setExecutor (this .executorService );
@@ -262,6 +264,60 @@ public void handle(HttpExchange httpExchange) throws IOException {
262
264
}
263
265
}
264
266
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
+
265
321
private boolean isWindows () {
266
322
return System .getProperty ("os.name" ).startsWith ("Windows" );
267
323
}
0 commit comments