Skip to content

Use new System.Net.ServerSentEvents package in SignalR #56206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ and are generated based on the last package release.
<LatestPackageReference Include="System.Net.Http" />
<LatestPackageReference Include="System.Net.Http.Json" />
<LatestPackageReference Include="System.Net.Sockets" />
<LatestPackageReference Include="System.Net.ServerSentEvents" />
<LatestPackageReference Include="System.Private.Uri" />
<LatestPackageReference Include="System.Reflection.Metadata" />
<LatestPackageReference Include="System.Runtime.InteropServices.RuntimeInformation" />
Expand Down
4 changes: 4 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>8fac5af2b11dc98fa0504f6fd06df790164ec958</Sha>
</Dependency>
<Dependency Name="System.Net.ServerSentEvents" Version="9.0.0-preview.6.24307.2">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>8fac5af2b11dc98fa0504f6fd06df790164ec958</Sha>
</Dependency>
<Dependency Name="System.Reflection.Metadata" Version="9.0.0-preview.6.24307.2">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>8fac5af2b11dc98fa0504f6fd06df790164ec958</Sha>
Expand Down
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<SystemIOPipelinesVersion>9.0.0-preview.6.24307.2</SystemIOPipelinesVersion>
<SystemNetHttpJsonVersion>9.0.0-preview.6.24307.2</SystemNetHttpJsonVersion>
<SystemNetHttpWinHttpHandlerVersion>9.0.0-preview.6.24307.2</SystemNetHttpWinHttpHandlerVersion>
<SystemNetServerSentEventsVersion>9.0.0-preview.6.24307.2</SystemNetServerSentEventsVersion>
<SystemReflectionMetadataVersion>9.0.0-preview.6.24307.2</SystemReflectionMetadataVersion>
<SystemResourcesExtensionsVersion>9.0.0-preview.6.24307.2</SystemResourcesExtensionsVersion>
<SystemSecurityCryptographyPkcsVersion>9.0.0-preview.6.24307.2</SystemSecurityCryptographyPkcsVersion>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Diagnostics;
using System.IO.Pipelines;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Connections.Client.Internal;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.SignalR.Tests;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.AspNetCore.SignalR.Tests;
using Microsoft.Extensions.Logging.Testing;
using Moq;
using Moq.Protected;
using Xunit;
using System.Net;

namespace Microsoft.AspNetCore.SignalR.Client.Tests;

Expand Down Expand Up @@ -121,7 +117,7 @@ await sseTransport.StartAsync(
}

[Fact]
public async Task SSETransportStopsWithErrorIfServerSendsIncompleteResults()
public async Task SSETransportStopIfServerSendsIncompleteResults()
{
var mockHttpHandler = new Mock<HttpMessageHandler>();
var calls = 0;
Expand Down Expand Up @@ -156,11 +152,9 @@ public async Task SSETransportStopsWithErrorIfServerSendsIncompleteResults()
await sseTransport.StartAsync(
new Uri("http://fakeuri.org"), TransferFormat.Text).DefaultTimeout();

var exception = await Assert.ThrowsAsync<FormatException>(() => sseTransport.Input.ReadAllAsync());
await sseTransport.Input.ReadAllAsync().DefaultTimeout();

await sseTransport.Running.DefaultTimeout();

Assert.Equal("Incomplete message.", exception.Message);
}
}

Expand Down Expand Up @@ -444,4 +438,81 @@ public async Task StartAsyncSetsCorrectAcceptHeaderForSSE()
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}

[Theory]
[InlineData(new[] { "\r\n" }, "")]
[InlineData(new[] { "\r\n:\r\n" }, "")]
[InlineData(new[] { "\r\n:comment\r\n" }, "")]
[InlineData(new[] { "data: \r\r\n\n" }, "")]
[InlineData(new[] { ":comment\r\ndata: \r\r\n\r\n" }, "")]
[InlineData(new[] { "data: A\rB\r\n\r\n" }, "A")]
[InlineData(new[] { "data: Hello, World\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "data: Hello, World\r\n\r\ndata: " }, "Hello, World")]
[InlineData(new[] { "data: Hello, World\r\n\r\n:comment\r\ndata: " }, "Hello, World")]
[InlineData(new[] { "data: Hello, World\r\n\r\n:comment" }, "Hello, World")]
[InlineData(new[] { "data: Hello, World\r\n\r\n:comment\r\n" }, "Hello, World")]
[InlineData(new[] { "data: Hello, World\r\n:comment\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "data: SGVsbG8sIFdvcmxk\r\n\r\n" }, "SGVsbG8sIFdvcmxk")]
[InlineData(new[] { "d", "ata: Hello, World\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "da", "ta: Hello, World\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "dat", "a: Hello, World\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "data", ": Hello, World\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "data:", " Hello, World\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "data: Hello, World", "\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "data: Hello, World\r\n", "\r\n" }, "Hello, World")]
[InlineData(new[] { "data: ", "Hello, World\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "data: ", "Hello, World\n\n" }, "Hello, World")]
[InlineData(new[] { "data: ", "Hello, World\r\n\n" }, "Hello, World")]
[InlineData(new[] { ":", "comment", "\r\n", "d", "ata: Hello, World\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { ":comment", "\r\n", "data: Hello, World", "\r\n\r\n" }, "Hello, World")]
[InlineData(new[] { "data: Hello, World\r\n", ":comment\r\n", "\r\n" }, "Hello, World")]
[InlineData(new[] { "data: Hello \r\n", "data: World\r\n\r\n" }, "Hello \nWorld")]
public async Task CanProcessMessagesSuccessfully(string[] messageParts, string expectedMessage)
{
var mockHttpHandler = new Mock<HttpMessageHandler>();
mockHttpHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.Returns<HttpRequestMessage, CancellationToken>(async (request, cancellationToken) =>
{
await Task.Yield();
return new HttpResponseMessage { Content = new StreamContent(new OneAtATimeStream(messageParts)) };
});

using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (StartVerifiableLog())
{
var sseTransport = new ServerSentEventsTransport(httpClient, loggerFactory: LoggerFactory);

await sseTransport.StartAsync(
new Uri("http://fakeuri.org"), TransferFormat.Text).DefaultTimeout();

var message = await sseTransport.Input.ReadAllAsync().DefaultTimeout();
Assert.Equal(expectedMessage, Encoding.ASCII.GetString(message));

await sseTransport.Running.DefaultTimeout();
}
}

public sealed class OneAtATimeStream : MemoryStream
{
private readonly string[] _contents;
private int _index;

public OneAtATimeStream(string[] contents)
{
_contents = contents;
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
if (_index == _contents.Length)
{
return new(0);
}

Debug.Assert(buffer.Length > _contents[_index].Length);

return new(Encoding.UTF8.GetBytes(_contents[_index++], buffer.Span));
}
}
}
Loading
Loading