titaniumbunker.com

Evil geniuses and world domination are 2 of our goals... we also like Dr Who

Will the real OWIN please stand up!

no comment
Not OWIN, rather sportswear and beat poetry enthusiast Mr Slim Shady

Not OWIN, rather sportswear and beat poetry enthusiast Mr Slim Shady

I’ve recently become singularly interested in unit testing and continuous integration.  I’ve recently set up a project and I now take great delight in the system automatically building and telling me that my test were no good, and that the project is unstable.  It’s good because it drives you to make sure that your code changes don’t break existing functionality – and that’s only something that can be done if there are repeatable and technically inexpensive tests that can be executed when code is checked in.

This project is a .NET MVC based project – but also has a rather interesting REST based interface, allowing potential integration from any number of clients.  I have tests for the controller, and I can sort of test the REST interface (by calling the controller in a Nunit test) – however I recently started thinking that I don’t really have a way to test that REST interface from a Javascript client perspective in the same way that I would expect to test it from a controller perspective.

I suppose what I wanted to do was to start a web service process when I start my testing, and tear it down at the end of testing.  The point here is that testing can occur before deployment, protecting the environment from errors or changes in functionality that now break unit tests.

Cassini might be a possibility, and another option might be OWIN.

OWIN (Open Web Interface for .NET) – and in particular Microsoft.Owin.Testing.TestServer allows a web server to be instantiated in code.  This means that a unit test can create an instance of a web application and then perform an activity against that executing web site.

As part of my experiments, I wrote some tests for a project I am working on – the App-Utility-Store.

Web Sservices

I wrote a simple Values Controller API which creates a simple REST interface.  My idea was to test this using a simple rest client.  To accomplish this I wrote a simple Test class which firstly stands up an OWIN server, then performs a call against that server.

 

        [Test]
        public void testRestCall()
        {
            const int port = 8086;
            using (WebApp.Start<APIHost>("http://localhost:"+port))
            {
                var client = new HttpClient { BaseAddress = new Uri("http://localhost:" + port )};
                var response = client.GetAsync("/api/Values").Result;
                var body = response.Content.ReadAsStringAsync().Result;
                
            }
        }

 

So what is this code doing?

This code creates a web server running on port 8086, and it uses a class called APIHost to configure it.

    public class APIHost
    {
        
        public void Configuration(IAppBuilder app)
        {
            // Configure Web API for self-host. 
            var config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { controller="API", id = RouteParameter.Optional }
            );

            app.UseWebApi(config);
        }
    }

 

Once the server is up we create a HTTPClient, and perform a GET request to the Values Controller.  Debugging the API controller confirmed that the website was indeed firing.  Adding OWIN testing to a project can be done through NuGet’s Microsoft.Owin.Testing package.

Comments are closed.



Categories

Archives

Tags