Self-Hosted Web Api testing with Owin/Katana

TL;DR – You don’t really need IIS to perform full automation testing to your web project. Today I want to show you how I am testing my ASP.NET WebApi services without IIS by using OWIN/Katana.

Download source code from my GitHub account.

(This article is targeting the pre .NET Core era).

serveroff
Recently I was working on a project where I was developing a web service using ASP.NET WebApi (targeting .NET 4.5.2).

The main technologies that I used for the solution were:

After I was done with the main development (including unit-testing) I started thinking about how to go around the automation testing story.

As I consulted some of my developer friends (and Google), I was very surprised that the recurring theme was that they either don’t do full automation testing or they do, but only partially. Why? Most of them blamed the issue of hosting – in other words they needed IIS to host their WebApi service.

At this point I asked myself, do I really need to spin-up the IIS beast? Or can I somehow overcome that dependency and self-host my service? As it turns out, there is a pretty simple way to self-host your service in-process, without the need of any external web servers.

Enters: OWIN (Katana)

So what is this OWIN thing? Here is a short description from owin.org:

owin-one-liner

This basically means that  you can build your OWIN implementation and use it to host your web project yourself, without the need for a web server (like IIS). The good new is, Microsoft already implemented OWIN in a project called Katana.

This meant really great news for my automation testing needs. I decided to take it for a ride, and so I wrapped the whole thing in a UnitTesting project and the rest is history.
Note that I also incorporated Ninject for Dependency Injection in the source code, that way you can also see how to provide dependencies for your api controllers during testing.

You can find a working template on my GitHub account. Simply clone the repository and run the unit test.

 

Shonn Lyga for PoZeCode.

About Shonn Lyga

Obsessed with anything and everything in Software Engineering, Technology and Science
This entry was posted in .NET, Software Concepts and tagged , , , . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s