aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/tests/IOL.GreatOffice.IntegrationTests/Helpers/WebServerFixture.cs
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2023-02-25 13:15:44 +0100
committerivarlovlie <git@ivarlovlie.no>2023-02-25 13:15:44 +0100
commit900bb5e845c3ad44defbd427cae3d44a4a43321f (patch)
treedf3d96a93771884add571e82336c29fc3d9c7a1c /code/api/tests/IOL.GreatOffice.IntegrationTests/Helpers/WebServerFixture.cs
downloadgreatoffice-900bb5e845c3ad44defbd427cae3d44a4a43321f.tar.xz
greatoffice-900bb5e845c3ad44defbd427cae3d44a4a43321f.zip
feat: Initial commit
Diffstat (limited to 'code/api/tests/IOL.GreatOffice.IntegrationTests/Helpers/WebServerFixture.cs')
-rw-r--r--code/api/tests/IOL.GreatOffice.IntegrationTests/Helpers/WebServerFixture.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/code/api/tests/IOL.GreatOffice.IntegrationTests/Helpers/WebServerFixture.cs b/code/api/tests/IOL.GreatOffice.IntegrationTests/Helpers/WebServerFixture.cs
new file mode 100644
index 0000000..080fa9f
--- /dev/null
+++ b/code/api/tests/IOL.GreatOffice.IntegrationTests/Helpers/WebServerFixture.cs
@@ -0,0 +1,48 @@
+using System.Net;
+using System.Net.Sockets;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.Playwright;
+using Xunit;
+using Program = IOL.GreatOffice.Api.Program;
+
+namespace IOL.GreatOffice.IntegrationTests.Helpers;
+
+// ReSharper disable once ClassNeverInstantiated.Global
+public class WebServerFixture : IAsyncLifetime, IDisposable
+{
+ private readonly WebApplication Host;
+ private IPlaywright Playwright { get; set; }
+ public IBrowser Browser { get; private set; }
+ public string BaseUrl { get; } = $"https://localhost:{GetRandomUnusedPort()}";
+
+ public WebServerFixture() {
+ Host = Program.CreateWebApplication(Program.CreateAppBuilder(default));
+ }
+
+ public async Task InitializeAsync() {
+ Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
+ Browser = await Playwright.Chromium.LaunchAsync();
+ await Host.StartAsync();
+ }
+
+ public async Task DisposeAsync() {
+ await Host.StopAsync();
+ await Host.DisposeAsync();
+ Playwright?.Dispose();
+ }
+
+ public void Dispose() {
+ Host.StopAsync();
+ Host.DisposeAsync();
+ Playwright?.Dispose();
+ GC.SuppressFinalize(this);
+ }
+
+ private static int GetRandomUnusedPort() {
+ var listener = new TcpListener(IPAddress.Any, 0);
+ listener.Start();
+ var port = ((IPEndPoint)listener.LocalEndpoint).Port;
+ listener.Stop();
+ return port;
+ }
+}