Skip to content

Commit

Permalink
Fixed user agent for v2 requests (#1908)
Browse files Browse the repository at this point in the history
  • Loading branch information
prathmesh-stripe authored Oct 25, 2024
1 parent fc2bfa8 commit 180acd8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/stripe/net/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ public StripeResponseStream requestStreamWithRetries(StripeRequest request)
*
* @return a string containing the value of the {@code User-Agent} header
*/
protected static String buildUserAgentString() {
String userAgent = String.format("Stripe/v1 JavaBindings/%s", Stripe.VERSION);
protected static String buildUserAgentString(StripeRequest request) {
String apiMode = request.apiMode() == ApiMode.V2 ? "v2" : "v1";

String userAgent = String.format("Stripe/%s JavaBindings/%s", apiMode, Stripe.VERSION);

if (Stripe.getAppInfo() != null) {
userAgent += " " + formatAppInfo(Stripe.getAppInfo());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/stripe/net/HttpURLConnectionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public StripeResponse request(StripeRequest request) throws ApiConnectionExcepti
static HttpHeaders getHeaders(StripeRequest request) {
Map<String, List<String>> userAgentHeadersMap = new HashMap<>();

userAgentHeadersMap.put("User-Agent", Arrays.asList(buildUserAgentString()));
userAgentHeadersMap.put("User-Agent", Arrays.asList(buildUserAgentString(request)));
userAgentHeadersMap.put(
"X-Stripe-Client-User-Agent", Arrays.asList(buildXStripeClientUserAgentString()));

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/stripe/net/StripeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class StripeRequest {
/** The special modifiers of the request. */
RequestOptions options;

/** The version of the API (ApiMode.V1 or ApiMode.V2) */
ApiMode apiMode;

/**
* Initializes a new instance of the {@link StripeRequest} class.
*
Expand All @@ -70,6 +73,7 @@ private StripeRequest(
this.method = method;
this.url = buildURL(method, url, params, apiMode);
this.headers = buildHeaders(method, this.options, this.content, apiMode);
this.apiMode = apiMode;
} catch (IOException e) {
throw new ApiConnectionException(
String.format(
Expand Down Expand Up @@ -106,6 +110,7 @@ private StripeRequest(
this.url = buildURL(method, url, params, apiMode);
this.content = buildContent(method, params, apiMode);
this.headers = buildHeaders(method, this.options, this.content, apiMode);
this.apiMode = apiMode;
} catch (IOException e) {
throw new ApiConnectionException(
String.format(
Expand Down Expand Up @@ -209,7 +214,8 @@ public StripeRequest withAdditionalHeader(String name, String value) {
this.content,
this.headers.withAdditionalHeader(name, value),
this.params,
this.options);
this.options,
this.apiMode);
}

private static URL buildURL(
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/stripe/net/HttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.stripe.BaseStripeTest;
import com.stripe.Stripe;
import com.stripe.exception.ApiConnectionException;
import com.stripe.exception.StripeException;
import java.net.ConnectException;
Expand Down Expand Up @@ -180,4 +181,34 @@ public void testRequestWithRetriesConflictInternalServerError() throws StripeExc
assertEquals(200, response.code());
assertEquals(1, response.numRetries());
}

@Test
public void testV1RequestSetsCorrectUserAgent() throws StripeException {
StripeRequest request =
StripeRequest.create(
ApiResource.RequestMethod.GET,
"http://example.com/get",
null,
RequestOptions.builder().setApiKey("sk_test_123").setMaxNetworkRetries(2).build(),
ApiMode.V1);

assertEquals(
HttpClient.buildUserAgentString(request),
String.format("Stripe/v1 JavaBindings/%s", Stripe.VERSION));
}

@Test
public void testV2RequestSetsCorrectUserAgent() throws StripeException {
StripeRequest request =
StripeRequest.create(
ApiResource.RequestMethod.GET,
"http://example.com/get",
null,
RequestOptions.builder().setApiKey("sk_test_123").setMaxNetworkRetries(2).build(),
ApiMode.V2);

assertEquals(
HttpClient.buildUserAgentString(request),
String.format("Stripe/v2 JavaBindings/%s", Stripe.VERSION));
}
}

0 comments on commit 180acd8

Please sign in to comment.