My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace CodeBackpack.Test.Http
{
/// <summary>
/// The <see cref="IHttpHandler"/> unit test utility.
/// </summary>
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));
}

/// <summary>
/// Pretend a GET request was issued.
/// </summary>
/// <param name="virtualPath">The path part of URL. Don't give a URL like
/// <code>http://foo/myhandler.ashx</code>, instead supply just <code>myhandler.ashx</code>
/// </param>
public static HttpHandlerTest ImitateGet(String virtualPath)
{
HttpHandlerTest httpHandlerTest = new HttpHandlerTest("GET", null, null, virtualPath, null);
return httpHandlerTest;
}

/// <summary>
/// Pretend a POST request was issued.
/// </summary>
/// <param name="virtualPath">See above.</param>
/// <param name="requestBody">The POST request content.</param>
/// <param name="requestEncoding">The POST request encoding that the handler is supposed to expect.</param>
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; }
}
}
}
Show details Hide details

Change log

r19 by prepin on May 14, 2008   Diff
Created a permanent checkpoint of testhtto
from trunk.
You can checkout just this directory and
everything should build.
Go to: 
Project members, sign in to write a code review

Older revisions

r18 by prepin on May 14, 2008   Diff
[testhttp] When ImitatePost is used,
ensure the request's ContentLength is
valid.
r17 by prepin on May 12, 2008   Diff
[testhttp] Minor refactoring &
documentation.
r15 by prepin on May 12, 2008   Diff
HttpHandler unit testing - proof of
concept that works.
All revisions of this file

File info

Size: 3158 bytes, 83 lines

File properties

svn:eol-style
native
Hosted by Google Code