44 lines
1.7 KiB
C#
44 lines
1.7 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NetClient.Abstractions;
|
|
using NetClient.DependencyInjection;
|
|
|
|
namespace NetClient.Test.Tests;
|
|
|
|
[TestClass]
|
|
public class NamedClientFactoryTests
|
|
{
|
|
[TestMethod]
|
|
public async Task Factory_Creates_Named_Clients_With_Different_Configs()
|
|
{
|
|
var services = new ServiceCollection();
|
|
|
|
services.AddNamedNetClient("catalog", opt => { opt.BaseAddress = new Uri("https://catalog.test"); })
|
|
.ConfigurePrimaryHttpMessageHandler(() => new TestDoubles.TestHttpMessageHandler(req =>
|
|
{
|
|
StringAssert.StartsWith(req.RequestUri!.AbsoluteUri, "https://catalog.test");
|
|
return Task.FromResult(TestDoubles.TestHttpMessageHandler.Text(HttpStatusCode.OK, "catalog"));
|
|
}));
|
|
|
|
services.AddNamedNetClient("order", opt => { opt.BaseAddress = new Uri("https://order.test"); })
|
|
.ConfigurePrimaryHttpMessageHandler(() => new TestDoubles.TestHttpMessageHandler(req =>
|
|
{
|
|
StringAssert.StartsWith(req.RequestUri!.AbsoluteUri, "https://order.test");
|
|
return Task.FromResult(TestDoubles.TestHttpMessageHandler.Text(HttpStatusCode.OK, "order"));
|
|
}));
|
|
|
|
var sp = services.BuildServiceProvider();
|
|
var factory = sp.GetRequiredService<INetClientFactory>();
|
|
|
|
var c = factory.Create("catalog");
|
|
var o = factory.Create("order");
|
|
|
|
var cRes = await c.GetStringAsync("/ping");
|
|
var oRes = await o.GetStringAsync("/ping");
|
|
|
|
Assert.AreEqual("catalog", cRes);
|
|
Assert.AreEqual("order", oRes);
|
|
}
|
|
} |