From 9196dc1f9c0ca587855fe2acfe9e9e1a27d3ca48 Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Tue, 12 Nov 2024 18:02:09 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ExampleSignalR.sln | 6 ++ ExampleSignalR/Hubs/ChatHub.cs | 9 ++- ExampleSignalR/IMessageService.cs | 7 +++ ExampleSignalR/MessageService.cs | 10 +++ ExampleSignalR/Program.cs | 61 +++++++++++-------- ExampleSignalR/Properties/launchSettings.json | 52 ++++++++-------- ExampleSignalR/appsettings.json | 7 +++ TestProject/AppFixture.cs | 26 ++++++++ TestProject/BaseIntegrationTest.cs | 20 ++++++ TestProject/ExampleSignalR.Test.csproj | 42 +++++++++++++ TestProject/UnitTest1.cs | 38 ++++++++++++ TestProject/WebAppFactoryFixture.cs | 50 +++++++++++++++ TestProject/appsettings.json | 16 +++++ 13 files changed, 291 insertions(+), 53 deletions(-) create mode 100644 ExampleSignalR/IMessageService.cs create mode 100644 ExampleSignalR/MessageService.cs create mode 100644 TestProject/AppFixture.cs create mode 100644 TestProject/BaseIntegrationTest.cs create mode 100644 TestProject/ExampleSignalR.Test.csproj create mode 100644 TestProject/UnitTest1.cs create mode 100644 TestProject/WebAppFactoryFixture.cs create mode 100644 TestProject/appsettings.json diff --git a/ExampleSignalR.sln b/ExampleSignalR.sln index d2e1bc7..8af30b3 100644 --- a/ExampleSignalR.sln +++ b/ExampleSignalR.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleSignalR", "ExampleSi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HubListener", "HubListener\HubListener.csproj", "{16E4F78D-5D80-4A9E-A817-1ABA6392DF91}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleSignalR.Test", "TestProject\ExampleSignalR.Test.csproj", "{D5D33655-9C04-440C-9C07-085C2C20D2A1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {16E4F78D-5D80-4A9E-A817-1ABA6392DF91}.Debug|Any CPU.Build.0 = Debug|Any CPU {16E4F78D-5D80-4A9E-A817-1ABA6392DF91}.Release|Any CPU.ActiveCfg = Release|Any CPU {16E4F78D-5D80-4A9E-A817-1ABA6392DF91}.Release|Any CPU.Build.0 = Release|Any CPU + {D5D33655-9C04-440C-9C07-085C2C20D2A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D5D33655-9C04-440C-9C07-085C2C20D2A1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D5D33655-9C04-440C-9C07-085C2C20D2A1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D5D33655-9C04-440C-9C07-085C2C20D2A1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ExampleSignalR/Hubs/ChatHub.cs b/ExampleSignalR/Hubs/ChatHub.cs index 74b096a..74df083 100644 --- a/ExampleSignalR/Hubs/ChatHub.cs +++ b/ExampleSignalR/Hubs/ChatHub.cs @@ -4,9 +4,16 @@ namespace ExampleSignalR.Hubs { public class ChatHub : Hub { + private readonly IMessageService messageService; + + public ChatHub(IMessageService messageService) { + this.messageService = messageService; + } public async Task SendMessage(string user, string message) { - await Clients.All.SendAsync("ReceiveMessage", user, message); + var msg = messageService.GetMessage(); + + await Clients.All.SendAsync("ReceiveMessage", user, msg); } } } diff --git a/ExampleSignalR/IMessageService.cs b/ExampleSignalR/IMessageService.cs new file mode 100644 index 0000000..7f08279 --- /dev/null +++ b/ExampleSignalR/IMessageService.cs @@ -0,0 +1,7 @@ +namespace ExampleSignalR +{ + public interface IMessageService + { + string GetMessage(); + } +} \ No newline at end of file diff --git a/ExampleSignalR/MessageService.cs b/ExampleSignalR/MessageService.cs new file mode 100644 index 0000000..54eae50 --- /dev/null +++ b/ExampleSignalR/MessageService.cs @@ -0,0 +1,10 @@ +namespace ExampleSignalR +{ + public class MessageService : IMessageService + { + public string GetMessage() + { + return "123"; + } + } +} diff --git a/ExampleSignalR/Program.cs b/ExampleSignalR/Program.cs index 64830f6..44043fa 100644 --- a/ExampleSignalR/Program.cs +++ b/ExampleSignalR/Program.cs @@ -1,36 +1,45 @@ using ExampleSignalR.Hubs; using Microsoft.AspNetCore.Http.Connections; -var builder = WebApplication.CreateBuilder(args); +namespace ExampleSignalR; -// Add services to the container. -builder.Services.AddRazorPages(); -builder.Services.AddSignalR(); - -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (!app.Environment.IsDevelopment()) +public class Program { - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); -} + private static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); -app.UseHttpsRedirection(); -app.UseStaticFiles(); + // Add services to the container. + builder.Services.AddTransient(); + builder.Services.AddRazorPages(); + builder.Services.AddSignalR(); -app.UseRouting(); + var app = builder.Build(); -app.UseAuthorization(); + // Configure the HTTP request pipeline. + if (!app.Environment.IsDevelopment()) + { + app.UseExceptionHandler("/Error"); + app.UseHsts(); + } -app.MapRazorPages(); -app.MapHub("/chatHub", - options => { - options.ApplicationMaxBufferSize = 128; - options.TransportMaxBufferSize = 128; - options.LongPolling.PollTimeout = TimeSpan.FromMinutes(1); - options.Transports = HttpTransportType.LongPolling | HttpTransportType.WebSockets; - }); + app.UseHttpsRedirection(); + app.UseStaticFiles(); -app.Run(); + app.UseRouting(); + + app.UseAuthorization(); + + app.MapRazorPages(); + app.MapHub("/chatHub", + options => + { + options.ApplicationMaxBufferSize = 128; + options.TransportMaxBufferSize = 128; + options.LongPolling.PollTimeout = TimeSpan.FromMinutes(1); + options.Transports = HttpTransportType.LongPolling | HttpTransportType.WebSockets; + }); + + app.Run(); + } +} \ No newline at end of file diff --git a/ExampleSignalR/Properties/launchSettings.json b/ExampleSignalR/Properties/launchSettings.json index 5afeadb..b392af3 100644 --- a/ExampleSignalR/Properties/launchSettings.json +++ b/ExampleSignalR/Properties/launchSettings.json @@ -1,38 +1,38 @@ { - "$schema": "http://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:57527", - "sslPort": 44355 - } - }, + //"$schema": "http://json.schemastore.org/launchsettings.json", + //"iisSettings": { + // "windowsAuthentication": false, + // "anonymousAuthentication": true, + // "iisExpress": { + // "applicationUrl": "http://localhost:57527", + // "sslPort": 44355 + // } + //}, "profiles": { - "http": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "applicationUrl": "http://localhost:5114", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, + //"http": { + // "commandName": "Project", + // "dotnetRunMessages": true, + // "launchBrowser": true, + // "applicationUrl": "http://localhost:5114", + // "environmentVariables": { + // "ASPNETCORE_ENVIRONMENT": "Development" + // } + //}, "https": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:7083;http://localhost:5114", + "applicationUrl": "https://localhost:7083", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } + //"IIS Express": { + // "commandName": "IISExpress", + // "launchBrowser": true, + // "environmentVariables": { + // "ASPNETCORE_ENVIRONMENT": "Development" + // } + //} } } diff --git a/ExampleSignalR/appsettings.json b/ExampleSignalR/appsettings.json index 10f68b8..e2b5075 100644 --- a/ExampleSignalR/appsettings.json +++ b/ExampleSignalR/appsettings.json @@ -1,4 +1,11 @@ { + "Kestrel": { + "Endpoints": { + "Https": { + "Url": "https://localhost:7083" + } + } + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/TestProject/AppFixture.cs b/TestProject/AppFixture.cs new file mode 100644 index 0000000..6762260 --- /dev/null +++ b/TestProject/AppFixture.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore; + +namespace ExampleSignalR.Test +{ + public class AppFixture + { + public const string BaseUrl = "http://localhost:54321"; + + static AppFixture() + { + var webhost = WebHost + .CreateDefaultBuilder(null) + .UseStartup() + .UseUrls(BaseUrl) + .Build(); + + webhost.Start(); + } + } +} diff --git a/TestProject/BaseIntegrationTest.cs b/TestProject/BaseIntegrationTest.cs new file mode 100644 index 0000000..84b1e56 --- /dev/null +++ b/TestProject/BaseIntegrationTest.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace ExampleSignalR.Test; + +public abstract class BaseIntegrationTest : IClassFixture, + IDisposable +{ + protected readonly IServiceScope scope; + protected readonly WebAppFactoryFixture factory; + protected BaseIntegrationTest(WebAppFactoryFixture factory) + { + scope = factory.Services.CreateScope(); + this.factory = factory; + } + + public void Dispose() + { + scope.Dispose(); + } +} \ No newline at end of file diff --git a/TestProject/ExampleSignalR.Test.csproj b/TestProject/ExampleSignalR.Test.csproj new file mode 100644 index 0000000..7703088 --- /dev/null +++ b/TestProject/ExampleSignalR.Test.csproj @@ -0,0 +1,42 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + PreserveNewest + true + PreserveNewest + + + + + + + + + + + + + + + + + + + + + + diff --git a/TestProject/UnitTest1.cs b/TestProject/UnitTest1.cs new file mode 100644 index 0000000..8bf8c13 --- /dev/null +++ b/TestProject/UnitTest1.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.SignalR.Client; +using static System.Net.WebRequestMethods; +using static Microsoft.AspNetCore.Http.StatusCodes; + +namespace ExampleSignalR.Test; + +public class UnitTest1 : BaseIntegrationTest +{ + private readonly HttpClient _httpClient; + public UnitTest1(WebAppFactoryFixture factory) : base(factory) + { + _httpClient = factory.CreateClient(); + } + + [Fact] + public async void ConnectToSignalRHub() + { + var hubConnection = new HubConnectionBuilder() + .WithUrl($"{factory.url}/chatHub") // ...appsettings.json + .Build(); + + + await hubConnection.StartAsync(); + } + + //public async Task Connect() + //{ + // try + // { + // await hubConnection.StartAsync(); + // Console.WriteLine("×àò ïîäêëþ÷åí..."); + // } + // catch (Exception ex) + // { + // Console.WriteLine($"Îøèáêà ïîäêëþ÷åíèÿ: {ex.Message}"); + // } + //} +} \ No newline at end of file diff --git a/TestProject/WebAppFactoryFixture.cs b/TestProject/WebAppFactoryFixture.cs new file mode 100644 index 0000000..c93a981 --- /dev/null +++ b/TestProject/WebAppFactoryFixture.cs @@ -0,0 +1,50 @@ +using System.Net.Http.Headers; +using System.Text.Json; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using static Microsoft.AspNetCore.Http.StatusCodes; + +namespace ExampleSignalR.Test; + +public class WebAppFactoryFixture : WebApplicationFactory +{ + public string url = "https://localhost:7083"; + + //private static readonly JsonSerializerOptions JsonSerializerOptions = new() + //{ + + //}; + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + + builder.ConfigureServices(ConfigureServices); + //builder.UseEnvironment("Development"); + builder.UseUrls(url); + base.ConfigureWebHost(builder); + } + + private void ConfigureServices(IServiceCollection services) + { + //var t = services.Select(e => e.ServiceType == typeof(IMessageService)); + services.RemoveAll(typeof(IMessageService)); + services.AddTransient(); + services.AddHttpsRedirection(options => + { + options.RedirectStatusCode = Status307TemporaryRedirect; + options.HttpsPort = 7083; + }); + } +} + +public class MessageServiceT() : IMessageService +{ + public string GetMessage() + { + return "321"; + } +} \ No newline at end of file diff --git a/TestProject/appsettings.json b/TestProject/appsettings.json new file mode 100644 index 0000000..e2b5075 --- /dev/null +++ b/TestProject/appsettings.json @@ -0,0 +1,16 @@ +{ + "Kestrel": { + "Endpoints": { + "Https": { + "Url": "https://localhost:7083" + } + } + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}