|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using BasicTestApp; |
| 7 | +using BasicTestApp.Reconnection; |
| 8 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; |
| 9 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; |
| 10 | +using Microsoft.AspNetCore.E2ETesting; |
| 11 | +using OpenQA.Selenium; |
| 12 | +using TestServer; |
| 13 | +using Xunit; |
| 14 | +using Xunit.Abstractions; |
| 15 | + |
| 16 | +namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests |
| 17 | +{ |
| 18 | + public class ServerTransportsTest : ServerTestBase<BasicTestAppServerSiteFixture<TransportsServerStartup>> |
| 19 | + { |
| 20 | + public ServerTransportsTest( |
| 21 | + BrowserFixture browserFixture, |
| 22 | + BasicTestAppServerSiteFixture<TransportsServerStartup> serverFixture, |
| 23 | + ITestOutputHelper output) |
| 24 | + : base(browserFixture, serverFixture, output) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void DefaultTransportsWorks_WithWebSockets() |
| 30 | + { |
| 31 | + Navigate("/defaultTransport/Transports"); |
| 32 | + |
| 33 | + Browser.Exists(By.Id("startBlazorServerBtn")).Click(); |
| 34 | + |
| 35 | + var javascript = (IJavaScriptExecutor)Browser; |
| 36 | + Browser.True(() => (bool)javascript.ExecuteScript("return window['__aspnetcore__testing__blazor__start__script__executed__'] === true;")); |
| 37 | + |
| 38 | + AssertLogContainsMessages( |
| 39 | + "Starting up Blazor server-side application.", |
| 40 | + "WebSocket connected to ws://", |
| 41 | + "Received render batch with", |
| 42 | + "The HttpConnection connected successfully.", |
| 43 | + "Blazor server-side application started."); |
| 44 | + |
| 45 | + AssertGlobalErrorState(hasGlobalError: false); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void ErrorIfClientAttemptsLongPolling_WithServerOnWebSockets() |
| 50 | + { |
| 51 | + Navigate("/webSockets/Transports"); |
| 52 | + |
| 53 | + Browser.Exists(By.Id("startWithLongPollingBtn")).Click(); |
| 54 | + |
| 55 | + var javascript = (IJavaScriptExecutor)Browser; |
| 56 | + Browser.True(() => (bool)javascript.ExecuteScript("return window['__aspnetcore__testing__blazor__start__script__executed__'] === true;")); |
| 57 | + |
| 58 | + AssertLogContainsMessages( |
| 59 | + "Information: Starting up Blazor server-side application.", |
| 60 | + "Skipping transport 'WebSockets' because it was disabled by the client", |
| 61 | + "Failed to start the connection: Error: Unable to connect to the server with any of the available transports.", |
| 62 | + "Failed to start the circuit."); |
| 63 | + |
| 64 | + AssertGlobalErrorState(hasGlobalError: true); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void WebSocketsConnectionIsRejected_FallbackToLongPolling() |
| 69 | + { |
| 70 | + Navigate("/defaultTransport/Transports"); |
| 71 | + |
| 72 | + Browser.Exists(By.Id("startAndRejectWebSocketConnectionBtn")).Click(); |
| 73 | + |
| 74 | + var javascript = (IJavaScriptExecutor)Browser; |
| 75 | + Browser.True(() => (bool)javascript.ExecuteScript("return window['__aspnetcore__testing__blazor__start__script__executed__'] === true;")); |
| 76 | + |
| 77 | + AssertLogContainsMessages( |
| 78 | + "Information: Starting up Blazor server-side application.", |
| 79 | + "Selecting transport 'LongPolling'", |
| 80 | + "Failed to connect via WebSockets, using the Long Polling fallback transport. This may be due to a VPN or proxy blocking the connection. To troubleshoot this, visit", |
| 81 | + "Blazor server-side application started."); |
| 82 | + |
| 83 | + AssertGlobalErrorState(hasGlobalError: false); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void ErrorIfWebSocketsConnectionIsRejected_WithServerOnWebSockets() |
| 88 | + { |
| 89 | + Navigate("/webSockets/Transports"); |
| 90 | + |
| 91 | + Browser.Exists(By.Id("startAndRejectWebSocketConnectionBtn")).Click(); |
| 92 | + |
| 93 | + var javascript = (IJavaScriptExecutor)Browser; |
| 94 | + Browser.True(() => (bool)javascript.ExecuteScript("return window['__aspnetcore__testing__blazor__start__script__executed__'] === true;")); |
| 95 | + |
| 96 | + AssertLogContainsMessages( |
| 97 | + "Information: Starting up Blazor server-side application.", |
| 98 | + "Selecting transport 'WebSockets'.", |
| 99 | + "Error: Failed to start the transport 'WebSockets': Error: Don't allow Websockets.", |
| 100 | + "Error: Failed to start the connection: Error: Unable to connect to the server with any of the available transports. Error: WebSockets failed: Error: Don't allow Websockets.", |
| 101 | + "Failed to start the circuit."); |
| 102 | + |
| 103 | + AssertGlobalErrorState(hasGlobalError: true); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void ServerOnlySupportsLongPolling_FallbackToLongPolling() |
| 108 | + { |
| 109 | + Navigate("/longPolling/Transports"); |
| 110 | + |
| 111 | + Browser.Exists(By.Id("startBlazorServerBtn")).Click(); |
| 112 | + |
| 113 | + var javascript = (IJavaScriptExecutor)Browser; |
| 114 | + Browser.True(() => (bool)javascript.ExecuteScript("return window['__aspnetcore__testing__blazor__start__script__executed__'] === true;")); |
| 115 | + |
| 116 | + AssertLogContainsMessages( |
| 117 | + "Starting up Blazor server-side application.", |
| 118 | + "Selecting transport 'LongPolling'.", |
| 119 | + "Failed to connect via WebSockets, using the Long Polling fallback transport. This may be due to a VPN or proxy blocking the connection. To troubleshoot this, visit", |
| 120 | + "Blazor server-side application started."); |
| 121 | + |
| 122 | + AssertGlobalErrorState(hasGlobalError: false); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void ErrorIfClientDisablesLongPolling_WithServerOnLongPolling() |
| 127 | + { |
| 128 | + Navigate("/longPolling/Transports"); |
| 129 | + |
| 130 | + Browser.Exists(By.Id("startWithWebSocketsBtn")).Click(); |
| 131 | + |
| 132 | + var javascript = (IJavaScriptExecutor)Browser; |
| 133 | + Browser.True(() => (bool)javascript.ExecuteScript("return window['__aspnetcore__testing__blazor__start__script__executed__'] === true;")); |
| 134 | + |
| 135 | + AssertLogContainsMessages( |
| 136 | + "Starting up Blazor server-side application.", |
| 137 | + "Unable to connect to the server with any of the available transports. LongPolling failed: Error: 'LongPolling' is disabled by the client.", |
| 138 | + "Unable to initiate a SignalR connection to the server. This might be because the server is not configured to support WebSockets. For additional details, visit"); |
| 139 | + |
| 140 | + AssertGlobalErrorState(hasGlobalError: true); |
| 141 | + } |
| 142 | + |
| 143 | + void AssertLogContainsMessages(params string[] messages) |
| 144 | + { |
| 145 | + var log = Browser.Manage().Logs.GetLog(LogType.Browser); |
| 146 | + foreach (var message in messages) |
| 147 | + { |
| 148 | + Assert.Contains(log, entry => |
| 149 | + { |
| 150 | + return entry.Message.Contains(message, StringComparison.InvariantCulture); |
| 151 | + }); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + void AssertGlobalErrorState(bool hasGlobalError) |
| 156 | + { |
| 157 | + var globalErrorUi = Browser.Exists(By.Id("blazor-error-ui")); |
| 158 | + Browser.Equal(hasGlobalError ? "block" : "none", () => globalErrorUi.GetCssValue("display")); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments