diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/EchoEndPoint.cs b/test/Microsoft.AspNetCore.SignalR.Tests/EchoEndPoint.cs new file mode 100644 index 0000000000..c67d18d8d1 --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Tests/EchoEndPoint.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.IO.Pipelines; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Sockets; + + +namespace Microsoft.AspNetCore.SignalR.Tests +{ + public class EchoEndPoint : EndPoint + { + public async override Task OnConnectedAsync(Connection connection) + { + await connection.Transport.Output.WriteAsync(await connection.Transport.Input.ReadAsync()); + } + } +} diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs new file mode 100644 index 0000000000..e019379f98 --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs @@ -0,0 +1,48 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Net.WebSockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Testing.xunit; +using Xunit; + +namespace Microsoft.AspNetCore.SignalR.Tests +{ + [CollectionDefinition(Name)] + public class EndToEndTestsCollection : ICollectionFixture + { + public const string Name = "EndToEndTests"; + } + + [Collection(EndToEndTestsCollection.Name)] + public class EndToEndTests + { + private readonly ServerFixture _serverFixture; + + public EndToEndTests(ServerFixture serverFixture) + { + _serverFixture = serverFixture; + } + + [Fact] + public async Task WebSocketsTest() + { + const string message = "Hello, World!"; + using (var ws = new ClientWebSocket()) + { + await ws.ConnectAsync(new Uri(_serverFixture.WebSocketsUrl + "/echo/ws"), CancellationToken.None); + var bytes = Encoding.UTF8.GetBytes(message); + await ws.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Binary, true, CancellationToken.None); + var buffer = new ArraySegment(new byte[1024]); + var result = await ws.ReceiveAsync(buffer, CancellationToken.None); + + Assert.Equal(bytes, buffer.Array.Slice(0, message.Length).ToArray()); + + await ws.CloseAsync(WebSocketCloseStatus.Empty, "", CancellationToken.None); + } + } + } +} diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/ServerFixture.cs b/test/Microsoft.AspNetCore.SignalR.Tests/ServerFixture.cs new file mode 100644 index 0000000000..eb088bc657 --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Tests/ServerFixture.cs @@ -0,0 +1,72 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Builder; + +namespace Microsoft.AspNetCore.SignalR.Tests +{ + public class ServerFixture : IDisposable + { + private ILoggerFactory _loggerFactory; + private IWebHost host; + private IApplicationLifetime lifetime; + + public string BaseUrl => "http://localhost:3000"; + + public string WebSocketsUrl => BaseUrl.Replace("http", "ws"); + + public ServerFixture() + { + _loggerFactory = new LoggerFactory(); + + var _verbose = string.Equals(Environment.GetEnvironmentVariable("SIGNALR_TESTS_VERBOSE"), "1"); + if (_verbose) + { + _loggerFactory.AddConsole(); + } + StartServer(); + } + + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddSockets(); + services.AddSignalR(); + services.AddSingleton(); + } + + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + app.UseSockets(options => options.MapEndpoint("/echo")); + } + } + + private void StartServer() + { + host = new WebHostBuilder() + .UseKestrel() + .UseUrls(BaseUrl) + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .Build(); + + Task.Run(() => host.Start()); + Console.WriteLine("Starting test server..."); + lifetime = host.Services.GetRequiredService(); + lifetime.ApplicationStarted.WaitHandle.WaitOne(); + } + + public void Dispose() + { + host.Dispose(); + } + } + +} diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/project.json b/test/Microsoft.AspNetCore.SignalR.Tests/project.json index d7c4dcf30c..03f7faa33d 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/project.json +++ b/test/Microsoft.AspNetCore.SignalR.Tests/project.json @@ -15,7 +15,13 @@ "type": "build" }, "Moq": "4.6.36-*", - "xunit": "2.2.0-*" + "xunit": "2.2.0-*", + "Microsoft.AspNetCore.Http.Abstractions": "1.2.0-*", + "Microsoft.Extensions.DependencyInjection.Abstractions": "1.2.0-*", + "Microsoft.AspNetCore.Hosting.Abstractions": "1.2.0-*", + "Microsoft.AspNetCore.Server.Kestrel": "1.2.0-*", + "Microsoft.Extensions.Logging.Console": "1.2.0-*", + "Microsoft.AspNetCore.Server.IntegrationTesting": "0.3.0-*" }, "frameworks": { "netcoreapp1.1": { @@ -23,7 +29,8 @@ "Microsoft.NETCore.App": { "version": "1.2.0-*", "type": "platform" - } + }, + "System.Net.WebSockets.Client": "4.4.0-*" } }, "net46": {}