title: ASP.NET Core 3.1 - signalR integration tests - webhost fixture with auth
body: <p>I write simple test for my signalR app. I create fixture for web host and start connection to hub. Unfortunately in OnConnected hub method I read Context.User.Identity.Name which is null when I start test. How to configure authentication for my webhost fixture? I'd like to test scenario where few users are connected to hub. In Production mode user first sign in using /Account/LogIn controller method, then auth cookie is created and send with every request.</p>
  <pre><code>public class AppFixture
      {
          public const string BaseUrl = &quot;http://localhost:54321&quot;;
  
          static AppFixture()
          {
              var webhost = WebHost
                  .CreateDefaultBuilder(null)
                  .UseEnvironment(&quot;Testing&quot;)
                  .UseStartup&lt;Startup&gt;()
                  .UseUrls(BaseUrl)
                  .Build();
  
             
              webhost.Start();
          }
  
          public string GetCompleteServerUrl(string route)
          {
              route = route?.TrimStart('/', '\\');
              return $&quot;{BaseUrl}/{route}&quot;;
          }
      }
  
  
  
  
    public class DataHubTest
      {
          private readonly ITestOutputHelper output;
          public DataHubTest( ITestOutputHelper output)
          {
              this.output = output;
          }
  
  
          private static async Task&lt;HubConnection&gt; StartConnectionAsync(string hubUrl)
          {
              HubConnection connection = new HubConnectionBuilder()
                 .WithUrl(hubUrl)
                 .Build();
              
              
              await connection.StartAsync();
              return connection;
          }
  
          [Fact]
          public async Task HubConnectionEchoTestAsync()
          {
              // Arrange
              var fixture = new AppFixture();
              var url = fixture.GetCompleteServerUrl(&quot;/dataHub&quot;);
              var connection = await StartConnectionAsync(url); //TODO: User Auth? =&gt; Context.User.Identity.Name
             
              // using mock handler to verify return type and value
              var handler = new Mock&lt;Action&lt;string&gt;&gt;();
              connection.On(&quot;Echo&quot;, handler.Object);
             
              // Act
              await connection.SendAsync(&quot;echo&quot;, &quot;Hello from testing method&quot;);
             
  
              // Assert
              handler.Verify(x =&gt; x(It.Is&lt;string&gt;(n =&gt; n == &quot;Hello from testing method&quot;)), Times.Once());
  
          }
  
  
  
  
  
     public class DataHub: Hub {
  //... skip rest of hub methods
        public override Task OnConnectedAsync()
              {
                  string name = Context.User.Identity.Name; // Here .Name is null when I run test
      
                  _connections.Add(name, Context.ConnectionId);
                  _logger.LogInformation(&quot;User: {User} has been connected to hub. ConnID: {connectionId}&quot;, name, Context.ConnectionId);
      
                  return base.OnConnectedAsync();
              }
      }
  </code></pre>