A:初版
This commit is contained in:
1 parent
237cbd8788
commit
ab3721912d
102 files changed
+7929
-20
No files matched your search
@@ -0,0 +1,49 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user