Reputation: 18308
I am writing a unit test for a Web API controller that accepts a file upload. I'd like to be able to take a Raw Request message stored as a string in a "Test Resources" file and turn it into an HttpRequestMessage. How can I do this?
For example:
POST http://local/api/File/26 HTTP/1.1
Accept: */*
Authorization: f1697cb7-7dd4-49c7-87fd-3f09bc3f3d7a
ServiceId: 0
Partition: 9000
Content-Type: multipart/form-data; boundary="Upload----05/06/2014 14:55:27"
Host: local
Content-Length: 179
Expect: 100-continue
--Upload----05/06/2014 14:55:27
Content-Disposition: attachment; filename=MyTestFile.txt
Content-Type: application/x-object
Hello World!!
--Upload----05/06/2014 14:55:27--
This Raw request would be stored in the resource file, and I would end up with it parsed and processed into a HttpRequestMessage
for my test.
Short of doing this manually building out the Content structure is there an easy way to do this?
Upvotes: 0
Views: 2002
Reputation: 57989
I am not aware of any easy way to parse this raw text to HttpRequestMessage...but have you considered building this multipart form data request using MutlipartFormDataContent
instead? following is an example
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:9095");
HttpRequestMessage request = new HttpRequestMessage();
MultipartFormDataContent mfdc = new MultipartFormDataContent();
mfdc.Add(new StringContent("Hell, World!"), "greeting");
mfdc.Add(new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("This is from a file"))), name: "Data", fileName: "File1.txt");
HttpResponseMessage response = client.PostAsync("/api/File", mfdc).Result;
Upvotes: 2