22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
44using System ;
5+ using System . Collections . Generic ;
56using System . IO ;
67using System . Linq ;
8+ using System . Net ;
79using System . Reflection ;
810using System . Threading . Tasks ;
11+ using Microsoft . AspNetCore . Builder ;
12+ using Microsoft . AspNetCore . Hosting . Server ;
13+ using Microsoft . AspNetCore . Hosting . Server . Features ;
14+ using Microsoft . AspNetCore . Http ;
915using Microsoft . Azure . SignalR . Tests . Common ;
1016using Microsoft . Extensions . Configuration ;
1117using Microsoft . Extensions . DependencyInjection ;
18+ using Microsoft . Extensions . Logging ;
1219using Microsoft . Extensions . Options ;
1320using Newtonsoft . Json ;
1421using Xunit ;
@@ -33,7 +40,7 @@ public DependencyInjectionExtensionFacts(ITestOutputHelper outputHelper)
3340 public async Task FileConfigHotReloadTest ( )
3441 {
3542 // to avoid possible file name conflict with another FileConfigHotReloadTest
36- string configPath = nameof ( DependencyInjectionExtensionFacts ) ;
43+ var configPath = nameof ( DependencyInjectionExtensionFacts ) ;
3744 var originUrl = "http://origin.url" ;
3845 var newUrl = "http://new.url" ;
3946 var configObj = new
@@ -47,7 +54,7 @@ public async Task FileConfigHotReloadTest()
4754 }
4855 } ;
4956 File . WriteAllText ( configPath , JsonConvert . SerializeObject ( configObj ) ) ;
50- ServiceCollection services = new ServiceCollection ( ) ;
57+ var services = new ServiceCollection ( ) ;
5158 services . AddSignalRServiceManager ( ) ;
5259 services . AddSingleton < IConfiguration > ( new ConfigurationBuilder ( ) . AddJsonFile ( configPath , false , true ) . Build ( ) ) ;
5360 using var provider = services . BuildServiceProvider ( ) ;
@@ -170,7 +177,7 @@ public void ConnectionStringNull_TransientMode_Throw()
170177 public async Task MultiServiceEndpoints_NotAppliedToTransientModeAsync ( )
171178 {
172179 // to avoid possible file name conflict with another FileConfigHotReloadTest
173- string configPath = nameof ( MultiServiceEndpoints_NotAppliedToTransientModeAsync ) ;
180+ var configPath = nameof ( MultiServiceEndpoints_NotAppliedToTransientModeAsync ) ;
174181 var connStr = FakeEndpointUtils . GetFakeConnectionString ( 1 ) . Single ( ) ;
175182 var configObj = new
176183 {
@@ -208,5 +215,38 @@ public async Task MultiServiceEndpoints_NotAppliedToTransientModeAsync()
208215 await Task . Delay ( 5000 ) ;
209216 Assert . Equal ( connStr , optionsMonitor . CurrentValue . ConnectionString ) ; // as new config don't pass validation, it is not reloaded
210217 }
218+
219+ [ Fact ]
220+ public async Task ProxyApplyToTransientModeTestAsync ( )
221+ {
222+ var requestUrls = new Queue < string > ( ) ;
223+
224+ //create a simple proxy server
225+ var appBuilder = WebApplication . CreateBuilder ( ) ;
226+ appBuilder . Services . AddLogging ( b => b . AddXunit ( _outputHelper ) ) ;
227+ using var app = appBuilder . Build ( ) ;
228+ //randomly choose a free port, listen to all interfaces
229+ app . Urls . Add ( "http://[::1]:0" ) ;
230+ app . Run ( async context =>
231+ {
232+ requestUrls . Enqueue ( context . Request . Path ) ;
233+ await context . Response . WriteAsync ( "" ) ;
234+ } ) ;
235+ await app . StartAsync ( ) ;
236+
237+ var serviceManager = new ServiceManagerBuilder ( ) . WithOptions ( o =>
238+ {
239+ // use http schema to avoid SSL handshake
240+ o . ConnectionString = "Endpoint=http://abc;AccessKey=nOu3jXsHnsO5urMumc87M9skQbUWuQ+PE5IvSUEic8w=;Version=1.0;" ;
241+ o . Proxy = new WebProxy ( app . Services . GetRequiredService < IServer > ( ) . Features . Get < IServerAddressesFeature > ( ) . Addresses . First ( ) ) ;
242+ } ) . BuildServiceManager ( ) ;
243+ Assert . True ( await serviceManager . IsServiceHealthy ( default ) ) ;
244+ Assert . Equal ( "/api/v1/health" , requestUrls . Dequeue ( ) ) ;
245+
246+ using var hubContext = await serviceManager . CreateHubContextAsync ( "hub" , default ) ;
247+ Assert . True ( await hubContext . ClientManager . UserExistsAsync ( "userId" ) ) ;
248+ Assert . Equal ( "/api/hubs/hub/users/userId" , requestUrls . Dequeue ( ) ) ;
249+ await app . StopAsync ( ) ;
250+ }
211251 }
212252}
0 commit comments