forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpContentMultipartExtensionsTests.cs
More file actions
507 lines (436 loc) · 21.9 KB
/
Copy pathHttpContentMultipartExtensionsTests.cs
File metadata and controls
507 lines (436 loc) · 21.9 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
// 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.Parsers;
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 HttpContentMultipartExtensionsTests
{
private const string ValidBoundary = "-A-";
private const string DefaultContentType = "text/plain";
private const string DefaultContentDisposition = "form-data";
private const string ExceptionStreamProviderMessage = "Bad Stream Provider!";
private const string ExceptionSyncStreamMessage = "Bad Sync Stream!";
private const string ExceptionAsyncStreamMessage = "Bad Async Stream!";
public static TheoryDataSet<string, bool, string, bool> IsMimeMultipartContentTestData
{
get
{
return new TheoryDataSet<string, bool, string, bool>
{
{ "text/plain", false, "plain", false },
{ "application/*", false, "related", false },
{ "*/*", false, "related", false },
{ "multipart/form-data", false, "form-data", false },
{ "multipart/form-data; boundary=1234", true, "related", false },
{ "multipart/form-data; boundary=1234; charset=utf-8", true, "form-data", true },
{ "Multipart/Related; boundary=example-1; start=\"<950120.aaCC@XIson.com>\"; type=\"Application/X-FixedRecord\"; start-info=\"-o ps\"", true, "related", true },
};
}
}
public static TheoryDataSet<string, bool> IsMimeMultipartContentTestData_NoSubType
{
get
{
var dataSet = new TheoryDataSet<string, bool>();
foreach (var item in IsMimeMultipartContentTestData)
{
dataSet.Add((string)item[0], (bool)item[1]);
}
return dataSet;
}
}
private static HttpContent CreateContent(string boundary, params string[] bodyEntity)
{
return CreateContentWithContentType(boundary, DefaultContentType, bodyEntity);
}
private static HttpContent CreateContentWithContentType(string boundary, string partContentType, params string[] bodyEntity)
{
List<string> entities = new List<string>();
int cnt = 0;
foreach (var body in bodyEntity)
{
byte[] header = InternetMessageFormatHeaderParserTests.CreateBuffer(
String.Format("N{0}: V{0}", cnt),
String.Format("Content-Type: {0}", partContentType),
String.Format("Content-Disposition: {0}; FileName=\"N{1}\"", DefaultContentDisposition, cnt));
entities.Add(Encoding.UTF8.GetString(header) + body);
cnt++;
}
byte[] message = MimeMultipartParserTests.CreateBuffer(boundary, entities.ToArray());
HttpContent result = new ByteArrayContent(message);
var contentType = new MediaTypeHeaderValue("multipart/form-data");
contentType.Parameters.Add(new NameValueHeaderValue("boundary", String.Format("\"{0}\"", boundary)));
result.Headers.ContentType = contentType;
return result;
}
private static async Task ValidateContentsAsync(IEnumerable<HttpContent> contents)
{
int cnt = 0;
foreach (var content in contents)
{
Assert.NotNull(content);
Assert.NotNull(content.Headers);
Assert.Equal(4, content.Headers.Count());
IEnumerable<string> parsedValues = content.Headers.GetValues(String.Format("N{0}", cnt));
string parsedValue = Assert.Single(parsedValues);
Assert.Equal(String.Format("V{0}", cnt), parsedValue);
Assert.Equal(DefaultContentType, content.Headers.ContentType.MediaType);
Assert.Equal(DefaultContentDisposition, content.Headers.ContentDisposition.DispositionType);
Assert.Equal(String.Format("\"N{0}\"", cnt), content.Headers.ContentDisposition.FileName);
await AssertContentLengthHeaderValueAsync(content);
cnt++;
}
}
private static async Task AssertContentLengthHeaderValueAsync(HttpContent content)
{
long contentLength = (await content.ReadAsByteArrayAsync()).LongLength;
long contentLengthHeaderValue = content.Headers.ContentLength.GetValueOrDefault();
Assert.Equal(contentLength, contentLengthHeaderValue);
}
[Fact]
public void IsMimeMultipartContent_ThrowsOnNullContent()
{
Assert.ThrowsArgumentNull(() => HttpContentMultipartExtensions.IsMimeMultipartContent(null), "content");
}
[Fact]
public void IsMimeMultipartContent_ThrowsOnNullSubType()
{
StringContent content = new StringContent(String.Empty);
Assert.ThrowsArgumentNull(() => HttpContentMultipartExtensions.IsMimeMultipartContent(content, null), "subtype");
}
[Theory]
[PropertyData("IsMimeMultipartContentTestData")]
public void IsMimeMultipartContent_ReturnsCorrectValue(string mediaType, bool isMultipart, string subtype, bool hasSubtype)
{
StringContent content = new StringContent(String.Empty);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
Assert.Equal(isMultipart, content.IsMimeMultipartContent());
Assert.Equal(hasSubtype, content.IsMimeMultipartContent(subtype));
}
[Fact]
public Task ReadAsMultipartAsync_ThrowsOnNullStreamProvider()
{
HttpContent content = CreateContent(ValidBoundary);
return Assert.ThrowsArgumentNullAsync(() => content.ReadAsMultipartAsync((MultipartStreamProvider)null), "streamProvider");
}
[Fact]
public Task ReadAsMultipartAsync_ThrowsOnInvalidBufferSize()
{
HttpContent content = CreateContent(ValidBoundary);
return Assert.ThrowsArgumentGreaterThanOrEqualToAsync(
() => content.ReadAsMultipartAsync(new MultipartMemoryStreamProvider(), ParserData.MinBufferSize - 1),
"bufferSize", ParserData.MinBufferSize.ToString(), ParserData.MinBufferSize - 1);
}
[Theory]
[PropertyData("IsMimeMultipartContentTestData_NoSubType")]
public async Task ReadAsMultipartAsync_DetectsNonMultipartContent(string mediaType, bool isMultipart)
{
StringContent content = new StringContent(String.Empty);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
if (!isMultipart)
{
await Assert.ThrowsArgumentAsync(() => content.ReadAsMultipartAsync(), "content");
}
}
[Theory]
[TestDataSet(typeof(MimeMultipartParserTests), "Boundaries")]
public async Task ReadAsMultipartAsync_ParsesContent(string boundary)
{
HttpContent successContent;
MultipartMemoryStreamProvider result;
successContent = CreateContent(boundary, "A", "B", "C");
result = await successContent.ReadAsMultipartAsync();
Assert.Equal(3, result.Contents.Count);
successContent = CreateContent(boundary, "A", "B", "C");
result = await successContent.ReadAsMultipartAsync(new MultipartMemoryStreamProvider());
Assert.Equal(3, result.Contents.Count);
successContent = CreateContent(boundary, "A", "B", "C");
result = await successContent.ReadAsMultipartAsync(new MultipartMemoryStreamProvider(), 1024);
Assert.Equal(3, result.Contents.Count);
}
[Fact]
public async Task ReadAsMultipartAsync_SkipsHeaderValidation()
{
// Arrange
var content = CreateContentWithContentType("--boundary", "invalid", "SomeContent");
// Act
var result = await content.ReadAsMultipartAsync(CancellationToken.None);
// Assert
var bodyPart = Assert.Single(result.Contents);
Assert.Null(bodyPart.Headers.ContentType);
Assert.Equal("invalid", Assert.Single(bodyPart.Headers.GetValues("Content-Type")));
}
[Fact]
public async Task ReadAsMultipartAsync_SetsStronglyTypedHeader_WhenHeaderIsValid()
{
// Arrange
var content = CreateContentWithContentType("--boundary", "application/json", "SomeContent");
// Act
var result = await content.ReadAsMultipartAsync(CancellationToken.None);
// Assert
var bodyPart = Assert.Single(result.Contents);
Assert.NotNull(bodyPart.Headers.ContentType);
Assert.Equal("application/json", bodyPart.Headers.ContentType.MediaType);
}
[Theory]
[TestDataSet(typeof(MimeMultipartParserTests), "Boundaries")]
public async Task ReadAsMultipartAsync_ParsesEmptyContent(string boundary)
{
HttpContent content = CreateContent(boundary);
MultipartMemoryStreamProvider result = await content.ReadAsMultipartAsync();
Assert.Empty(result.Contents);
}
[Fact]
public async Task ReadAsMultipartAsync_ThrowsOnBadStreamProvider()
{
HttpContent content = CreateContent(ValidBoundary, "A", "B", "C");
IOException exception = await Assert.ThrowsAsync<IOException>(() => content.ReadAsMultipartAsync(new BadStreamProvider()));
InvalidOperationException invalidOperationException = exception.InnerException as InvalidOperationException;
Assert.NotNull(invalidOperationException);
Assert.NotNull(invalidOperationException.InnerException);
Assert.Equal(ExceptionStreamProviderMessage, invalidOperationException.InnerException.Message);
}
[Fact]
public async Task ReadAsMultipartAsync_ThrowsOnNullProvider()
{
HttpContent content = CreateContent(ValidBoundary, "A", "B", "C");
IOException exception = await Assert.ThrowsAsync<IOException>(() => content.ReadAsMultipartAsync(new NullProvider()));
Assert.IsType<InvalidOperationException>(exception.InnerException);
}
[Fact]
public async Task ReadAsMultipartAsync_ThrowsOnReadOnlyStream()
{
HttpContent content = CreateContent(ValidBoundary, "A", "B", "C");
IOException exception = await Assert.ThrowsAsync<IOException>(() => content.ReadAsMultipartAsync(new ReadOnlyStreamProvider()));
Assert.IsType<InvalidOperationException>(exception.InnerException);
}
[Fact]
public Task ReadAsMultipartAsync_ThrowsOnPrematureEndOfStream()
{
HttpContent content = new StreamContent(Stream.Null);
string mediaType = String.Format("multipart/form-data; boundary=\"{0}\"", ValidBoundary);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
return Assert.ThrowsAsync<IOException>(() => content.ReadAsMultipartAsync());
}
[Fact]
public async Task ReadAsMultipartAsync_ThrowsOnReadError()
{
HttpContent content = new StreamContent(new ReadErrorStream());
string mediaType = String.Format("multipart/form-data; boundary=\"{0}\"", ValidBoundary);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
IOException exception = await Assert.ThrowsAsync<IOException>(() => content.ReadAsMultipartAsync());
Assert.NotNull(exception.InnerException);
Assert.Equal(ExceptionAsyncStreamMessage, exception.InnerException.Message);
}
[Fact]
public async Task ReadAsMultipartAsync_ThrowsOnWriteError()
{
HttpContent content = CreateContent(ValidBoundary, "A", "B", "C");
IOException exception = await Assert.ThrowsAsync<IOException>(() => content.ReadAsMultipartAsync(new WriteErrorStreamProvider()));
Assert.NotNull(exception.InnerException);
Assert.Equal(ExceptionAsyncStreamMessage, exception.InnerException.Message);
}
[Theory]
[TestDataSet(typeof(MimeMultipartParserTests), "Boundaries", typeof(MimeMultipartParserTests), "SingleShortBodies")]
public async Task ReadAsMultipartAsync_SingleShortBodyPart(string boundary, string singleShortBody)
{
HttpContent content = CreateContent(boundary, singleShortBody);
MultipartMemoryStreamProvider result = await content.ReadAsMultipartAsync();
HttpContent resultContent = Assert.Single(result.Contents);
Assert.Equal(singleShortBody, await resultContent.ReadAsStringAsync());
await ValidateContentsAsync(result.Contents);
}
[Fact]
public async Task ReadAsMultipartAsync_WithHugeBody_AvoidStackOverflow()
{
// Arrange
var fiftyMegs = 1024 * 1024 * 50;
HttpContent content = CreateContent("---3123---", new string('x', fiftyMegs));
// Act
MultipartMemoryStreamProvider result = await content.ReadAsMultipartAsync(new MultipartMemoryStreamProvider(), 256);
// Assert
// this is for sanity. The actual test here is that the Act part did not cause a stack overflow
Assert.Equal(fiftyMegs, (await result.Contents[0].ReadAsStringAsync()).Length);
await ValidateContentsAsync(result.Contents);
}
[Theory]
[TestDataSet(typeof(MimeMultipartParserTests), "Boundaries", typeof(MimeMultipartParserTests), "MultipleShortBodies")]
public async Task ReadAsMultipartAsync_MultipleShortBodyParts(string boundary, string[] multipleShortBodies)
{
HttpContent content = CreateContent(boundary, multipleShortBodies);
MultipartMemoryStreamProvider result = await content.ReadAsMultipartAsync();
Assert.Equal(multipleShortBodies.Length, result.Contents.Count);
for (var check = 0; check < multipleShortBodies.Length; check++)
{
Assert.Equal(multipleShortBodies[check], await result.Contents[check].ReadAsStringAsync());
}
await ValidateContentsAsync(result.Contents);
}
[Theory]
[TestDataSet(typeof(MimeMultipartParserTests), "Boundaries", typeof(MimeMultipartParserTests), "SingleLongBodies")]
public async Task ReadAsMultipartAsync_SingleLongBodyPart(string boundary, string singleLongBody)
{
HttpContent content = CreateContent(boundary, singleLongBody);
MultipartMemoryStreamProvider result = await content.ReadAsMultipartAsync();
HttpContent resultContent = Assert.Single(result.Contents);
Assert.Equal(singleLongBody, await resultContent.ReadAsStringAsync());
await ValidateContentsAsync(result.Contents);
}
[Theory]
[TestDataSet(typeof(MimeMultipartParserTests), "Boundaries", typeof(MimeMultipartParserTests), "MultipleLongBodies")]
public async Task ReadAsMultipartAsync_MultipleLongBodyParts(string boundary, string[] multipleLongBodies)
{
HttpContent content = CreateContent(boundary, multipleLongBodies);
MultipartMemoryStreamProvider result = await content.ReadAsMultipartAsync(new MultipartMemoryStreamProvider(), ParserData.MinBufferSize);
Assert.Equal(multipleLongBodies.Length, result.Contents.Count);
for (var check = 0; check < multipleLongBodies.Length; check++)
{
Assert.Equal(multipleLongBodies[check], await result.Contents[check].ReadAsStringAsync());
}
await ValidateContentsAsync(result.Contents);
}
[Theory]
[TestDataSet(typeof(MimeMultipartParserTests), "Boundaries")]
public async Task ReadAsMultipartAsync_UsingMultipartContent(string boundary)
{
MultipartContent content = new MultipartContent("mixed", boundary);
content.Add(new StringContent("A"));
content.Add(new StringContent("B"));
content.Add(new StringContent("C"));
MemoryStream memStream = new MemoryStream();
await content.CopyToAsync(memStream);
memStream.Position = 0;
byte[] data = memStream.ToArray();
var byteContent = new ByteArrayContent(data);
byteContent.Headers.ContentType = content.Headers.ContentType;
MultipartMemoryStreamProvider result = await byteContent.ReadAsMultipartAsync();
Assert.Equal(3, result.Contents.Count);
Assert.Equal("A", await result.Contents[0].ReadAsStringAsync());
Assert.Equal("B", await result.Contents[1].ReadAsStringAsync());
Assert.Equal("C", await result.Contents[2].ReadAsStringAsync());
}
[Theory]
[TestDataSet(typeof(MimeMultipartParserTests), "Boundaries")]
public async Task ReadAsMultipartAsync_NestedMultipartContent(string boundary)
{
const int nesting = 10;
const string innerText = "Content";
MultipartContent innerContent = new MultipartContent("mixed", boundary);
innerContent.Add(new StringContent(innerText));
for (var cnt = 0; cnt < nesting; cnt++)
{
string outerBoundary = String.Format("{0}_{1}", boundary, cnt);
MultipartContent outerContent = new MultipartContent("mixed", outerBoundary);
outerContent.Add(innerContent);
innerContent = outerContent;
}
MemoryStream memStream = new MemoryStream();
await innerContent.CopyToAsync(memStream);
memStream.Position = 0;
byte[] data = memStream.ToArray();
HttpContent content = new ByteArrayContent(data);
content.Headers.ContentType = innerContent.Headers.ContentType;
for (var cnt = 0; cnt < nesting + 1; cnt++)
{
MultipartMemoryStreamProvider result = await content.ReadAsMultipartAsync();
content = Assert.Single(result.Contents);
Assert.NotNull(content);
}
string text = await content.ReadAsStringAsync();
Assert.Equal(innerText, text);
}
[Fact]
public async Task ReadAsMultipartAsyncOfT_PassesCancellationToken()
{
CancellationToken token = new CancellationToken();
HttpContent content = CreateContent("boundary");
Mock<MultipartStreamProvider> provider = new Mock<MultipartStreamProvider>();
provider.Setup(p => p.ExecutePostProcessingAsync(token))
.Returns(Task.FromResult(42))
.Verifiable();
await content.ReadAsMultipartAsync<MultipartStreamProvider>(provider.Object, token);
provider.Verify();
}
public class ReadOnlyStream : MemoryStream
{
public override bool CanWrite
{
get
{
return false;
}
}
}
public class ReadErrorStream : MemoryStream
{
public override int Read(byte[] buffer, int offset, int count)
{
throw new IOException(ExceptionSyncStreamMessage);
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
throw new IOException(ExceptionAsyncStreamMessage);
}
#if !NETFX_CORE // BeginX and EndX not supported on Streams in portable libraries
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
throw new IOException(ExceptionAsyncStreamMessage);
}
#endif
}
public class WriteErrorStream : MemoryStream
{
public override void Write(byte[] buffer, int offset, int count)
{
throw new IOException(ExceptionSyncStreamMessage);
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
throw new IOException(ExceptionAsyncStreamMessage);
}
#if !NETFX_CORE // BeginX and EndX not supported on Streams in portable libraries
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
throw new IOException(ExceptionAsyncStreamMessage);
}
#endif
}
public class BadStreamProvider : MultipartStreamProvider
{
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
throw new Exception(ExceptionStreamProviderMessage);
}
}
public class NullProvider : MultipartStreamProvider
{
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
return null;
}
}
public class ReadOnlyStreamProvider : MultipartStreamProvider
{
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
return new ReadOnlyStream();
}
}
public class WriteErrorStreamProvider : MultipartStreamProvider
{
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
return new WriteErrorStream();
}
}
}
}