-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathAutoPagingTest.cs
184 lines (164 loc) · 7.57 KB
/
AutoPagingTest.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
namespace StripeTests
{
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using Moq.Protected;
using Newtonsoft.Json;
using Stripe;
using Xunit;
public class AutoPagingTest : BaseStripeTest
{
public AutoPagingTest(MockHttpClientFixture mockHttpClientFixture)
: base(mockHttpClientFixture)
{
}
[Fact]
public void ListAutoPaging()
{
// Set up stubbed requests
var response1 = new HttpResponseMessage(HttpStatusCode.OK);
response1.Content = new StringContent(GetResourceAsString("pageable_models.0.json"));
var response2 = new HttpResponseMessage(HttpStatusCode.OK);
response2.Content = new StringContent(GetResourceAsString("pageable_models.1.json"));
var response3 = new HttpResponseMessage(HttpStatusCode.OK);
response3.Content = new StringContent(GetResourceAsString("pageable_models.2.json"));
this.MockHttpClientFixture.MockHandler.Protected()
.SetupSequence<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels"),
ItExpr.IsAny<CancellationToken>())
.Returns(Task.FromResult(response1))
.Returns(Task.FromResult(response2))
.Returns(Task.FromResult(response3))
.Throws(new StripeTestException("Unexpected invocation!"));
// Call auto-paging method
var service = new PageableService(this.StripeClient);
service.ExpandFoo = true;
var options = new ListOptions
{
Limit = 2,
};
var models = service.ListAutoPaging(options).ToList();
// Check results
Assert.Equal(5, models.Count);
Assert.Equal("pm_123", models[0].Id);
Assert.Equal("pm_124", models[1].Id);
Assert.Equal("pm_125", models[2].Id);
Assert.Equal("pm_126", models[3].Id);
Assert.Equal("pm_127", models[4].Id);
// Check invocations
this.MockHttpClientFixture.MockHandler.Protected()
.Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == "?limit=2&expand[0]=data.foo"),
ItExpr.IsAny<CancellationToken>());
this.MockHttpClientFixture.MockHandler.Protected()
.Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == "?limit=2&starting_after=pm_124&expand[0]=data.foo"),
ItExpr.IsAny<CancellationToken>());
this.MockHttpClientFixture.MockHandler.Protected()
.Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == "?limit=2&starting_after=pm_126&expand[0]=data.foo"),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public void ListAutoPaging_NoParams()
{
// Set up stubbed requests
var response1 = new HttpResponseMessage(HttpStatusCode.OK);
response1.Content = new StringContent(GetResourceAsString("pageable_models.0.json"));
var response2 = new HttpResponseMessage(HttpStatusCode.OK);
response2.Content = new StringContent(GetResourceAsString("pageable_models.1.json"));
var response3 = new HttpResponseMessage(HttpStatusCode.OK);
response3.Content = new StringContent(GetResourceAsString("pageable_models.2.json"));
this.MockHttpClientFixture.MockHandler.Protected()
.SetupSequence<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels"),
ItExpr.IsAny<CancellationToken>())
.Returns(Task.FromResult(response1))
.Returns(Task.FromResult(response2))
.Returns(Task.FromResult(response3))
.Throws(new StripeTestException("Unexpected invocation!"));
// Call auto-paging method
var service = new PageableService(this.StripeClient);
var models = service.ListAutoPaging().ToList();
// Check results
Assert.Equal(5, models.Count);
Assert.Equal("pm_123", models[0].Id);
Assert.Equal("pm_124", models[1].Id);
Assert.Equal("pm_125", models[2].Id);
Assert.Equal("pm_126", models[3].Id);
Assert.Equal("pm_127", models[4].Id);
// Check invocations
this.MockHttpClientFixture.MockHandler.Protected()
.Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == string.Empty),
ItExpr.IsAny<CancellationToken>());
this.MockHttpClientFixture.MockHandler.Protected()
.Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == "?starting_after=pm_124"),
ItExpr.IsAny<CancellationToken>());
this.MockHttpClientFixture.MockHandler.Protected()
.Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == "?starting_after=pm_126"),
ItExpr.IsAny<CancellationToken>());
}
public class PageableModel : StripeEntity<PageableModel>, IHasId
{
[JsonProperty("id")]
public string Id { get; set; }
}
public class PageableService : Service<PageableModel>
{
public PageableService(IStripeClient client)
: base(client)
{
}
public bool ExpandFoo { get; set; }
public override string BasePath => "/v1/pageablemodels";
public IEnumerable<PageableModel> ListAutoPaging(ListOptions options = null)
{
return this.ListEntitiesAutoPaging(options, null);
}
}
}
}