NUnitAsp Upload File Tester
When I was working with NUnitAsp before, there was one part of my application that relied on a file upload control . The page would parse the uploaded file and create some database objects based on the content of the file. Since the functionality of this page was critical to the operation of the site, it would be ideal to have some sort of test control to test file uploads. NUnitAsp, unfortunately, did not have any such support built in.
But alas, it was open source - so I made a few modifications. The result is my HtmlInputFileTester, which allows you to upload a file within an NUnitAsp unit test. This is the full NUnitAsp distribution with some small changes, in particular adding a control to test file uploads.
Using the control is easy. In your UnitTest, you can create a test object like this:
// There is an upload control with id/name = "file"
HtmlInputFileTester fileUpload =
new HtmlInputFileTester("file", CurrentWebForm, true);
// Specify the full path of the filename
fileUpload.Filename = "C:\MyFiles\MyFile.txt";
// And then click a button control as you normally would
If the filename is left blank, then no upload will take place.
In order to get this to work, I had to modify the way that forms were submitted under the NUnitAsp HttpClient. Forms were orignally submitted using the Http content type "application/x-www-form-urlencoded". This method uses a query string type format of passing in all the form variables through the request body.
My modification uses "multipart/form-data" as the content type, and thus each form variable has its own section in the request body. This allows writing a serires of bytes as a form value, making file uploads possible. The only other change to the suite is adding the HtmlInputFileTester control, which recognizes file uploads in your ASP.NET HTML and allows you to submit a file within your tests.
The definitive reference for managing file uploads in browsers is supplied in RFC 1867.