Home > Enterprise >  C# ASP.NET, testing websocket with Microsoft.AspNetCore.Mvc.Testing
C# ASP.NET, testing websocket with Microsoft.AspNetCore.Mvc.Testing

Time:10-14

I am currently writing an integration test for a websocket connection. I want to test a ClientWebSocket connection against a server instantiated by Microsoft.AspNetCore.Mvc.Testing. This does not seem to work. Does anybody have an idea how to get this to work?

My setup is as follows:

  • I have an API application, which offers some normal http endpoints and one websocket
  • I have some application code, which establishes a websocket connection
  • I have integration tests, which instantiate the API application using Microsoft.AspNetCore.Mvc.Testing and use my application code against it

I general am following https://learn.microsoft.com/de-de/aspnet/core/test/integration-tests?view=aspnetcore-6.0 . This works great for normal HTTP(S) endpoints. My Code looks like this:

_application = new WebApplicationFactory<Program>().WithWebHostBuilder(builder => { });
//HttpClient for http endpoints
_client = _application.CreateClient();

The WebSocket endpoint is created as described here: https://learn.microsoft.com/de-de/aspnet/core/fundamentals/websockets?view=aspnetcore-6.0

app.Use(async (context, next) =>
{
   if (context.Request.Path == "/" nameof(TestModel.WebsocketEvent))
   {
       if (context.WebSockets.IsWebSocketRequest)
       {
            TestModel.Instance.WebsocketEvent = await context.WebSockets.AcceptWebSocketAsync();
            await ListenForClose(TestModel.Instance.WebsocketEvent);
       }
   }
 }

For the connection to the endpoint I am using ClientWebsocket in the code I want to test.

 var ws = new ClientWebSocket();
 await ws.ConnectAsync(_href, cancellation);

When I run my API application manually and execute my code against that instance, everything works as expected. The WebSocket connection is established.

When I try to run it with Microsoft.AspNetCore.Mvc.Testing I get an exception that the server refused the connection.

To some degree it makes sense to me, since the ClientWebSocket is not using anything generated by the WebApplicationFactory.(e.g. _application.Server.CreateWebSocketClient. On the other Hand I do want to use ClientWebSocket in my code and not inject the WebSocketClient of Microsoft.AspNetCore.Mvc.Testing into my production code. In Contrast to http where I actually get a regular HttpClient which I am fine with to inject.

Does anybody have an idea, how I can make the integration test working with ClientWebSocket? Is there the possiblity to reuse the server Microsoft.AspNetCore.Mvc.Testing starts for requests other than what the WebApplicationFactory generates? Is Microsoft.AspNetCore.Mvc.Testing running the API application at all as a server? Or is there some black magic in the background?

What I checked so far:

  • _href is correct
  • _application.Server.BaseAddress does not seem to have an impact

CodePudding user response:

What I want to do is simply not possible. The answer of the following thread pushed me in the right direction. Inject HttpClient from WebApplicationFactory

This matches the official documentation if you know what you are looking for:https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0

Hence I can only access the instance of my Web API application using what WebApplicationFactory offers me. I can't access the instance of the Web API application over my local network.

Thanks to everybody taking the time to read and think about my question.

  • Related