-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResponseMessageExtensionsTests.cs
More file actions
191 lines (160 loc) · 5.81 KB
/
HttpResponseMessageExtensionsTests.cs
File metadata and controls
191 lines (160 loc) · 5.81 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
using System.Net;
using System.Text;
using System.Text.Json;
namespace Crews.Web.JsonApiClient.Tests;
public class HttpResponseMessageExtensionsTests
{
private readonly JsonSerializerOptions _options;
public HttpResponseMessageExtensionsTests()
{
_options = new JsonSerializerOptions();
}
private static HttpResponseMessage CreateResponse(string content)
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(content, Encoding.UTF8, "application/vnd.api+json")
};
}
[Fact(DisplayName = "ReadJsonApiDocumentAsync deserializes single resource document")]
public async Task ReadJsonApiDocumentAsyncDeserializesSingleResourceDocument()
{
const string json = """{"data": {"type": "articles", "id": "1"}}""";
using var response = CreateResponse(json);
JsonApiDocument? doc = await response.ReadJsonApiDocumentAsync(_options);
Assert.NotNull(doc);
Assert.False(doc.HasCollectionResource);
}
[Fact(DisplayName = "ReadJsonApiDocumentAsync deserializes collection resource document")]
public async Task ReadJsonApiDocumentAsyncDeserializesCollectionResourceDocument()
{
const string json = """{"data": [{"type": "articles", "id": "1"}, {"type": "articles", "id": "2"}]}""";
using var response = CreateResponse(json);
JsonApiDocument? doc = await response.ReadJsonApiDocumentAsync(_options);
Assert.NotNull(doc);
Assert.True(doc.HasCollectionResource);
}
[Fact(DisplayName = "ReadJsonApiDocumentAsync deserializes error document")]
public async Task ReadJsonApiDocumentAsyncDeserializesErrorDocument()
{
const string json = """
{
"errors": [
{
"status": "404",
"title": "Not Found",
"detail": "The requested resource was not found."
}
]
}
""";
using var response = CreateResponse(json);
JsonApiDocument? doc = await response.ReadJsonApiDocumentAsync(_options);
Assert.NotNull(doc);
Assert.True(doc.HasErrors);
JsonApiError error = doc.Errors!.First();
Assert.Equal("404", error.Status);
Assert.Equal("Not Found", error.Title);
}
[Fact(DisplayName = "ReadJsonApiDocumentAsync generic deserializes strongly-typed document")]
public async Task ReadJsonApiDocumentAsyncGenericDeserializesStronglyTypedDocument()
{
const string json = """
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "Test Article"
}
}
}
""";
using var response = CreateResponse(json);
JsonApiDocument<JsonApiResource>? doc = await response.ReadJsonApiDocumentAsync<JsonApiResource>(_options);
Assert.NotNull(doc);
Assert.NotNull(doc.Data);
Assert.Equal("articles", doc.Data.Type);
Assert.Equal("1", doc.Data.Id);
}
[Fact(DisplayName = "ReadJsonApiCollectionDocumentAsync deserializes collection document")]
public async Task ReadJsonApiCollectionDocumentAsyncDeserializesCollectionDocument()
{
const string json = """
{
"data": [
{"type": "articles", "id": "1"},
{"type": "articles", "id": "2"},
{"type": "articles", "id": "3"}
]
}
""";
using var response = CreateResponse(json);
JsonApiCollectionDocument<JsonApiResource>? doc = await response.ReadJsonApiCollectionDocumentAsync<JsonApiResource>(_options);
Assert.NotNull(doc);
Assert.NotNull(doc.Data);
JsonApiResource[] resources = doc.Data.ToArray();
Assert.Equal(3, resources.Length);
Assert.Equal("1", resources[0].Id);
Assert.Equal("2", resources[1].Id);
Assert.Equal("3", resources[2].Id);
}
[Fact(DisplayName = "ReadJsonApiDocumentAsync returns null for null JSON")]
public async Task ReadJsonApiDocumentAsyncReturnsNullForNullJson()
{
const string json = """null""";
using var response = CreateResponse(json);
JsonApiDocument? doc = await response.ReadJsonApiDocumentAsync(_options);
Assert.Null(doc);
}
[Fact(DisplayName = "ReadJsonApiDocumentAsync supports cancellation")]
public async Task ReadJsonApiDocumentAsyncSupportsCancellation()
{
const string json = """{"data": {"type": "articles", "id": "1"}}""";
using var response = CreateResponse(json);
using var cts = new CancellationTokenSource();
cts.Cancel();
await Assert.ThrowsAsync<TaskCanceledException>(
() => response.ReadJsonApiDocumentAsync(_options, cts.Token));
}
[Fact(DisplayName = "ReadJsonApiDocumentAsync generic supports cancellation")]
public async Task ReadJsonApiDocumentAsyncGenericSupportsCancellation()
{
const string json = """{"data": {"type": "articles", "id": "1"}}""";
using var response = CreateResponse(json);
using var cts = new CancellationTokenSource();
cts.Cancel();
await Assert.ThrowsAsync<TaskCanceledException>(
() => response.ReadJsonApiDocumentAsync<JsonApiResource>(_options, cts.Token));
}
[Fact(DisplayName = "ReadJsonApiCollectionDocumentAsync supports cancellation")]
public async Task ReadJsonApiCollectionDocumentAsyncSupportsCancellation()
{
const string json = """{"data": [{"type": "articles", "id": "1"}]}""";
using var response = CreateResponse(json);
using var cts = new CancellationTokenSource();
cts.Cancel();
await Assert.ThrowsAsync<TaskCanceledException>(
() => response.ReadJsonApiCollectionDocumentAsync<JsonApiResource>(_options, cts.Token));
}
[Fact(DisplayName = "ReadJsonApiDocumentAsync deserializes document with all properties")]
public async Task ReadJsonApiDocumentAsyncDeserializesDocumentWithAllProperties()
{
const string json = """
{
"jsonapi": {"version": "1.1"},
"data": {"type": "articles", "id": "1"},
"links": {"self": "https://example.com/articles/1"},
"meta": {"copyright": "2024"}
}
""";
using var response = CreateResponse(json);
JsonApiDocument? doc = await response.ReadJsonApiDocumentAsync(_options);
Assert.NotNull(doc);
Assert.NotNull(doc.JsonApi);
Assert.Equal("1.1", doc.JsonApi.Version);
Assert.NotNull(doc.Links);
Assert.NotNull(doc.Meta);
Assert.Equal("2024", doc.Meta["copyright"]!.GetValue<string>());
}
}