-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstackServiceTest.cs
More file actions
368 lines (273 loc) · 15.2 KB
/
ContentstackServiceTest.cs
File metadata and controls
368 lines (273 loc) · 15.2 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using AutoFixture;
using AutoFixture.AutoMoq;
using Contentstack.Management.Core.Http;
using Contentstack.Management.Core.Queryable;
using Contentstack.Management.Core.Services;
using Contentstack.Management.Core.Unit.Tests.Mokes;
using Contentstack.Management.Core.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
namespace Contentstack.Management.Core.Unit.Tests.Core.Services
{
[TestClass]
public class ContentstackServiceTest
{
JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings());
private readonly IFixture _fixture = new Fixture()
.Customize(new AutoMoqCustomization());
[TestMethod]
public void Should_Not_Allow_Null_serializer()
{
Assert.ThrowsException<ArgumentNullException>(() => new ContentstackService(null));
}
[TestMethod]
public void Should_Throw_Object_Disposed_Exception_On_Dispose()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.Dispose();
Assert.ThrowsException<ObjectDisposedException>(() => contentstackService.CreateHttpRequest(new HttpClient(), new ContentstackClientOptions()));
Assert.ThrowsException<ObjectDisposedException>(() => contentstackService.AddPathResource("resourcePath", "val"));
Assert.ThrowsException<ObjectDisposedException>(() => contentstackService.AddQueryResource("resourcePath", "val"));
Assert.ThrowsException<ObjectDisposedException>(() => contentstackService.GetHeaderValue("header"));
contentstackService.Dispose();
}
[TestMethod]
public void Returns_Null_Content()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.ContentBody();
Assert.IsNotNull(contentstackService);
Assert.IsNull(contentstackService.Content);
Assert.IsNotNull(contentstackService.Serializer);
}
[TestMethod]
public void Return_Content_data()
{
var contentstackService = new ContentstackService(serializer);
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
{
JsonWriter writer = new JsonTextWriter(stringWriter);
writer.WriteStartObject();
writer.WritePropertyName("user");
writer.WriteValue("test_Mock_user");
writer.WriteEndObject();
string snippet = stringWriter.ToString();
contentstackService.ByteContent = System.Text.Encoding.UTF8.GetBytes(snippet);
}
contentstackService.WriteContentByte();
Assert.IsNotNull(contentstackService.Content);
}
[TestMethod]
public void Return_Null_On_Response()
{
var contentstackService = new ContentstackService(serializer);
var config = new ContentstackClientOptions();
ContentstackResponse httpResponse = MockResponse.CreateContentstackResponse("LoginResponse.txt");
Assert.IsNull(config.Authtoken);
contentstackService.OnResponse(httpResponse, config);
Assert.IsNull(config.Authtoken);
}
[TestMethod]
public void Return_True_On_Get_Method()
{
var contentstackService = new ContentstackService(serializer);
Assert.AreEqual(HttpMethod.Get.ToString(), contentstackService.HttpMethod);
Assert.IsTrue(contentstackService.UseQueryString);
}
[TestMethod]
public void Return_False_Other_Than_Get_Method()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.HttpMethod = HttpMethod.Post.ToString();
Assert.AreEqual(HttpMethod.Post.ToString(), contentstackService.HttpMethod);
Assert.IsFalse(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Patch.ToString();
Assert.AreEqual(HttpMethod.Patch.ToString(), contentstackService.HttpMethod);
Assert.IsFalse(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Delete.ToString();
Assert.AreEqual(HttpMethod.Delete.ToString(), contentstackService.HttpMethod);
Assert.IsFalse(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Head.ToString();
Assert.AreEqual(HttpMethod.Head.ToString(), contentstackService.HttpMethod);
Assert.IsFalse(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Options.ToString();
Assert.AreEqual(HttpMethod.Options.ToString(), contentstackService.HttpMethod);
Assert.IsFalse(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Put.ToString();
Assert.AreEqual(HttpMethod.Put.ToString(), contentstackService.HttpMethod);
Assert.IsFalse(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Trace.ToString();
Assert.AreEqual(HttpMethod.Trace.ToString(), contentstackService.HttpMethod);
Assert.IsFalse(contentstackService.UseQueryString);
}
[TestMethod]
public void Return_True_On_Setting_UseQueryString_To_True_For_All_Methods()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.UseQueryString = true;
contentstackService.HttpMethod = HttpMethod.Post.ToString();
Assert.AreEqual(HttpMethod.Post.ToString(), contentstackService.HttpMethod);
Assert.IsTrue(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Patch.ToString();
Assert.AreEqual(HttpMethod.Patch.ToString(), contentstackService.HttpMethod);
Assert.IsTrue(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Delete.ToString();
Assert.AreEqual(HttpMethod.Delete.ToString(), contentstackService.HttpMethod);
Assert.IsTrue(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Head.ToString();
Assert.AreEqual(HttpMethod.Head.ToString(), contentstackService.HttpMethod);
Assert.IsTrue(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Options.ToString();
Assert.AreEqual(HttpMethod.Options.ToString(), contentstackService.HttpMethod);
Assert.IsTrue(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Put.ToString();
Assert.AreEqual(HttpMethod.Put.ToString(), contentstackService.HttpMethod);
Assert.IsTrue(contentstackService.UseQueryString);
contentstackService.HttpMethod = HttpMethod.Trace.ToString();
Assert.AreEqual(HttpMethod.Trace.ToString(), contentstackService.HttpMethod);
Assert.IsTrue(contentstackService.UseQueryString);
}
[TestMethod]
public void Return_ResourcePath_On_Setting_ResourcePath()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.ResourcePath = "resourcePath";
Assert.AreEqual("resourcePath", contentstackService.ResourcePath);
}
[TestMethod]
public void Return_PathResources_On_Adding_Path_Resource()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.AddPathResource("content_type_uid", "contentTypeUid");
contentstackService.AddPathResource("entry_uid", "entryUid");
Assert.AreEqual(2, contentstackService.PathResources.Count);
Assert.AreEqual("contentTypeUid", contentstackService.PathResources["content_type_uid"]);
Assert.AreEqual("entryUid", contentstackService.PathResources["entry_uid"]);
}
[TestMethod]
public void Return_PathResources_On_Adding_Query_Resource()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.AddQueryResource("content_type_uid", "contentTypeUid");
contentstackService.AddQueryResource("entry_uid", "entryUid");
Assert.AreEqual(2, contentstackService.QueryResources.Count);
Assert.AreEqual("contentTypeUid", contentstackService.QueryResources["content_type_uid"]);
Assert.AreEqual("entryUid", contentstackService.QueryResources["entry_uid"]);
}
[TestMethod]
public void Return_True_HttpBody_On_PUT_POST_PATCH_Methods()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.HttpMethod = HttpMethod.Put.ToString();
Assert.IsTrue(contentstackService.HasRequestBody());
contentstackService.HttpMethod = HttpMethod.Post.ToString();
Assert.IsTrue(contentstackService.HasRequestBody());
contentstackService.HttpMethod = HttpMethod.Patch.ToString();
Assert.IsTrue(contentstackService.HasRequestBody());
contentstackService.HttpMethod = HttpMethod.Delete.ToString();
Assert.IsTrue(contentstackService.HasRequestBody());
}
[TestMethod]
public void Return_False_HttpBody_On_Other_Then_PUT_POST_PATCH_Methods()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.HttpMethod = HttpMethod.Get.ToString();
Assert.IsFalse(contentstackService.HasRequestBody());
contentstackService.HttpMethod = HttpMethod.Head.ToString();
Assert.IsFalse(contentstackService.HasRequestBody());
contentstackService.HttpMethod = HttpMethod.Options.ToString();
Assert.IsFalse(contentstackService.HasRequestBody());
contentstackService.HttpMethod = HttpMethod.Trace.ToString();
Assert.IsFalse(contentstackService.HasRequestBody());
}
[TestMethod]
public void Return_Empty_String_On_Non_Exist_HeaderKey()
{
var contentstackService = new ContentstackService(serializer);
Assert.AreEqual(string.Empty, contentstackService.GetHeaderValue("unknown"));
}
[TestMethod]
public void Return_Value_For_HeaderKey()
{
var contentstackService = new ContentstackService(serializer);
contentstackService.Headers[HeadersKey.ContentTypeHeader] = "application/json";
Assert.AreEqual("application/json", contentstackService.GetHeaderValue(HeadersKey.ContentTypeHeader));
}
[TestMethod]
public void CreateHttpRequest_Should_Set_Content_Type_When_ShouldSetContentType_Returns_True()
{
var contentstackService = new ContentstackService(serializer);
var config = new ContentstackClientOptions();
config.Authtoken = _fixture.Create<string>();
contentstackService.CreateHttpRequest(new HttpClient(), config);
Assert.IsTrue(contentstackService.Headers.ContainsKey(HeadersKey.ContentTypeHeader));
Assert.AreEqual("application/json", contentstackService.GetHeaderValue(HeadersKey.ContentTypeHeader));
}
[TestMethod]
public void Return_HttpRequest_On_Create_HttpRequest()
{
var contentstackService = new ContentstackService(serializer);
var config = new ContentstackClientOptions();
config.Authtoken = _fixture.Create<string>();
ContentstackHttpRequest httpClient = (ContentstackHttpRequest)contentstackService.CreateHttpRequest(new HttpClient(), config);
IEnumerable<string> headerValues = httpClient.Request.Headers.GetValues("authtoken");
Assert.IsNotNull(httpClient);
Assert.AreEqual(config.Authtoken, headerValues.FirstOrDefault());
Assert.IsFalse(httpClient.Request.Headers.Contains("authorization"));
}
[TestMethod]
public void Return_Management_Token_In_Header()
{
var token = _fixture.Create<string>();
var contentstackService = new ContentstackService(serializer, new Management.Core.Models.Stack(null, managementToken: token));
ContentstackHttpRequest httpClient = (ContentstackHttpRequest)contentstackService.CreateHttpRequest(new HttpClient(), new ContentstackClientOptions());
IEnumerable<string> headerValues = httpClient.Request.Headers.GetValues("authorization");
Assert.IsNotNull(httpClient);
Assert.IsFalse(httpClient.Request.Headers.Contains("authtoken"));
Assert.AreEqual(token, headerValues.FirstOrDefault());
}
[TestMethod]
public void Return_API_Key_In_Header()
{
var apiKey = _fixture.Create<string>();
var contentstackService = new ContentstackService(serializer, new Management.Core.Models.Stack(null, apiKey: apiKey));
ContentstackHttpRequest httpClient = (ContentstackHttpRequest)contentstackService.CreateHttpRequest(new HttpClient(), new ContentstackClientOptions());
IEnumerable<string> headerValues = httpClient.Request.Headers.GetValues("api_key");
Assert.IsNotNull(httpClient);
Assert.AreEqual(apiKey, contentstackService.Headers["api_key"]);
Assert.AreEqual(apiKey, headerValues.FirstOrDefault());
}
[TestMethod]
public void Return_Branch_ID_In_Header()
{
var token = _fixture.Create<string>();
var contentstackService = new ContentstackService(serializer, new Management.Core.Models.Stack(null, branchUid: token));
ContentstackHttpRequest httpClient = (ContentstackHttpRequest)contentstackService.CreateHttpRequest(new HttpClient(), new ContentstackClientOptions());
IEnumerable<string> headerValues = httpClient.Request.Headers.GetValues("branch");
Assert.IsNotNull(httpClient);
Assert.AreEqual(token, contentstackService.Headers["branch"]);
Assert.AreEqual(token, headerValues.FirstOrDefault());
}
[TestMethod]
public void Should_Add_query_Parameter()
{
var parameter = new ParameterCollection();
parameter.Add("limit", 10);
parameter.Add("include", new List<string>() { "1", "2", "3" });
var contentstackService = new ContentstackService(serializer, collection: parameter);
contentstackService.HttpMethod = HttpMethod.Post.ToString();
contentstackService.UseQueryString = true;
ContentstackHttpRequest httpClient = (ContentstackHttpRequest)contentstackService.CreateHttpRequest(new HttpClient(), new ContentstackClientOptions());
Assert.IsNotNull(httpClient);
Assert.AreEqual("?include[]=1&include[]=2&include[]=3&limit=10", httpClient.RequestUri.Query);
}
}
}