Thursday, March 23, 2006

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.

Happy uploading!


At Tue Jun 06, 04:43:00 PM, Anonymous George Bell said...

The link no longer works to the control...please re-add...thanks!

At Wed Oct 25, 04:07:00 PM, Anonymous Anonymous said...

The link
doesn't work can you read. thanks!

At Thu Nov 29, 10:40:00 AM, Anonymous Anonymous said...

What files were changed? besides adding the htmlUploadFileTester, what other source code should be added to make NUnit accept file upload asp objects?

Also, the link he has works.... just delete the period at the end.


Post a Comment

<< Home