Skip to content

Commit

Permalink
Send ContentType and ContentDisposition
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio1192 committed Jun 11, 2024
1 parent d2fef41 commit d25cfcc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/Acheve.TestHost/Extensions/TestServerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Acheve.TestHost.Routing;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
using System;
using System.IO;
using System.Linq.Expressions;
using System.Net.Mime;
using System.Text;

namespace Microsoft.AspNetCore.TestHost;
Expand Down Expand Up @@ -48,11 +50,18 @@ public static RequestBuilder CreateHttpApiRequest<TController, TActionResponse>(
where TController : class
=> UriDiscover.CreateHttpApiRequest<TController>(server, actionSelector, tokenValues, contentOptions);

public static IFormFile GivenFile(this TestServer _, string parameterName = "file", string filename = "test.txt", string content = "test")
public static IFormFile GivenFile(this TestServer _, string parameterName = "file", string filename = "test.txt", string content = "test", string contentType = MediaTypeNames.Text.Plain)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));

IFormFile file = new FormFile(stream, 0, stream.Length, parameterName, filename);
IFormFile file = new FormFile(stream, 0, stream.Length, parameterName, filename)
{
Headers = new HeaderDictionary()
{
[HeaderNames.ContentType] = contentType
},
ContentType = contentType
};

return file;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;

namespace Microsoft.AspNetCore.TestHost;
Expand Down Expand Up @@ -45,13 +45,13 @@ private void AddToMultipartFormDataContent(MultipartFormDataContent multipartCon
case CancellationToken:
break;
case IFormFile file:
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
var fileContent = new ByteArrayContent(ms.ToArray());
var fileContent = new StreamContent(file.OpenReadStream());
if (!string.IsNullOrEmpty(file.ContentType))
fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);
if (!string.IsNullOrEmpty(file.ContentDisposition))
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue(file.ContentDisposition);

multipartContent.Add(fileContent, file.Name, file.FileName);
}
multipartContent.Add(fileContent, file.Name, file.FileName);
break;
case object when data.GetType().IsPrimitiveType():
multipartContent.Add(new StringContent(PrimitiveValueToString(data)), propertyName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -108,6 +109,11 @@ public async Task<ActionResult<string>> PostObjectWithFile([FromForm] ParamWithF

private static async Task<string> ReadFormFile(IFormFile file)
{
if (string.IsNullOrEmpty(file.FileName) || string.IsNullOrEmpty(file.Name) || string.IsNullOrEmpty(file.ContentType))
{
throw new ArgumentException("Error in file", nameof(file));
}

using var reader = new StreamReader(file.OpenReadStream());
return await reader.ReadToEndAsync();
}
Expand Down

0 comments on commit d25cfcc

Please sign in to comment.