3

I'm writing integration tests for my ASP.Net web application, so I want to start it and test on HTTP request/response level.

Because the tests are supposed to run concurrently and with minimal permissions, I do not want to expose them on any real HTTP port.

I read that OWIN is claimed to be an interface between ASP.Net applications and web servers.

I have an idea to use some mock web server object, which uses OWIN to host an ASP.Net application and doesn't expose it at any HTTP ports. Instead of this, the web server object should accept HTTP requests by calling its methods, feed them to the application it hosts and forward the response to the caller.

Are there any existing solutions?

stop-cran
  • 4,229
  • 2
  • 30
  • 47
  • 1
    Are you able to put IIS on one of your own servers, desktop or laptop? If so, then you can host it locally outside of visual studio – Simon Price Oct 19 '16 at 10:51
  • You don't need to use IIS or other hosting process. You can mock everything including HTTP Context. – Dawid Rutkowski Oct 19 '16 at 10:53
  • @SimonPrice yes. Another option is using OWIN self-host. However it causes performance penalties, and become a problem in case of multiple tests run in parallel. – stop-cran Oct 19 '16 at 10:54
  • @dawidr I'm going perform end-to-end tests - from HttpRequest to HttpResponse. I do not mean unit tests here. – stop-cran Oct 19 '16 at 10:56
  • 1
    Are you using .NET Core? If yes then take a look here: https://docs.asp.net/en/latest/testing/integration-testing.html – Dawid Rutkowski Oct 19 '16 at 10:58
  • Are you asking how to write a console app that serves as an HTTP server? – Kevin Le - Khnle Oct 19 '16 at 11:13
  • try using IIS ? or [smarterasp](https://www.smarterasp.net/) they provide free hosting for 6 month – Aamir Oct 19 '16 at 11:14
  • @dawidr how can I register my `ApiController` in `TestServer` or `WebHostBuilder`? Ideally reusing OWIN `Startup` object or at least `WebApiConfig`? – stop-cran Oct 19 '16 at 11:46

2 Answers2

1

If you are using .NET Core then I thing here you will find useful information's: https://docs.asp.net/en/latest/testing/integration-testing.html

Here is an example how you can configure your test server:

public static void Main(string[] args)
{
     var contentRoot = Directory.GetCurrentDirectory();

     var config = new ConfigurationBuilder()
        .SetBasePath(contentRoot)
        .AddJsonFile("hosting.json", optional: true)
        .Build();

    //WebHostBuilder is required to build the server. We are configurion all of the properties on it
    var hostBuilder = new WebHostBuilder()

    //Server
    .UseKestrel()

    //URL's
    .UseUrls("http://localhost:6000")

    //Content root - in this example it will be our current directory
    .UseContentRoot(contentRoot)

    //Web root - by the default it's wwwroot but here is the place where you can change it
    //.UseWebRoot("wwwroot")

    //Startup
    .UseStartup<Startup>()

    //Environment
    .UseEnvironment("Development")

    //Configuration - here we are reading host settings form configuration, we can put some of the server
    //setting into hosting.json file and read them from there
    .UseConfiguration(config);

   //Build the host
   var host = hostBuilder.Build();

   //Let's start listening for requests
   host.Run();
}

As you can see you can re-use existing Startup.cs class.

Dawid Rutkowski
  • 2,658
  • 1
  • 29
  • 36
  • Got this exception: "A public method named 'ConfigureProduction' or 'Configure' could not be found in the 'FxPro.WebTrader.Middleware.Web.Startup' type". Apparently `UseStartup` works with `Microsoft.AspNetCore.Builder.IApplicationBuilder` rather than `Owin.IAppBuilder`. – stop-cran Oct 19 '16 at 12:32
  • My project is in ASP.Net MVC, so this approach cannot be used. Anyway, you helped me to find a suitable option (see my answer), thanks a lot! – stop-cran Oct 21 '16 at 08:54
0

Thanks to dawidr I found a similar way for those who uses ASP.Net MVC/Web API - Microsoft.Owin.Testing:

using (var server = TestServer.Create<Startup>())
    server.HttpClient.GetAsync("api/ControllerName")
        .Result.EnsureSuccessStatusCode();

Going this way, one can employ (and test) Owin's Startup object, used in real hosting scenario.

Community
  • 1
  • 1
stop-cran
  • 4,229
  • 2
  • 30
  • 47