|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -using System; |
5 |
| -using System.IO; |
| 4 | +using System.Diagnostics; |
6 | 5 | using System.IO.Pipelines;
|
| 6 | +using System.Net; |
7 | 7 | using System.Net.Http;
|
8 | 8 | using System.Net.Http.Headers;
|
9 | 9 | using System.Text;
|
10 |
| -using System.Threading; |
11 |
| -using System.Threading.Tasks; |
12 | 10 | using Microsoft.AspNetCore.Connections;
|
13 | 11 | using Microsoft.AspNetCore.Http.Connections.Client.Internal;
|
14 | 12 | using Microsoft.AspNetCore.Internal;
|
15 |
| -using Microsoft.AspNetCore.SignalR.Tests; |
16 | 13 | using Microsoft.AspNetCore.InternalTesting;
|
| 14 | +using Microsoft.AspNetCore.SignalR.Tests; |
17 | 15 | using Microsoft.Extensions.Logging.Testing;
|
18 | 16 | using Moq;
|
19 | 17 | using Moq.Protected;
|
20 |
| -using Xunit; |
21 |
| -using System.Net; |
22 | 18 |
|
23 | 19 | namespace Microsoft.AspNetCore.SignalR.Client.Tests;
|
24 | 20 |
|
@@ -121,7 +117,7 @@ await sseTransport.StartAsync(
|
121 | 117 | }
|
122 | 118 |
|
123 | 119 | [Fact]
|
124 |
| - public async Task SSETransportStopsWithErrorIfServerSendsIncompleteResults() |
| 120 | + public async Task SSETransportStopIfServerSendsIncompleteResults() |
125 | 121 | {
|
126 | 122 | var mockHttpHandler = new Mock<HttpMessageHandler>();
|
127 | 123 | var calls = 0;
|
@@ -156,11 +152,9 @@ public async Task SSETransportStopsWithErrorIfServerSendsIncompleteResults()
|
156 | 152 | await sseTransport.StartAsync(
|
157 | 153 | new Uri("http://fakeuri.org"), TransferFormat.Text).DefaultTimeout();
|
158 | 154 |
|
159 |
| - var exception = await Assert.ThrowsAsync<FormatException>(() => sseTransport.Input.ReadAllAsync()); |
| 155 | + await sseTransport.Input.ReadAllAsync().DefaultTimeout(); |
160 | 156 |
|
161 | 157 | await sseTransport.Running.DefaultTimeout();
|
162 |
| - |
163 |
| - Assert.Equal("Incomplete message.", exception.Message); |
164 | 158 | }
|
165 | 159 | }
|
166 | 160 |
|
@@ -444,4 +438,81 @@ public async Task StartAsyncSetsCorrectAcceptHeaderForSSE()
|
444 | 438 | Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
445 | 439 | }
|
446 | 440 | }
|
| 441 | + |
| 442 | + [Theory] |
| 443 | + [InlineData(new[] { "\r\n" }, "")] |
| 444 | + [InlineData(new[] { "\r\n:\r\n" }, "")] |
| 445 | + [InlineData(new[] { "\r\n:comment\r\n" }, "")] |
| 446 | + [InlineData(new[] { "data: \r\r\n\n" }, "")] |
| 447 | + [InlineData(new[] { ":comment\r\ndata: \r\r\n\r\n" }, "")] |
| 448 | + [InlineData(new[] { "data: A\rB\r\n\r\n" }, "A")] |
| 449 | + [InlineData(new[] { "data: Hello, World\r\n\r\n" }, "Hello, World")] |
| 450 | + [InlineData(new[] { "data: Hello, World\r\n\r\ndata: " }, "Hello, World")] |
| 451 | + [InlineData(new[] { "data: Hello, World\r\n\r\n:comment\r\ndata: " }, "Hello, World")] |
| 452 | + [InlineData(new[] { "data: Hello, World\r\n\r\n:comment" }, "Hello, World")] |
| 453 | + [InlineData(new[] { "data: Hello, World\r\n\r\n:comment\r\n" }, "Hello, World")] |
| 454 | + [InlineData(new[] { "data: Hello, World\r\n:comment\r\n\r\n" }, "Hello, World")] |
| 455 | + [InlineData(new[] { "data: SGVsbG8sIFdvcmxk\r\n\r\n" }, "SGVsbG8sIFdvcmxk")] |
| 456 | + [InlineData(new[] { "d", "ata: Hello, World\r\n\r\n" }, "Hello, World")] |
| 457 | + [InlineData(new[] { "da", "ta: Hello, World\r\n\r\n" }, "Hello, World")] |
| 458 | + [InlineData(new[] { "dat", "a: Hello, World\r\n\r\n" }, "Hello, World")] |
| 459 | + [InlineData(new[] { "data", ": Hello, World\r\n\r\n" }, "Hello, World")] |
| 460 | + [InlineData(new[] { "data:", " Hello, World\r\n\r\n" }, "Hello, World")] |
| 461 | + [InlineData(new[] { "data: Hello, World", "\r\n\r\n" }, "Hello, World")] |
| 462 | + [InlineData(new[] { "data: Hello, World\r\n", "\r\n" }, "Hello, World")] |
| 463 | + [InlineData(new[] { "data: ", "Hello, World\r\n\r\n" }, "Hello, World")] |
| 464 | + [InlineData(new[] { "data: ", "Hello, World\n\n" }, "Hello, World")] |
| 465 | + [InlineData(new[] { "data: ", "Hello, World\r\n\n" }, "Hello, World")] |
| 466 | + [InlineData(new[] { ":", "comment", "\r\n", "d", "ata: Hello, World\r\n\r\n" }, "Hello, World")] |
| 467 | + [InlineData(new[] { ":comment", "\r\n", "data: Hello, World", "\r\n\r\n" }, "Hello, World")] |
| 468 | + [InlineData(new[] { "data: Hello, World\r\n", ":comment\r\n", "\r\n" }, "Hello, World")] |
| 469 | + [InlineData(new[] { "data: Hello \r\n", "data: World\r\n\r\n" }, "Hello \nWorld")] |
| 470 | + public async Task CanProcessMessagesSuccessfully(string[] messageParts, string expectedMessage) |
| 471 | + { |
| 472 | + var mockHttpHandler = new Mock<HttpMessageHandler>(); |
| 473 | + mockHttpHandler.Protected() |
| 474 | + .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) |
| 475 | + .Returns<HttpRequestMessage, CancellationToken>(async (request, cancellationToken) => |
| 476 | + { |
| 477 | + await Task.Yield(); |
| 478 | + return new HttpResponseMessage { Content = new StreamContent(new OneAtATimeStream(messageParts)) }; |
| 479 | + }); |
| 480 | + |
| 481 | + using (var httpClient = new HttpClient(mockHttpHandler.Object)) |
| 482 | + using (StartVerifiableLog()) |
| 483 | + { |
| 484 | + var sseTransport = new ServerSentEventsTransport(httpClient, loggerFactory: LoggerFactory); |
| 485 | + |
| 486 | + await sseTransport.StartAsync( |
| 487 | + new Uri("http://fakeuri.org"), TransferFormat.Text).DefaultTimeout(); |
| 488 | + |
| 489 | + var message = await sseTransport.Input.ReadAllAsync().DefaultTimeout(); |
| 490 | + Assert.Equal(expectedMessage, Encoding.ASCII.GetString(message)); |
| 491 | + |
| 492 | + await sseTransport.Running.DefaultTimeout(); |
| 493 | + } |
| 494 | + } |
| 495 | + |
| 496 | + public sealed class OneAtATimeStream : MemoryStream |
| 497 | + { |
| 498 | + private readonly string[] _contents; |
| 499 | + private int _index; |
| 500 | + |
| 501 | + public OneAtATimeStream(string[] contents) |
| 502 | + { |
| 503 | + _contents = contents; |
| 504 | + } |
| 505 | + |
| 506 | + public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| 507 | + { |
| 508 | + if (_index == _contents.Length) |
| 509 | + { |
| 510 | + return new(0); |
| 511 | + } |
| 512 | + |
| 513 | + Debug.Assert(buffer.Length > _contents[_index].Length); |
| 514 | + |
| 515 | + return new(Encoding.UTF8.GetBytes(_contents[_index++], buffer.Span)); |
| 516 | + } |
| 517 | + } |
447 | 518 | }
|
0 commit comments