Skip to content

Commit d6e17fa

Browse files
committed
Remove reference to HttpEndPoint from HttpConnection
1 parent f755768 commit d6e17fa

File tree

11 files changed

+93
-50
lines changed

11 files changed

+93
-50
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,26 @@ public HttpConnectionFactory(IHubProtocol hubProtocol, IOptions<HttpConnectionOp
6161
/// </returns>
6262
public async ValueTask<ConnectionContext> ConnectAsync(EndPoint httpEndPoint, CancellationToken cancellationToken = default)
6363
{
64-
var castedHttpEndPoint = httpEndPoint as HttpEndPoint;
65-
6664
if (httpEndPoint == null)
6765
{
6866
throw new ArgumentNullException(nameof(httpEndPoint));
6967
}
7068

69+
var castedHttpEndPoint = httpEndPoint as HttpEndPoint;
7170
if (castedHttpEndPoint == null)
7271
{
7372
throw new NotSupportedException($"The provided {nameof(EndPoint)} must be of type {nameof(HttpEndPoint)}.");
7473
}
7574

76-
var connection = new HttpConnection(castedHttpEndPoint, _httpConnectionOptions, _transferFormat, _loggerFactory);
75+
if (_httpConnectionOptions.Url != null && _httpConnectionOptions.Url != castedHttpEndPoint.Url)
76+
{
77+
throw new InvalidOperationException($"If {nameof(HttpConnectionOptions)}.{nameof(HttpConnectionOptions.Url)} was set, it must match the {nameof(HttpEndPoint)}.{nameof(HttpEndPoint.Url)} passed to {nameof(ConnectAsync)}.");
78+
}
79+
80+
var shallowCopiedOptions = ShallowCopyHttpConnectionOptions(_httpConnectionOptions);
81+
shallowCopiedOptions.Url ??= castedHttpEndPoint.Url;
82+
83+
var connection = new HttpConnection(shallowCopiedOptions, _transferFormat, _loggerFactory);
7784

7885
try
7986
{
@@ -87,5 +94,26 @@ public async ValueTask<ConnectionContext> ConnectAsync(EndPoint httpEndPoint, Ca
8794
throw;
8895
}
8996
}
97+
98+
// Internal for testing
99+
internal HttpConnectionOptions ShallowCopyHttpConnectionOptions(HttpConnectionOptions options)
100+
{
101+
return new HttpConnectionOptions
102+
{
103+
HttpMessageHandlerFactory = options.HttpMessageHandlerFactory,
104+
Headers = options.Headers,
105+
ClientCertificates = options.ClientCertificates,
106+
Cookies = options.Cookies,
107+
Url = options.Url,
108+
Transports = options.Transports,
109+
SkipNegotiation = options.SkipNegotiation,
110+
AccessTokenProvider = options.AccessTokenProvider,
111+
CloseTimeout = options.CloseTimeout,
112+
Credentials = options.Credentials,
113+
Proxy = options.Proxy,
114+
UseDefaultCredentials = options.UseDefaultCredentials,
115+
WebSocketConfiguration = options.WebSocketConfiguration,
116+
};
117+
}
90118
}
91119
}

src/SignalR/clients/csharp/Client/test/FunctionalTests/HubConnectionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private Func<EndPoint, ValueTask<ConnectionContext>> GetHttpConnectionFactory(st
7373
{
7474
var httpEndpoint = (HttpEndPoint)endPoint;
7575
var options = new HttpConnectionOptions { Url = httpEndpoint.Url, Transports = transportType };
76-
var connection = new HttpConnection(httpEndpoint, options, transferFormat, loggerFactory);
76+
var connection = new HttpConnection(options, transferFormat, loggerFactory);
7777

7878
await connection.StartAsync();
7979

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net;
23
using System.Net.Http;
34
using System.Threading.Tasks;
45
using Microsoft.AspNetCore.Connections;
@@ -32,5 +33,36 @@ public async Task ConnectionIsDisposedIfItFailsToStartAsync()
3233
// We care that the handler (and by extension the client) was disposed
3334
Assert.True(testHandler.Disposed);
3435
}
36+
37+
[Fact]
38+
public async Task DoesNotSupportNonHttpEndPoints()
39+
{
40+
var factory = new HttpConnectionFactory(
41+
Mock.Of<IHubProtocol>(p => p.TransferFormat == TransferFormat.Text),
42+
Options.Create(new HttpConnectionOptions()),
43+
NullLoggerFactory.Instance);
44+
45+
var ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await factory.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 0)));
46+
47+
Assert.Equal("The provided EndPoint must be of type HttpEndPoint.", ex.Message);
48+
}
49+
50+
[Fact]
51+
public async Task OptionsUrlMustMatchEndPointIfSet()
52+
{
53+
var url1 = new Uri("http://example.com/1");
54+
var url2 = new Uri("http://example.com/2");
55+
56+
var factory = new HttpConnectionFactory(
57+
Mock.Of<IHubProtocol>(p => p.TransferFormat == TransferFormat.Text),
58+
Options.Create(new HttpConnectionOptions
59+
{
60+
Url = url1
61+
}),
62+
NullLoggerFactory.Instance);
63+
64+
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () => await factory.ConnectAsync(new HttpEndPoint(url2)));
65+
Assert.Equal("If HttpConnectionOptions.Url was set, it must match the HttpEndPoint.Url passed to ConnectAsync.", ex.Message);
66+
}
3567
}
3668
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void BuildCanOnlyBeCalledOnce()
6363
{
6464
var builder = new HubConnectionBuilder();
6565
builder.Services.AddSingleton<IConnectionFactory>(new HttpConnectionFactory(Mock.Of<IHubProtocol>(), Options.Create(new HttpConnectionOptions()), NullLoggerFactory.Instance));
66-
builder.WithUrl("https://www.example.com");
66+
builder.WithUrl("http://example.com");
6767

6868
Assert.NotNull(builder.Build());
6969

src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.ConnectionLifecycle.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ ValueTask<ConnectionContext> ConnectionFactory(EndPoint endPoint)
8484
createCount += 1;
8585
return new TestConnection().StartAsync();
8686
}
87-
var builder = new HubConnectionBuilder().WithUrl("https://www.example.com");
87+
var builder = new HubConnectionBuilder().WithUrl("http://example.com");
8888
var delegateConnectionFactory = new DelegateConnectionFactory(ConnectionFactory);
8989
builder.Services.AddSingleton<IConnectionFactory>(delegateConnectionFactory);
9090

@@ -117,7 +117,7 @@ ValueTask<ConnectionContext> ConnectionFactory(EndPoint endPoint)
117117
return new TestConnection(onDispose: createCount == 1 ? onDisposeForFirstConnection : null).StartAsync();
118118
}
119119

120-
var builder = new HubConnectionBuilder().WithUrl("https://www.example.com");
120+
var builder = new HubConnectionBuilder().WithUrl("http://example.com");
121121
var delegateConnectionFactory = new DelegateConnectionFactory(ConnectionFactory);
122122
builder.Services.AddSingleton<IConnectionFactory>(delegateConnectionFactory);
123123

@@ -589,7 +589,7 @@ bool ExpectedErrors(WriteContext writeContext)
589589
[Fact]
590590
public async Task HubConnectionClosesWithErrorIfTerminatedWithPartialMessage()
591591
{
592-
var builder = new HubConnectionBuilder().WithUrl("https://www.example.com");
592+
var builder = new HubConnectionBuilder().WithUrl("http://example.com");
593593
var innerConnection = new TestConnection();
594594

595595
var delegateConnectionFactory = new DelegateConnectionFactory(

src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.Helpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public partial class HubConnectionTests
1313
{
1414
private static HubConnection CreateHubConnection(TestConnection connection, IHubProtocol protocol = null, ILoggerFactory loggerFactory = null)
1515
{
16-
var builder = new HubConnectionBuilder().WithUrl("https://www.example.com");
16+
var builder = new HubConnectionBuilder().WithUrl("http://example.com");
1717

1818
var delegateConnectionFactory = new DelegateConnectionFactory(
1919
endPoint => connection.StartAsync());

src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.Reconnect.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ bool ExpectedErrors(WriteContext writeContext)
7878

7979
using (StartVerifiableLog(ExpectedErrors))
8080
{
81-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
81+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
8282
var testConnectionFactory = default(ReconnectingConnectionFactory);
8383
var startCallCount = 0;
8484
var originalConnectionId = "originalConnectionId";
@@ -192,7 +192,7 @@ bool ExpectedErrors(WriteContext writeContext)
192192

193193
using (StartVerifiableLog(ExpectedErrors))
194194
{
195-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
195+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
196196
var startCallCount = 0;
197197

198198
Task OnTestConnectionStart()
@@ -284,7 +284,7 @@ bool ExpectedErrors(WriteContext writeContext)
284284

285285
using (StartVerifiableLog(ExpectedErrors))
286286
{
287-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
287+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
288288
var testConnectionFactory = new ReconnectingConnectionFactory();
289289
builder.Services.AddSingleton<IConnectionFactory>(testConnectionFactory);
290290

@@ -379,7 +379,7 @@ bool ExpectedErrors(WriteContext writeContext)
379379

380380
using (StartVerifiableLog(ExpectedErrors))
381381
{
382-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
382+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
383383
var testConnectionFactory = new ReconnectingConnectionFactory();
384384
builder.Services.AddSingleton<IConnectionFactory>(testConnectionFactory);
385385

@@ -434,7 +434,7 @@ bool ExpectedErrors(WriteContext writeContext)
434434

435435
using (StartVerifiableLog(ExpectedErrors))
436436
{
437-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
437+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
438438
var testConnectionFactory = new ReconnectingConnectionFactory(() => new TestConnection(autoHandshake: false));
439439
builder.Services.AddSingleton<IConnectionFactory>(testConnectionFactory);
440440

@@ -492,7 +492,7 @@ bool ExpectedErrors(WriteContext writeContext)
492492

493493
using (StartVerifiableLog(ExpectedErrors))
494494
{
495-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
495+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
496496
var testConnectionFactory = new ReconnectingConnectionFactory(() => new TestConnection(autoHandshake: false));
497497
builder.Services.AddSingleton<IConnectionFactory>(testConnectionFactory);
498498

@@ -599,7 +599,7 @@ bool ExpectedErrors(WriteContext writeContext)
599599

600600
using (StartVerifiableLog(ExpectedErrors))
601601
{
602-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
602+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
603603
var testConnectionFactory = new ReconnectingConnectionFactory(() => new TestConnection(autoHandshake: false));
604604
builder.Services.AddSingleton<IConnectionFactory>(testConnectionFactory);
605605

@@ -718,7 +718,7 @@ bool ExpectedErrors(WriteContext writeContext)
718718

719719
using (StartVerifiableLog(ExpectedErrors))
720720
{
721-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
721+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
722722
var connectionStartTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
723723

724724
async Task OnTestConnectionStart()
@@ -809,7 +809,7 @@ bool ExpectedErrors(WriteContext writeContext)
809809

810810
using (StartVerifiableLog(ExpectedErrors))
811811
{
812-
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("https://www.example.com");
812+
var builder = new HubConnectionBuilder().WithLoggerFactory(LoggerFactory).WithUrl("http://example.com");
813813
var testConnectionFactory = new ReconnectingConnectionFactory();
814814
builder.Services.AddSingleton<IConnectionFactory>(testConnectionFactory);
815815

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public async Task SendAsyncThrowsIfSerializingMessageFails()
5959
[Fact]
6060
public async Task ClosedEventRaisedWhenTheClientIsStopped()
6161
{
62-
var builder = new HubConnectionBuilder().WithUrl("https://www.example.com");
62+
var builder = new HubConnectionBuilder().WithUrl("http://example.com");
6363

6464
var delegateConnectionFactory = new DelegateConnectionFactory(
6565
endPoint => new TestConnection().StartAsync());

src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public partial class HttpConnection : ConnectionContext, IConnectionInherentKeep
3737
private bool _disposed;
3838
private bool _hasInherentKeepAlive;
3939

40-
private readonly HttpEndPoint _httpEndPoint;
4140
private readonly HttpClient _httpClient;
4241
private readonly HttpConnectionOptions _httpConnectionOptions;
4342
private readonly TransferFormat _transferFormat;
@@ -130,11 +129,17 @@ private static HttpConnectionOptions CreateHttpOptions(Uri url, HttpTransportTyp
130129
/// <param name="httpConnectionOptions">The connection options to use.</param>
131130
/// <param name="loggerFactory">The logger factory.</param>
132131
public HttpConnection(HttpConnectionOptions httpConnectionOptions, ILoggerFactory loggerFactory)
133-
: this(ValidateHttpConnnectionOptionAndCreateHttpEndpoint(httpConnectionOptions), httpConnectionOptions, TransferFormat.Binary, loggerFactory)
132+
: this(httpConnectionOptions, TransferFormat.Binary, loggerFactory)
134133
{
135134
}
136135

137-
private static HttpEndPoint ValidateHttpConnnectionOptionAndCreateHttpEndpoint(HttpConnectionOptions httpConnectionOptions)
136+
/// <summary>
137+
/// Initializes a new instance of the <see cref="HttpConnection"/> class.
138+
/// </summary>
139+
/// <param name="httpConnectionOptions">The connection options to use.</param>
140+
/// <param name="transferFormat">The transfer format the connection should use.</param>
141+
/// <param name="loggerFactory">The logger factory.</param>
142+
public HttpConnection(HttpConnectionOptions httpConnectionOptions, TransferFormat transferFormat, ILoggerFactory loggerFactory)
138143
{
139144
if (httpConnectionOptions == null)
140145
{
@@ -146,30 +151,12 @@ private static HttpEndPoint ValidateHttpConnnectionOptionAndCreateHttpEndpoint(H
146151
throw new ArgumentException("Options does not have a URL specified.", nameof(httpConnectionOptions));
147152
}
148153

149-
return new HttpEndPoint(httpConnectionOptions.Url);
150-
}
151-
152-
/// <summary>
153-
/// Initializes a new instance of the <see cref="HttpConnection"/> class.
154-
/// </summary>
155-
/// <param name="httpEndPoint">The <see cref="HttpEndPoint"/> to connect to. This overrides <see cref="HttpConnectionOptions.Url"/>.</param>
156-
/// <param name="httpConnectionOptions">The connection options to use.</param>
157-
/// <param name="transferFormat">The transfer format the connection should use.</param>
158-
/// <param name="loggerFactory">The logger factory.</param>
159-
public HttpConnection(HttpEndPoint httpEndPoint, HttpConnectionOptions httpConnectionOptions, TransferFormat transferFormat, ILoggerFactory loggerFactory)
160-
{
161154
_loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
162155

163156
_logger = _loggerFactory.CreateLogger<HttpConnection>();
164-
_httpEndPoint = httpEndPoint ?? throw new ArgumentNullException(nameof(httpEndPoint));
165157
_httpConnectionOptions = httpConnectionOptions ?? throw new ArgumentNullException(nameof(httpConnectionOptions));
166158
_transferFormat = transferFormat;
167159

168-
if (httpConnectionOptions.Url != null && httpConnectionOptions.Url != httpEndPoint.Url)
169-
{
170-
throw new InvalidOperationException($"If {nameof(HttpConnectionOptions)}.{nameof(HttpConnectionOptions.Url)} is set, it must match {nameof(HttpEndPoint)}.{nameof(httpEndPoint.Url)}.");
171-
}
172-
173160
if (!httpConnectionOptions.SkipNegotiation || httpConnectionOptions.Transports != HttpTransportType.WebSockets)
174161
{
175162
_httpClient = CreateHttpClient();
@@ -183,7 +170,7 @@ public HttpConnection(HttpEndPoint httpEndPoint, HttpConnectionOptions httpConne
183170

184171
// Used by unit tests
185172
internal HttpConnection(HttpConnectionOptions httpConnectionOptions, TransferFormat transferFormat, ILoggerFactory loggerFactory, ITransportFactory transportFactory)
186-
: this(new HttpEndPoint(httpConnectionOptions.Url), httpConnectionOptions, transferFormat, loggerFactory)
173+
: this(httpConnectionOptions, transferFormat, loggerFactory)
187174
{
188175
// Don't null out the _transportFactory if one isn't provided.
189176
if (transportFactory != null)
@@ -299,7 +286,7 @@ private async Task DisposeAsyncCore()
299286

300287
private async Task SelectAndStartTransport(TransferFormat transferFormat, CancellationToken cancellationToken)
301288
{
302-
var uri = _httpEndPoint.Url;
289+
var uri = _httpConnectionOptions.Url;
303290
// Set the initial access token provider back to the original one from options
304291
_accessTokenProvider = _httpConnectionOptions.AccessTokenProvider;
305292

src/SignalR/samples/ClientSample/RawSample.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public static async Task<int> ExecuteAsync(string baseUrl)
3535

3636
Console.WriteLine($"Connecting to {baseUrl}...");
3737

38-
var uri = new Uri(baseUrl);
39-
var connection = new HttpConnection(new HttpEndPoint(uri), new HttpConnectionOptions(), TransferFormat.Text, loggerFactory: null);
38+
var connection = new HttpConnection(new HttpConnectionOptions() { Url = new Uri(baseUrl) }, TransferFormat.Text, loggerFactory: null);
4039

4140
try
4241
{

0 commit comments

Comments
 (0)