Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Stripe.net/Infrastructure/Public/LiveApiRequestor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Stripe
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -225,6 +226,18 @@ private static StripeException BuildV2StripeException(StripeResponse response)
}
}

private static void MaybeEmitStripeNotice(HttpResponseHeaders headers)
{
if (headers != null && headers.Contains("Stripe-Notice"))
Comment thread
xavdid marked this conversation as resolved.
{
var notice = headers.GetValues("Stripe-Notice").FirstOrDefault();
if (!string.IsNullOrEmpty(notice))
{
Console.Error.WriteLine(notice);
}
}
}

// Note: BaseOptions options really means query params here
private StripeRequest MakeStripeRequest(
BaseAddress baseAddress,
Expand Down Expand Up @@ -274,6 +287,8 @@ private T ProcessResponse<T>(StripeResponse response, ApiMode apiMode)
throw BuildStripeException(response);
}

MaybeEmitStripeNotice(response.Headers);

T obj;
try
{
Expand Down Expand Up @@ -362,6 +377,8 @@ public override async Task<StripeResponse> RawRequestAsync(
throw BuildStripeException(response);
}

MaybeEmitStripeNotice(response.Headers);

return response;
}
}
Expand Down
132 changes: 132 additions & 0 deletions src/StripeTests/Infrastructure/Public/LiveApiRequestorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace StripeTests
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -711,6 +712,137 @@ public async Task StripeAccountHeaderSet()
Assert.Equal("acct_2345", lastRequest.StripeHeaders["Stripe-Account"]);
}

[Fact]
public async Task RequestAsync_WritesToStderr_WhenStripeNoticeHeaderPresent()
{
var headers = BuildHeaders("Stripe-Notice", "test notice message");
var response = new StripeResponse(HttpStatusCode.OK, headers, "{\"id\": \"ch_123\"}");
this.httpClient.Response = response;

var stderr = new StringWriter();
Console.SetError(stderr);
try
{
await this.apiRequestor.RequestAsync<Charge>(
BaseAddress.Api,
HttpMethod.Post,
"/v1/charges",
this.options,
this.requestOptions);

Assert.Contains("test notice message", stderr.ToString());
}
finally
{
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
}
}

[Fact]
public async Task RequestAsync_NoStderrWrite_WhenStripeNoticeHeaderAbsent()
{
var response = new StripeResponse(HttpStatusCode.OK, null, "{\"id\": \"ch_123\"}");
this.httpClient.Response = response;

var stderr = new StringWriter();
Console.SetError(stderr);
try
{
await this.apiRequestor.RequestAsync<Charge>(
BaseAddress.Api,
HttpMethod.Post,
"/v1/charges",
this.options,
this.requestOptions);

Assert.Empty(stderr.ToString());
}
finally
{
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
}
}

[Fact]
public async Task RequestAsync_NoStderrWrite_WhenStripeNoticeHeaderEmpty()
{
var headers = BuildHeaders("Stripe-Notice", string.Empty);
var response = new StripeResponse(HttpStatusCode.OK, headers, "{\"id\": \"ch_123\"}");
this.httpClient.Response = response;

var stderr = new StringWriter();
Console.SetError(stderr);
try
{
await this.apiRequestor.RequestAsync<Charge>(
BaseAddress.Api,
HttpMethod.Post,
"/v1/charges",
this.options,
this.requestOptions);

Assert.Empty(stderr.ToString());
}
finally
{
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
}
}

[Fact]
public async Task RawRequestAsync_WritesToStderr_WhenStripeNoticeHeaderPresent()
{
var headers = BuildHeaders("Stripe-Notice", "raw notice message");
var response = new StripeResponse(HttpStatusCode.OK, headers, "{\"id\": \"ch_123\"}");
this.httpClient.Response = response;

var stderr = new StringWriter();
Console.SetError(stderr);
try
{
await this.apiRequestor.RawRequestAsync(
HttpMethod.Post,
"/v1/charges",
"foo=bar");

Assert.Contains("raw notice message", stderr.ToString());
}
finally
{
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
}
}

[Fact]
public async Task RawRequestAsync_NoStderrWrite_WhenStripeNoticeHeaderAbsent()
{
var response = new StripeResponse(HttpStatusCode.OK, null, "{\"id\": \"ch_123\"}");
this.httpClient.Response = response;

var stderr = new StringWriter();
Console.SetError(stderr);
try
{
await this.apiRequestor.RawRequestAsync(
HttpMethod.Post,
"/v1/charges",
"foo=bar");

Assert.Empty(stderr.ToString());
}
finally
{
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
}
}

private static HttpResponseHeaders BuildHeaders(string name, string value)
{
var message = new HttpResponseMessage();
message.Headers.Add(name, value);
return message.Headers;
}

private class Foo : StripeEntity<Foo>
{
[JsonProperty("bar")]
Expand Down
Loading