Skip to content

Commit

Permalink
construct fake response during webhook parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
xavdid-stripe committed Feb 5, 2025
1 parent dfafa65 commit 4e27c66
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/stripe/net/Webhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.security.NoSuchAlgorithmException;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
Expand Down Expand Up @@ -72,6 +73,12 @@ public static Event constructEvent(
StripeObject.deserializeStripeObject(
payload, Event.class, ApiResource.getGlobalResponseGetter());
Signature.verifyHeader(payload, sigHeader, secret, tolerance, clock);
// StripeObjects source their raw JSON object from their last response, but constructed webhooks don't have that
// in order to make the raw object available on parsed events, we fake the response.
if (event.getLastResponse() == null) {
event.setLastResponse(
new StripeResponse(200, HttpHeaders.of(Collections.emptyMap()), payload));
}

return event;
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/stripe/net/WebhookTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stripe.net;

import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -325,4 +326,13 @@ public void testStripeClientConstructEventWithTolerance()

Mockito.verify(responseGetter).request(Mockito.any(), Mockito.any());
}

@Test
public void testConstructEventWithRawJson()
throws StripeException, NoSuchAlgorithmException, InvalidKeyException {

final Event event = Webhook.constructEvent(payload, generateSigHeader(), secret);

assertNotNull(event.getRawJsonObject());
}
}

0 comments on commit 4e27c66

Please sign in to comment.