Skip to content

Commit

Permalink
Fix proxy not applied to serverless transient mode (#1708)
Browse files Browse the repository at this point in the history
Fix #1700
  • Loading branch information
Y-Sindo authored Nov 7, 2022
1 parent 5826b42 commit b2670fe
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ private static IServiceCollection TrySetProductInfo(this IServiceCollection serv
}

private static IServiceCollection AddRestClientFactory(this IServiceCollection services) => services
.AddHttpClient()
.AddHttpClient(Options.DefaultName)
.ConfigurePrimaryHttpMessageHandler(sp => new HttpClientHandler() { Proxy = sp.GetRequiredService<IOptions<ServiceManagerOptions>>().Value.Proxy }).Services
.AddSingleton(sp =>
{
var options = sp.GetRequiredService<IOptions<ServiceManagerOptions>>().Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.SignalR.Tests.Common;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Xunit;
Expand All @@ -33,7 +40,7 @@ public DependencyInjectionExtensionFacts(ITestOutputHelper outputHelper)
public async Task FileConfigHotReloadTest()
{
// to avoid possible file name conflict with another FileConfigHotReloadTest
string configPath = nameof(DependencyInjectionExtensionFacts);
var configPath = nameof(DependencyInjectionExtensionFacts);
var originUrl = "http://origin.url";
var newUrl = "http://new.url";
var configObj = new
Expand All @@ -47,7 +54,7 @@ public async Task FileConfigHotReloadTest()
}
};
File.WriteAllText(configPath, JsonConvert.SerializeObject(configObj));
ServiceCollection services = new ServiceCollection();
var services = new ServiceCollection();
services.AddSignalRServiceManager();
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().AddJsonFile(configPath, false, true).Build());
using var provider = services.BuildServiceProvider();
Expand Down Expand Up @@ -170,7 +177,7 @@ public void ConnectionStringNull_TransientMode_Throw()
public async Task MultiServiceEndpoints_NotAppliedToTransientModeAsync()
{
// to avoid possible file name conflict with another FileConfigHotReloadTest
string configPath = nameof(MultiServiceEndpoints_NotAppliedToTransientModeAsync);
var configPath = nameof(MultiServiceEndpoints_NotAppliedToTransientModeAsync);
var connStr = FakeEndpointUtils.GetFakeConnectionString(1).Single();
var configObj = new
{
Expand Down Expand Up @@ -208,5 +215,38 @@ public async Task MultiServiceEndpoints_NotAppliedToTransientModeAsync()
await Task.Delay(5000);
Assert.Equal(connStr, optionsMonitor.CurrentValue.ConnectionString);// as new config don't pass validation, it is not reloaded
}

[Fact]
public async Task ProxyApplyToTransientModeTestAsync()
{
var requestUrls = new Queue<string>();

//create a simple proxy server
var appBuilder = WebApplication.CreateBuilder();
appBuilder.Services.AddLogging(b => b.AddXunit(_outputHelper));
using var app = appBuilder.Build();
//randomly choose a free port, listen to all interfaces
app.Urls.Add("http://[::1]:0");
app.Run(async context =>
{
requestUrls.Enqueue(context.Request.Path);
await context.Response.WriteAsync("");
});
await app.StartAsync();

var serviceManager = new ServiceManagerBuilder().WithOptions(o =>
{
// use http schema to avoid SSL handshake
o.ConnectionString = "Endpoint=http://abc;AccessKey=nOu3jXsHnsO5urMumc87M9skQbUWuQ+PE5IvSUEic8w=;Version=1.0;";
o.Proxy = new WebProxy(app.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses.First());
}).BuildServiceManager();
Assert.True(await serviceManager.IsServiceHealthy(default));
Assert.Equal("/api/v1/health", requestUrls.Dequeue());

using var hubContext = await serviceManager.CreateHubContextAsync("hub", default);
Assert.True(await hubContext.ClientManager.UserExistsAsync("userId"));
Assert.Equal("/api/hubs/hub/users/userId", requestUrls.Dequeue());
await app.StopAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public async Task TestRestHealthCheckServiceWithEndpointFromHealthyToUnhealthy()
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadGateway))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadGateway))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadGateway));

var checkInterval = TimeSpan.FromSeconds(3);
var retryInterval = TimeSpan.FromSeconds(0.5);
using var _ = StartLog(out var loggerFactory);
var services = new ServiceCollection()
.AddHttpClient(Options.DefaultName).ConfigurePrimaryHttpMessageHandler(() => handlerMock.Object).Services
.AddSignalRServiceManager()
.AddHttpClient(Options.DefaultName).ConfigurePrimaryHttpMessageHandler(() => handlerMock.Object).Services
.Configure<HealthCheckOption>(o =>
{
o.CheckInterval = checkInterval;
Expand All @@ -84,7 +84,7 @@ public async Task TestRestHealthCheckServiceWithEndpointFromHealthyToUnhealthy()
.CreateHubContextAsync(HubName, default);

var endpoint = (serviceHubContext as ServiceHubContextImpl).ServiceProvider.GetRequiredService<IServiceEndpointManager>().GetEndpoints(HubName).First();

//The first health check is OK
Assert.True(endpoint.Online);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Azure.Core.Serialization;
using Microsoft.AspNetCore.SignalR;
Expand All @@ -26,7 +27,7 @@ public static IEnumerable<object[]> IgnoreNullObjectSerializers
{
get
{
yield return new object[] { new JsonObjectSerializer(new() { IgnoreNullValues = true }) };
yield return new object[] { new JsonObjectSerializer(new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }) };
yield return new object[] { new NewtonsoftJsonObjectSerializer(new() { NullValueHandling = NullValueHandling.Ignore }) };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ void assertion(HttpRequestMessage request, CancellationToken t)
{
Assert.EndsWith($"/groups/{groupName}/connections/{connectionId}?api-version=2022-06-01", request.RequestUri.AbsoluteUri);
}
var services = new ServiceCollection().AddHttpClient(Options.DefaultName)
.ConfigurePrimaryHttpMessageHandler(() => new TestRootHandler(assertion)).Services
.AddSignalRServiceManager();
var services = new ServiceCollection()
.AddSignalRServiceManager()
.AddHttpClient(Options.DefaultName).ConfigurePrimaryHttpMessageHandler(() => new TestRootHandler(assertion)).Services;
await using var hubContext = await Create(ServiceTransportType.Transient, services);

await hubContext.Groups.AddToGroupAsync(connectionId, groupName);
Expand Down Expand Up @@ -156,9 +156,9 @@ void assertion(HttpRequestMessage request, CancellationToken t)
{
Assert.EndsWith($"/users/{userId}/groups/{groupName}?api-version=2022-06-01", request.RequestUri.AbsoluteUri);
}
var services = new ServiceCollection().AddHttpClient(Options.DefaultName)
.ConfigurePrimaryHttpMessageHandler(() => new TestRootHandler(assertion)).Services
.AddSignalRServiceManager();
var services = new ServiceCollection()
.AddSignalRServiceManager()
.AddHttpClient(Options.DefaultName).ConfigurePrimaryHttpMessageHandler(() => new TestRootHandler(assertion)).Services;
await using var hubContext = await Create(ServiceTransportType.Transient, services);

await hubContext.UserGroups.AddToGroupAsync(userId, groupName);
Expand Down Expand Up @@ -198,9 +198,9 @@ void assertion(HttpRequestMessage request, CancellationToken t)
Assert.EndsWith($"/connections/{connectionId}?api-version=2022-06-01", request.RequestUri.AbsoluteUri);
Assert.Equal(HttpMethod.Delete, request.Method);
}
var services = new ServiceCollection().AddHttpClient(Options.DefaultName)
.ConfigurePrimaryHttpMessageHandler(() => new TestRootHandler(assertion)).Services
.AddSignalRServiceManager();
var services = new ServiceCollection()
.AddSignalRServiceManager()
.AddHttpClient(Options.DefaultName).ConfigurePrimaryHttpMessageHandler(() => new TestRootHandler(assertion)).Services;
await using var hubContext = await Create(ServiceTransportType.Transient, services);

await hubContext.ClientManager.CloseConnectionAsync(connectionId);
Expand Down

0 comments on commit b2670fe

Please sign in to comment.