mercredi 23 septembre 2015

Access Cookies inside unit test in AspNet.TestHost.TestServer context on ASP.NET 5 / MVC 6

There is no easy way to get an access to a CookieContainer in response object running integration tests with AspNet.TestHost.TestServer. Cookies have to be set by the controller action. What is the best way to achieve that?

            var client = TestServer.Create(app =>
            {
                app.UseMvc(routes => 
                       routes.MapRoute("default", "{controller}/{action}/{id?}"));
                app.UseIdentity();
            }).CreateClient();

            var request = new HttpRequestMessage(HttpMethod.Get, "account/login");
            var response = await client.SendAsync(request);

            // how to get an access to cookie container ?????????
            // response.Cookies prop doesn't exist
            Assert.NotEmpty(response.Cookies["auth"]);

Solution that I see is to extend instance of the TestServer, return instance of a class CustomClientHandler : ClientHandler and override the whole process of sending a request in that handler, but it needs literally to change all logic except relatively small code of the TestServer.

Any better suggestion how to implement an access to Cookies in a response?

Aucun commentaire:

Enregistrer un commentaire