26

I'm using the following approach to upload files through ASP.NET Web API controllers.

[System.Web.Http.HttpPost]
public HttpResponseMessage UploadFile()
{
    HttpResponseMessage response;

    try
    {
        int id = 0;
        int? qId = null;
        if (int.TryParse(HttpContext.Current.Request.Form["id"], out id))
        {
            qId = id;
        }

        var file = HttpContext.Current.Request.Files[0];

        int filePursuitId = bl.UploadFile(qId, file);
    }
    catch (Exception ex)
    {

    }

    return response;
}

In my unit tests I've created an HTTPContext class manually before calling the UploadFile action:

var request = new HttpRequest("", "http://localhost", "");
var context = new HttpContext(request, new HttpResponse(new StringWriter()));
HttpContext.Current = context;

response = controller.UploadFile();

Unfortunately, I wasn't able to add custom values to the Form collection, since it's read-only. Also I couldn't change the Files collection.

Is there any way to add custom values to the Form and Files properties of the Request to add needed data (id and file content) during the unit test?

2 Answers 2

1

Use some mocking framework like Moq instead. Create a mock HttpRequestBase and mock HttpContextBase with whatever data you need and set them on the controller.

using Moq;
using NUnit.Framework;
using SharpTestsEx;

namespace StackOverflowExample.Moq
{
    public class MyController : Controller
    {
        public string UploadFile()
        {
            return Request.Form["id"];
        }
    }

    [TestFixture]
    public class WebApiTests
    {
        [Test]
        public void Should_return_form_data()
        {
            //arrange
            var formData = new NameValueCollection {{"id", "test"}};
            var request = new Mock<HttpRequestBase>();
            request.SetupGet(r => r.Form).Returns(formData);
            var context = new Mock<HttpContextBase>();
            context.SetupGet(c => c.Request).Returns(request.Object);

            var myController = new MyController();
            myController.ControllerContext = new ControllerContext(context.Object, new RouteData(), myController);

            //act
            var result = myController.UploadFile();

            //assert
            result.Should().Be.EqualTo(formData["id"]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply. Since we use web.api controllers we need to use HttpControllerContext instead of ControllerContext. HttpControllerContext doesn't allow to pass in the constructor HttpContextBase.
just wondering why this was upvoted when Pylyp Lebediev said is true...
0

Since you have no control over those classes why not wrap/abstract the functionality behind one do control

IRequestService request;

[HttpPost]
public HttpResponseMessage UploadFile() {
    HttpResponseMessage response;

    try {
        int id = 0;
        int? qId = null;
        if (int.TryParse(request.GetFormValue("id"), out id)) {
            qId = id;
        }

        var file = request.GetFile(0);

        int filePursuitId = bl.UploadFile(qId, file);
    } catch (Exception ex) {
        //...
    }

    return response;
}

Where request is one of your custom defined types IRequestService

public interface IRequestService {
    string GetFormValue(string key);
    HttpPostedFileBase GetFile(int index);
    //...other functionality you may need to abstract
}

and can be implemented like this to be injected into your controller

public class RequestService : IRequestService {

    public string GetFormValue(string key) {
        return HttpContext.Current.Request.Form[key];
    }

    public HttpPostedFileBase GetFile(int index) {
        return new HttpPostedFileWrapper(HttpContext.Current.Request.Files[index]);
    }
}

in your unit test

var requestMock = new Mock<IRequestService>();
//you then setup the mock to return your fake data
//...
//and then inject it into your controller
var controller = new MyController(requestMock.Object);
//Act
response = controller.UploadFile();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.