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?

8
  • 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 Commented Oct 19, 2016 at 10:51
  • You don't need to use IIS or other hosting process. You can mock everything including HTTP Context. Commented Oct 19, 2016 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. Commented Oct 19, 2016 at 10:54
  • @dawidr I'm going perform end-to-end tests - from HttpRequest to HttpResponse. I do not mean unit tests here. Commented Oct 19, 2016 at 10:56
  • 1
    Are you using .NET Core? If yes then take a look here: docs.asp.net/en/latest/testing/integration-testing.html Commented Oct 19, 2016 at 10:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.