Skip to content

Commit

Permalink
- Fire response hook before parsing API error to avoid early exit
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 committed Jul 24, 2024
1 parent 40d1591 commit 43dfbf2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Fix bug where response hooks were not being called if an API request failed

## v7.4.0 (2024-07-24)

- Add new `Claim` service for filing claims on EasyPost shipments and insurances
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/easypost/http/Requestor.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,15 +606,15 @@ private static <T> T httpRequest(final RequestMethod method, final String url, f
}
int rCode = response.getResponseCode();
String rBody = response.getResponseBody();
if (rCode < HttpURLConnection.HTTP_OK || rCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
handleAPIError(rBody, rCode);
}

ResponseHookResponses responseHookResponses = new ResponseHookResponses(rCode, headers, method.toString(), url,
rBody, Instant.now().toString(), requestTimestamp.toString(), requestUuid.toString());

client.getResponseHooks().executeEventHandler(responseHookResponses);

if (rCode < HttpURLConnection.HTTP_OK || rCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
handleAPIError(rBody, rCode);
}

return Constants.Http.GSON.fromJson(rBody, clazz);
}

Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/easypost/HookTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.easypost;

import com.easypost.exception.API.NotFoundError;
import com.easypost.exception.EasyPostException;
import com.easypost.hooks.RequestHookResponses;
import com.easypost.hooks.ResponseHookResponses;
Expand All @@ -9,13 +10,16 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.function.Function;

public class HookTest {
private static TestUtils.VCR vcr;

private static boolean hookHit = false;

/**
* Set up the testing environment for this file.
*
Expand Down Expand Up @@ -67,6 +71,21 @@ public static Object testRequestHooks(RequestHookResponses data) {
return true;
}

public static Object testResponseHookOnHttpError(ResponseHookResponses data) {
assertEquals("https://api.easypost.com/v2/parcels/par_123", data.getPath());
assertEquals("GET", data.getMethod());
assertEquals(404, data.getHttpStatus());
assertNotNull(data.getHeaders());
assertNotNull(data.getResponseBody());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestUuid());

hookHit = true;

return true;
}

/**
* Test subscribing a response hook.
*
Expand Down Expand Up @@ -132,6 +151,29 @@ public void testUnsubscribeHooks() throws EasyPostException {
vcr.client.unsubscribeFromResponseHook(failedResponseHook);

vcr.client.parcel.create(Fixtures.basicParcel());
}

/**
* Test that response hooks are still fired even if the HTTP call fails.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testResponseHookFiredOnHTTPError() throws EasyPostException {
vcr.setUpTest("http_error");

hookHit = false;

Function<ResponseHookResponses, Object> requestHook = HookTest::testResponseHookOnHttpError;

vcr.client.subscribeToResponseHook(requestHook);

try {
vcr.client.parcel.retrieve("par_123");
} catch (NotFoundError e) {
assertEquals(404, e.getStatusCode());
}

assertTrue(hookHit);
}
}

0 comments on commit 43dfbf2

Please sign in to comment.