title: Testing OWIN Middleware in ASP.NET WebApi 2: How to set cookies?
body: <p>I'm trying to test an OWIN Middleware component in an ASP.NET WebApi 2 application. The middleware is supposed to look at the cookies of the incoming request, modify some stuff on the request before it's handed to the next component, and potentially set a cookie on the way out as well.</p>
  
  <p>What's tripping me up is that the <code>OwinRequest.Cookies</code> property is of type <a href="https://docs.microsoft.com/en-us/previous-versions/aspnet/dn308198%28v%3dvs.113%29" rel="nofollow noreferrer"><code>RequestCookieCollection</code></a>, which doesn't seem to allow modifying, and the property itself is read-only which means I can't use the <code>RequestCookieCollection(IDictionary&lt;string,string&gt;)</code> constructor to initialize a collection with cookies already in it and set that on the request.</p>
  
  <p>I want do do something like this:</p>
  
  <pre><code>var context = new OwinContext();
  // option 1:
  context.Request.Cookies.Append("cookie-name", "cookie-value");
  // option 2:
  var cookies = new RequestCookieCollection(new Dictionary&lt;string, string&gt;{ { "cookie-name", "cookie-value" } });
  context.Request.Cookies = cookies;
  
  await myMiddleware.Invoke(context);
  
  // Assert stuff about context.Response
  </code></pre>
  
  <p>but that doesn't work for aforementioned reasons.</p>
  
  <p>I had hoped not to have to mock <code>IOwinContext</code> entierly, because it's quite convenient to have it set up with well-functioning <code>Request</code> and <code>Response</code> objects (I need to, among other things, look at <code>Request.User.Identity.IsAuthenticated</code> in my implementation).</p>