Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting cookies to httpClient of Asp.Net Core TestServer

I,m testing ASP.NET Core app with TestServer, and there are controllers that require cookie auth. I've created test server instance like this:

_testServer = new TestServer(new WebHostBuilder()
            .UseEnvironment(CustomEnvironments.Test)
            .UseContentRoot(currentDirectory)                
            .UseStartup<Web.Startup>()
            .UseUrls("http://localhost/"));

ApiClient = _testServer.CreateClient();

and now I have to add auth cookie, but it is ignored by server. If the client could be created directly I could pass HttpClientHandler to constractor and set UseCookies to false, and it works, but I can't access the handler when I get client from test server. Is there a way to add auth cookies to test client?

like image 756
AlexK Avatar asked Feb 09 '18 10:02

AlexK


1 Answers

I've found the solution. TestServer has method CreateRequest(string path), it returns RequestBuilder, which allows to insert cookies to header

like image 183
AlexK Avatar answered Oct 28 '22 01:10

AlexK