1

I am working with ASP.NET/.NET 4.8 application.

My action method is as follows:

public class TestController: System.Web.Http.ApiController
{
    // pseudo code
    public async Task<IHttpActionResult> Upload()
    {
        var request = HttpContext.Current.Request;
        var file = request.Files[0];//I want to mock this
    }
}

How to mock a HttpRequest which has a file in it?

var mockedRequest = new Mock<HttpRequest>();//not allowed: Type to mock (HttpRequest) must be an interface, a delegate, or a non-sealed, non-static class.

I wonder if both HttpContext and HttpRequest classes are sealed and does not inherit from anything, then how does .NET itself creates objects of these classes (n00b question).

As a workaround, I am trying to construct HttpRequest object. Unfortunately it has only one constructor and it does not take Files as parameter:

var Request = new HttpRequest("Upload", "https://www.google.com", "Id=1");
HttpContext.Current = new HttpContext(
    Request, new HttpResponse(new StringWriter())
);

How do I set this FormData to the HttpRequest object?

string filePath = @"D:\login.txt";
var form = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(File.ReadAllBytes(filePath));
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
form.Add(fileContent, "file", Path.GetFileName(filePath));

Or is there any other way to mock HttpRequest?

Update I realize HttpContext and HttpRequest has no base class and isn't virtual, and hence is unusable for testing, cannot mock it.
https://www.splinter.com.au/httpcontext-vs-httpcontextbase-vs-httpcontext/
I wonder how do I test my code

2
  • Might be related: stackoverflow.com/questions/46217164/… Commented Sep 29 at 11:14
  • 3
    Have you tried using the testable base classes that Microsoft provides in ASP.NET Framework, such as HttpContextBase, HttpRequestBase, or HttpPostedFileBase instead of the sealed classes? Commented Sep 29 at 15:38

1 Answer 1

1

I introduced a HttpContextBase property and modified my code

private HttpContextBase _httpContextBase;
public HttpContextBase HttpContextBase
{
    get {
        return _httpContextBase?? new HttpContextWrapper(HttpContext.Current);
    }
    set {
        _httpContextBase = value;
    }
}

modified code

public class TestController: System.Web.Http.ApiController
{
    // pseudo code
    public async Task<IHttpActionResult> Upload()
    {
        var request = HttpContextBase.Request;
        var file = request.Files[0];
    }
}

now I am able to mock the HttpContextBase, HttpRequestBase and HttpResponseBase
as mentioned in this post https://stackoverflow.com/a/1228208/223752

Sign up to request clarification or add additional context in comments.

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.