-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFluentJsonContentExtensionsAotTests.cs
More file actions
165 lines (127 loc) · 6.36 KB
/
Copy pathFluentJsonContentExtensionsAotTests.cs
File metadata and controls
165 lines (127 loc) · 6.36 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
#if NET7_0_OR_GREATER
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
namespace FluentHttpClient.Tests;
public class FluentJsonContentExtensionsAotTests
{
private static HttpRequestBuilder CreateBuilder()
{
var client = new HttpClient
{
BaseAddress = new Uri("https://example.com/")
};
return client.UsingBase();
}
public class JsonTypeInfoOverloads
{
[Fact]
public async Task WithJsonContent_SetsUtf8JsonContentWithDefaultMediaType_WhenUsingJsonTypeInfo()
{
var builder = CreateBuilder();
var model = new SampleModel { Id = 42, Name = "Grant" };
builder.WithJsonContent(model, SampleModelJsonContext.Default.SampleModel);
builder.Content.ShouldNotBeNull();
var content = builder.Content as StringContent;
content.ShouldNotBeNull();
var payload = await content!.ReadAsStringAsync();
payload.ShouldBe(JsonSerializer.Serialize(model, SampleModelJsonContext.Default.SampleModel));
content.Headers.ContentType.ShouldNotBeNull();
content.Headers.ContentType!.MediaType.ShouldBe(FluentJsonSerializer.DefaultContentType);
content.Headers.ContentType!.CharSet.ShouldBe(Encoding.UTF8.WebName);
}
[Fact]
public async Task WithJsonContent_SetsUtf8JsonContentWithSpecifiedMediaType_WhenUsingJsonTypeInfo()
{
var builder = CreateBuilder();
var model = new SampleModel { Id = 7, Name = "Lex" };
const string contentType = "application/vnd.sample+json";
builder.WithJsonContent(model, SampleModelJsonContext.Default.SampleModel, contentType);
var content = builder.Content as StringContent;
content.ShouldNotBeNull();
var payload = await content!.ReadAsStringAsync();
payload.ShouldBe(JsonSerializer.Serialize(model, SampleModelJsonContext.Default.SampleModel));
content.Headers.ContentType.ShouldNotBeNull();
content.Headers.ContentType!.MediaType.ShouldBe(contentType);
content.Headers.ContentType!.CharSet.ShouldBe(Encoding.UTF8.WebName);
}
[Fact]
public async Task WithJsonContent_SetsUtf8JsonContentWithSpecifiedMediaTypeHeader_WhenUsingJsonTypeInfo()
{
var builder = CreateBuilder();
var model = new SampleModel { Id = 13, Name = "Malcolm" };
var header = new MediaTypeHeaderValue("application/json")
{
CharSet = "utf-8"
};
builder.WithJsonContent(model, SampleModelJsonContext.Default.SampleModel, header);
var content = builder.Content as StringContent;
content.ShouldNotBeNull();
var payload = await content!.ReadAsStringAsync();
payload.ShouldBe(JsonSerializer.Serialize(model, SampleModelJsonContext.Default.SampleModel));
content.Headers.ContentType.ShouldNotBeNull();
content.Headers.ContentType!.MediaType.ShouldBe(header.MediaType);
content.Headers.ContentType!.CharSet.ShouldBe(header.CharSet);
}
}
public class JsonSerializerContextOverloads
{
[Fact]
public async Task WithJsonContent_SetsUtf8JsonContentWithDefaultMediaType_WhenUsingJsonSerializerContext()
{
var builder = CreateBuilder();
var model = new SampleModel { Id = 21, Name = "Sattler" };
builder.WithJsonContent(model, SampleModelJsonContext.Default);
var content = builder.Content as StringContent;
content.ShouldNotBeNull();
var payload = await content!.ReadAsStringAsync();
payload.ShouldBe(JsonSerializer.Serialize(model, SampleModelJsonContext.Default.SampleModel));
content.Headers.ContentType.ShouldNotBeNull();
content.Headers.ContentType!.MediaType.ShouldBe(FluentJsonSerializer.DefaultContentType);
content.Headers.ContentType!.CharSet.ShouldBe(Encoding.UTF8.WebName);
}
[Fact]
public async Task WithJsonContent_SetsUtf8JsonContentWithSpecifiedMediaType_WhenUsingJsonSerializerContext()
{
var builder = CreateBuilder();
var model = new SampleModel { Id = 5, Name = "Tim" };
const string contentType = "application/problem+json";
builder.WithJsonContent(model, SampleModelJsonContext.Default, contentType);
var content = builder.Content as StringContent;
content.ShouldNotBeNull();
var payload = await content!.ReadAsStringAsync();
payload.ShouldBe(JsonSerializer.Serialize(model, SampleModelJsonContext.Default.SampleModel));
content.Headers.ContentType.ShouldNotBeNull();
content.Headers.ContentType!.MediaType.ShouldBe(contentType);
content.Headers.ContentType!.CharSet.ShouldBe(Encoding.UTF8.WebName);
}
[Fact]
public async Task WithJsonContent_SetsUtf8JsonContentWithSpecifiedMediaTypeHeader_WhenUsingJsonSerializerContext()
{
var builder = CreateBuilder();
var model = new SampleModel { Id = 8, Name = "Ellie" };
var header = new MediaTypeHeaderValue("application/json")
{
CharSet = "utf-8"
};
builder.WithJsonContent(model, SampleModelJsonContext.Default, header);
var content = builder.Content as StringContent;
content.ShouldNotBeNull();
var payload = await content!.ReadAsStringAsync();
payload.ShouldBe(JsonSerializer.Serialize(model, SampleModelJsonContext.Default.SampleModel));
content.Headers.ContentType.ShouldNotBeNull();
content.Headers.ContentType!.MediaType.ShouldBe(header.MediaType);
content.Headers.ContentType!.CharSet.ShouldBe(header.CharSet);
}
[Fact]
public void WithJsonContent_ThrowsInvalidOperation_WhenContextMissingTypeInfoForT()
{
var builder = CreateBuilder();
var model = new SampleModel { Id = 99, Name = "Dodgson" };
var exception = Should.Throw<InvalidOperationException>(
() => builder.WithJsonContent(model, OtherModelJsonContext.Default));
exception.Message.ShouldContain(nameof(SampleModel));
}
}
}
#endif