title: Testing TestHost.WebSocketClient with ClientWebSocket .net core
body: <p>I have build a WebSocket web application in .net core by following <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets" rel="nofollow noreferrer">this</a> and <a href="https://code.msdn.microsoft.com/How-to-use-websocket-with-2c6d8b6e" rel="nofollow noreferrer">this</a> or <a href="https://radu-matei.com/blog/aspnet-core-websockets-middleware/" rel="nofollow noreferrer">this</a> tutorials. Now I am trying to write some <strong>integration</strong> tests on this by using Microsoft.AspNetCore.TestHost</p>
  
  <p>The way I create the WebSocket</p>
  
  <pre><code>public static WebSocketClient StartWebSocketServer(string url)
  {
      var baseAddress = $"http://{url}";
      var builder =
          WebHost.CreateDefaultBuilder(new string[]{})
              .UseEnvironment("Development")
              .ConfigureLogging((ctx, logging) =&gt; logging.AddConsole())
              .UseUrls(baseAddress)
              .UseStartup&lt;SocketsTestStartup&gt;();
  
      var server = new TestServer(builder);
      return server.CreateWebSocketClient();
  }
  </code></pre>
  
  <p>And websocket client creation is done in the code below</p>
  
  <pre><code>ClientWebSocket testAppClient = new ClientWebSocket();
  await
      testAppClient 
      .ConnectAsync(
          new Uri("ws://loclahost:52015/local"),
          CancellationToken.None);
  </code></pre>
  
  <p><code>local</code> is the path name on the websocket server</p>
  
  <blockquote>
    <p>Startup.cs</p>
  </blockquote>
  
  <pre><code>app.MapWebSocketManager(
      "/local",
      serviceProvider.GetService&lt;LocalServiceSocketHandler&gt;());
  </code></pre>
  
  <p>Anyway, the connection produce the following error</p>
  
  <pre><code>System.Net.WebSockets.WebSocketException : Unable to connect to the remote server
  </code></pre>
  
  <p>My question is how can I ask <code>ClientWebSocket</code> to connect to <code>TestHost.WebSocketClient</code>? </p>