|
1 | 1 | package com.easypost; |
2 | 2 |
|
| 3 | +import com.easypost.exception.API.NotFoundError; |
3 | 4 | import com.easypost.exception.EasyPostException; |
4 | 5 | import com.easypost.hooks.RequestHookResponses; |
5 | 6 | import com.easypost.hooks.ResponseHookResponses; |
|
9 | 10 |
|
10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; |
11 | 12 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
12 | 14 | import static org.junit.jupiter.api.Assertions.fail; |
13 | 15 |
|
14 | 16 | import java.util.function.Function; |
15 | 17 |
|
16 | 18 | public class HookTest { |
17 | 19 | private static TestUtils.VCR vcr; |
18 | 20 |
|
| 21 | + private static boolean hookHit = false; |
| 22 | + |
19 | 23 | /** |
20 | 24 | * Set up the testing environment for this file. |
21 | 25 | * |
@@ -67,6 +71,27 @@ public static Object testRequestHooks(RequestHookResponses data) { |
67 | 71 | return true; |
68 | 72 | } |
69 | 73 |
|
| 74 | + /** |
| 75 | + * Test subscribing a response hook when an HTTP error occurs. |
| 76 | + * |
| 77 | + * @param data The ResponseHookResponses object representing the hook data. |
| 78 | + * @return The result of the test. |
| 79 | + */ |
| 80 | + public static Object testResponseHookOnHttpError(ResponseHookResponses data) { |
| 81 | + assertEquals("https://api.easypost.com/v2/parcels/par_123", data.getPath()); |
| 82 | + assertEquals("GET", data.getMethod()); |
| 83 | + assertEquals(404, data.getHttpStatus()); |
| 84 | + assertNotNull(data.getHeaders()); |
| 85 | + assertNotNull(data.getResponseBody()); |
| 86 | + assertNotNull(data.getRequestTimestamp()); |
| 87 | + assertNotNull(data.getRequestTimestamp()); |
| 88 | + assertNotNull(data.getRequestUuid()); |
| 89 | + |
| 90 | + hookHit = true; |
| 91 | + |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
70 | 95 | /** |
71 | 96 | * Test subscribing a response hook. |
72 | 97 | * |
@@ -132,6 +157,29 @@ public void testUnsubscribeHooks() throws EasyPostException { |
132 | 157 | vcr.client.unsubscribeFromResponseHook(failedResponseHook); |
133 | 158 |
|
134 | 159 | vcr.client.parcel.create(Fixtures.basicParcel()); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Test that response hooks are still fired even if the HTTP call fails. |
| 164 | + * |
| 165 | + * @throws EasyPostException when the request fails. |
| 166 | + */ |
| 167 | + @Test |
| 168 | + public void testResponseHookFiredOnHTTPError() throws EasyPostException { |
| 169 | + vcr.setUpTest("http_error"); |
| 170 | + |
| 171 | + hookHit = false; |
| 172 | + |
| 173 | + Function<ResponseHookResponses, Object> requestHook = HookTest::testResponseHookOnHttpError; |
| 174 | + |
| 175 | + vcr.client.subscribeToResponseHook(requestHook); |
| 176 | + |
| 177 | + try { |
| 178 | + vcr.client.parcel.retrieve("par_123"); |
| 179 | + } catch (NotFoundError e) { |
| 180 | + assertEquals(404, e.getStatusCode()); |
| 181 | + } |
135 | 182 |
|
| 183 | + assertTrue(hookHit); |
136 | 184 | } |
137 | 185 | } |
0 commit comments