remove our WebSocket client

we'll use the one from dotnet/corefx now instead

fixes #108
This commit is contained in:
Andrew Stanton-Nurse 2016-08-03 13:48:52 -07:00
parent 45f0fe178f
commit 33e3e5b186
56 changed files with 651 additions and 987 deletions

View File

@ -8,21 +8,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C45106D0-7
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{9E55FC5B-FD9C-4266-AB24-F3AA649D7C8B}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.Protocol", "src\Microsoft.AspNetCore.WebSockets.Protocol\Microsoft.AspNetCore.WebSockets.Protocol.xproj", "{E0C10DEC-3339-4A47-85BC-3100C5D34AD4}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.Client", "src\Microsoft.AspNetCore.WebSockets.Client\Microsoft.AspNetCore.WebSockets.Client.xproj", "{4A1C4875-AE21-4A78-979A-F0E4DF5EB518}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.Client.Test", "test\Microsoft.AspNetCore.WebSockets.Client.Test\Microsoft.AspNetCore.WebSockets.Client.Test.xproj", "{6604D154-817F-4BC5-BE95-FF7E851179D9}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.Server", "src\Microsoft.AspNetCore.WebSockets.Server\Microsoft.AspNetCore.WebSockets.Server.xproj", "{78A097D0-C0A4-4AED-93E2-84A65392FB52}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{86E1ADA2-631C-484F-906C-2D0BCF628E65}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TestClient", "samples\TestClient\TestClient.xproj", "{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestServer", "samples\TestServer\TestServer.csproj", "{4E5F5FCC-172C-44D9-BEA0-39098A79CD0B}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AutobahnTestApp", "test\AutobahnTestApp\AutobahnTestApp.xproj", "{9755F612-A155-4BDD-9E20-37ADE0B4B3BA}"
@ -36,9 +26,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{1959
scripts\UpdateCoreFxCode.ps1 = scripts\UpdateCoreFxCode.ps1
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.Server.ConformanceTest", "test\Microsoft.AspNetCore.WebSockets.Server.ConformanceTest\Microsoft.AspNetCore.WebSockets.Server.ConformanceTest.xproj", "{A722BB6C-9114-4F25-9BB0-2191D4405F3A}"
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets", "src\Microsoft.AspNetCore.WebSockets\Microsoft.AspNetCore.WebSockets.xproj", "{CDE16880-0374-46FA-8896-99F1B90B4B6F}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.Protocol.Test", "test\Microsoft.AspNetCore.WebSockets.Protocol.Test\Microsoft.AspNetCore.WebSockets.Protocol.Test.xproj", "{AAF2DFCF-845E-4410-BBF0-0683AD60DD6A}"
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.Test", "test\Microsoft.AspNetCore.WebSockets.Test\Microsoft.AspNetCore.WebSockets.Test.xproj", "{5AFA74F5-9B1D-4FC5-815F-EF471F5AC1EF}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.ConformanceTest", "test\Microsoft.AspNetCore.WebSockets.ConformanceTest\Microsoft.AspNetCore.WebSockets.ConformanceTest.xproj", "{74F45408-1959-4FEE-9511-25D40F4913FD}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TestClient", "samples\TestClient\TestClient.xproj", "{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -46,26 +40,6 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E0C10DEC-3339-4A47-85BC-3100C5D34AD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0C10DEC-3339-4A47-85BC-3100C5D34AD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0C10DEC-3339-4A47-85BC-3100C5D34AD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0C10DEC-3339-4A47-85BC-3100C5D34AD4}.Release|Any CPU.Build.0 = Release|Any CPU
{4A1C4875-AE21-4A78-979A-F0E4DF5EB518}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A1C4875-AE21-4A78-979A-F0E4DF5EB518}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A1C4875-AE21-4A78-979A-F0E4DF5EB518}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A1C4875-AE21-4A78-979A-F0E4DF5EB518}.Release|Any CPU.Build.0 = Release|Any CPU
{6604D154-817F-4BC5-BE95-FF7E851179D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6604D154-817F-4BC5-BE95-FF7E851179D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6604D154-817F-4BC5-BE95-FF7E851179D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6604D154-817F-4BC5-BE95-FF7E851179D9}.Release|Any CPU.Build.0 = Release|Any CPU
{78A097D0-C0A4-4AED-93E2-84A65392FB52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78A097D0-C0A4-4AED-93E2-84A65392FB52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78A097D0-C0A4-4AED-93E2-84A65392FB52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78A097D0-C0A4-4AED-93E2-84A65392FB52}.Release|Any CPU.Build.0 = Release|Any CPU
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Release|Any CPU.Build.0 = Release|Any CPU
{4E5F5FCC-172C-44D9-BEA0-39098A79CD0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4E5F5FCC-172C-44D9-BEA0-39098A79CD0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E5F5FCC-172C-44D9-BEA0-39098A79CD0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -82,29 +56,34 @@ Global
{B7246F23-6A4B-492F-AB61-292AA1A9E9D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7246F23-6A4B-492F-AB61-292AA1A9E9D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7246F23-6A4B-492F-AB61-292AA1A9E9D5}.Release|Any CPU.Build.0 = Release|Any CPU
{A722BB6C-9114-4F25-9BB0-2191D4405F3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A722BB6C-9114-4F25-9BB0-2191D4405F3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A722BB6C-9114-4F25-9BB0-2191D4405F3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A722BB6C-9114-4F25-9BB0-2191D4405F3A}.Release|Any CPU.Build.0 = Release|Any CPU
{AAF2DFCF-845E-4410-BBF0-0683AD60DD6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAF2DFCF-845E-4410-BBF0-0683AD60DD6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAF2DFCF-845E-4410-BBF0-0683AD60DD6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAF2DFCF-845E-4410-BBF0-0683AD60DD6A}.Release|Any CPU.Build.0 = Release|Any CPU
{CDE16880-0374-46FA-8896-99F1B90B4B6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDE16880-0374-46FA-8896-99F1B90B4B6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDE16880-0374-46FA-8896-99F1B90B4B6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDE16880-0374-46FA-8896-99F1B90B4B6F}.Release|Any CPU.Build.0 = Release|Any CPU
{5AFA74F5-9B1D-4FC5-815F-EF471F5AC1EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5AFA74F5-9B1D-4FC5-815F-EF471F5AC1EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AFA74F5-9B1D-4FC5-815F-EF471F5AC1EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AFA74F5-9B1D-4FC5-815F-EF471F5AC1EF}.Release|Any CPU.Build.0 = Release|Any CPU
{74F45408-1959-4FEE-9511-25D40F4913FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74F45408-1959-4FEE-9511-25D40F4913FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74F45408-1959-4FEE-9511-25D40F4913FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74F45408-1959-4FEE-9511-25D40F4913FD}.Release|Any CPU.Build.0 = Release|Any CPU
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E0C10DEC-3339-4A47-85BC-3100C5D34AD4} = {2C7947A5-9FBD-4267-97C1-2D726D7B3BAF}
{4A1C4875-AE21-4A78-979A-F0E4DF5EB518} = {2C7947A5-9FBD-4267-97C1-2D726D7B3BAF}
{6604D154-817F-4BC5-BE95-FF7E851179D9} = {C45106D0-76C8-4776-A140-F7DD83CA2958}
{78A097D0-C0A4-4AED-93E2-84A65392FB52} = {2C7947A5-9FBD-4267-97C1-2D726D7B3BAF}
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9} = {9E55FC5B-FD9C-4266-AB24-F3AA649D7C8B}
{4E5F5FCC-172C-44D9-BEA0-39098A79CD0B} = {9E55FC5B-FD9C-4266-AB24-F3AA649D7C8B}
{9755F612-A155-4BDD-9E20-37ADE0B4B3BA} = {C45106D0-76C8-4776-A140-F7DD83CA2958}
{72E3AB32-682F-42AF-B7C7-0B777244FF11} = {9E55FC5B-FD9C-4266-AB24-F3AA649D7C8B}
{B7246F23-6A4B-492F-AB61-292AA1A9E9D5} = {9E55FC5B-FD9C-4266-AB24-F3AA649D7C8B}
{A722BB6C-9114-4F25-9BB0-2191D4405F3A} = {C45106D0-76C8-4776-A140-F7DD83CA2958}
{AAF2DFCF-845E-4410-BBF0-0683AD60DD6A} = {C45106D0-76C8-4776-A140-F7DD83CA2958}
{CDE16880-0374-46FA-8896-99F1B90B4B6F} = {2C7947A5-9FBD-4267-97C1-2D726D7B3BAF}
{5AFA74F5-9B1D-4FC5-815F-EF471F5AC1EF} = {C45106D0-76C8-4776-A140-F7DD83CA2958}
{74F45408-1959-4FEE-9511-25D40F4913FD} = {C45106D0-76C8-4776-A140-F7DD83CA2958}
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9} = {9E55FC5B-FD9C-4266-AB24-F3AA649D7C8B}
EndGlobalSection
EndGlobal

View File

@ -41,7 +41,7 @@ namespace TestServer
while (received.MessageType != WebSocketMessageType.Close)
{
// Console.WriteLine("Echo, " + received.Count + ", " + received.MessageType + ", " + received.EndOfMessage);
Console.WriteLine($"Echoing {received.Count} bytes received in a {received.MessageType} message; Fin={received.EndOfMessage}");
// Echo anything we receive
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, received.Count), received.MessageType, received.EndOfMessage, CancellationToken.None);

View File

@ -16,7 +16,7 @@ if(!(Test-Path $CoreFxRepoRoot)) {
}
$CoreFxRepoRoot = Convert-Path $CoreFxRepoRoot
$DestinationRoot = "$RepoRoot\src\Microsoft.AspNetCore.WebSockets.Protocol\ext"
$DestinationRoot = "$RepoRoot\src\Microsoft.AspNetCore.WebSockets\Internal\fx"
$FilesToCopy | foreach {
$Source = Join-Path $CoreFxRepoRoot $_
@ -34,7 +34,15 @@ $FilesToCopy | foreach {
}
Write-Host "Copying $_"
# The trailing blank line is important!
$Pragmas = @"
#pragma warning disable 1572
#pragma warning disable 1573
"@;
$SourceCode = [IO.File]::ReadAllText($Source)
$SourceCode = $Pragmas + $SourceCode
$SourceCode = $SourceCode.Replace("Task.FromException", "CompatHelpers.FromException")
$SourceCode = $SourceCode.Replace("Task.CompletedTask", "CompatHelpers.CompletedTask")
$SourceCode = $SourceCode.Replace("Array.Empty", "CompatHelpers.Empty")

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>4a1c4875-ae21-4a78-979a-f0e4df5eb518</ProjectGuid>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -1,114 +0,0 @@
// 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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.WebSockets.Protocol;
namespace Microsoft.AspNetCore.WebSockets.Client
{
public class WebSocketClient
{
static WebSocketClient()
{
try
{
// Only call once
WebSocket.RegisterPrefixes();
}
catch (Exception)
{
// Already registered
}
}
public WebSocketClient()
{
ReceiveBufferSize = 1024 * 16;
KeepAliveInterval = TimeSpan.FromMinutes(2);
SubProtocols = new List<string>();
}
public IList<string> SubProtocols
{
get;
private set;
}
public TimeSpan KeepAliveInterval
{
get;
set;
}
public int ReceiveBufferSize
{
get;
set;
}
public Action<HttpWebRequest> ConfigureRequest
{
get;
set;
}
public Action<HttpWebResponse> InspectResponse
{
get;
set;
}
public async Task<WebSocket> ConnectAsync(Uri uri, CancellationToken cancellationToken)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
CancellationTokenRegistration cancellation = cancellationToken.Register(() => request.Abort());
request.Headers[Constants.Headers.SecWebSocketVersion] = Constants.Headers.SupportedVersion;
if (SubProtocols.Count > 0)
{
request.Headers[Constants.Headers.SecWebSocketProtocol] = string.Join(", ", SubProtocols);
}
if (ConfigureRequest != null)
{
ConfigureRequest(request);
}
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
cancellation.Dispose();
if (InspectResponse != null)
{
InspectResponse(response);
}
// TODO: Validate handshake
HttpStatusCode statusCode = response.StatusCode;
if (statusCode != HttpStatusCode.SwitchingProtocols)
{
response.Dispose();
throw new InvalidOperationException("Incomplete handshake, invalid status code: " + statusCode);
}
// TODO: Validate Sec-WebSocket-Key/Sec-WebSocket-Accept
string subProtocol = response.Headers[Constants.Headers.SecWebSocketProtocol];
if (!string.IsNullOrEmpty(subProtocol) && !SubProtocols.Contains(subProtocol, StringComparer.OrdinalIgnoreCase))
{
throw new InvalidOperationException("Incomplete handshake, the server specified an unknown sub-protocol: " + subProtocol);
}
Stream stream = response.GetResponseStream();
return CommonWebSocket.CreateClientWebSocket(stream, subProtocol, KeepAliveInterval, ReceiveBufferSize);
}
}
}

View File

@ -1,24 +0,0 @@
{
"version": "0.2.0-*",
"description": "Client for communicating over web sockets.",
"packOptions": {
"repository": {
"type": "git",
"url": "git://github.com/aspnet/websockets"
}
},
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"dependencies": {
"Microsoft.AspNetCore.WebSockets.Protocol": "0.2.0-*"
},
"frameworks": {
"net451": {}
}
}

View File

@ -1,41 +0,0 @@
// 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.
namespace Microsoft.AspNetCore.WebSockets.Protocol
{
public static class Constants
{
public static class Headers
{
public const string Upgrade = "Upgrade";
public const string UpgradeWebSocket = "websocket";
public const string Connection = "Connection";
public const string ConnectionUpgrade = "Upgrade";
public const string SecWebSocketKey = "Sec-WebSocket-Key";
public const string SecWebSocketVersion = "Sec-WebSocket-Version";
public const string SecWebSocketProtocol = "Sec-WebSocket-Protocol";
public const string SecWebSocketAccept = "Sec-WebSocket-Accept";
public const string SupportedVersion = "13";
}
public static class OpCodes
{
public const int ContinuationFrame = 0x0;
public const int TextFrame = 0x1;
public const int BinaryFrame = 0x2;
public const int CloseFrame = 0x8;
public const int PingFrame = 0x9;
public const int PongFrame = 0xA;
internal static readonly int[] ValidOpCodes = new int[]
{
ContinuationFrame,
TextFrame,
BinaryFrame,
CloseFrame,
PingFrame,
PongFrame,
};
}
}
}

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>e0c10dec-3339-4a47-85bc-3100c5d34ad4</ProjectGuid>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -1,17 +0,0 @@
// 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.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("Microsoft.Net.WebSockets")]
[assembly: ComVisible(false)]
[assembly: Guid("9a9e41ae-1494-4d87-a66f-a4019ff68ce5")]
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: NeutralResourcesLanguage("en-us")]
[assembly: AssemblyCompany("Microsoft Corporation.")]
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyProduct("Microsoft ASP.NET Core")]

View File

@ -1,5 +0,0 @@
# External Code
External code copied from CoreFX. Do not modify files in this directory, use the `scripts\UpdateCoreFxCore.ps1` script in the repo root.
This folder structure is designed to exactly mirror the structure in the CoreFX repo (hence the deep nesting).

View File

@ -1,42 +0,0 @@
{
"version": "0.2.0-*",
"description": "Managed web socket protocol parser.",
"packOptions": {
"repository": {
"type": "git",
"url": "git://github.com/aspnet/websockets"
}
},
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591",
"CS1572",
"CS1573"
],
"xmlDoc": true,
"allowUnsafe": true
},
"frameworks": {
"net451": {},
"netstandard1.3": {
"dependencies": {
"System.Diagnostics.Debug": "4.0.11-*",
"System.Diagnostics.Contracts": "4.0.1-*",
"System.Globalization": "4.0.11-*",
"System.IO": "4.1.0-*",
"System.Linq": "4.1.0-*",
"System.Net.WebSockets": "4.0.0-*",
"System.Resources.ResourceManager": "4.0.1-*",
"System.Runtime.InteropServices": "4.1.0-*",
"System.Runtime.Extensions": "4.1.0-*",
"System.Security.Cryptography.Algorithms": "4.2.0-*",
"System.Text.Encoding.Extensions": "4.0.11-*",
"System.Threading": "4.0.11-*",
"System.Threading.Tasks": "4.0.11-*",
"System.Threading.Timer": "4.0.1-*"
}
}
}
}

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>78a097d0-c0a4-4aed-93e2-84a65392fb52</ProjectGuid>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -1,11 +0,0 @@
// 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.Reflection;
using System.Resources;
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: NeutralResourcesLanguage("en-us")]
[assembly: AssemblyCompany("Microsoft Corporation.")]
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyProduct("Microsoft ASP.NET Core")]

View File

@ -4,14 +4,12 @@
using System;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.WebSockets.Server
namespace Microsoft.AspNetCore.WebSockets
{
public class ExtendedWebSocketAcceptContext : WebSocketAcceptContext
{
public override string SubProtocol { get; set; }
public int? ReceiveBufferSize { get; set; }
public TimeSpan? KeepAliveInterval { get; set; }
// public ArraySegment<byte>? Buffer { get; set; } // TODO
}
}

View File

@ -1,7 +1,7 @@
// 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.
namespace Microsoft.AspNetCore.WebSockets.Client
namespace Microsoft.AspNetCore.WebSockets.Internal
{
public static class Constants
{

View File

@ -6,26 +6,20 @@ using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace Microsoft.AspNetCore.WebSockets.Protocol
namespace Microsoft.AspNetCore.WebSockets.Internal
{
public static class HandshakeHelpers
{
/// <summary>
/// Gets request headers needed process the handshake on the server.
/// </summary>
public static IEnumerable<string> NeededHeaders
public static readonly IEnumerable<string> NeededHeaders = new[]
{
get
{
return new[]
{
Constants.Headers.Upgrade,
Constants.Headers.Connection,
Constants.Headers.SecWebSocketKey,
Constants.Headers.SecWebSocketVersion
};
}
}
Constants.Headers.Upgrade,
Constants.Headers.Connection,
Constants.Headers.SecWebSocketKey,
Constants.Headers.SecWebSocketVersion
};
// Verify Method, Upgrade, Connection, version, key, etc..
public static bool CheckSupportedWebSocketRequest(string method, IEnumerable<KeyValuePair<string, string>> headers)
@ -103,20 +97,6 @@ namespace Microsoft.AspNetCore.WebSockets.Protocol
}
}
public static bool IsResponseKeyValid(string value)
{
throw new NotImplementedException();
}
/// <summary>
/// "The value of this header field MUST be a nonce consisting of a randomly selected 16-byte value that has been base64-encoded."
/// </summary>
/// <returns></returns>
public static string CreateRequestKey()
{
throw new NotImplementedException();
}
/// <summary>
/// "...the base64-encoded SHA-1 of the concatenation of the |Sec-WebSocket-Key| (as a string, not base64-decoded) with the string
/// '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'"

View File

@ -5,9 +5,9 @@ using System;
using System.IO;
using System.Net.WebSockets;
namespace Microsoft.AspNetCore.WebSockets.Protocol
namespace Microsoft.AspNetCore.WebSockets.Internal
{
public static class CommonWebSocket
public static class WebSocketFactory
{
public static WebSocket CreateClientWebSocket(Stream stream, string subProtocol, TimeSpan keepAliveInterval, int receiveBufferSize)
{

View File

@ -0,0 +1,5 @@
# Code copied from dotnet/corefx
This is code copied out of dotnet/corefx. **Do not** modify anything under the `src` subdirectory as it is copied by `scripts\UpdateCoreFxCore.ps1` in this repo. The files in the root of this directory are only here to shim out code that the corefx code depends on.
Any changes to code under `src` in this directory MUST be made in dotnet/corefx as well and should mention `Tratcher`, `anurse` and `stephentoub`.

View File

@ -1,3 +1,5 @@
#pragma warning disable 1572
#pragma warning disable 1573
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

View File

@ -1,3 +1,5 @@
#pragma warning disable 1572
#pragma warning disable 1573
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

View File

@ -1,22 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="14.0.25420" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25420</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>aaf2dfcf-845e-4410-bbf0-0683ad60dd6a</ProjectGuid>
<RootNamespace>Microsoft.AspNetCore.WebSockets.Protocol.Test</RootNamespace>
<ProjectGuid>cde16880-0374-46fa-8896-99f1b90b4b6f</ProjectGuid>
<RootNamespace>Microsoft.AspNetCore.WebSockets</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -9,10 +9,10 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.WebSockets.Protocol;
using Microsoft.AspNetCore.WebSockets.Internal;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.WebSockets.Server
namespace Microsoft.AspNetCore.WebSockets
{
public class WebSocketMiddleware
{
@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.WebSockets.Server
}
Stream opaqueTransport = await _upgradeFeature.UpgradeAsync(); // Sets status code to 101
return CommonWebSocket.CreateServerWebSocket(opaqueTransport, subProtocol, keepAliveInterval, receiveBufferSize);
return WebSocketFactory.CreateServerWebSocket(opaqueTransport, subProtocol, keepAliveInterval, receiveBufferSize);
}
}
}

View File

@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.WebSockets.Server;
using Microsoft.AspNetCore.WebSockets;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Builder

View File

@ -6,7 +6,8 @@
"nowarn": [
"CS1591"
],
"xmlDoc": true
"xmlDoc": true,
"allowUnsafe": true
},
"description": "ASP.NET 5 web socket middleware for use on top of opaque servers.",
"packOptions": {
@ -17,13 +18,15 @@
},
"dependencies": {
"Microsoft.AspNetCore.Http.Extensions": "1.1.0-*",
"Microsoft.AspNetCore.WebSockets.Protocol": "0.2.0-*",
"Microsoft.Extensions.Options": "1.1.0-*"
},
"frameworks": {
"net451": {},
"netstandard1.3": {
"dependencies": {}
"dependencies": {
"System.Text.Encoding.Extensions": "4.0.11",
"System.Threading.Timer": "4.0.1"
}
}
}
}

View File

@ -1,6 +1,6 @@
{
"dependencies": {
"Microsoft.AspNetCore.WebSockets.Server": "0.2.0-*",
"Microsoft.AspNetCore.WebSockets": "0.2.0-*",
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"

View File

@ -1,536 +0,0 @@
// 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.WebSockets.Client.Test
{
public class WebSocketClientTests
{
private static string ClientAddress = "ws://localhost:54321/";
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task Connect_Success()
{
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task NegotiateSubProtocol_Success()
{
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
Assert.Equal("alpha, bravo, charlie", context.Request.Headers["Sec-WebSocket-Protocol"]);
var webSocket = await context.WebSockets.AcceptWebSocketAsync("Bravo");
}))
{
var client = new WebSocketClient();
client.SubProtocols.Add("alpha");
client.SubProtocols.Add("bravo");
client.SubProtocols.Add("charlie");
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
Assert.Equal("Bravo", clientSocket.SubProtocol);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task SendEmptyData_Success()
{
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[0];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
var orriginalData = new byte[0];
await clientSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task SendShortData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[orriginalData.Length];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
await clientSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task SendMediumData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes(new string('a', 130));
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[orriginalData.Length];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
await clientSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task SendLongData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes(new string('a', 0x1FFFF));
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[orriginalData.Length];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
int intermediateCount = result.Count;
Assert.False(result.EndOfMessage);
Assert.Equal(WebSocketMessageType.Text, result.MessageType);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer, intermediateCount, orriginalData.Length - intermediateCount), CancellationToken.None);
intermediateCount += result.Count;
Assert.False(result.EndOfMessage);
Assert.Equal(WebSocketMessageType.Text, result.MessageType);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer, intermediateCount, orriginalData.Length - intermediateCount), CancellationToken.None);
intermediateCount += result.Count;
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, intermediateCount);
Assert.Equal(WebSocketMessageType.Text, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
await clientSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task SendFragmentedData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[orriginalData.Length];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(2, result.Count);
int totalReceived = result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
result = await webSocket.ReceiveAsync(
new ArraySegment<byte>(serverBuffer, totalReceived, serverBuffer.Length - totalReceived), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(2, result.Count);
totalReceived += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
result = await webSocket.ReceiveAsync(
new ArraySegment<byte>(serverBuffer, totalReceived, serverBuffer.Length - totalReceived), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(7, result.Count);
totalReceived += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
await clientSocket.SendAsync(new ArraySegment<byte>(orriginalData, 0, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await clientSocket.SendAsync(new ArraySegment<byte>(orriginalData, 2, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await clientSocket.SendAsync(new ArraySegment<byte>(orriginalData, 4, 7), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task ReceiveEmptyData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
var clientBuffer = new byte[0];
var result = await clientSocket.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task ReceiveShortData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
var clientBuffer = new byte[orriginalData.Length];
var result = await clientSocket.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, clientBuffer);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task ReceiveMediumData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes(new string('a', 130));
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
var clientBuffer = new byte[orriginalData.Length];
var result = await clientSocket.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, clientBuffer);
}
}
}
[ConditionalTheory]
[InlineData(1024 * 16)]
[InlineData(0xFFFFFF)]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task ReceiveLongData(int receiveBufferSize)
{
var orriginalData = Encoding.UTF8.GetBytes(new string('a', 0x1FFFF));
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
var client = new WebSocketClient() { ReceiveBufferSize = receiveBufferSize };
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
var clientBuffer = new byte[orriginalData.Length];
WebSocketReceiveResult result;
int receivedCount = 0;
do
{
result = await clientSocket.ReceiveAsync(new ArraySegment<byte>(clientBuffer, receivedCount, clientBuffer.Length - receivedCount), CancellationToken.None);
receivedCount += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
}
while (!result.EndOfMessage);
Assert.Equal(orriginalData.Length, receivedCount);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, clientBuffer);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task ReceiveFragmentedData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 0, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 2, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 4, 7), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
var clientBuffer = new byte[orriginalData.Length];
var result = await clientSocket.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(2, result.Count);
int totalReceived = result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
result = await clientSocket.ReceiveAsync(
new ArraySegment<byte>(clientBuffer, totalReceived, clientBuffer.Length - totalReceived), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(2, result.Count);
totalReceived += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
result = await clientSocket.ReceiveAsync(
new ArraySegment<byte>(clientBuffer, totalReceived, clientBuffer.Length - totalReceived), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(7, result.Count);
totalReceived += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, clientBuffer);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task SendClose_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
await clientSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.CloseSent, clientSocket.State);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task ReceiveClose_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
var clientBuffer = new byte[1024];
var result = await clientSocket.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
Assert.Equal(WebSocketState.CloseReceived, clientSocket.State);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task CloseFromOpen_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
await clientSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.Closed, clientSocket.State);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task CloseFromCloseSent_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
await clientSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.CloseSent, clientSocket.State);
await clientSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.Closed, clientSocket.State);
}
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
public async Task CloseFromCloseReceived_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
var serverBuffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
}))
{
var client = new WebSocketClient();
using (var clientSocket = await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None))
{
var clientBuffer = new byte[1024];
var result = await clientSocket.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
Assert.Equal(WebSocketState.CloseReceived, clientSocket.State);
await clientSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
Assert.Equal(WebSocketState.Closed, clientSocket.State);
}
}
}
}
}

View File

@ -1,14 +0,0 @@
{
"dependencies": {
"dotnet-test-xunit": "2.2.0-*",
"Microsoft.AspNetCore.WebSockets.Client": "0.2.0-*",
"Microsoft.AspNetCore.WebSockets.Server": "0.2.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-*",
"Microsoft.AspNetCore.Testing": "1.1.0-*",
"xunit": "2.2.0-*"
},
"testRunner": "xunit",
"frameworks": {
"net451": {}
}
}

View File

@ -2,7 +2,7 @@
using System.Linq;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public class AutobahnCaseResult
{

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Server.Testing;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public class AutobahnExpectations
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using Microsoft.AspNetCore.Server.Testing;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public class AutobahnResult
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using Microsoft.AspNetCore.Server.Testing;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public class AutobahnServerResult
{

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public class AutobahnSpec
{

View File

@ -11,7 +11,7 @@ using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public class AutobahnTester : IDisposable
{

View File

@ -5,7 +5,7 @@ using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public class Executable
{

View File

@ -1,4 +1,4 @@
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public enum Expectation
{

View File

@ -1,7 +1,7 @@
using System;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
public class ServerSpec
{

View File

@ -1,6 +1,6 @@
using System;
namespace Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn
{
/// <summary>
/// Wrapper around the Autobahn Test Suite's "wstest" app.

View File

@ -6,11 +6,11 @@ using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn;
using Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNetCore.WebSockets.Server.Test
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest
{
public class AutobahnTests
{

View File

@ -2,7 +2,7 @@
using System.IO;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNetCore.WebSockets.Server.Test
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest
{
public class Helpers
{

View File

@ -6,8 +6,8 @@
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>a722bb6c-9114-4f25-9bb0-2191d4405f3a</ProjectGuid>
<RootNamespace>Microsoft.AspNetCore.WebSockets.Server.ConformanceTest</RootNamespace>
<ProjectGuid>74f45408-1959-4fee-9511-25d40f4913fd</ProjectGuid>
<RootNamespace>Microsoft.AspNetCore.WebSockets.ConformanceTest</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>

View File

@ -1,8 +1,8 @@
using System;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.AspNetCore.WebSockets.Server.Test.Autobahn;
using Microsoft.AspNetCore.WebSockets.ConformanceTest.Autobahn;
namespace Microsoft.AspNetCore.WebSockets.Server.Test
namespace Microsoft.AspNetCore.WebSockets.ConformanceTest
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class SkipIfWsTestNotPresentAttribute : Attribute, ITestCondition

View File

@ -8,7 +8,7 @@ using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.WebSockets.Protocol.Test
namespace Microsoft.AspNetCore.WebSockets.Test
{
// This steam accepts writes from one side, buffers them internally, and returns the data via Reads
// when requested on the other side.

View File

@ -5,7 +5,7 @@ using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.WebSockets.Protocol.Test
namespace Microsoft.AspNetCore.WebSockets.Test
{
// A duplex wrapper around a read and write stream.
public class DuplexStream : Stream

View File

@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
namespace Microsoft.AspNetCore.WebSockets.Client.Test
namespace Microsoft.AspNetCore.WebSockets.Test
{
public class KestrelWebSocketHelpers
{

View File

@ -1,20 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="14.0.25420" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25420</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>6604d154-817f-4bc5-be95-ff7e851179d9</ProjectGuid>
<ProjectGuid>5afa74f5-9b1d-4fc5-815f-ef471f5ac1ef</ProjectGuid>
<RootNamespace>Microsoft.AspNetCore.WebSockets.Test</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -6,7 +6,7 @@ using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.WebSockets.Protocol.Test
namespace Microsoft.AspNetCore.WebSockets.Test
{
public class SendReceiveTests
{

View File

@ -0,0 +1,541 @@
// 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.WebSockets.Test
{
public class WebSocketMiddlewareTests
{
private static string ClientAddress = "ws://localhost:54321/";
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task Connect_Success()
{
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task NegotiateSubProtocol_Success()
{
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
Assert.Equal("alpha, bravo, charlie", context.Request.Headers["Sec-WebSocket-Protocol"]);
var webSocket = await context.WebSockets.AcceptWebSocketAsync("Bravo");
}))
{
using (var client = new ClientWebSocket())
{
client.Options.AddSubProtocol("alpha");
client.Options.AddSubProtocol("bravo");
client.Options.AddSubProtocol("charlie");
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
Assert.Equal("Bravo", client.SubProtocol);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task SendEmptyData_Success()
{
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[0];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
var orriginalData = new byte[0];
await client.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task SendShortData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[orriginalData.Length];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task SendMediumData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes(new string('a', 130));
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[orriginalData.Length];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task SendLongData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes(new string('a', 0x1FFFF));
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[orriginalData.Length];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
int intermediateCount = result.Count;
Assert.False(result.EndOfMessage);
Assert.Equal(WebSocketMessageType.Text, result.MessageType);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer, intermediateCount, orriginalData.Length - intermediateCount), CancellationToken.None);
intermediateCount += result.Count;
Assert.False(result.EndOfMessage);
Assert.Equal(WebSocketMessageType.Text, result.MessageType);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer, intermediateCount, orriginalData.Length - intermediateCount), CancellationToken.None);
intermediateCount += result.Count;
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, intermediateCount);
Assert.Equal(WebSocketMessageType.Text, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task SendFragmentedData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[orriginalData.Length];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(2, result.Count);
int totalReceived = result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
result = await webSocket.ReceiveAsync(
new ArraySegment<byte>(serverBuffer, totalReceived, serverBuffer.Length - totalReceived), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(2, result.Count);
totalReceived += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
result = await webSocket.ReceiveAsync(
new ArraySegment<byte>(serverBuffer, totalReceived, serverBuffer.Length - totalReceived), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(7, result.Count);
totalReceived += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData, 0, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData, 2, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData, 4, 7), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task ReceiveShortData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
var clientBuffer = new byte[orriginalData.Length];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, clientBuffer);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task ReceiveMediumData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes(new string('a', 130));
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
var clientBuffer = new byte[orriginalData.Length];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, clientBuffer);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task ReceiveLongData()
{
var orriginalData = Encoding.UTF8.GetBytes(new string('a', 0x1FFFF));
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
var clientBuffer = new byte[orriginalData.Length];
WebSocketReceiveResult result;
int receivedCount = 0;
do
{
result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer, receivedCount, clientBuffer.Length - receivedCount), CancellationToken.None);
receivedCount += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
}
while (!result.EndOfMessage);
Assert.Equal(orriginalData.Length, receivedCount);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, clientBuffer);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task ReceiveFragmentedData_Success()
{
var orriginalData = Encoding.UTF8.GetBytes("Hello World");
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 0, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 2, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 4, 7), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
var clientBuffer = new byte[orriginalData.Length];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(2, result.Count);
int totalReceived = result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
result = await client.ReceiveAsync(
new ArraySegment<byte>(clientBuffer, totalReceived, clientBuffer.Length - totalReceived), CancellationToken.None);
Assert.False(result.EndOfMessage);
Assert.Equal(2, result.Count);
totalReceived += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
result = await client.ReceiveAsync(
new ArraySegment<byte>(clientBuffer, totalReceived, clientBuffer.Length - totalReceived), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(7, result.Count);
totalReceived += result.Count;
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, clientBuffer);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task SendClose_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.CloseSent, client.State);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task ReceiveClose_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
var clientBuffer = new byte[1024];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
Assert.Equal(WebSocketState.CloseReceived, client.State);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task CloseFromOpen_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.Closed, client.State);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task CloseFromCloseSent_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var serverBuffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.CloseSent, client.State);
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.Closed, client.State);
}
}
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No WebSockets Client for this platform")]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "No WebSockets Client for this platform")]
public async Task CloseFromCloseReceived_Success()
{
string closeDescription = "Test Closed";
using (var server = KestrelWebSocketHelpers.CreateServer(async context =>
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
var serverBuffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(serverBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
}))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
var clientBuffer = new byte[1024];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
Assert.Equal(WebSocketState.CloseReceived, client.State);
await client.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
Assert.Equal(WebSocketState.Closed, client.State);
}
}
}
}
}

View File

@ -1,7 +1,8 @@
using System;
using System.Net.WebSockets;
using Microsoft.AspNetCore.WebSockets.Internal;
namespace Microsoft.AspNetCore.WebSockets.Protocol.Test
namespace Microsoft.AspNetCore.WebSockets.Test
{
internal class WebSocketPair
{
@ -21,8 +22,8 @@ namespace Microsoft.AspNetCore.WebSockets.Protocol.Test
var clientStream = serverStream.CreateReverseDuplexStream();
return new WebSocketPair(
clientSocket: CommonWebSocket.CreateClientWebSocket(clientStream, null, TimeSpan.FromMinutes(2), 1024),
serverSocket: CommonWebSocket.CreateServerWebSocket(serverStream, null, TimeSpan.FromMinutes(2), 1024));
clientSocket: WebSocketFactory.CreateClientWebSocket(clientStream, null, TimeSpan.FromMinutes(2), 1024),
serverSocket: WebSocketFactory.CreateServerWebSocket(serverStream, null, TimeSpan.FromMinutes(2), 1024));
}
}
}

View File

@ -1,13 +1,17 @@
{
{
"dependencies": {
"dotnet-test-xunit": "2.2.0-*",
"Microsoft.AspNetCore.WebSockets.Protocol": "0.2.0-*",
"Microsoft.AspNetCore.Testing": "1.1.0-*",
"Microsoft.AspNetCore.WebSockets": "0.2.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-*",
"xunit": "2.2.0-*"
},
"testRunner": "xunit",
"frameworks": {
"net451": {},
"netcoreapp1.0": {
"dependencies": {
"System.Net.WebSockets.Client": "4.0.0",
"Microsoft.NETCore.App": {
"version": "1.0.0-*",
"type": "platform"