title: Owin.TestServer To Test a Middleware that Requires Authentication
body: <p>I have a middleware method that requires <em>context.Authentication.User.Identity.Name</em> to be resolved for proper execution.  However, when writing a unit test these properties are obviously null as no sign-in has occurred.  I am not using Oauth or anything authentication related in this middleware (beyond the obvious name property), as it should be handled elsewhere in another middleware (to promote re-use/flexibility of the component I am developing).  Is there a way to mock/fake this value so I can run my test?  I have tried everything I can think of to fake a sign-on and I am just stuck at this point.  To be clear the middleware needs the value not a webapi call or the like.</p>
  
  <pre><code>//Arrange
  var resolver = A.Fake&lt;IDependencyResolver&gt;();
  A.CallTo(() =&gt; resolver.GetService(typeof(ISomeService))).Returns(new TestService());
  
  using (var server = TestServer.Create(app =&gt;
  {
      app.UseMyMiddleware(new MyMiddlewareOptions()
      {
          DependencyResolver = resolver
      });
  
      app.Run(async ctx =&gt;
      {
          await ctx.Response.WriteAsync(ctx.Request.Path.Value);
      });
  }))
  {
      //Act
      var response = await server.CreateRequest("/").GetAsync();
  
      //Assert
      A.CallTo(() =&gt; resolver.GetService(typeof(ISomeService)))
                             .MustHaveHappened(Repeated.Exactly.Once);
      Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
      //Etc.
  }
  </code></pre>