|
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.VCRException; |
12 | 13 | import com.easypost.easyvcr.clients.httpurlconnection.RecordableHttpsURLConnection; |
13 | 14 | import com.google.gson.JsonParseException; |
14 | 15 | import org.junit.Assert; |
@@ -534,4 +535,42 @@ public void testReplayHttpError() throws Exception { |
534 | 535 | Assert.assertNotNull(clientAfterRequest); |
535 | 536 | Assert.assertNotNull(clientAfterRequest.getErrorStream()); |
536 | 537 | } |
| 538 | + |
| 539 | + @Test |
| 540 | + public void testCachedInteractionDoesNotExist() throws Exception { |
| 541 | + Cassette cassette = TestUtils.getCassette("test_cached_interaction_does_not_exist"); |
| 542 | + cassette.erase(); // Erase cassette before recording |
| 543 | + |
| 544 | + final String url = "https://google.com/path/to/endpoint"; |
| 545 | + |
| 546 | + // make connection using Mode.Record |
| 547 | + RecordableHttpsURLConnection connection = |
| 548 | + (RecordableHttpsURLConnection) HttpClients.newClient(HttpClientType.HttpsUrlConnection, |
| 549 | + url, cassette, Mode.Record); |
| 550 | + // make HTTP call (record to cassette) |
| 551 | + connection.connect(); |
| 552 | + Assert.assertTrue(cassette.numInteractions() > 0); // make sure we recorded something |
| 553 | + |
| 554 | + // Attempt to replay a cached interaction that does not exist |
| 555 | + // need to use strict matching to ensure we don't match a different interaction |
| 556 | + AdvancedSettings advancedSettings = new AdvancedSettings(); |
| 557 | + advancedSettings.matchRules = MatchRules.strict(); |
| 558 | + |
| 559 | + connection = (RecordableHttpsURLConnection) HttpClients.newClient(HttpClientType.HttpsUrlConnection, |
| 560 | + url + "1", cassette, Mode.Replay, advancedSettings); |
| 561 | + // make HTTP call (attempt replay from cassette) |
| 562 | + connection.connect(); |
| 563 | + |
| 564 | + // Attempt to pull data (e.g. body) from the response (via the input stream) |
| 565 | + // this throws a RuntimeException because of the way the exceptions are coalesced internally |
| 566 | + try { |
| 567 | + connection.getInputStream(); |
| 568 | + // if we get here, the exception was not thrown as expected |
| 569 | + Assert.fail(); |
| 570 | + } catch (Exception e) { |
| 571 | + Assert.assertTrue(e instanceof RuntimeException); |
| 572 | + Assert.assertTrue(e.getCause() instanceof VCRException); |
| 573 | + Assert.assertEquals(e.getCause().getMessage(), "No matching interaction found."); |
| 574 | + } |
| 575 | + } |
537 | 576 | } |
0 commit comments