This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
/
ClientTest.cs
249 lines (216 loc) · 9.64 KB
/
ClientTest.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Consul.Test
{
public class ClientTest : IDisposable
{
AsyncReaderWriterLock.Releaser m_lock;
public ClientTest()
{
m_lock = AsyncHelpers.RunSync(() => SelectiveParallel.NoParallel());
}
public void Dispose()
{
m_lock.Dispose();
}
[Fact]
public void Client_DefaultConfig_env()
{
const string addr = "1.2.3.4:5678";
const string token = "abcd1234";
const string auth = "username:password";
Environment.SetEnvironmentVariable("CONSUL_HTTP_ADDR", addr);
Environment.SetEnvironmentVariable("CONSUL_HTTP_TOKEN", token);
Environment.SetEnvironmentVariable("CONSUL_HTTP_AUTH", auth);
Environment.SetEnvironmentVariable("CONSUL_HTTP_SSL", "1");
Environment.SetEnvironmentVariable("CONSUL_HTTP_SSL_VERIFY", "0");
var client = new ConsulClient();
var config = client.Config;
Assert.Equal(addr, string.Format("{0}:{1}", config.Address.Host, config.Address.Port));
Assert.Equal(token, config.Token);
#pragma warning disable CS0618 // Type or member is obsolete
Assert.NotNull(config.HttpAuth);
Assert.Equal("username", config.HttpAuth.UserName);
Assert.Equal("password", config.HttpAuth.Password);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Equal("https", config.Address.Scheme);
Environment.SetEnvironmentVariable("CONSUL_HTTP_ADDR", string.Empty);
Environment.SetEnvironmentVariable("CONSUL_HTTP_TOKEN", string.Empty);
Environment.SetEnvironmentVariable("CONSUL_HTTP_AUTH", string.Empty);
Environment.SetEnvironmentVariable("CONSUL_HTTP_SSL", string.Empty);
Environment.SetEnvironmentVariable("CONSUL_HTTP_SSL_VERIFY", string.Empty);
#if !CORECLR
Assert.True((client.HttpHandler as WebRequestHandler).ServerCertificateValidationCallback(null, null, null,
SslPolicyErrors.RemoteCertificateChainErrors));
ServicePointManager.ServerCertificateValidationCallback = null;
#else
Assert.True((client.HttpHandler as HttpClientHandler).ServerCertificateCustomValidationCallback(null, null, null,
SslPolicyErrors.RemoteCertificateChainErrors));
#endif
Assert.NotNull(client);
}
[Fact]
public async Task Client_SetQueryOptions()
{
var client = new ConsulClient();
var opts = new QueryOptions()
{
Datacenter = "foo",
Consistency = ConsistencyMode.Consistent,
WaitIndex = 1000,
WaitTime = new TimeSpan(0, 0, 100),
Token = "12345"
};
var request = client.Get<KVPair>("/v1/kv/foo", opts);
await Assert.ThrowsAsync<ConsulRequestException>(async () => await request.Execute(CancellationToken.None));
Assert.Equal("foo", request.Params["dc"]);
Assert.True(request.Params.ContainsKey("consistent"));
Assert.Equal("1000", request.Params["index"]);
Assert.Equal("1m40s", request.Params["wait"]);
}
[Fact]
public async Task Client_SetClientOptions()
{
var client = new ConsulClient((c) =>
{
c.Datacenter = "foo";
c.WaitTime = new TimeSpan(0, 0, 100);
c.Token = "12345";
});
var request = client.Get<KVPair>("/v1/kv/foo");
await Assert.ThrowsAsync<ConsulRequestException>(async () => await request.Execute(CancellationToken.None));
Assert.Equal("foo", request.Params["dc"]);
Assert.Equal("1m40s", request.Params["wait"]);
}
[Fact]
public async Task Client_SetWriteOptions()
{
var client = new ConsulClient();
var opts = new WriteOptions()
{
Datacenter = "foo",
Token = "12345"
};
var request = client.Put("/v1/kv/foo", new KVPair("kv/foo"), opts);
await Assert.ThrowsAsync<ConsulRequestException>(async () => await request.Execute(CancellationToken.None));
Assert.Equal("foo", request.Params["dc"]);
}
[Fact]
public async Task Client_CustomHttpClient()
{
using (var hc = new HttpClient())
{
hc.Timeout = TimeSpan.FromDays(10);
hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
#pragma warning disable CS0618 // Type or member is obsolete
using (var client = new ConsulClient(new ConsulClientConfiguration(), hc))
#pragma warning restore CS0618 // Type or member is obsolete
{
await client.KV.Put(new KVPair("customhttpclient") { Value = System.Text.Encoding.UTF8.GetBytes("hello world") });
Assert.Equal(TimeSpan.FromDays(10), client.HttpClient.Timeout);
Assert.True(client.HttpClient.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")));
}
Assert.Equal("hello world", await (await hc.GetAsync("http://localhost:8500/v1/kv/customhttpclient?raw")).Content.ReadAsStringAsync());
}
}
[Fact]
public async Task Client_DisposeBehavior()
{
var client = new ConsulClient();
var opts = new WriteOptions()
{
Datacenter = "foo",
Token = "12345"
};
client.Dispose();
var request = client.Put("/v1/kv/foo", new KVPair("kv/foo"), opts);
await Assert.ThrowsAsync<ObjectDisposedException>(() => request.Execute(CancellationToken.None));
}
[Fact]
public async Task Client_ReuseAndUpdateConfig()
{
var config = new ConsulClientConfiguration();
#pragma warning disable CS0618 // Type or member is obsolete
using (var client = new ConsulClient(config))
#pragma warning restore CS0618 // Type or member is obsolete
{
#pragma warning disable CS0618 // Type or member is obsolete
config.DisableServerCertificateValidation = true;
#pragma warning restore CS0618 // Type or member is obsolete
await client.KV.Put(new KVPair("kv/reuseconfig") { Flags = 1000 });
Assert.Equal<ulong>(1000, (await client.KV.Get("kv/reuseconfig")).Response.Flags);
#if !CORECLR
Assert.True(client.HttpHandler.ServerCertificateValidationCallback(null, null, null,
SslPolicyErrors.RemoteCertificateChainErrors));
#endif
}
#pragma warning disable CS0618 // Type or member is obsolete
using (var client = new ConsulClient(config))
#pragma warning restore CS0618 // Type or member is obsolete
{
#pragma warning disable CS0618 // Type or member is obsolete
config.DisableServerCertificateValidation = false;
#pragma warning restore CS0618 // Type or member is obsolete
await client.KV.Put(new KVPair("kv/reuseconfig") { Flags = 2000 });
Assert.Equal<ulong>(2000, (await client.KV.Get("kv/reuseconfig")).Response.Flags);
#if !CORECLR
Assert.Null(client.HttpHandler.ServerCertificateValidationCallback);
#endif
}
#pragma warning disable CS0618 // Type or member is obsolete
using (var client = new ConsulClient(config))
#pragma warning restore CS0618 // Type or member is obsolete
{
await client.KV.Delete("kv/reuseconfig");
}
}
[Fact]
public void Client_Constructors()
{
Action<ConsulClientConfiguration> cfgAction2 = (cfg) => { cfg.Token = "yep"; };
Action<ConsulClientConfiguration> cfgAction = (cfg) => { cfg.Datacenter = "foo"; cfgAction2(cfg); };
using (var c = new ConsulClient())
{
Assert.NotNull(c.Config);
Assert.NotNull(c.HttpHandler);
Assert.NotNull(c.HttpClient);
}
using (var c = new ConsulClient(cfgAction))
{
Assert.NotNull(c.Config);
Assert.NotNull(c.HttpHandler);
Assert.NotNull(c.HttpClient);
Assert.Equal("foo", c.Config.Datacenter);
}
using (var c = new ConsulClient(cfgAction,
(client) => { client.Timeout = TimeSpan.FromSeconds(30); }))
{
Assert.NotNull(c.Config);
Assert.NotNull(c.HttpHandler);
Assert.NotNull(c.HttpClient);
Assert.Equal("foo", c.Config.Datacenter);
Assert.Equal(TimeSpan.FromSeconds(30), c.HttpClient.Timeout);
}
#if !CORECLR
using (var c = new ConsulClient(cfgAction,
(client) => { client.Timeout = TimeSpan.FromSeconds(30); },
(handler) => { handler.Proxy = new WebProxy("127.0.0.1", 8080); }))
{
Assert.NotNull(c.Config);
Assert.NotNull(c.HttpHandler);
Assert.NotNull(c.HttpClient);
Assert.Equal("foo", c.Config.Datacenter);
Assert.Equal(TimeSpan.FromSeconds(30), c.HttpClient.Timeout);
Assert.NotNull(c.HttpHandler.Proxy);
}
#endif
}
}
}