From 4aa237d1acd714c390f502c3c9811e2caca1b257 Mon Sep 17 00:00:00 2001 From: xuef Date: Sat, 25 Nov 2023 09:07:01 +0000 Subject: [PATCH] Add tests. --- Aiursoft.ChessServer.sln | 12 +++++ .../Aiursoft.ChessServer.Tests.csproj | 25 +++++++++++ .../Aiursoft.ChessServer.Tests/BasicTests.cs | 45 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tests/Aiursoft.ChessServer.Tests/Aiursoft.ChessServer.Tests.csproj create mode 100644 tests/Aiursoft.ChessServer.Tests/BasicTests.cs diff --git a/Aiursoft.ChessServer.sln b/Aiursoft.ChessServer.sln index c518327..e3c2ad4 100644 --- a/Aiursoft.ChessServer.sln +++ b/Aiursoft.ChessServer.sln @@ -7,6 +7,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{1F6B948D-79A EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aiursoft.ChessServer", "src\Aiursoft.ChessServer\Aiursoft.ChessServer.csproj", "{22E46CC4-A869-492C-88D0-05E2F7515DD1}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{5B3CA58E-61CA-4A0F-99BB-CDCA95D43D64}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aiursoft.ChessServer.Tests", "tests\Aiursoft.ChessServer.Tests\Aiursoft.ChessServer.Tests.csproj", "{7D70A645-AB40-453B-860F-96F05FE02CE8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -17,11 +21,19 @@ Global {22E46CC4-A869-492C-88D0-05E2F7515DD1}.Debug|Any CPU.Build.0 = Debug|Any CPU {22E46CC4-A869-492C-88D0-05E2F7515DD1}.Release|Any CPU.ActiveCfg = Release|Any CPU {22E46CC4-A869-492C-88D0-05E2F7515DD1}.Release|Any CPU.Build.0 = Release|Any CPU + {7D70A645-AB40-453B-860F-96F05FE02CE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D70A645-AB40-453B-860F-96F05FE02CE8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D70A645-AB40-453B-860F-96F05FE02CE8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D70A645-AB40-453B-860F-96F05FE02CE8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {22E46CC4-A869-492C-88D0-05E2F7515DD1} = {1F6B948D-79AC-41CA-9C00-9A839DB56B2A} + {7D70A645-AB40-453B-860F-96F05FE02CE8} = {5B3CA58E-61CA-4A0F-99BB-CDCA95D43D64} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {00EE1758-7DF7-4926-AAC4-8B9CB5A6F459} EndGlobalSection EndGlobal diff --git a/tests/Aiursoft.ChessServer.Tests/Aiursoft.ChessServer.Tests.csproj b/tests/Aiursoft.ChessServer.Tests/Aiursoft.ChessServer.Tests.csproj new file mode 100644 index 0000000..634247e --- /dev/null +++ b/tests/Aiursoft.ChessServer.Tests/Aiursoft.ChessServer.Tests.csproj @@ -0,0 +1,25 @@ + + + Exe + net7.0 + Aiursoft.ChessServer.Tests + true + false + enable + enable + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + \ No newline at end of file diff --git a/tests/Aiursoft.ChessServer.Tests/BasicTests.cs b/tests/Aiursoft.ChessServer.Tests/BasicTests.cs new file mode 100644 index 0000000..2dd9e57 --- /dev/null +++ b/tests/Aiursoft.ChessServer.Tests/BasicTests.cs @@ -0,0 +1,45 @@ +using Aiursoft.CSTools.Tools; +using Microsoft.Extensions.Hosting; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using static Aiursoft.WebTools.Extends; + +namespace Aiursoft.ChessServer.Tests; + +[TestClass] +public class BasicTests +{ + private readonly string _endpointUrl; + private readonly int _port; + private readonly HttpClient _http; + private IHost? _server; + + public BasicTests() + { + _port = Network.GetAvailablePort(); + _endpointUrl = $"http://localhost:{_port}"; + _http = new HttpClient(); + } + + [TestInitialize] + public async Task CreateServer() + { + _server = App(Array.Empty(), port: _port); + await _server.StartAsync(); + } + + [TestCleanup] + public async Task CleanServer() + { + if (_server == null) return; + await _server.StopAsync(); + _server.Dispose(); + } + + [TestMethod] + [DataRow("/")] + public async Task GetHome(string url) + { + var response = await _http.GetAsync(_endpointUrl + url); + response.EnsureSuccessStatusCode(); // Status Code 200-299 + } +} \ No newline at end of file