/
StripeMockFixture.cs
150 lines (131 loc) · 5.38 KB
/
StripeMockFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
namespace StripeTests
{
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using Stripe;
public class StripeMockFixture : IDisposable
{
/// <value>Minimum required version of stripe-mock</value>
/// <remarks>
/// If you bump this, don't forget to bump `STRIPE_MOCK_VERSION` in `appveyor.yml` as well.
/// </remarks>
private const string MockMinimumVersion = "0.63.0";
private readonly string port;
public StripeMockFixture()
{
if (StripeMockHandler.StartStripeMock())
{
this.port = StripeMockHandler.Port.ToString();
}
else
{
this.port = Environment.GetEnvironmentVariable("STRIPE_MOCK_PORT") ?? "12111";
}
this.EnsureStripeMockMinimumVersion();
}
public void Dispose()
{
StripeMockHandler.StopStripeMock();
}
/// <summary>
/// Creates and returns a new instance of <see cref="StripeClient"/> suitable for use with
/// stripe-mock.
/// </summary>
/// <param name="httpClient">
/// The <see cref="IHttpClient"/> client to use. If <c>null</c>, an HTTP client will be
/// created with default parameters.
/// </param>
public StripeClient BuildStripeClient(IHttpClient httpClient = null)
{
return new StripeClient(
"sk_test_123",
"ca_123",
httpClient: httpClient,
apiBase: $"http://localhost:{this.port}",
filesBase: $"http://localhost:{this.port}");
}
/// <summary>
/// Gets fixture data with expansions specified. Expansions are specified the same way as
/// they are in the normal API like <c>customer</c> or <c>data.customer</c>.
/// Use the special <c>*</c> character to specify that all fields should be
/// expanded.
/// </summary>
/// <param name="path">API path to use to get a fixture for stripe-mock</param>
/// <param name="expansions">Set of expansions that should be applied</param>
/// <returns>Fixture data encoded as JSON</returns>
public string GetFixture(string path, string[] expansions = null)
{
string url = $"http://localhost:{this.port}{path}";
if (expansions != null)
{
string query = string.Join("&", expansions.Select(x => $"expand[]={x}").ToArray());
url += $"?{query}";
}
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
{
client.DefaultRequestHeaders.Authorization
= new System.Net.Http.Headers.AuthenticationHeaderValue(
"Bearer",
"sk_test_123");
HttpResponseMessage response;
try
{
response = client.GetAsync(url).Result;
}
catch (Exception)
{
throw new StripeTestException(
$"Couldn't reach stripe-mock at `localhost:{this.port}`. "
+ "Is it running? Please see README for setup instructions.");
}
if (response.StatusCode != HttpStatusCode.OK)
{
throw new StripeTestException(
$"stripe-mock returned status code: {response.StatusCode}.");
}
return response.Content.ReadAsStringAsync().Result;
}
}
/// <summary>
/// Compares two version strings.
/// </summary>
/// <param name="a">A version string (e.g. "1.2.3").</param>
/// <param name="b">Another version string.</param>
/// <returns>-1 if a > b, 1 if a < b, 0 if a == b</returns>
private static int CompareVersions(string a, string b)
{
var version1 = new Version(a);
var version2 = new Version(b);
return version2.CompareTo(version1);
}
private void EnsureStripeMockMinimumVersion()
{
string url = $"http://localhost:{this.port}";
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
{
HttpResponseMessage response;
try
{
response = client.GetAsync(url).Result;
}
catch (Exception)
{
throw new StripeTestException(
$"Couldn't reach stripe-mock at `localhost:{this.port}`. "
+ "Is it running? Please see README for setup instructions.");
}
string version = response.Headers.GetValues("Stripe-Mock-Version").FirstOrDefault();
if (!version.Equals("master") &&
(CompareVersions(version, MockMinimumVersion) > 0))
{
throw new StripeTestException(
$"Your version of stripe-mock ({version}) is too old. The minimum "
+ $"version to run this test suite is {MockMinimumVersion}. Please see its "
+ "repository for upgrade instructions.");
}
}
}
}
}