|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.IO; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Text; |
@@ -347,5 +348,129 @@ public async Task ReadFormAsync_MultipartWithFieldAndFile_ReturnsParsedFormColle |
347 | 348 |
|
348 | 349 | await responseFeature.CompleteAsync(); |
349 | 350 | } |
| 351 | + |
| 352 | + [Theory] |
| 353 | + // FileBufferingReadStream transitions to disk storage after 30kb, and stops pooling buffers at 1mb. |
| 354 | + [InlineData(true, 1024)] |
| 355 | + [InlineData(false, 1024)] |
| 356 | + [InlineData(true, 40 * 1024)] |
| 357 | + [InlineData(false, 40 * 1024)] |
| 358 | + [InlineData(true, 4 * 1024 * 1024)] |
| 359 | + [InlineData(false, 4 * 1024 * 1024)] |
| 360 | + public async Task ReadFormAsync_MultipartWithFieldAndMediumFile_ReturnsParsedFormCollection(bool bufferRequest, int fileSize) |
| 361 | + { |
| 362 | + var fileContents = CreateFile(fileSize); |
| 363 | + var formContent = CreateMultipartWithFormAndFile(fileContents); |
| 364 | + var context = new DefaultHttpContext(); |
| 365 | + var responseFeature = new FakeResponseFeature(); |
| 366 | + context.Features.Set<IHttpResponseFeature>(responseFeature); |
| 367 | + context.Request.ContentType = MultipartContentType; |
| 368 | + context.Request.Body = new NonSeekableReadStream(formContent); |
| 369 | + |
| 370 | + if (bufferRequest) |
| 371 | + { |
| 372 | + context.Request.EnableRewind(); |
| 373 | + } |
| 374 | + |
| 375 | + // Not cached yet |
| 376 | + var formFeature = context.Features.Get<IFormFeature>(); |
| 377 | + Assert.Null(formFeature); |
| 378 | + |
| 379 | + var formCollection = await context.Request.ReadFormAsync(); |
| 380 | + |
| 381 | + Assert.NotNull(formCollection); |
| 382 | + |
| 383 | + // Cached |
| 384 | + formFeature = context.Features.Get<IFormFeature>(); |
| 385 | + Assert.NotNull(formFeature); |
| 386 | + Assert.NotNull(formFeature.Form); |
| 387 | + Assert.Same(formFeature.Form, formCollection); |
| 388 | + Assert.Same(formCollection, context.Request.Form); |
| 389 | + |
| 390 | + // Content |
| 391 | + Assert.Equal(1, formCollection.Count); |
| 392 | + Assert.Equal("Foo", formCollection["description"]); |
| 393 | + |
| 394 | + Assert.NotNull(formCollection.Files); |
| 395 | + Assert.Equal(1, formCollection.Files.Count); |
| 396 | + |
| 397 | + var file = formCollection.Files["myfile1"]; |
| 398 | + Assert.Equal("text/html", file.ContentType); |
| 399 | + Assert.Equal(@"form-data; name=""myfile1""; filename=""temp.html""", file.ContentDisposition); |
| 400 | + using (var body = file.OpenReadStream()) |
| 401 | + { |
| 402 | + Assert.True(body.CanSeek); |
| 403 | + CompareStreams(fileContents, body); |
| 404 | + } |
| 405 | + |
| 406 | + await responseFeature.CompleteAsync(); |
| 407 | + } |
| 408 | + |
| 409 | + private Stream CreateFile(int size) |
| 410 | + { |
| 411 | + var stream = new MemoryStream(size); |
| 412 | + var bytes = Encoding.ASCII.GetBytes("HelloWorld_ABCDEFGHIJKLMNOPQRSTUVWXYZ.abcdefghijklmnopqrstuvwxyz,0123456789;"); |
| 413 | + int written = 0; |
| 414 | + while (written < size) |
| 415 | + { |
| 416 | + var toWrite = Math.Min(size - written, bytes.Length); |
| 417 | + stream.Write(bytes, 0, toWrite); |
| 418 | + written += toWrite; |
| 419 | + } |
| 420 | + stream.Position = 0; |
| 421 | + return stream; |
| 422 | + } |
| 423 | + |
| 424 | + private Stream CreateMultipartWithFormAndFile(Stream fileContents) |
| 425 | + { |
| 426 | + var stream = new MemoryStream(); |
| 427 | + var header = |
| 428 | +"--WebKitFormBoundary5pDRpGheQXaM8k3T\r\n" + |
| 429 | +"Content-Disposition: form-data; name=\"description\"\r\n" + |
| 430 | +"\r\n" + |
| 431 | +"Foo\r\n" + |
| 432 | +"--WebKitFormBoundary5pDRpGheQXaM8k3T\r\n" + |
| 433 | +"Content-Disposition: form-data; name=\"myfile1\"; filename=\"temp.html\"\r\n" + |
| 434 | +"Content-Type: text/html\r\n" + |
| 435 | +"\r\n"; |
| 436 | + var footer = |
| 437 | +"\r\n--WebKitFormBoundary5pDRpGheQXaM8k3T--"; |
| 438 | + |
| 439 | + var bytes = Encoding.ASCII.GetBytes(header); |
| 440 | + stream.Write(bytes, 0, bytes.Length); |
| 441 | + |
| 442 | + fileContents.CopyTo(stream); |
| 443 | + fileContents.Position = 0; |
| 444 | + |
| 445 | + bytes = Encoding.ASCII.GetBytes(footer); |
| 446 | + stream.Write(bytes, 0, bytes.Length); |
| 447 | + stream.Position = 0; |
| 448 | + return stream; |
| 449 | + } |
| 450 | + |
| 451 | + private void CompareStreams(Stream streamA, Stream streamB) |
| 452 | + { |
| 453 | + Assert.Equal(streamA.Length, streamB.Length); |
| 454 | + byte[] bytesA = new byte[1024], bytesB = new byte[1024]; |
| 455 | + var readA = streamA.Read(bytesA, 0, bytesA.Length); |
| 456 | + var readB = streamB.Read(bytesB, 0, bytesB.Length); |
| 457 | + Assert.Equal(readA, readB); |
| 458 | + var loops = 0; |
| 459 | + while (readA > 0) |
| 460 | + { |
| 461 | + for (int i = 0; i < readA; i++) |
| 462 | + { |
| 463 | + if (bytesA[i] != bytesB[i]) |
| 464 | + { |
| 465 | + throw new Exception($"Value mismatch at loop {loops}, index {i}; A:{bytesA[i]}, B:{bytesB[i]}"); |
| 466 | + } |
| 467 | + } |
| 468 | + |
| 469 | + readA = streamA.Read(bytesA, 0, bytesA.Length); |
| 470 | + readB = streamB.Read(bytesB, 0, bytesB.Length); |
| 471 | + Assert.Equal(readA, readB); |
| 472 | + loops++; |
| 473 | + } |
| 474 | + } |
350 | 475 | } |
351 | 476 | } |
0 commit comments