using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace CodeBackpack.Test.Http
{
///
/// The unit test utility.
///
public class HttpHandlerTest
{
private readonly StringWriter output = new StringWriter();
private readonly HttpContext context;
protected HttpHandlerTest(string requestMethod, string requestBody, Encoding requestEncoding, string page, string query)
{
WorkerRequestStub workerRequest = new WorkerRequestStub(requestMethod,
requestBody, requestEncoding, page, query, output);
context = new HttpContext(workerRequest);
if (requestMethod == "POST")
{
context.Request.ContentEncoding = requestEncoding;
}
// If the handler under test happens to need the session state, here you go:
SessionIDManager idManager = new SessionIDManager();
SessionStateUtility.AddHttpSessionStateToContext(context,
new HttpSessionStateContainer(
idManager.CreateSessionID(context),
new SessionStateItemCollection(),
new HttpStaticObjectsCollection(),
60,
true,
HttpCookieMode.UseCookies,
SessionStateMode.InProc,
true));
}
///
/// Pretend a GET request was issued.
///
/// The path part of URL. Don't give a URL like
/// http://foo/myhandler.ashx, instead supply just myhandler.ashx
///
public static HttpHandlerTest ImitateGet(String virtualPath)
{
HttpHandlerTest httpHandlerTest = new HttpHandlerTest("GET", null, null, virtualPath, null);
return httpHandlerTest;
}
///
/// Pretend a POST request was issued.
///
/// See above.
/// The POST request content.
/// The POST request encoding that the handler is supposed to expect.
public static HttpHandlerTest ImitatePost(String virtualPath, String requestBody, Encoding requestEncoding)
{
HttpHandlerTest httpHandlerTest = new HttpHandlerTest("POST", requestBody, requestEncoding, virtualPath, null);
return httpHandlerTest;
}
public virtual void Execute(IHttpHandler testSubject)
{
testSubject.ProcessRequest(context);
// Force the data accumulated in internal response buffers to be written
// to 'output' StringWriter.
context.Response.Flush();
}
public virtual String Output
{
get { return output.ToString(); }
}
public virtual HttpContext Context
{
get { return context; }
}
}
}