-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathBbsClientTests.cs
More file actions
593 lines (484 loc) · 22.2 KB
/
Copy pathBbsClientTests.cs
File metadata and controls
593 lines (484 loc) · 22.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using Moq.Protected;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.Services;
using Xunit;
namespace OctoshiftCLI.Tests.Octoshift.Services;
public sealed class BbsClientTests : IDisposable
{
private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>();
private readonly HttpResponseMessage _httpResponse;
private readonly RetryPolicy _retryPolicy;
private readonly object _rawRequestBody;
private const string EXPECTED_JSON_REQUEST_BODY = "{\"id\":\"ID\"}";
private const string EXPECTED_RESPONSE_CONTENT = "RESPONSE_CONTENT";
private const string USERNAME = "USER";
private const string PASSWORD = "abc123";
private const string URL = "http://example.com/resource";
public BbsClientTests()
{
_rawRequestBody = new { id = "ID" };
_httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(EXPECTED_RESPONSE_CONTENT)
};
_retryPolicy = new RetryPolicy(_mockOctoLogger.Object)
{
_httpRetryInterval = 1,
_retryInterval = 1
};
}
[Fact]
public void It_Adds_The_Authorization_Header()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForGet().Object);
var expectedAuthToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{USERNAME}:{PASSWORD}"));
// Act
_ = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Assert
httpClient.DefaultRequestHeaders.Authorization.Should().NotBeNull();
httpClient.DefaultRequestHeaders.Authorization.Parameter.Should().Be(expectedAuthToken);
httpClient.DefaultRequestHeaders.Authorization.Scheme.Should().Be("Basic");
}
[Fact]
public void It_Doesnt_Add_Authorization_Header_When_No_Credentials_Passed()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForGet().Object);
// Act
_ = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy);
// Assert
httpClient.DefaultRequestHeaders.Authorization.Should().BeNull();
}
[Fact]
public async Task GetAsync_Logs_The_Url()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForGet().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
await bbsClient.GetAsync(URL);
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"HTTP GET: {URL}"), Times.Once);
}
[Fact]
public async Task GetAsync_Returns_String_Response()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForGet().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
var actualContent = await bbsClient.GetAsync(URL);
// Assert
actualContent.Should().Be(EXPECTED_RESPONSE_CONTENT);
}
[Fact]
public async Task GetAsync_Logs_The_Response_Status_Code_And_Content()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForGet().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
await bbsClient.GetAsync(URL);
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"RESPONSE ({_httpResponse.StatusCode}): {EXPECTED_RESPONSE_CONTENT}"), Times.Once);
}
[Theory]
[InlineData(HttpStatusCode.InternalServerError)]
[InlineData(HttpStatusCode.ServiceUnavailable)]
public async Task GetAsync_Retries_On_Non_Success(HttpStatusCode httpStatusCode)
{
// Arrange
using var firstHttpResponse = new HttpResponseMessage(httpStatusCode)
{
Content = new StringContent("FIRST_RESPONSE")
};
using var secondHttpResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("SECOND_RESPONSE")
};
var handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.SetupSequence<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(firstHttpResponse)
.ReturnsAsync(secondHttpResponse);
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
var returnedContent = await bbsClient.GetAsync(URL);
// Assert
returnedContent.Should().Be("SECOND_RESPONSE");
}
[Fact]
public async Task GetAsync_Bubbles_UnAuthorized_Error()
{
// Arrange
using var firstHttpResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("FIRST_RESPONSE")
};
using var secondHttpResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("SECOND_RESPONSE")
};
var handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.SetupSequence<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(firstHttpResponse)
.ReturnsAsync(secondHttpResponse);
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
// Assert
await bbsClient
.Invoking(async x => await x.GetAsync(URL))
.Should()
.ThrowExactlyAsync<OctoshiftCliException>()
.WithMessage("Unauthorized. Please check your token and try again");
}
[Fact]
public async Task PostAsync_Logs_The_Url()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForPost().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
await bbsClient.PostAsync(URL, _rawRequestBody);
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"HTTP POST: {URL}"), Times.Once);
}
[Fact]
public async Task PostAsync_Logs_The_Request_Body()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForPost().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
await bbsClient.PostAsync(URL, _rawRequestBody);
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"HTTP BODY: {EXPECTED_JSON_REQUEST_BODY}"), Times.Once);
}
[Fact]
public async Task PostAsync_Returns_String_Response()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForPost().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
var actualContent = await bbsClient.PostAsync(URL, _rawRequestBody);
// Assert
actualContent.Should().Be(EXPECTED_RESPONSE_CONTENT);
}
[Fact]
public async Task PostAsync_Logs_The_Response_Status_Code_And_Content()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForPost().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
await bbsClient.PostAsync(URL, _rawRequestBody);
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"RESPONSE ({_httpResponse.StatusCode}): {EXPECTED_RESPONSE_CONTENT}"), Times.Once);
}
[Fact]
public async Task DeleteAsync_Logs_The_Url()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForDelete().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
await bbsClient.DeleteAsync(URL);
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"HTTP DELETE: {URL}"), Times.Once);
}
[Fact]
public async Task DeleteAsync_Returns_String_Response()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForDelete().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
var actualContent = await bbsClient.DeleteAsync(URL);
// Assert
actualContent.Should().Be(EXPECTED_RESPONSE_CONTENT);
}
[Fact]
public async Task DeleteAsync_Logs_The_Response_Status_Code_And_Content()
{
// Arrange
using var httpClient = new HttpClient(MockHttpHandlerForDelete().Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy, USERNAME, PASSWORD);
// Act
await bbsClient.DeleteAsync(URL);
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"RESPONSE ({_httpResponse.StatusCode}): {EXPECTED_RESPONSE_CONTENT}"), Times.Once);
}
[Fact]
public async Task GetAllAsync_Should_Get_All_Pages()
{
// Arrange
const string url = "http://localhost:7990/rest/api/1.0/projects";
var firstValue = new { key = "PR1", id = 1 };
var secondValue = new { key = "PR2", id = 2 };
var thirdValue = new { key = "PR3", id = 3 };
var fourthValue = new { key = "PR4", id = 4 };
var fifthValue = new { key = "PR5", id = 5 };
var firstResponseContent = new
{
isLastPage = false,
nextPageStart = 2,
values = new[]
{
firstValue,
secondValue
}
}.ToJson();
using var firstResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(firstResponseContent)
};
var secondResponseContent = new
{
isLastPage = false,
nextPageStart = 4,
values = new[]
{
thirdValue,
fourthValue
}
}.ToJson();
using var secondResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(secondResponseContent)
};
var thirdResponseContent = new
{
isLastPage = true,
values = new[]
{
fifthValue
}
}.ToJson();
using var thirdResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(thirdResponseContent)
};
var handlerMock = new Mock<HttpMessageHandler>();
// first request
MockHttpHandler(
req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == $"{url}?start=0&limit=100",
firstResponse,
handlerMock);
// second request
MockHttpHandler(
req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == $"{url}?start=2&limit=100",
secondResponse,
handlerMock);
// third request
MockHttpHandler(
req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == $"{url}?start=4&limit=100",
thirdResponse,
handlerMock);
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy);
// Act
var results = await bbsClient.GetAllAsync(url).ToListAsync();
// Assert
results.Should().HaveCount(5);
results[0].ToJson().Should().Be(firstValue.ToJson());
results[1].ToJson().Should().Be(secondValue.ToJson());
results[2].ToJson().Should().Be(thirdValue.ToJson());
results[3].ToJson().Should().Be(fourthValue.ToJson());
results[4].ToJson().Should().Be(fifthValue.ToJson());
}
[Fact]
public async Task GetAllAsync_Logs_The_Url_Per_Each_Page_Request()
{
// Arrange
const string url = "http://example.com/resource";
var firstResponseContent = new { isLastPage = false, nextPageStart = 2, values = Array.Empty<object>() }.ToJson();
using var firstResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(firstResponseContent) };
var secondResponseContent = new { isLastPage = true, values = Array.Empty<object>() }.ToJson();
using var secondResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(secondResponseContent) };
var handlerMock = new Mock<HttpMessageHandler>();
// first request
const string firstRequestUrl = $"{url}?start=0&limit=100";
MockHttpHandler(req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == firstRequestUrl, firstResponse, handlerMock);
// second request
const string secondRequestUrl = $"{url}?start=2&limit=100";
MockHttpHandler(req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == secondRequestUrl, secondResponse, handlerMock);
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy);
// Act
await bbsClient.GetAllAsync(url).ToListAsync();
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"HTTP GET: {firstRequestUrl}"));
_mockOctoLogger.Verify(m => m.LogVerbose($"HTTP GET: {secondRequestUrl}"));
}
[Fact]
public async Task GetAllAsync_Logs_The_Response_Per_Each_Page_Request()
{
// Arrange
const string url = "http://example.com/resource";
var firstResponseContent = new { isLastPage = false, nextPageStart = 2, values = new[] { new { key = "value 1" } } }.ToJson();
using var firstResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(firstResponseContent) };
var secondResponseContent = new { isLastPage = true, values = new[] { new { key = "value 2" } } }.ToJson();
using var secondResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(secondResponseContent) };
var handlerMock = new Mock<HttpMessageHandler>();
// first request
const string firstRequestUrl = $"{url}?start=0&limit=100";
MockHttpHandler(req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == firstRequestUrl, firstResponse, handlerMock);
// second request
const string secondRequestUrl = $"{url}?start=2&limit=100";
MockHttpHandler(req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == secondRequestUrl, secondResponse, handlerMock);
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy);
// Act
await bbsClient.GetAllAsync(url).ToListAsync();
// Assert
_mockOctoLogger.Verify(m => m.LogVerbose($"RESPONSE ({HttpStatusCode.OK}): {firstResponseContent}"));
_mockOctoLogger.Verify(m => m.LogVerbose($"RESPONSE ({HttpStatusCode.OK}): {secondResponseContent}"));
}
[Fact]
public async Task GetAllAsync_Throws_HttpRequestException_On_Non_Success_Response()
{
// Arrange
const string url = "http://example.com/resource";
var firstResponseContent = new { isLastPage = false, nextPageStart = 2, values = Array.Empty<object>() }.ToJson();
var handlerMock = new Mock<HttpMessageHandler>();
// first request
const string firstRequestUrl = $"{url}?start=0&limit=100";
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == firstRequestUrl),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(() => new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(firstResponseContent) });
// second request - return a fresh response each time so retries don't reuse a disposed instance
const string secondRequestUrl = $"{url}?start=2&limit=100";
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get && req.RequestUri!.ToString() == secondRequestUrl),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(() => new HttpResponseMessage(HttpStatusCode.InternalServerError));
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy);
// Act, Assert
await bbsClient
.Invoking(async x => await x.GetAllAsync(url).ToListAsync())
.Should()
.ThrowExactlyAsync<HttpRequestException>();
}
[Fact]
public async Task SendAsync_Includes_Response_Body_In_Exception_Data_On_5xx()
{
// Arrange
const string url = "http://example.com/resource";
const string responseBody = "{\"errors\":[{\"message\":\"Could not start migration job\"}]}";
using var response = new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)
{
Content = new StringContent(responseBody)
};
var handlerMock = MockHttpHandler(req => req.Method == HttpMethod.Post, response);
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy);
// Act
var thrown = await bbsClient
.Invoking(async x => await x.PostAsync(url, _rawRequestBody))
.Should()
.ThrowExactlyAsync<HttpRequestException>();
// Assert
thrown.Which.StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable);
thrown.Which.Data["ResponseBody"].Should().Be(responseBody);
}
[Fact]
public async Task GetAllAsync_Overrides_Pagination_Query_Params_In_Request_Url()
{
// Arrange
const string actualUrl = "http://example.com/resource?start=1&limit=1";
const string expectedUrl = "http://example.com/resource?start=0&limit=100";
var responseContent = new { values = Array.Empty<object>() }.ToJson();
using var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(responseContent) };
var handlerMock = MockHttpHandler(req => req.Method == HttpMethod.Get, response);
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy);
// Act
await bbsClient.GetAllAsync(actualUrl).ToListAsync();
// Assert
handlerMock.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(msg => msg.RequestUri.ToString() == expectedUrl),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public async Task GetAllAsync_Retries_On_Non_Success_Response()
{
const string url = "http://example.com/resource";
var firstResponseContent = new { isLastPage = false, nextPageStart = 2, values = new[] { new { key = "value 1" } } }.ToJson();
using var firstResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(firstResponseContent) };
var expectedValue = new { key = "value 2" };
var secondResponseContent = new { isLastPage = true, values = new[] { expectedValue } }.ToJson();
using var secondResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(secondResponseContent) };
var handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.SetupSequence<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(firstResponse)
.ReturnsAsync(secondResponse);
using var httpClient = new HttpClient(handlerMock.Object);
var bbsClient = new BbsClient(_mockOctoLogger.Object, httpClient, null, _retryPolicy);
// Act
var results = await bbsClient.GetAllAsync(url).ToListAsync();
// Assert
results.Should().HaveCount(1);
results[0].ToJson().Should().Be(expectedValue.ToJson());
}
private Mock<HttpMessageHandler> MockHttpHandlerForGet() =>
MockHttpHandler(req => req.Method == HttpMethod.Get);
private Mock<HttpMessageHandler> MockHttpHandlerForPost() =>
MockHttpHandler(req =>
req.Content.ReadAsStringAsync().Result == EXPECTED_JSON_REQUEST_BODY
&& req.Method == HttpMethod.Post);
private Mock<HttpMessageHandler> MockHttpHandlerForDelete() =>
MockHttpHandler(req => req.Method == HttpMethod.Delete);
private Mock<HttpMessageHandler> MockHttpHandler(
Func<HttpRequestMessage, bool> requestMatcher,
HttpResponseMessage httpResponse = null,
Mock<HttpMessageHandler> handlerMock = null)
{
handlerMock ??= new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(x => requestMatcher(x)),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(httpResponse ?? _httpResponse);
return handlerMock;
}
public void Dispose() => _httpResponse?.Dispose();
}