<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stephen Lacy &#187; httpcontext</title>
	<atom:link href="http://www.lacy.ie/tag/httpcontext/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lacy.ie</link>
	<description>Impressions as an ASP.Net Developer</description>
	<lastBuildDate>Mon, 05 Jul 2010 09:26:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to Mock the HttpContext in asp.net</title>
		<link>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net</link>
		<comments>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net#comments</comments>
		<pubDate>Tue, 13 Oct 2009 10:18:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[httpcontext]]></category>
		<category><![CDATA[mocking]]></category>

		<guid isPermaLink="false">http://www.lacy.ie/?p=232</guid>
		<description><![CDATA[I find mocking the http context rather difficult, you can&#8217;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&#8217;t seem to remember where I got the code, if anyone finds out [...]


Related posts:<ol><li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I find mocking the http context rather difficult, you can&#8217;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&#8217;t seem to remember where I got the code, if anyone finds out where it comes from please comment and I&#8217;ll add it to the post.</p>
<p><span id="more-232"></span></p>
<pre>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;
        }
    }
}</pre>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lacy.ie%2Fhow-to-mock-the-httpcontext-in-asp-net&amp;linkname=How%20to%20Mock%20the%20HttpContext%20in%20asp.net"><img src="http://www.lacy.ie/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>Related posts:<ol><li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->