-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathDiscoveryWebApplicationBuilderExtensionsTest.cs
More file actions
112 lines (87 loc) · 4.76 KB
/
Copy pathDiscoveryWebApplicationBuilderExtensionsTest.cs
File metadata and controls
112 lines (87 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Runtime.InteropServices;
using FluentAssertions.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.ServiceDiscovery.Http;
using RichardSzalay.MockHttp;
using Steeltoe.Common.Discovery;
using Steeltoe.Common.Http.HttpClientPooling;
using Steeltoe.Common.TestResources;
using Steeltoe.Discovery.Consul;
using Steeltoe.Discovery.Eureka;
namespace Steeltoe.Discovery.HttpClients.Test;
public sealed class DiscoveryWebApplicationBuilderExtensionsTest
{
private static readonly Dictionary<string, string?> EurekaSettings = new()
{
["eureka:client:enabled"] = "true",
["eureka:client:shouldRegisterWithEureka"] = "true",
["eureka:client:eurekaServer:connectTimeoutSeconds"] = "1",
["eureka:client:eurekaServer:retryCount"] = "0"
};
private static readonly Dictionary<string, string?> ConsulSettings = new()
{
["consul:discovery:serviceName"] = "test-host",
["consul:discovery:enabled"] = "true",
["consul:discovery:failFast"] = "false",
["consul:discovery:register"] = "false"
};
[Fact]
public async Task AddEurekaDiscoveryClient_WebApplicationBuilder_AddsServiceDiscovery_Eureka()
{
WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
builder.Configuration.AddInMemoryCollection(EurekaSettings);
builder.Services.AddEurekaDiscoveryClient();
await using WebApplication host = builder.Build();
host.Services.GetServices<IDiscoveryClient>().Should().ContainSingle().Which.Should().BeOfType<EurekaDiscoveryClient>();
host.Services.GetServices<IHostedService>().OfType<DiscoveryClientHostedService>().Should().ContainSingle();
}
[Fact]
public async Task AddConsulDiscoveryClient_WebApplicationBuilder_AddsServiceDiscovery_Consul()
{
WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
builder.Configuration.AddInMemoryCollection(ConsulSettings);
builder.Services.AddConsulDiscoveryClient();
await using WebApplication host = builder.Build();
host.Services.GetServices<IDiscoveryClient>().Should().ContainSingle().Which.Should().BeOfType<ConsulDiscoveryClient>();
host.Services.GetServices<IHostedService>().OfType<DiscoveryClientHostedService>().Should().ContainSingle();
}
[FactSkippedOnPlatform(nameof(OSPlatform.OSX))]
public async Task AddEurekaDiscoveryClient_WorksWithGlobalServiceDiscovery()
{
WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
builder.Configuration.AddInMemoryCollection(EurekaSettings);
builder.Services.AddEurekaDiscoveryClient();
builder.Services.ConfigureHttpClientDefaults(action => action.AddServiceDiscovery());
await using WebApplication host = builder.Build();
Task<EurekaDiscoveryClient> resolveTask = Task.Run(() => _ = host.Services.GetServices<IDiscoveryClient>().OfType<EurekaDiscoveryClient>().Single());
Func<Task> action = async () => await resolveTask.WaitAsync(5.Seconds(), TestContext.Current.CancellationToken);
await action.Should().NotThrowAsync();
}
[Fact]
public async Task AddEurekaDiscoveryClient_WorksWithAspireGlobalServiceDiscovery()
{
WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
["Eureka:Client:ShouldFetchRegistry"] = "false",
["Eureka:Client:ShouldRegisterWithEureka"] = "false"
});
builder.Services.AddEurekaDiscoveryClient();
builder.Services.AddTransient<ResolvingHttpDelegatingHandler>();
builder.Services.ConfigureHttpClientDefaults(action => action.AddHttpMessageHandler<ResolvingHttpDelegatingHandler>());
var handler = new DelegateToMockHttpClientHandler();
handler.Mock.Expect(HttpMethod.Get, "http://localhost:8761/eureka/apps").Respond("application/json", "{}");
await using WebApplication host = builder.Build();
host.Services.GetRequiredService<HttpClientHandlerFactory>().Using(handler);
var discoveryClient = host.Services.GetRequiredService<EurekaDiscoveryClient>();
Func<Task> action = async () => await discoveryClient.FetchRegistryAsync(true, TestContext.Current.CancellationToken);
await action.Should().NotThrowAsync();
handler.Mock.VerifyNoOutstandingExpectation();
}
}