diff --git a/TestProject/AppFixture.cs b/TestProject/AppFixture.cs deleted file mode 100644 index 6762260..0000000 --- a/TestProject/AppFixture.cs +++ /dev/null @@ -1,26 +0,0 @@ -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/ExampleSignalR - Backup.Test.csproj b/TestProject/ExampleSignalR - Backup.Test.csproj new file mode 100644 index 0000000..7703088 --- /dev/null +++ b/TestProject/ExampleSignalR - Backup.Test.csproj @@ -0,0 +1,42 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + PreserveNewest + true + PreserveNewest + + + + + + + + + + + + + + + + + + + + + + diff --git a/TestProject/ExampleSignalR.Test.csproj b/TestProject/ExampleSignalR.Test.csproj index 7703088..ba30374 100644 --- a/TestProject/ExampleSignalR.Test.csproj +++ b/TestProject/ExampleSignalR.Test.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -9,18 +9,6 @@ true - - - - - - - PreserveNewest - true - PreserveNewest - - - diff --git a/TestProject/Services/MessageServiceTest.cs b/TestProject/Services/MessageServiceTest.cs new file mode 100644 index 0000000..1d61ab1 --- /dev/null +++ b/TestProject/Services/MessageServiceTest.cs @@ -0,0 +1,9 @@ +namespace ExampleSignalR.Test.Services; + +public class MessageServiceTest() : IMessageService +{ + public string GetMessage() + { + return "321"; + } +} diff --git a/TestProject/Tests/HubTest.cs b/TestProject/Tests/HubTest.cs new file mode 100644 index 0000000..dd6726c --- /dev/null +++ b/TestProject/Tests/HubTest.cs @@ -0,0 +1,63 @@ +using Microsoft.AspNetCore.SignalR.Client; +using Microsoft.Extensions.DependencyInjection; + +namespace ExampleSignalR.Test.Tests; + +public class HubTest : BaseIntegrationTest +{ + private const string url = $"wss://localhost/chatHub"; + private readonly WebAppFactoryFixture _factory; + public HubTest(WebAppFactoryFixture factory) : base(factory) + { + _factory = factory; + } + + [Fact] + public async Task ConnectToSignalRHub() + { + var hubConnection = await CreateHubConnection(); + } + + [Fact] + public async Task SignalRHubResponsibility() + { + var hubConnection = await CreateHubConnection(); + + bool success = false; + hubConnection.On("ReceiveMessage", (user, message) => + { + using var scope = _factory.Services.CreateScope(); + + var messageService = scope.ServiceProvider.GetRequiredService(); + + var testMessage = messageService.GetMessage(); + if (message == testMessage) + { + success = true; + } + }); + + await hubConnection.InvokeAsync("SendMessage", nameof(HubTest), "test"); + + await Task.Delay(10000); + if (!success) + { + throw new Exception(); + } + } + + private async Task CreateHubConnection() + { + var server = _factory.Server; + var hubConnection = new HubConnectionBuilder() + .WithUrl(url, options => + { + options.HttpMessageHandlerFactory = _ => server.CreateHandler(); + }) + .Build(); + + await hubConnection.StartAsync(); + + return hubConnection; + } +} \ No newline at end of file diff --git a/TestProject/UnitTest1.cs b/TestProject/UnitTest1.cs deleted file mode 100644 index 8bf8c13..0000000 --- a/TestProject/UnitTest1.cs +++ /dev/null @@ -1,38 +0,0 @@ -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 index c93a981..e79acf8 100644 --- a/TestProject/WebAppFactoryFixture.cs +++ b/TestProject/WebAppFactoryFixture.cs @@ -1,50 +1,22 @@ -using System.Net.Http.Headers; -using System.Text.Json; -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Builder; +using ExampleSignalR.Test.Services; 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"; + services.AddTransient(); } } \ No newline at end of file diff --git a/TestProject/appsettings.json b/TestProject/appsettings.json deleted file mode 100644 index e2b5075..0000000 --- a/TestProject/appsettings.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "Kestrel": { - "Endpoints": { - "Https": { - "Url": "https://localhost:7083" - } - } - }, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -}