Skip to content

Commit 285158b

Browse files
committed
Add ShallowCopyHttpConnectionOptionsCopiesAllPublicProperties
1 parent d6e17fa commit 285158b

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/SignalR/clients/csharp/Client/src/HttpConnectionFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async ValueTask<ConnectionContext> ConnectAsync(EndPoint httpEndPoint, Ca
9696
}
9797

9898
// Internal for testing
99-
internal HttpConnectionOptions ShallowCopyHttpConnectionOptions(HttpConnectionOptions options)
99+
internal static HttpConnectionOptions ShallowCopyHttpConnectionOptions(HttpConnectionOptions options)
100100
{
101101
return new HttpConnectionOptions
102102
{

src/SignalR/clients/csharp/Client/src/Microsoft.AspNetCore.SignalR.Client.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Client for ASP.NET Core SignalR</Description>
@@ -11,4 +11,7 @@
1111
<Reference Include="Microsoft.AspNetCore.Http.Connections.Client" />
1212
</ItemGroup>
1313

14+
<ItemGroup>
15+
<InternalsVisibleTo Include="Microsoft.AspNetCore.SignalR.Client.Tests" />
16+
</ItemGroup>
1417
</Project>

src/SignalR/clients/csharp/Client/test/UnitTests/HttpConnectionFactoryTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
14
using System;
5+
using System.Collections.Generic;
26
using System.Net;
37
using System.Net.Http;
8+
using System.Net.WebSockets;
9+
using System.Reflection;
10+
using System.Security.Cryptography.X509Certificates;
411
using System.Threading.Tasks;
512
using Microsoft.AspNetCore.Connections;
13+
using Microsoft.AspNetCore.Http.Connections;
614
using Microsoft.AspNetCore.Http.Connections.Client;
715
using Microsoft.AspNetCore.SignalR.Protocol;
816
using Microsoft.Extensions.Logging.Abstractions;
@@ -64,5 +72,49 @@ public async Task OptionsUrlMustMatchEndPointIfSet()
6472
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () => await factory.ConnectAsync(new HttpEndPoint(url2)));
6573
Assert.Equal("If HttpConnectionOptions.Url was set, it must match the HttpEndPoint.Url passed to ConnectAsync.", ex.Message);
6674
}
75+
76+
[Fact]
77+
public void ShallowCopyHttpConnectionOptionsCopiesAllPublicProperties()
78+
{
79+
Func<HttpMessageHandler, HttpMessageHandler> handlerFactory = handler => handler;
80+
Func<Task<string>> tokenProvider = () => Task.FromResult("");
81+
Action<ClientWebSocketOptions> webSocketConfig = options => { };
82+
83+
var testValues = new Dictionary<string, object>
84+
{
85+
{ $"{nameof(HttpConnectionOptions.HttpMessageHandlerFactory)}", handlerFactory },
86+
{ $"{nameof(HttpConnectionOptions.Headers)}", new Dictionary<string, string>() },
87+
{ $"{nameof(HttpConnectionOptions.ClientCertificates)}", new X509CertificateCollection() },
88+
{ $"{nameof(HttpConnectionOptions.Cookies)}", new CookieContainer() },
89+
{ $"{nameof(HttpConnectionOptions.Url)}", new Uri("https://example.com") },
90+
{ $"{nameof(HttpConnectionOptions.Transports)}", HttpTransportType.ServerSentEvents },
91+
{ $"{nameof(HttpConnectionOptions.SkipNegotiation)}", true },
92+
{ $"{nameof(HttpConnectionOptions.AccessTokenProvider)}", tokenProvider },
93+
{ $"{nameof(HttpConnectionOptions.CloseTimeout)}", TimeSpan.FromDays(1) },
94+
{ $"{nameof(HttpConnectionOptions.Credentials)}", Mock.Of<ICredentials>() },
95+
{ $"{nameof(HttpConnectionOptions.Proxy)}", Mock.Of<IWebProxy>() },
96+
{ $"{nameof(HttpConnectionOptions.UseDefaultCredentials)}", true },
97+
{ $"{nameof(HttpConnectionOptions.WebSocketConfiguration)}", webSocketConfig },
98+
};
99+
100+
var options = new HttpConnectionOptions();
101+
var properties = typeof(HttpConnectionOptions)
102+
.GetProperties(BindingFlags.Public | BindingFlags.Instance);
103+
104+
foreach (var property in properties)
105+
{
106+
property.SetValue(options, testValues[property.Name]);
107+
}
108+
109+
var shallowCopiedOptions = HttpConnectionFactory.ShallowCopyHttpConnectionOptions(options);
110+
111+
foreach (var property in properties)
112+
{
113+
Assert.Equal(testValues[property.Name], property.GetValue(shallowCopiedOptions));
114+
testValues.Remove(property.Name);
115+
}
116+
117+
Assert.Empty(testValues);
118+
}
67119
}
68120
}

0 commit comments

Comments
 (0)