Sometimes you need to perform additional operations, such as setting up request headers, or analyzing response bytes. This is where WebRequest and WebResponse classes can be helpful.

Controlling requests and responses.

If you want to have more control over your request and response data, you should use the following pattern:

  • Create a WebRequest instance, setting up various properties, such as Headers, or Cookies. If you want to test file upload scenario, you can add one or more UploadedFile instances to the Files collection.

  • Execute the request using the ProcessRequest(WebRequest) method, obtaining a reference to a WebResponse instance.

  • Investigate the properties of the response, object, including headers, cookies, or raw bytes, or obtain a reference to the Page object.

Executing Http Handlers

If you need to test requests to files other than aspx pages, you can use the same pattern, and either run asserts against the response's text output using the BodyAsString property, raw bytes (Body property), or a reference to the IHttpHandler using the Handler property.

Testing page redirects

If your page sends a 302 response, your browser automatically issues a new request to the indicated page. By default, Ivonna does the same. You can verify that you have been redirected using the Url property.

If you want to disable this behaviour, you can set the AutoRedirect property to false. You can verify then that the response code is 302 indeed, and the appropriate header is set to the redirect location.

Handling page events with your code.

You can add your code to the page's lifecycle event handlers using the EventHandlers property. This can be useful in a lot of ways, including setting up mock services, running asserts etc.

Visual Basic Copy Code
Dim request As New WebRequest("Default.aspx")
request.EventHandlers.Page_Init = New EventHandler(AddressOf Page_Init)
'...
Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
	Dim page As Page = DirectCast(sender, Page)
	'other code
End Sub
				
C# Copy Code
WebRequest request = new WebRequest("Default.aspx");
request.EventHandlers.Page_Init = delegate(object sender, System.EventArgs e) {
	Page page = sender as Page;
	//other code
};
				

Unit testing App_Code classes

Sometimes you need to test a class from the App_Code folder (or any class other than a Web page) in a context of a Web request. For example, your class might be using the current HttpContext object. In order to test it, you can use an event handler as described in the previous section, and write your test code inside it.

Note that in order to use the class in your test, you should reference a compiled assembly. Unless you compile the tested class manually, you should use a precompiled web in your test, as described in Project setup scenarios.

Using multiple sessions

A TestSession instance corresponds to a browsing session. For example, if the first request returns a cookie, the second sends this cookie back to the "server". The viewstate is also stored between these requests.

If you want to test a situation when a user starts another browsing session, you can use a second TestSession instance. For example, you might want to check that if a user selects "Remember me" when logging in, the next time she is logged in from the first request indeed. In this case, simulate checking the appropriate checkbox in the postback in the first session, then create a new TestSession instance, call the GetPage(String) method, and verify that you are logged in (note that you should manually resend the persisted cookies using the CookiePersister class).

Using TypeMock Isolator

You should call the Init()()() method, like you usually do when you use TypeMock in your tests, before creating the first TestSession instance.