|
1 | 1 | package io.ipinfo; |
2 | 2 |
|
3 | 3 | import io.ipinfo.api.IPinfo; |
| 4 | +import io.ipinfo.api.errors.ErrorResponseException; |
4 | 5 | import io.ipinfo.api.errors.RateLimitedException; |
5 | 6 | import io.ipinfo.api.model.ASNResponse; |
6 | 7 | import io.ipinfo.api.model.IPResponse; |
@@ -388,4 +389,47 @@ public void testLookupResproxyEmpty() throws IOException { |
388 | 389 | server.shutdown(); |
389 | 390 | } |
390 | 391 | } |
| 392 | + |
| 393 | + @Test |
| 394 | + public void testServerErrorThrowsErrorResponseException() throws IOException { |
| 395 | + MockWebServer server = new MockWebServer(); |
| 396 | + server.enqueue(new MockResponse() |
| 397 | + .setResponseCode(503) |
| 398 | + .setBody("Service Temporarily Unavailable") |
| 399 | + .addHeader("Content-Type", "text/plain")); |
| 400 | + server.start(); |
| 401 | + |
| 402 | + OkHttpClient client = new OkHttpClient.Builder() |
| 403 | + .addInterceptor(chain -> { |
| 404 | + okhttp3.Request originalRequest = chain.request(); |
| 405 | + okhttp3.HttpUrl newUrl = originalRequest.url().newBuilder() |
| 406 | + .scheme("http") |
| 407 | + .host(server.getHostName()) |
| 408 | + .port(server.getPort()) |
| 409 | + .build(); |
| 410 | + okhttp3.Request newRequest = originalRequest.newBuilder() |
| 411 | + .url(newUrl) |
| 412 | + .build(); |
| 413 | + return chain.proceed(newRequest); |
| 414 | + }) |
| 415 | + .build(); |
| 416 | + |
| 417 | + IPinfo ii = new IPinfo.Builder() |
| 418 | + .setToken("test_token") |
| 419 | + .setClient(client) |
| 420 | + .build(); |
| 421 | + |
| 422 | + try { |
| 423 | + ErrorResponseException ex = assertThrows( |
| 424 | + ErrorResponseException.class, |
| 425 | + () -> ii.lookupIP("8.8.8.8") |
| 426 | + ); |
| 427 | + assertEquals(503, ex.getStatusCode()); |
| 428 | + assertTrue(ex.getMessage().contains("503"), "message should contain status code"); |
| 429 | + assertTrue(ex.getMessage().contains("Service Temporarily Unavailable"), |
| 430 | + "message should contain response body"); |
| 431 | + } finally { |
| 432 | + server.shutdown(); |
| 433 | + } |
| 434 | + } |
391 | 435 | } |
0 commit comments