JG Vimalan's Blog

It is all about coding!

Unit test for Http Handler

In this post, I will discuss about creating unit tests for http handlers.

The functionality of my http handler is, it receives request from an ASP.NET page, processes it and sends back a response .

When I right clicked the ‘Process Request’ method in Http Handler and selected ‘Create unit tests…’, Visual Studio created unit test methods for me. But, when I tried to run those tests, I got the following error message, “The test adapter ‘WebHostAdapter’ threw an exception while running test…”.

So, the elegant way I can think of testing my custom http handler is, post data to the http handler url using HttpWebRequest and get the response back using HttpWebResponse.

Here is the unit test which I have written and it is working perfectly,

[TestMethod]
public void AuthTest()
{
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = postData += (“&username=” + “testuser@gmail.com“);
postData += (“&password=” + “testpassword”);

byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create(“https://customerdemoserver/WebApplication2/Auth.ashx“);
myRequest.Method = “POST”;
myRequest.ContentType = “application/x-www-form-urlencoded”;
myRequest.ContentLength = data.Length;
myRequest.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Stream newStream = myRequest.GetRequestStream();

newStream.Write(data, 0, data.Length);
newStream.Close();

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream stream = myResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();

Assert.IsNotNull(result);
}

Now, I am able to verify the functionality of my custom http handler, using the above unit test.

August 30, 2011 Posted by | ASP.NET, C#.NET, VS 2010 | 4 Comments

Sending response back from http handlers

In this post, I will explain about sending a response back from http handler to the calling page. The page that invoked http handler can be either .NET or Java.

I am sending response back in json format.

In the following method, AuthResponse is my custom class. You can use you own class there.

/// <summary>
/// This method sends the response in json format
/// </summary>
/// <param name=”context”></param>
/// <param name=”authResponse”></param>
private void SendResponseInJson(HttpContext context, AuthResponse authResponse)
{
context.Response.ContentType = “application/json”;
context.Response.ContentEncoding = Encoding.UTF8;

System.Web.Script.Serialization.JavaScriptSerializer jsSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
string jResponse = jsSerializer.Serialize(authResponse);

context.Response.Write(jResponse);
context.Response.Flush();
}

Read the response from http handler in an ASP.NET page,

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream stream = myResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();

The result string will contain the actual text that was sent from the http handler.

This can be useful when you want to send and receive data between two different web applications.

August 21, 2011 Posted by | ASP.NET, C#.NET | Leave a comment

Read AppSettings in JavaScript

The following code snippet provides information on reading AppSettings value in JavaScript.

<script type=”text/javascript” >

        window.location = ‘<%=(System.Configuration.ConfigurationManager.AppSettings[“DefaultRedirectUrl”].ToString()) %>’;

</script>

August 16, 2011 Posted by | ASP.NET, JavaScript | Leave a comment