Add SignalR Client on WASM smoke test (#25526)
* Add SignalR Client on WASM smoke test * Move test into BasicTestApp Co-authored-by: Steve Sanderson <SteveSandersonMS@users.noreply.github.com>
This commit is contained in:
parent
2ab6436cdd
commit
c32089a5e2
|
|
@ -0,0 +1,51 @@
|
|||
// 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.Linq;
|
||||
using BasicTestApp;
|
||||
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
|
||||
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
|
||||
using Microsoft.AspNetCore.E2ETesting;
|
||||
using OpenQA.Selenium;
|
||||
using TestServer;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
||||
{
|
||||
public class SignalRClientTest : ServerTestBase<DevHostServerFixture<BasicTestApp.Program>>,
|
||||
IClassFixture<BasicTestAppServerSiteFixture<CorsStartup>>
|
||||
{
|
||||
private readonly ServerFixture _apiServerFixture;
|
||||
|
||||
public SignalRClientTest(
|
||||
BrowserFixture browserFixture,
|
||||
DevHostServerFixture<BasicTestApp.Program> devHostServerFixture,
|
||||
BasicTestAppServerSiteFixture<CorsStartup> apiServerFixture,
|
||||
ITestOutputHelper output)
|
||||
: base(browserFixture, devHostServerFixture, output)
|
||||
{
|
||||
_serverFixture.PathBase = "/subdir";
|
||||
_apiServerFixture = apiServerFixture;
|
||||
}
|
||||
|
||||
protected override void InitializeAsyncCore()
|
||||
{
|
||||
Navigate(ServerPathBase);
|
||||
Browser.MountTestComponent<SignalRClientComponent>();
|
||||
Browser.Exists(By.Id("signalr-client"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SignalRClientWorks()
|
||||
{
|
||||
Browser.FindElement(By.Id("hub-url")).SendKeys(
|
||||
new Uri(_apiServerFixture.RootUri, "/subdir/chathub").AbsoluteUri);
|
||||
Browser.FindElement(By.Id("hub-connect")).Click();
|
||||
|
||||
Browser.Equal("SignalR Client: Echo",
|
||||
() => Browser.FindElements(By.CssSelector("li")).FirstOrDefault()?.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@
|
|||
<Reference Include="Microsoft.AspNetCore.Components.WebAssembly" />
|
||||
<Reference Include="Microsoft.AspNetCore.Components.Authorization" />
|
||||
<Reference Include="Microsoft.AspNetCore.Components.Web.Extensions" />
|
||||
<Reference Include="Microsoft.AspNetCore.SignalR.Client" />
|
||||
<Reference Include="Microsoft.Extensions.Logging.Configuration" />
|
||||
<Reference Include="Newtonsoft.Json" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -80,6 +80,7 @@
|
|||
<option value="BasicTestApp.RouterTest.TestRouterWithOnNavigate">Router with OnNavigate</option>
|
||||
<option value="BasicTestApp.RouterTest.TestRouterWithLazyAssembly">Router with dynamic assembly</option>
|
||||
<option value="BasicTestApp.RouterTest.TestRouterWithAdditionalAssembly">Router with additional assembly</option>
|
||||
<option value="BasicTestApp.SignalRClientComponent">SignalR client</option>
|
||||
<option value="BasicTestApp.StringComparisonComponent">StringComparison</option>
|
||||
<option value="BasicTestApp.SvgComponent">SVG</option>
|
||||
<option value="BasicTestApp.SvgWithChildComponent">SVG with child component</option>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
|
||||
<h1 id="signalr-client">SignalR Client</h1>
|
||||
|
||||
<p>This shows that the SignalR client can be used on WebAssembly.</p>
|
||||
|
||||
<p>
|
||||
Hub URL:
|
||||
<input id="hub-url" @bind="hubUrl" />
|
||||
<button id="hub-connect" @onclick="Connect">Connect</button>
|
||||
</p>
|
||||
|
||||
<div>Connected: @IsConnected</div>
|
||||
|
||||
<ul id="messagesList">
|
||||
@foreach (var message in messages)
|
||||
{
|
||||
<li>@message</li>
|
||||
}
|
||||
</ul>
|
||||
|
||||
@code {
|
||||
private string hubUrl;
|
||||
private HubConnection hubConnection;
|
||||
private List<string> messages = new List<string>();
|
||||
|
||||
protected async Task Connect()
|
||||
{
|
||||
hubConnection = new HubConnectionBuilder()
|
||||
.WithUrl(hubUrl)
|
||||
.Build();
|
||||
|
||||
hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
|
||||
{
|
||||
var encodedMsg = $"{user}: {message}";
|
||||
messages.Add(encodedMsg);
|
||||
StateHasChanged();
|
||||
});
|
||||
|
||||
await hubConnection.StartAsync();
|
||||
await hubConnection.SendAsync("SendMessage", "SignalR Client", "Echo");
|
||||
}
|
||||
|
||||
public bool IsConnected =>
|
||||
hubConnection != null && hubConnection.State == HubConnectionState.Connected;
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
return hubConnection.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
// 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.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace TestServer
|
||||
{
|
||||
public class ChatHub : Hub
|
||||
{
|
||||
public async Task SendMessage(string user, string message)
|
||||
{
|
||||
await Clients.All.SendAsync("ReceiveMessage", user, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@ namespace TestServer
|
|||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSignalR();
|
||||
services.AddMvc();
|
||||
services.AddCors(options =>
|
||||
{
|
||||
|
|
@ -50,10 +51,11 @@ namespace TestServer
|
|||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseCors();
|
||||
app.UseCors("AllowAll");
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapHub<ChatHub>("/chathub");
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapFallbackToFile("index.html");
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue