-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathServiceTest.cs
154 lines (122 loc) · 5.07 KB
/
ServiceTest.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
namespace StripeTests
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Stripe;
using Xunit;
public class ServiceTest : BaseStripeTest
{
[Fact]
public void Get_ExpandProperties()
{
var client = new TestClient();
var service = new TestService(client);
service.ExpandSimple = true;
service.ExpandMultiWordProperty = true;
service.Get("foo");
Assert.Equal(2, client.LastOptions.Expand.Count);
Assert.Contains("simple", client.LastOptions.Expand);
Assert.Contains("multi_word_property", client.LastOptions.Expand);
}
[Fact]
public void Get_ThrowsIfIdIsNull()
{
var client = new TestClient();
var service = new TestService(client);
Assert.Throws<ArgumentException>(() => service.Get(null));
}
[Fact]
public void Get_ThrowsIfIdIsEmpty()
{
var client = new TestClient();
var service = new TestService(client);
Assert.Throws<ArgumentException>(() => service.Get(string.Empty));
}
[Fact]
public void Get_ThrowsIfIdIsWhitespace()
{
var client = new TestClient();
var service = new TestService(client);
Assert.Throws<ArgumentException>(() => service.Get(" "));
}
[Fact]
public void List_ExpandProperties()
{
var client = new TestClient();
var service = new TestService(client);
service.ExpandSimple = true;
service.ExpandMultiWordProperty = true;
var options = new ListOptions();
service.List(options);
Assert.Equal(2, client.LastOptions.Expand.Count);
Assert.Contains("data.simple", client.LastOptions.Expand);
Assert.Contains("data.multi_word_property", client.LastOptions.Expand);
// Do a second request to check that expand properties are not duplicated
service.List(options);
Assert.Equal(2, client.LastOptions.Expand.Count);
Assert.Contains("data.simple", client.LastOptions.Expand);
Assert.Contains("data.multi_word_property", client.LastOptions.Expand);
}
private class TestClient : IStripeClient
{
public string ApiBase { get; }
public string ApiKey { get; }
public string ClientId { get; }
public string ConnectBase { get; }
public string FilesBase { get; }
public BaseOptions LastOptions { get; protected set; }
public Task<T> RequestAsync<T>(
HttpMethod method,
string path,
BaseOptions options,
RequestOptions requestOptions,
CancellationToken cancellationToken = default(CancellationToken))
where T : IStripeEntity
{
this.LastOptions = options;
return Task.FromResult(default(T));
}
}
private class TestEntity : StripeEntity, IHasId
{
[JsonProperty("id")]
public string Id { get; set; }
}
private class TestService : Service<TestEntity>,
IListable<TestEntity, ListOptions>,
IRetrievable<TestEntity, BaseOptions>
{
public TestService(IStripeClient client)
: base(client)
{
}
public override string BasePath => "/v1/test_entities";
public bool ExpandSimple { get; set; }
public bool ExpandMultiWordProperty { get; set; }
public virtual TestEntity Get(string id, BaseOptions options = null, RequestOptions requestOptions = null)
{
return this.GetEntity(id, options, requestOptions);
}
public virtual Task<TestEntity> GetAsync(string id, BaseOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.GetEntityAsync(id, options, requestOptions, cancellationToken);
}
public virtual StripeList<TestEntity> List(ListOptions options = null, RequestOptions requestOptions = null)
{
return this.ListEntities(options, requestOptions);
}
public virtual Task<StripeList<TestEntity>> ListAsync(ListOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.ListEntitiesAsync(options, requestOptions, cancellationToken);
}
public virtual IEnumerable<TestEntity> ListAutoPaging(ListOptions options = null, RequestOptions requestOptions = null)
{
return this.ListEntitiesAutoPaging(options, requestOptions);
}
}
}
}