Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing http handlers?

My current project based in Asp .net makes considerable use of Http handlers to process various requests? So, is there any way by which I can test the functionality of each of the handlers using unit test cases? We are using Nunit and Moq framework to facilitate unit testing.

like image 325
MockedMan.Object Avatar asked May 07 '10 19:05

MockedMan.Object


People also ask

What is HTTP handler interface?

The HTTP handler is a Java component that consists of properties. The handler delivers an outbound integration message to a URL by using HTTP or HTTPS protocols. The HTTP handler also evaluates the response code received from the external system.

What is HTTP handler Golang?

The Handler interface is an interface with a single method ServeHTTP which takes a http. Response and a http. Request as inputs. type Handler interface { ServeHTTP(ResponseWriter, *Request)

How do you run a go test?

To run your tests in this mode, run go test in your project's root directory. In the package list mode, go test compiles and tests each package listed as arguments to the command. If a package test passes, go test prints only the final 'ok' summary line. If a package test fails, go test prints the complete test output.


3 Answers

I think these blog entries from a while back are relevant:

http://www.kongsli.net/nblog/2009/05/03/aspnet-35-improving-testability-with-systemwebabstractions/

http://www.kongsli.net/nblog/2009/05/28/testability-with-systemwebabstractions-and-no-mock-framework/

See example #2 in the first post for an example on how to unit test an HttpHandler.

like image 108
Vidar Kongsli Avatar answered Oct 24 '22 01:10

Vidar Kongsli


If you dont care about unit tests and want something quick and dirty you can use Fiddler

if you want a more integrated approach (Unit testing) you can use the WebRequest and WebResponse.

like image 26
Robert Guyett Avatar answered Oct 23 '22 23:10

Robert Guyett


You sure can, although I haven't done myself "in anger".
Use a System.Net.WebClient to make HTTP calls against your handlers, and evaluate what comes back, that will allow you to test the public facing interface of the handler.

In this example I've hard-coded my target, and I'm using a method on the WebClient that will return a string.

The WebClient also gives you access to the ResponseHeaders, Encoding and other useful 'webby' stuff; you can also upload info as well.

using System.Net;

namespace UnitTestHttpHandler
{
    public class TestHarness
    {
        public static string GetString()
        {
            WebClient myWebClient = new WebClient();
            return myWebClient.DownloadString("http://localhost/Morphfolia.Web/ContentList.ashx");
        }
    }
}

You can then use the TestHarness to call the target HttpHandler and verify the results in your tests (or use a better approach to your testing if you know one - I'm not a unit testing guru).

    [TestMethod]
    public void TestMethod1()
    {
        string x = UnitTestHttpHandler.TestHarness.GetString();

        Assert.IsTrue(x.Length > 5);
    }
like image 37
Adrian K Avatar answered Oct 24 '22 00:10

Adrian K