How to Mock the HttpContext in asp.net

October 13, 2009

I find mocking the http context rather difficult, you can’t inherit from it and making a wrapper class is a lot of work. Thankfully someone came up with a solution, unfortunately the link they included with the code is dead and I can’t seem to remember where I got the code, if anyone finds out where it comes from please comment and I’ll add it to the post.

using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Web;
using System.Web.Hosting;
using System.Web.SessionState;

public sealed class MockHttpContext
{
    // NOTE: This code is based on the following article:
    // http://righteousindignation.gotdns.org/blog/archive/2004/04/13/149.aspx
    private const string ContextKeyAspSession = "AspSession";
    private const string ThreadDataKeyAppPath = ".appPath";
    private const string ThreadDataKeyAppPathValue = "c:\\inetpub\\wwwroot\\webapp\\";
    private const string ThreadDataKeyAppVPath = ".appVPath";
    private const string ThreadDataKeyAppVPathValue = "/webapp";
    private const string WorkerRequestPage = "default.aspx";

    private HttpContext context = null;

    private MockHttpContext()
        : base()
    {
    }

    public MockHttpContext(bool isSecure)
        : this()
    {
        Thread.GetDomain().SetData(
            MockHttpContext.ThreadDataKeyAppPath, MockHttpContext.ThreadDataKeyAppPathValue);
        Thread.GetDomain().SetData(
            MockHttpContext.ThreadDataKeyAppVPath, MockHttpContext.ThreadDataKeyAppVPathValue);
        SimpleWorkerRequest request = new WorkerRequest(MockHttpContext.WorkerRequestPage,
            string.Empty, new StringWriter(), isSecure);
        this.context = new HttpContext(request);

        HttpSessionStateContainer container = new HttpSessionStateContainer(
            Guid.NewGuid().ToString("N"), new SessionStateItemCollection(), new HttpStaticObjectsCollection(),
            5, true, HttpCookieMode.AutoDetect, SessionStateMode.InProc, false);

        HttpSessionState state = Activator.CreateInstance(
             typeof(HttpSessionState),
             BindingFlags.Public | BindingFlags.NonPublic |
             BindingFlags.Instance | BindingFlags.CreateInstance,
             null,
             new object[] { container }, CultureInfo.CurrentCulture) as HttpSessionState;
        this.context.Items[ContextKeyAspSession] = state;
    }

    public HttpContext Context
    {
        get
        {
            return this.context;
        }
    }

    private class WorkerRequest : SimpleWorkerRequest
    {
        private bool isSecure = false;

        public WorkerRequest(string page, string query, TextWriter output, bool isSecure)
            : base(page, query, output)
        {
            this.isSecure = isSecure;
        }

        public override bool IsSecure()
        {
            return this.isSecure;
        }
    }
}
  • Share/Bookmark

Related posts:

  1. Redirecting using strong types in an asp.net web application project It recently began to annoy me that every time I...
  2. ResolveUrl in your Business Logic I’ve recently started moving a large amount of code on...

Related posts brought to you by Yet Another Related Posts Plugin.

tags: , , ,
posted in Testing by admin

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org