-
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.
Merge pull request #421 from sedouard/success-response-headers
Expose response objects
- Loading branch information
Showing
10 changed files
with
350 additions
and
42 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
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
package com.stripe.functional; | ||
|
||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
|
||
import com.stripe.Stripe; | ||
import com.stripe.model.Customer; | ||
import com.stripe.model.CustomerCollection; | ||
|
||
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; | ||
|
||
import java.util.HashMap; | ||
|
||
public class StripeResponseTest extends BaseStripeFunctionalTest { | ||
@Test | ||
public void testResponseIncluded() throws | ||
AuthenticationException, | ||
InvalidRequestException, | ||
APIException, | ||
APIConnectionException, | ||
CardException { | ||
String idempotencyKey = Long.toString(System.currentTimeMillis()); | ||
RequestOptions requestOptions = RequestOptions.builder() | ||
.setStripeVersion(Stripe.apiVersion) | ||
.setIdempotencyKey(idempotencyKey) | ||
.build(); | ||
Customer cus = Customer.create(defaultCustomerParams, requestOptions); | ||
cus = Customer.retrieve(cus.getId(), requestOptions); | ||
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); | ||
} | ||
@Test | ||
public void testResponseIncludedList() throws | ||
AuthenticationException, | ||
InvalidRequestException, | ||
APIException, | ||
APIConnectionException, | ||
CardException { | ||
CustomerCollection cusCollection = Customer.list(new HashMap<String, Object>()); | ||
StripeResponse resp = cusCollection.getLastResponse(); | ||
assertThat(resp, instanceOf(StripeResponse.class)); | ||
assertEquals(200, resp.code()); | ||
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,50 @@ | ||
package com.stripe.net; | ||
|
||
import com.stripe.BaseStripeTest; | ||
import com.stripe.net.StripeHeaders; | ||
|
||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
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> multiValueHeader = new ArrayList<String>(); | ||
multiValueHeader.add("FirstValue"); | ||
multiValueHeader.add("SecondValue"); | ||
List<String> requestIdHeader = new ArrayList<String>(); | ||
requestIdHeader.add("req_12345"); | ||
headerMap.put("Request-Id", requestIdHeader); | ||
headerMap.put("Multi-Val", multiValueHeader); | ||
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("FirstValue", headers.get("Multi-Val")); | ||
} | ||
|
||
@Test | ||
public void testValues() { | ||
StripeHeaders headers = new StripeHeaders(generateHeaderMap()); | ||
assertEquals(2, headers.values("Multi-Val").size()); | ||
assertEquals("FirstValue", headers.values("Multi-Val").get(0)); | ||
assertEquals("SecondValue", headers.values("Multi-Val").get(1)); | ||
} | ||
|
||
@Test | ||
public void testGetEmpty() { | ||
StripeHeaders headers = new StripeHeaders(generateHeaderMap()); | ||
assertEquals(null, headers.get("Empty-Header")); | ||
} | ||
} |
Oops, something went wrong.