forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpContentExtensionsTest.cs
More file actions
381 lines (316 loc) · 18 KB
/
Copy pathHttpContentExtensionsTest.cs
File metadata and controls
381 lines (316 loc) · 18 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
namespace System.Net.Http
{
public class HttpContentExtensionsTest
{
private static readonly IEnumerable<MediaTypeFormatter> _emptyFormatterList = Enumerable.Empty<MediaTypeFormatter>();
private readonly Mock<MediaTypeFormatter> _formatterMock = new Mock<MediaTypeFormatter> { CallBase = true };
private readonly MediaTypeHeaderValue _mediaType = new MediaTypeHeaderValue("foo/bar");
private readonly MediaTypeFormatter[] _formatters;
public HttpContentExtensionsTest()
{
_formatterMock.Object.SupportedMediaTypes.Add(_mediaType);
_formatters = new[] { _formatterMock.Object };
}
[Fact]
public void ReadAsAsync_WhenContentParameterIsNull_Throws()
{
Assert.ThrowsArgumentNull(() => HttpContentExtensions.ReadAsAsync(null, typeof(string), _emptyFormatterList), "content");
}
[Fact]
public void ReadAsAsync_WhenTypeParameterIsNull_Throws()
{
Assert.ThrowsArgumentNull(() => HttpContentExtensions.ReadAsAsync(new StringContent(""), null, _emptyFormatterList), "type");
}
[Fact]
public void ReadAsAsync_WhenFormattersParameterIsNull_Throws()
{
Assert.ThrowsArgumentNull(() => HttpContentExtensions.ReadAsAsync(new StringContent(""), typeof(string), null), "formatters");
}
[Fact]
public void ReadAsAsyncOfT_WhenContentParameterIsNull_Throws()
{
Assert.ThrowsArgumentNull(() => HttpContentExtensions.ReadAsAsync<string>(null, _emptyFormatterList), "content");
}
[Fact]
public void ReadAsAsyncOfT_WhenFormattersParameterIsNull_Throws()
{
Assert.ThrowsArgumentNull(() => HttpContentExtensions.ReadAsAsync<string>(new StringContent(""), null), "formatters");
}
[Fact]
public async Task ReadAsAsyncOfT_WhenContentIsObjectContent_GoesThroughSerializationCycleToConvertTypes()
{
var content = new ObjectContent<int[]>(new int[] { 10, 20, 30, 40 }, new JsonMediaTypeFormatter());
byte[] result = await content.ReadAsAsync<byte[]>();
Assert.Equal(new byte[] { 10, 20, 30, 40 }, result);
}
[Fact]
public void ReadAsAsyncOfT_WhenNoMatchingFormatterFound_Throws()
{
var content = new StringContent("{}");
content.Headers.ContentType = _mediaType;
content.Headers.ContentType.CharSet = "utf-16";
var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
Assert.Throws<UnsupportedMediaTypeException>(() => content.ReadAsAsync<List<string>>(formatters),
"No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'foo/bar'.");
}
[Fact]
public void ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_Throws()
{
var content = new StringContent("{}");
content.Headers.ContentType = null;
var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
Assert.Throws<UnsupportedMediaTypeException>(() => content.ReadAsAsync<List<string>>(formatters),
"No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'application/octet-stream'.");
}
[Fact]
public void ReadAsAsyncOfT_WhenTypeIsValueTypeAndNoMediaType_Throws()
{
var content = new StringContent("123456");
content.Headers.ContentType = null;
var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
Assert.Throws<UnsupportedMediaTypeException>(() => content.ReadAsAsync<int>(formatters),
"No MediaTypeFormatter is available to read an object of type 'Int32' from content with media type 'application/octet-stream'.");
}
[Fact]
public async Task ReadAsAsyncOfT_ReadsFromContent_ThenInvokesFormattersReadFromStreamMethod()
{
Stream contentStream = null;
string value = "42";
var contentMock = new Mock<TestableHttpContent> { CallBase = true };
contentMock.Setup(c => c.SerializeToStreamAsyncPublic(It.IsAny<Stream>(), It.IsAny<TransportContext>()))
.Returns(TaskHelpers.Completed)
.Callback((Stream s, TransportContext _) => contentStream = s)
.Verifiable();
HttpContent content = contentMock.Object;
content.Headers.ContentType = _mediaType;
_formatterMock
.Setup(f => f.ReadFromStreamAsync(typeof(string), It.IsAny<Stream>(), It.IsAny<HttpContent>(), It.IsAny<IFormatterLogger>()))
.Returns(Task.FromResult<object>(value));
_formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
var resultValue = await content.ReadAsAsync<string>(_formatters);
Assert.Same(value, resultValue);
contentMock.Verify();
_formatterMock.Verify(f => f.ReadFromStreamAsync(typeof(string), contentStream, content, null), Times.Once());
}
[Fact]
public async Task ReadAsAsyncOfT_InvokesFormatterEvenIfContentLengthIsZero()
{
var content = new StringContent("");
_formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
_formatterMock.Object.SupportedMediaTypes.Add(content.Headers.ContentType);
_formatterMock
.Setup(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), It.IsAny<HttpContent>(), It.IsAny<IFormatterLogger>()))
.Returns(Task.FromResult<object>(result: null));
await content.ReadAsAsync<string>(_formatters);
_formatterMock.Verify(f => f.ReadFromStreamAsync(typeof(string), It.IsAny<Stream>(), content, It.IsAny<IFormatterLogger>()), Times.Once());
}
[Fact]
public async Task ReadAsAsync_WhenContentIsObjectContentAndValueIsCompatibleType_ReadsValueFromObjectContent()
{
_formatterMock.Setup(f => f.CanWriteType(typeof(TestClass))).Returns(true);
var value = new TestClass();
var content = new ObjectContent<TestClass>(value, _formatterMock.Object);
Assert.Same(value, await content.ReadAsAsync<object>(_formatters));
Assert.Same(value, await content.ReadAsAsync<TestClass>(_formatters));
Assert.Same(value, await content.ReadAsAsync(typeof(object), _formatters));
Assert.Same(value, await content.ReadAsAsync(typeof(TestClass), _formatters));
_formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), content, It.IsAny<IFormatterLogger>()), Times.Never());
}
[Fact]
public async Task ReadAsAsync_WhenContentIsObjectContentAndValueIsNull_IfTypeIsNullable_SerializesAndDeserializesValue()
{
_formatterMock.Setup(f => f.CanWriteType(typeof(object))).Returns(true);
_formatterMock.Setup(f => f.CanReadType(It.IsAny<Type>())).Returns(true);
var content = new ObjectContent<object>(null, _formatterMock.Object);
SetupUpRoundTripSerialization(type => null);
Assert.Null(await content.ReadAsAsync<object>(_formatters));
Assert.Null(await content.ReadAsAsync<TestClass>(_formatters));
Assert.Null(await content.ReadAsAsync<Nullable<int>>(_formatters));
Assert.Null(await content.ReadAsAsync(typeof(object), _formatters));
Assert.Null(await content.ReadAsAsync(typeof(TestClass), _formatters));
Assert.Null(await content.ReadAsAsync(typeof(Nullable<int>), _formatters));
_formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), content, It.IsAny<IFormatterLogger>()), Times.Exactly(6));
}
[Fact]
public async Task ReadAsAsync_WhenContentIsObjectContentAndValueIsNull_IfTypeIsNotNullable_SerializesAndDeserializesValue()
{
_formatterMock.Setup(f => f.CanWriteType(typeof(object))).Returns(true);
_formatterMock.Setup(f => f.CanReadType(typeof(Int32))).Returns(true);
var content = new ObjectContent<object>(null, _formatterMock.Object, _mediaType);
SetupUpRoundTripSerialization();
Assert.IsType<Int32>(await content.ReadAsAsync<Int32>(_formatters));
Assert.IsType<Int32>(await content.ReadAsAsync(typeof(Int32), _formatters));
_formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), content, It.IsAny<IFormatterLogger>()), Times.Exactly(2));
}
[Fact]
public async Task ReadAsAsync_WhenContentIsObjectContentAndValueIsNotCompatibleType_SerializesAndDeserializesValue()
{
_formatterMock.Setup(f => f.CanWriteType(typeof(TestClass))).Returns(true);
_formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
var value = new TestClass();
var content = new ObjectContent<TestClass>(value, _formatterMock.Object, _mediaType);
SetupUpRoundTripSerialization(type => new TestClass());
await Assert.ThrowsAsync<InvalidCastException>(() => content.ReadAsAsync<string>(_formatters));
Assert.IsNotType<string>(await content.ReadAsAsync(typeof(string), _formatters));
_formatterMock.Verify(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), content, It.IsAny<IFormatterLogger>()), Times.Exactly(2));
}
[Fact]
public async Task ReadAsAsync_WhenContentIsMultipartContentAndFormatterCanReadFromTheContent()
{
MultipartContent mimeContent = new MultipartContent();
mimeContent.Add(new StringContent("multipartContent"));
_formatterMock.Setup(f => f.CanWriteType(It.IsAny<Type>())).Returns(true);
_formatterMock.Setup(f => f.CanReadType(It.IsAny<Type>())).Returns(true);
_formatterMock.Setup(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), It.IsAny<HttpContent>(), It.IsAny<IFormatterLogger>()))
.Returns<Type, Stream, HttpContent, IFormatterLogger>(async (type, stream, content, logger) =>
{
MultipartMemoryStreamProvider provider = await content.ReadAsMultipartAsync();
HttpContent providerContent = Assert.Single(provider.Contents);
return await providerContent.ReadAsStringAsync();
});
MediaTypeFormatter formatter = _formatterMock.Object;
formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/mixed"));
Assert.Equal("multipartContent", await mimeContent.ReadAsAsync<string>(new[] { formatter }));
}
[Fact]
public async Task ReadAsAsync_type_cancellationToken_PassesCancellationTokenFurther()
{
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
HttpContent content = new StringContent("42", Encoding.Default, "application/json");
await Assert.ThrowsAsync<OperationCanceledException>(() => content.ReadAsAsync(typeof(int), cts.Token));
}
[Fact]
public async Task ReadAsAsync_type_formatters_cancellationToken_PassesCancellationTokenFurther()
{
// Arrange
Stream stream = new MemoryStream();
HttpContent content = new StreamContent(stream);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/test");
CancellationToken token = new CancellationToken();
Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>(MockBehavior.Strict);
formatter.Object.SupportedMediaTypes.Add(content.Headers.ContentType);
formatter.Setup(f => f.CanReadType(typeof(int))).Returns(true);
formatter
.Setup(f => f.ReadFromStreamAsync(typeof(int), It.IsAny<Stream>(), content, null, token))
.Returns(Task.FromResult<object>(42))
.Verifiable();
// Act
await content.ReadAsAsync(typeof(int), new[] { formatter.Object }, token);
// Assert
formatter.Verify();
}
[Fact]
public async Task ReadAsAsync_type_formatters_formatterLogger_cancellationToken_PassesCancellationTokenFurther()
{
// Arrange
Stream stream = new MemoryStream();
HttpContent content = new StreamContent(stream);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/test");
CancellationToken token = new CancellationToken();
IFormatterLogger formatterLogger = new Mock<IFormatterLogger>().Object;
Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>(MockBehavior.Strict);
formatter.Object.SupportedMediaTypes.Add(content.Headers.ContentType);
formatter.Setup(f => f.CanReadType(typeof(int))).Returns(true);
formatter
.Setup(f => f.ReadFromStreamAsync(typeof(int), It.IsAny<Stream>(), content, formatterLogger, token))
.Returns(Task.FromResult<object>(42))
.Verifiable();
// Act
await content.ReadAsAsync(typeof(int), new[] { formatter.Object }, formatterLogger, token);
// Assert
formatter.Verify();
}
[Fact]
public Task ReadAsAsyncOfT_cancellationToken_PassesCancellationTokenFurther()
{
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
HttpContent content = new StringContent("42", Encoding.Default, "application/json");
return Assert.ThrowsAsync<OperationCanceledException>(() => content.ReadAsAsync<int>(cts.Token));
}
[Fact]
public async Task ReadAsAsyncOfT_formatters_cancellationToken_PassesCancellationTokenFurther()
{
// Arrange
Stream stream = new MemoryStream();
HttpContent content = new StreamContent(stream);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/test");
CancellationToken token = new CancellationToken();
Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>(MockBehavior.Strict);
formatter.Object.SupportedMediaTypes.Add(content.Headers.ContentType);
formatter.Setup(f => f.CanReadType(typeof(int))).Returns(true);
formatter
.Setup(f => f.ReadFromStreamAsync(typeof(int), It.IsAny<Stream>(), content, null, token))
.Returns(Task.FromResult<object>(42))
.Verifiable();
// Act
await content.ReadAsAsync<int>(new[] { formatter.Object }, token);
// Assert
formatter.Verify();
}
[Fact]
public async Task ReadAsAsyncOfT_formatters_formatterLogger_cancellationToken_PassesCancellationTokenFurther()
{
// Arrange
Stream stream = new MemoryStream();
HttpContent content = new StreamContent(stream);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/test");
CancellationToken token = new CancellationToken();
IFormatterLogger formatterLogger = new Mock<IFormatterLogger>().Object;
Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>(MockBehavior.Strict);
formatter.Object.SupportedMediaTypes.Add(content.Headers.ContentType);
formatter.Setup(f => f.CanReadType(typeof(int))).Returns(true);
formatter
.Setup(f => f.ReadFromStreamAsync(typeof(int), It.IsAny<Stream>(), content, formatterLogger, token))
.Returns(Task.FromResult<object>(42))
.Verifiable();
// Act
await content.ReadAsAsync<int>(new[] { formatter.Object }, formatterLogger, token);
// Assert
formatter.Verify();
}
private void SetupUpRoundTripSerialization(Func<Type, object> factory = null)
{
factory = factory ?? Activator.CreateInstance;
_formatterMock.Setup(f => f.WriteToStreamAsync(It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<Stream>(), It.IsAny<HttpContent>(), It.IsAny<TransportContext>()))
.Returns(TaskHelpers.Completed());
_formatterMock.Setup(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), It.IsAny<HttpContent>(), It.IsAny<IFormatterLogger>()))
.Returns<Type, Stream, HttpContent, IFormatterLogger>((type, stream, content, logger) => Task.FromResult<object>(factory(type)));
}
public class TestClass { }
public abstract class TestableHttpContent : HttpContent
{
protected override Task<Stream> CreateContentReadStreamAsync()
{
return CreateContentReadStreamAsyncPublic();
}
public virtual Task<Stream> CreateContentReadStreamAsyncPublic()
{
return base.CreateContentReadStreamAsync();
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return SerializeToStreamAsyncPublic(stream, context);
}
public abstract Task SerializeToStreamAsyncPublic(Stream stream, TransportContext context);
protected override bool TryComputeLength(out long length)
{
return TryComputeLengthPublic(out length);
}
public abstract bool TryComputeLengthPublic(out long length);
}
}
}