Skip to content

Commit 9f40bd6

Browse files
authored
Merge pull request #2186 from aspnet/release/2.1
Fix not setting HttpConnection.ConnectionId (#2154)
2 parents 914d32b + 41c8dcf commit 9f40bd6

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/Microsoft.AspNetCore.Http.Connections.Client/HttpConnection.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,19 @@ public override IDuplexPipe Transport
6868
/// <inheritdoc />
6969
public override IFeatureCollection Features { get; } = new FeatureCollection();
7070

71-
/// <inheritdoc />
72-
public override string ConnectionId { get; set; }
71+
/// <summary>
72+
/// Gets or sets the connection ID.
73+
/// </summary>
74+
/// <remarks>
75+
/// The connection ID is set when the <see cref="HttpConnection"/> is started and should not be set by user code.
76+
/// If the connection was created with <see cref="HttpConnectionOptions.SkipNegotiation"/> set to <c>true</c>
77+
/// then the connection ID will be <c>null</c>.
78+
/// </remarks>
79+
public override string ConnectionId
80+
{
81+
get => _connectionId;
82+
set => throw new InvalidOperationException("The ConnectionId is set internally and should not be set by user code.");
83+
}
7384

7485
/// <inheritdoc />
7586
public override IDictionary<object, object> Items { get; set; } = new ConnectionItems();

test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.Negotiate.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,42 @@ await WithConnectionAsync(
8282
Assert.Equal(expectedNegotiate, await negotiateUrlTcs.Task.OrTimeout());
8383
}
8484

85+
[Fact]
86+
public async Task NegotiateReturnedConenctionIdIsSetOnConnection()
87+
{
88+
string connectionId = null;
89+
90+
var testHttpHandler = new TestHttpMessageHandler(autoNegotiate: false);
91+
testHttpHandler.OnNegotiate((request, cancellationToken) => ResponseUtils.CreateResponse(HttpStatusCode.OK,
92+
JsonConvert.SerializeObject(new
93+
{
94+
connectionId = "0rge0d00-0040-0030-0r00-000q00r00e00",
95+
availableTransports = new object[]
96+
{
97+
new
98+
{
99+
transport = "LongPolling",
100+
transferFormats = new[] { "Text" }
101+
},
102+
}
103+
})));
104+
testHttpHandler.OnLongPoll(cancellationToken => ResponseUtils.CreateResponse(HttpStatusCode.NoContent));
105+
testHttpHandler.OnLongPollDelete((token) => ResponseUtils.CreateResponse(HttpStatusCode.Accepted));
106+
107+
using (var noErrorScope = new VerifyNoErrorsScope())
108+
{
109+
await WithConnectionAsync(
110+
CreateConnection(testHttpHandler, loggerFactory: noErrorScope.LoggerFactory),
111+
async (connection) =>
112+
{
113+
await connection.StartAsync(TransferFormat.Text).OrTimeout();
114+
connectionId = connection.ConnectionId;
115+
});
116+
}
117+
118+
Assert.Equal("0rge0d00-0040-0030-0r00-000q00r00e00", connectionId);
119+
}
120+
85121
[Fact]
86122
public async Task NegotiateThatReturnsUrlGetFollowed()
87123
{

test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public void CannotCreateConnectionWithNullUrlOnOptions()
4040
Assert.Equal("httpConnectionOptions", exception.ParamName);
4141
}
4242

43+
[Fact]
44+
public void CannotSetConnectionId()
45+
{
46+
var connection = new HttpConnection(new Uri("http://fakeuri.org/"));
47+
var exception = Assert.Throws<InvalidOperationException>(() => connection.ConnectionId = "custom conneciton ID");
48+
Assert.Equal("The ConnectionId is set internally and should not be set by user code.", exception.Message);
49+
}
50+
4351
[Fact]
4452
public async Task HttpOptionsSetOntoHttpClientHandler()
4553
{

0 commit comments

Comments
 (0)