-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include last reponse on Stripe API Resources
- Loading branch information
Showing
10 changed files
with
328 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,7 @@ target | |
# Gradle files | ||
.gradle/* | ||
build/* | ||
|
||
# compiler output | ||
bin/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.stripe.net; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class StripeHeaders { | ||
|
||
Map<String, List<String>> headers; | ||
|
||
public StripeHeaders(Map<String, List<String>> headers) { | ||
this.headers = headers; | ||
} | ||
|
||
/** | ||
* Returns the first header value for a given key | ||
* @param name The name of the header key | ||
* @return the first value for the given key | ||
*/ | ||
public String get(String name) { | ||
List<String> valuesList = values(name); | ||
String value = null; | ||
if (valuesList != null && valuesList.size() > 0) { | ||
value = valuesList.get(0); | ||
} | ||
return value; | ||
} | ||
|
||
public List<String> values(String name) { | ||
return headers == null ? null : headers.get(name); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/com/stripe/functional/StripeResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.stripe.functional; | ||
|
||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
|
||
import com.stripe.Stripe; | ||
import com.stripe.model.Customer; | ||
|
||
import com.stripe.BaseStripeFunctionalTest; | ||
import com.stripe.exception.APIConnectionException; | ||
import com.stripe.exception.APIException; | ||
import com.stripe.exception.AuthenticationException; | ||
import com.stripe.exception.CardException; | ||
import com.stripe.exception.InvalidRequestException; | ||
import com.stripe.net.StripeResponse; | ||
import com.stripe.net.RequestOptions; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class StripeResponseTest extends BaseStripeFunctionalTest { | ||
@Test | ||
public void testResponseIncluded() throws | ||
AuthenticationException, | ||
InvalidRequestException, | ||
APIException, | ||
APIConnectionException, | ||
CardException { | ||
String idempotencyKey = Long.toString(System.currentTimeMillis()); | ||
RequestOptions includeResponseRequestOptions = RequestOptions.builder() | ||
.setStripeVersion(Stripe.apiVersion) | ||
.setIdempotencyKey(idempotencyKey) | ||
.build(); | ||
Customer cus = Customer.create(defaultCustomerParams, includeResponseRequestOptions); | ||
cus = Customer.retrieve(cus.getId(), includeResponseRequestOptions); | ||
StripeResponse resp = cus.getLastResponse(); | ||
assertThat(resp, instanceOf(StripeResponse.class)); | ||
assertEquals(200, resp.code()); | ||
assertEquals(idempotencyKey, resp.idempotencyKey()); | ||
assertTrue(resp.requestId().startsWith("req_")); | ||
assertTrue(resp.body().length() > 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.stripe.net; | ||
|
||
import com.stripe.BaseStripeTest; | ||
import com.stripe.Stripe; | ||
import com.stripe.net.StripeHeaders; | ||
|
||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class StripeHeadersTest extends BaseStripeTest { | ||
|
||
private Map<String, List<String>> generateHeaderMap () { | ||
Map<String, List<String>> headerMap = new HashMap<String, List<String>>(); | ||
List<String> bearerHeader = new ArrayList<String>(); | ||
bearerHeader.add("Bearer"); | ||
bearerHeader.add("sk_live_12345"); | ||
List<String> requestIdHeader = new ArrayList<String>(); | ||
requestIdHeader.add("req_12345"); | ||
headerMap.put("Request-Id", requestIdHeader); | ||
headerMap.put("Authorization", bearerHeader); | ||
headerMap.put("Empty-Header", new ArrayList<String>()); | ||
return headerMap; | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
StripeHeaders headers = new StripeHeaders(generateHeaderMap()); | ||
assertEquals("req_12345", headers.get("Request-Id")); | ||
assertEquals("Bearer", headers.get("Authorization")); | ||
} | ||
|
||
@Test | ||
public void testValues() { | ||
StripeHeaders headers = new StripeHeaders(generateHeaderMap()); | ||
assertEquals(2, headers.values("Authorization").size()); | ||
assertEquals("Bearer", headers.values("Authorization").get(0)); | ||
assertEquals("sk_live_12345", headers.values("Authorization").get(1)); | ||
} | ||
|
||
@Test | ||
public void testGetEmpty() { | ||
StripeHeaders headers = new StripeHeaders(generateHeaderMap()); | ||
assertEquals(null, headers.get("Empty-Header")); | ||
} | ||
|
||
} |
Oops, something went wrong.