Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration tests custom HTTP client in .NET Core

I'm trying to figure out whether it's possible to use custom HTTP client in integration tests?

Instead of getting client instance from TestServer

Client = Server.CreateClient() 

to use my own implementation. Smthng like:

Client = new DSClient(Server.BaseAddress.AbsoluteUri, Credential);

When I do so i'm constantly getting 404. WAIDW?

like image 254
Andrii Horda Avatar asked Oct 22 '25 01:10

Andrii Horda


1 Answers

I faced the same problem and in my situation I can not use the existing HttpClient because I need to modify the HttpClient when initializing it. So my solution is to create a server and test it using my own HttpClient.

using(var server = WebHost.CreateDefaultBuilder(new List<string>().ToArray())
    .UseEnvironment("Test")
    .UseStartup<Startup>()
    .UseKestrel(options =>
    {
        options.Listen(IPAddress.Loopback, 5000);
    })
.Build())
{
    server.Start();
    var client = new HttpClient
    {
        BaseAddress = new Uri("http://localhost:5000")
    }
    var resp = client.GetAsync("test/testme").Result;
}
like image 68
Onur Destanoğlu Avatar answered Oct 24 '25 15:10

Onur Destanoğlu