-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstackHttpRequestTest.cs
More file actions
314 lines (280 loc) · 12.9 KB
/
Copy pathContentstackHttpRequestTest.cs
File metadata and controls
314 lines (280 loc) · 12.9 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
using Moq;
using System;
using System.Net;
using System.Text;
using AutoFixture;
using System.Net.Http;
using AutoFixture.AutoMoq;
using System.Collections.Generic;
using Contentstack.Management.Core.Http;
using Contentstack.Management.Core.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using Moq.Protected;
using System.Threading;
using Contentstack.Management.Core.Unit.Tests.Mokes;
using System.IO;
using Contentstack.Management.Core.Exceptions;
namespace Contentstack.Management.Core.Unit.Tests.Http
{
[TestClass]
public class ContentstackHttpRequestTest
{
private readonly IFixture _fixture = new Fixture()
.Customize(new AutoMoqCustomization());
private ContentstackHttpRequest _httpRequest = null;
[TestInitialize]
public void Initialize()
{
_httpRequest = new ContentstackHttpRequest(new System.Net.Http.HttpClient(), Utils.Utilities.GetJsonSerializer());
}
[TestMethod]
public void Returns_Object_On_Initilize_Contentstack_Http_Request()
{
Assert.IsNotNull(_httpRequest);
Assert.IsNotNull(_httpRequest.HttpClient);
Assert.IsNotNull(_httpRequest.Request);
Assert.IsNotNull(_httpRequest.Method);
Assert.AreEqual(HttpMethod.Get, _httpRequest.Method);
}
[TestMethod]
public void Should_Allow_To_Set_Http_Method()
{
_httpRequest.Method = HttpMethod.Post;
Assert.AreEqual(HttpMethod.Post, _httpRequest.Method);
}
[TestMethod]
public void Should_Allow_To_Set_Request_Uri()
{
_httpRequest.RequestUri = new Uri("https://localhost");
Assert.AreEqual("localhost", _httpRequest.RequestUri.Host);
}
[TestMethod]
public void Should_Throw_Object_Disposed_Exception_On_Object_Dispose()
{
_httpRequest.Dispose();
var testContent = _fixture.Create<string>();
byte[] bytes = Encoding.ASCII.GetBytes(testContent);
HttpContent content = new ByteArrayContent(bytes);
Assert.ThrowsException<ObjectDisposedException>(() => _httpRequest.GetResponse());
Assert.ThrowsExceptionAsync<ObjectDisposedException>(() => _httpRequest.GetResponseAsync());
Assert.ThrowsException<ObjectDisposedException>(() => _httpRequest.GetRequestContent());
Assert.ThrowsException<ObjectDisposedException>(() => _httpRequest.WriteToRequestBody(content, new Dictionary<string, string>()));
_httpRequest.Dispose();
}
[TestMethod]
public void Should_Allow_To_Set_Headers()
{
var acceptHeader = _fixture.Create<string>();
var userAgentHeader = _fixture.Create<string>();
var date = DateTime.Now.ToUniversalTime();
var rangeHeader = _fixture.Create<string>();
_httpRequest.SetRequestHeaders(new Dictionary<string, string>
{
{"Accept", acceptHeader},
{"User-Agent", userAgentHeader},
{"Date", date.ToString("r")},
{"If-Modified-Since", date.ToString("r")},
{"Expires", date.ToString("r")},
});
Assert.AreEqual(acceptHeader, _httpRequest.Request.Headers.Accept.ToString());
Assert.AreEqual(userAgentHeader, _httpRequest.Request.Headers.UserAgent.ToString());
Assert.AreEqual(DateTime.Parse(date.ToString("r")), _httpRequest.Request.Headers.Date);
Assert.AreEqual(DateTime.Parse(date.ToString("r")), _httpRequest.Request.Headers.IfModifiedSince);
}
[TestMethod]
public async System.Threading.Tasks.Task Should_Allow_Write_Content()
{
var testContent = _fixture.Create<string>();
byte[] bytes = Encoding.ASCII.GetBytes(testContent);
HttpContent content = new ByteArrayContent(bytes);
_httpRequest.WriteToRequestBody(content, new Dictionary<string, string>() { { HeadersKey.ContentTypeHeader, "application/json" } });
Assert.AreEqual(testContent, await _httpRequest.GetRequestContent().ReadAsStringAsync());
}
[TestMethod]
public void Should_Throw_Exception_On_Null_Uri()
{
//Assert.ThrowsException<InvalidOperationException>(() => _httpRequest.GetResponse());
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _httpRequest.GetResponseAsync());
}
[TestMethod]
public async Task Return_OK_Response_On_Request_SuccessAsync()
{
var handlerMock = new Mock<HttpMessageHandler>();
var response = MockResponse.CreateFromResource("LoginResponse.txt");
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(response);
var httpClient = new HttpClient(handlerMock.Object);
var HttpRequest = new ContentstackHttpRequest(httpClient, Utils.Utilities.GetJsonSerializer());
HttpRequest.RequestUri = new Uri("https://localhost.com");
var httpResponse = HttpRequest.GetResponse();
Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
Assert.AreEqual(await response.Content.ReadAsStringAsync(), httpResponse.OpenResponse());
Assert.IsNotNull(httpResponse);
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>());
}
[TestMethod]
public void Return_UnprocessableEntity_Response_On_Request_FailuerAsync()
{
var handlerMock = new Mock<HttpMessageHandler>();
var response = MockResponse.CreateFromResource("422Response.txt");
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(response);
var httpClient = new HttpClient(handlerMock.Object);
var HttpRequest = new ContentstackHttpRequest(httpClient, Utils.Utilities.GetJsonSerializer());
HttpRequest.RequestUri = new Uri("https://localhost.com");
try
{
var httpResponse = HttpRequest.GetResponse();
} catch (Exception e)
{
ContentstackErrorException errorException = e as ContentstackErrorException;
Assert.AreEqual(response.StatusCode, errorException.StatusCode);
Assert.AreEqual("Looks like your email or password is invalid. You have 4 login attempt(s) left.", errorException.Message);
Assert.AreEqual("Looks like your email or password is invalid. You have 4 login attempt(s) left.", errorException.ErrorMessage);
Assert.AreEqual(104, errorException.ErrorCode);
}
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>());
}
[TestMethod]
public void Return_UnprocessableEntity_Response_On_Request_Fail_400()
{
var handlerMock = new Mock<HttpMessageHandler>();
var response = MockResponse.CreateFromResource("400Response.txt");
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(response);
var httpClient = new HttpClient(handlerMock.Object);
var HttpRequest = new ContentstackHttpRequest(httpClient, Utils.Utilities.GetJsonSerializer());
HttpRequest.RequestUri = new Uri("https://localhost.com");
try
{
var httpResponse = HttpRequest.GetResponse();
}
catch (Exception e)
{
ContentstackErrorException errorException = e as ContentstackErrorException;
Assert.AreEqual(response.StatusCode, errorException.StatusCode);
Assert.AreEqual("Please set a valid 'Content-Type' header", errorException.Message);
Assert.AreEqual("Please set a valid 'Content-Type' header", errorException.ErrorMessage);
}
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>());
}
[TestMethod]
public void Return_UnprocessableEntity_Response_On_Request_Fail_304()
{
var handlerMock = new Mock<HttpMessageHandler>();
var response = MockResponse.CreateFromResource("304Response.txt");
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(response);
var httpClient = new HttpClient(handlerMock.Object);
var HttpRequest = new ContentstackHttpRequest(httpClient, Utils.Utilities.GetJsonSerializer());
HttpRequest.RequestUri = new Uri("https://localhost.com");
try
{
var httpResponse = HttpRequest.GetResponse();
}
catch (Exception e)
{
ContentstackErrorException errorException = e as ContentstackErrorException;
Assert.AreEqual(response.StatusCode, errorException.StatusCode);
}
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>());
}
[TestMethod]
public void Return_Exception_Response_On_Request_Exception()
{
var handlerMock = new Mock<HttpMessageHandler>();
var response = "Something went Wrong";
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.Throws(new HttpRequestException(response));
var httpClient = new HttpClient(handlerMock.Object);
var HttpRequest = new ContentstackHttpRequest(httpClient, Utils.Utilities.GetJsonSerializer());
HttpRequest.RequestUri = new Uri("https://localhost.com");
try
{
var httpResponse = HttpRequest.GetResponse();
}
catch (Exception e)
{
HttpRequestException errorException = e as HttpRequestException;
Assert.AreEqual(response, errorException.Message);
}
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>());
}
[TestMethod]
public void Return_Inner_Exception_Response_On_Request_Exception()
{
var handlerMock = new Mock<HttpMessageHandler>();
var response = "Something went Wrong";
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.Throws(new HttpRequestException("Outer Exception" ,new IOException(response)));
var httpClient = new HttpClient(handlerMock.Object);
var HttpRequest = new ContentstackHttpRequest(httpClient, Utils.Utilities.GetJsonSerializer());
HttpRequest.RequestUri = new Uri("https://localhost.com");
try
{
var httpResponse = HttpRequest.GetResponse();
}
catch (Exception e)
{
IOException errorException = e as IOException;
Assert.AreEqual(response, errorException.Message);
}
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>());
}
}
}