Skip to content

Commit e84e43c

Browse files
authored
Adds public BaseUrl to RawRequestOptions (#3170)
renames internal BaseUrl in RequestOptions to InternalBaseUrl adds public BaseUrl to RawRequestOptions - it delegates to InternalBaseUrl for safety
1 parent 8ae0043 commit e84e43c

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

src/Stripe.net/Infrastructure/Public/ApiRequestorAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override Task<T> RequestAsync<T>(
5454
if (baseAddress != BaseAddress.Api)
5555
{
5656
requestOptions ??= new RequestOptions();
57-
requestOptions.BaseUrl = this.GetBaseUrl(baseAddress);
57+
requestOptions.InternalBaseUrl = this.GetBaseUrl(baseAddress);
5858
}
5959

6060
return this.client.RequestAsync<T>(method, path, options, requestOptions, cancellationToken);

src/Stripe.net/Infrastructure/Public/LiveApiRequestor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private StripeRequest MakeStripeRequest(
231231
}
232232

233233
var uri = StripeRequest.BuildUri(
234-
requestOptions?.BaseUrl ?? this.GetBaseUrl(baseAddress),
234+
requestOptions?.InternalBaseUrl ?? this.GetBaseUrl(baseAddress),
235235
method,
236236
path,
237237
options,

src/Stripe.net/Services/_common/RawRequestOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ namespace Stripe
44

55
public class RawRequestOptions : RequestOptions
66
{
7+
/// <summary>Gets or sets the base URL for the raw request.</summary>
8+
/// <remarks>
9+
/// Use this to send API calls to e.g. files.stripe.com or
10+
/// a proxy address.
11+
/// </remarks>
12+
public string BaseUrl { get => this.InternalBaseUrl; set => this.InternalBaseUrl = value; }
13+
714
/// <summary>Gets or sets additional headers for the request.</summary>
815
public Dictionary<string, string> AdditionalHeaders { get; set; } = new Dictionary<string, string>();
916

src/Stripe.net/Services/_common/RequestOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public class RequestOptions
2727
/// <summary>Gets or sets the value or Stripe-Context request header.</summary>
2828
public string StripeContext { get; set; }
2929

30-
/// <summary>Gets or sets the base URL for the request.</summary>
30+
/// <summary>Gets the base URL for the request.</summary>
3131
/// <remarks>
3232
/// This is an internal property. It is set by services or individual request methods when
3333
/// they need to send a request to a non-standard destination, e.g. <c>files.stripe.com</c>
3434
/// for file creation requests or <c>connect.stripe.com</c> for OAuth requests.
3535
/// </remarks>
36-
internal string BaseUrl { get; set; }
36+
internal string InternalBaseUrl { get; set; }
3737

3838
/// <summary>Gets or sets the API version for the request.</summary>
3939
/// <remarks>

src/StripeTests/Infrastructure/Public/StripeClientTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,42 @@ public async Task RawRequestAsync_Json()
164164
Assert.Equal("mes_123", obj.Id);
165165
}
166166

167+
[Fact]
168+
public async Task RawRequest_BaseUrl()
169+
{
170+
// Stub a request as stripe-mock does not support v2
171+
this.MockHttpClientFixture.StubRequest(
172+
HttpMethod.Post,
173+
"/v2/billing/meter_event_session",
174+
System.Net.HttpStatusCode.OK,
175+
"{\"id\": \"mes_123\",\"object\":\"v2.billing.meter_event_session\"}");
176+
177+
var expectedBaseUrl = "https://test.stripetest.com";
178+
var rawResponse = await this.stripeClient.RawRequestAsync(
179+
HttpMethod.Post,
180+
"/v2/billing/meter_event_session",
181+
"{}",
182+
new RawRequestOptions
183+
{
184+
BaseUrl = expectedBaseUrl,
185+
AdditionalHeaders =
186+
{
187+
{ "foo", "bar" },
188+
},
189+
});
190+
191+
this.MockHttpClientFixture.MockHandler.Protected()
192+
.Verify(
193+
"SendAsync",
194+
Times.Once(),
195+
ItExpr.Is<HttpRequestMessage>(m =>
196+
new Uri(expectedBaseUrl).Host == (string)m.Properties["OriginalHost"]),
197+
ItExpr.IsAny<CancellationToken>());
198+
199+
var obj = this.stripeClient.Deserialize<Stripe.V2.Billing.MeterEventSession>(rawResponse.Content);
200+
Assert.Equal("mes_123", obj.Id);
201+
}
202+
167203
[Fact]
168204
public async Task RawRequestAsyncIncludesCorrectUsage()
169205
{

0 commit comments

Comments
 (0)