Home > OWIN, Web API > Testing Web API

Testing Web API

Continuing with my previous post on Implementing Web API Versioning using OWIN, in this post I am going to show how to write unit test for your Web APIs.

As we know that the beauty of OWIN is able to run your web application independent of the web server and based on this principle the Microsoft.Owin.Testing takes advantage of this feature to host the Web API in memory and execute the methods. Very simple and straight forward.

Lets get started by creating a new Unit Test project in Visual Studio and get the Microsft.Owin.Testing package from the Nuget Server as shown below.

PM> Install-Package Microsoft.Owin.Testing

Next step is to create a custom Startup class and bring in any services or OWIN Middleware which our Web API depends upon. As this is a demo Web API application I will just configure the DefaultVersionSelector we build in the previous post.


public class CustomStartup
    public void Configuration(IAppBuilder app)
        var config = new HttpConfiguration();

                id = RouteParameter.Optional,
                version = "1"

            new DefaultVersionControllerSelector(
                new DefaultVersionSelector()));

As you can see nothing special in the startup class it is identical to our Web API project startup class and all I am doing is registering my DefaultVersionSelector and it dependencies and asking the IAppBuilder to invoke the OWIN Web API Middleware.

Now we are going to write a unit test which will use the OWIN testing component and invoke a simple get on our values controller.


public class WhenUsingOwinTestServer
    private const string ContentTypeJsonVersion1 = "application/json;version=1";

    private const string AcceptHeader = "Accept";

    private const string RequestUri = "/api/values";

    private const string ExpectValueVersion1 = "[\"value1-Version1\",\"value2-version1\"]";

    public void ShouldBeAbleToGetResultForVersion1()
        using (var server = TestServer.Create<CustomStartup>())
            var client = server.HttpClient;

            client.DefaultRequestHeaders.Add(AcceptHeader, ContentTypeJsonVersion1);
            var task = server.HttpClient.GetAsync(RequestUri);
            var response = task.Result.Content.ReadAsStringAsync();
            Assert.AreEqual(ExpectValueVersion1, response.Result);

As you can see in the above code we are using the TestServer to host Web API with our custom startup class. First we get a reference of the it’s HttpClient object and add our accept header with the versioning information. Next we make a GET request at the api/values end point,as everything is happening in memory you can see we don’t specify the web server in our URI.

Rest is pretty straight forward and we assert that the expected values and the results do match.

  1. August 3, 2015 at 6:46 am

    Reblogged this on Dinesh Ram Kali..

  1. September 8, 2015 at 5:55 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: