-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResponseMessageExtensionsTests.cs
More file actions
74 lines (60 loc) · 2.03 KB
/
Copy pathHttpResponseMessageExtensionsTests.cs
File metadata and controls
74 lines (60 loc) · 2.03 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
using System.Net;
using System.Net.Http.Json;
using Vulthil.xUnit;
namespace Vulthil.Extensions.Testing.Tests;
public sealed class HttpResponseMessageExtensionsTests : BaseUnitTestCase
{
[Fact]
public async Task DeserializesTheResponseBodyOnSuccess()
{
// Arrange
using var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = JsonContent.Create(new TestPayload("Ada")),
};
// Act
var payload = await response.GetResponseAsync<TestPayload>(CancellationToken);
// Assert
payload.Name.ShouldBe("Ada");
}
[Fact]
public async Task NullResponseThrowsArgumentNullException()
{
// Arrange
HttpResponseMessage? response = null;
// Act
var exception = await Should.ThrowAsync<ArgumentNullException>(
() => response.GetResponseAsync<TestPayload>(CancellationToken));
// Assert
exception.ParamName.ShouldBe("response");
}
[Fact]
public async Task NonSuccessStatusCodeThrows()
{
// Arrange
using var response = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = JsonContent.Create(new TestPayload("Ada")),
};
// Act
var exception = await Should.ThrowAsync<HttpRequestException>(
() => response.GetResponseAsync<TestPayload>(CancellationToken));
// Assert
exception.ShouldNotBeNull();
}
[Fact]
public async Task NullJsonBodyThrowsInvalidOperationException()
{
// Arrange
using var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = JsonContent.Create<TestPayload?>(null),
};
// Act
var exception = await Should.ThrowAsync<InvalidOperationException>(
() => response.GetResponseAsync<TestPayload>(CancellationToken));
// Assert
exception.Message.ShouldBe("Response content is empty or could not be deserialized.");
}
public sealed record TestPayload(string Name);
}