1

I have a class library which is included some DelegationHandlers, It is simple, get the request, add some headers based on request content and pass the request down.

So what I needed is write unit tests for my library. I'm using .NET Core 2.1 and xunit, I was wondering if there is a way to mock a web server, then I can send a request to this web server using my library and check the result of my request? Any Idea how could I mock a web server (app)? or how could I test my library by sending a http request?

Saeid
  • 13,224
  • 32
  • 107
  • 173
  • Have you tried to use [`TestServer`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost.testserver?view=aspnetcore-3.0) for that? This [article](https://visualstudiomagazine.com/articles/2017/07/01/testserver.aspx) might be helpful – Pavel Anikhouski Mar 11 '20 at 14:38
  • @PavelAnikhouski As I know in TestServer you already have a web app or api and then you will start the app with TestServer for testing. In my situation all I have is a class library how could I start a web api which does not exists with TestServer? – Saeid Mar 11 '20 at 14:43
  • @Saeid I think the idea is you make a Test.WebApp project alongside, you could then use the WebApplicationFactory and link it to that Startup class in order to have a WebApi that exists. I appreciate it isn't ideal – JEV Mar 11 '20 at 16:03
  • @JonE Thanks for comment, I thought about that, but I'm wondering how so nobody think about this scenario, either I missed something here or probably there is already a solution. – Saeid Mar 11 '20 at 17:32

3 Answers3

1

I found a solution here, I share it maybe useful for others.

public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, RequestDelegate del)
{
    _builder = new WebHostBuilder()
        .UseUrls(baseUrl)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .Configure(app =>
        {
            app.UsePathBase(basePath);
            app.Run(del);
        })
        .Build();

    _builder.Start();
}

The same WebHostBuilder we used in Integration Tests, now we could pass RequestDelegate to run the app:

GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
    _downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;

    if (_downstreamPath != basePath)
    {
        context.Response.StatusCode = statusCode;
        await context.Response.WriteAsync("downstream path didn't match base path");
    }
    else
    {
        context.Response.StatusCode = statusCode;
        await context.Response.WriteAsync(responseBody);
    }
});
Saeid
  • 13,224
  • 32
  • 107
  • 173
0

LightHTTP is an open-source library I've created that sets up a mock HTTP server.

  • It can be used in testing and mocking, or other scenarios where a lightweight HTTP server is preferred.
  • It works asynchronously.
  • Supports simultaneous connections.
  • It can serve anyway you'd need, since it's based on HttpListener.
Javid
  • 2,755
  • 2
  • 33
  • 60
-2

If you are writing unit tests, you should not need to fire up a test servers. However, to do integration tests using mock controllers then yes you can use the test server from microsoft

https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1