Skip to content

Instantly share code, notes, and snippets.

@hudsonmendes
Created July 19, 2018 06:56
Show Gist options
  • Save hudsonmendes/f4466ef844331c0843a59d1cd2f086c8 to your computer and use it in GitHub Desktop.
Save hudsonmendes/f4466ef844331c0843a59d1cd2f086c8 to your computer and use it in GitHub Desktop.
Test Utility class for creating POST MultipartFormData sending files (like a browser would) to the server
using System.Drawing;
using System.Net.Http;
using System.Net.Http.Headers;
namespace Infra.Assets.TestUtils
{
public static class TestHttpContentUtils
{
const string FileFormFieldName = "file";
public static HttpContent FileContent(
string fileName,
string contentType,
Image image)
{
var content = new MultipartFormDataContent();
var stream = TestImageUtils.StreamFrom(image);
var disposition = new ContentDispositionHeaderValue("form-data");
disposition.Name = FileFormFieldName;
disposition.FileName = fileName;
var file = new StreamContent(stream);
file.Headers.ContentLength = stream.Length;
file.Headers.ContentDisposition = disposition;
file.Headers.ContentType = new MediaTypeHeaderValue(contentType);
content.Add(file, FileFormFieldName);
return content;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment