|
9 | 9 | import com.easypost.easyvcr.Mode; |
10 | 10 | import com.easypost.easyvcr.RecordingExpirationException; |
11 | 11 | import com.easypost.easyvcr.TimeFrame; |
| 12 | +import com.easypost.easyvcr.Utilities; |
12 | 13 | import com.easypost.easyvcr.clients.httpurlconnection.RecordableHttpsURLConnection; |
13 | 14 | import com.google.gson.JsonParseException; |
14 | 15 | import org.junit.Assert; |
15 | 16 | import org.junit.Test; |
16 | 17 |
|
17 | 18 | import java.io.IOException; |
| 19 | +import java.io.InputStream; |
18 | 20 | import java.io.OutputStream; |
19 | 21 | import java.net.URISyntaxException; |
20 | 22 | import java.nio.charset.StandardCharsets; |
|
25 | 27 |
|
26 | 28 | import static com.easypost.easyvcr.internalutilities.Tools.readFromInputStream; |
27 | 29 |
|
28 | | -public class HttpUrlConnectionTest { |
| 30 | +public class HttpUrlConnectionTest { |
29 | 31 |
|
30 | 32 | private static FakeDataService.IPAddressData getIPAddressDataRequest(Cassette cassette, Mode mode) throws Exception { |
31 | 33 | RecordableHttpsURLConnection connection = TestUtils.getSimpleHttpsURLConnection(cassette.name, mode, null); |
@@ -419,4 +421,32 @@ public void testExpirationSettings() throws Exception { |
419 | 421 | Assert.assertThrows(RecordingExpirationException.class, () -> HttpClients.newClient(HttpClientType.HttpsUrlConnection, |
420 | 422 | FakeDataService.URL, cassette, Mode.Replay, finalAdvancedSettings)); |
421 | 423 | } |
| 424 | + |
| 425 | + @Test |
| 426 | + public void testReplayHttpError() throws Exception { |
| 427 | + Cassette cassette = TestUtils.getCassette("test_replay_http_error"); |
| 428 | + cassette.erase(); // Erase cassette before recording |
| 429 | + |
| 430 | + // make connection using Mode.Record |
| 431 | + RecordableHttpsURLConnection connection = |
| 432 | + (RecordableHttpsURLConnection) HttpClients.newClient(HttpClientType.HttpsUrlConnection, |
| 433 | + FakeDataService.URL, cassette, Mode.Record); |
| 434 | + // make data service using connection |
| 435 | + FakeDataService.HttpsUrlConnection fakeDataService = new FakeDataService.HttpsUrlConnection(connection); |
| 436 | + // make HTTP call with data service (record to cassette) |
| 437 | + RecordableHttpsURLConnection clientAfterRequest = fakeDataService.makeBadRequest(); |
| 438 | + Assert.assertTrue(cassette.numInteractions() > 0); // make sure we recorded something |
| 439 | + |
| 440 | + // make connection using Mode.Replay |
| 441 | + connection = (RecordableHttpsURLConnection) HttpClients.newClient(HttpClientType.HttpsUrlConnection, |
| 442 | + FakeDataService.URL, cassette, Mode.Replay); |
| 443 | + // make data service using connection |
| 444 | + fakeDataService = new FakeDataService.HttpsUrlConnection(connection); |
| 445 | + // make HTTP call with data service (replay from cassette) |
| 446 | + clientAfterRequest = fakeDataService.makeBadRequest(); |
| 447 | + |
| 448 | + // make sure the error stream was loaded properly |
| 449 | + Assert.assertNotNull(clientAfterRequest); |
| 450 | + Assert.assertNotNull(clientAfterRequest.getErrorStream()); |
| 451 | + } |
422 | 452 | } |
0 commit comments