More WebSockets testing (#100)

This commit is contained in:
Mikael Mengistu 2017-01-12 15:59:53 -08:00 committed by GitHub
parent 172a20c4ec
commit 3bba46d4d3
4 changed files with 147 additions and 2 deletions

View File

@ -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());
}
}
}

View File

@ -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<ServerFixture>
{
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<byte>(bytes), WebSocketMessageType.Binary, true, CancellationToken.None);
var buffer = new ArraySegment<byte>(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);
}
}
}
}

View File

@ -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<EchoEndPoint>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSockets(options => options.MapEndpoint<EchoEndPoint>("/echo"));
}
}
private void StartServer()
{
host = new WebHostBuilder()
.UseKestrel()
.UseUrls(BaseUrl)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
Task.Run(() => host.Start());
Console.WriteLine("Starting test server...");
lifetime = host.Services.GetRequiredService<IApplicationLifetime>();
lifetime.ApplicationStarted.WaitHandle.WaitOne();
}
public void Dispose()
{
host.Dispose();
}
}
}

View File

@ -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": {}