Conditional WebSockets testing (#714)

This commit is contained in:
BrennanConroy 2017-08-11 10:46:50 -07:00 committed by GitHub
parent 29f9c54b86
commit e8a673248a
6 changed files with 39 additions and 40 deletions

View File

@ -0,0 +1,24 @@
// 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;
namespace Microsoft.AspNetCore.SignalR.Tests.Common
{
public static class TestHelpers
{
public static bool IsWebSocketsSupported()
{
try
{
new System.Net.WebSockets.ClientWebSocket().Dispose();
}
catch (PlatformNotSupportedException)
{
return false;
}
return true;
}
}
}

View File

@ -243,7 +243,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
public static IEnumerable<TransportType> TransportTypes()
{
if (WebsocketsSupported())
if (TestHelpers.IsWebSocketsSupported())
{
// TODO: Currently we are always sending Text messages over websockets which does not work
// with binary protocols. It is getting fixed separately.
@ -253,20 +253,6 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
}
yield return TransportType.ServerSentEvents;
yield return TransportType.LongPolling;
bool WebsocketsSupported()
{
try
{
new System.Net.WebSockets.ClientWebSocket();
}
catch (PlatformNotSupportedException)
{
return false;
}
return true;
}
}
}
}

View File

@ -17,6 +17,7 @@
<Compile Include="..\Common\ServerFixture.cs" Link="ServerFixture.cs" />
<Compile Include="..\Common\TaskExtensions.cs" Link="TaskExtensions.cs" />
<Compile Include="..\Common\ChannelExtensions.cs" Link="ChannelExtensions.cs" />
<Compile Include="..\Common\TestHelpers.cs" Link="TestHelpers.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -3,6 +3,7 @@
using System;
using System.Net.Http;
using Microsoft.AspNetCore.SignalR.Tests.Common;
using Microsoft.AspNetCore.Sockets;
using Microsoft.AspNetCore.Sockets.Client;
using Microsoft.AspNetCore.Testing.xunit;
@ -73,7 +74,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
[InlineData(TransportType.LongPolling, typeof(LongPollingTransport))]
public void DefaultTransportFactoryCreatesRequestedTransportIfAvailable_Win7(TransportType requestedTransport, Type expectedTransportType)
{
if (!WebsocketsSupported())
if (!TestHelpers.IsWebSocketsSupported())
{
var transportFactory = new DefaultTransportFactory(requestedTransport, loggerFactory: null, httpClient: new HttpClient());
Assert.IsType(expectedTransportType,
@ -85,7 +86,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
[InlineData(TransportType.WebSockets)]
public void DefaultTransportFactoryThrowsIfItCannotCreateRequestedTransport_Win7(TransportType requestedTransport)
{
if (!WebsocketsSupported())
if (!TestHelpers.IsWebSocketsSupported())
{
var transportFactory =
new DefaultTransportFactory(requestedTransport, loggerFactory: null, httpClient: new HttpClient());
@ -95,19 +96,5 @@ namespace Microsoft.AspNetCore.SignalR.Tests
Assert.Equal("No requested transports available on the server.", ex.Message);
}
}
private bool WebsocketsSupported()
{
try
{
new System.Net.WebSockets.ClientWebSocket();
}
catch (PlatformNotSupportedException)
{
return false;
}
return true;
}
}
}

View File

@ -58,7 +58,6 @@ namespace Microsoft.AspNetCore.SignalR.Tests
public async Task CanStartAndStopConnectionUsingGivenTransport(TransportType transportType)
{
var url = _serverFixture.BaseUrl + "/echo";
// When WebSockets is attempted to be used on Windows 7/2008R2 it will instead use ServerSentEvents
var connection = new HttpConnection(new Uri(url), transportType);
await connection.StartAsync().OrTimeout();
await connection.DisposeAsync().OrTimeout();
@ -99,7 +98,6 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
[ConditionalTheory]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2, SkipReason = "No WebSockets Client for this platform")]
[MemberData(nameof(TransportTypes))]
// TODO: transfer types
public async Task ConnectionCanSendAndReceiveMessages(TransportType transportType)
@ -189,7 +187,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var logger = loggerFactory.CreateLogger<EndToEndTests>();
var url = _serverFixture.BaseUrl + "/echo";
var connection = new HttpConnection(new Uri(url), loggerFactory);
var connection = new HttpConnection(new Uri(url), TransportType.WebSockets, loggerFactory);
connection.Features.Set<ITransferModeFeature>(
new TransferModeFeature { TransferMode = TransferMode.Binary });
@ -294,12 +292,14 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
}
public static IEnumerable<object[]> TransportTypes =>
new[]
public static IEnumerable<object[]> TransportTypes()
{
if (TestHelpers.IsWebSocketsSupported())
{
new object[] { TransportType.WebSockets },
new object[] { TransportType.ServerSentEvents },
new object[] { TransportType.LongPolling }
};
yield return new object[] { TransportType.WebSockets };
}
yield return new object[] { TransportType.ServerSentEvents };
yield return new object[] { TransportType.LongPolling };
}
}
}

View File

@ -21,6 +21,7 @@
<ItemGroup>
<Compile Include="..\Common\ServerFixture.cs" Link="ServerFixture.cs" />
<Compile Include="..\Common\TaskExtensions.cs" Link="TaskExtensions.cs" />
<Compile Include="..\Common\TestHelpers.cs" Link="TestHelpers.cs" />
</ItemGroup>
<ItemGroup>