aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs
blob: 117d0cf7be8ab4f2f36ddd13862e0828bcfee88e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Microsoft.AspNetCore.TestHost;

namespace CrupestApi.Commons.Crud.Tests;

public abstract class CrudTestBase : IAsyncDisposable
{
    protected readonly WebApplication _app;

    protected readonly string _path;
    protected readonly string? _authKey;

    protected readonly HttpClient _client;

    public CrudTestBase(string path, string? authKey = null)
    {
        _path = path;
        _authKey = authKey;

        var builder = WebApplication.CreateBuilder();
        builder.WebHost.UseTestServer();
        builder.Services.AddCrudCore();
        ConfigureApplication(builder);
        _app = builder.Build();

        _client = CreateHttpClient();
    }

    protected abstract void ConfigureApplication(WebApplicationBuilder builder);

    public virtual async ValueTask DisposeAsync()
    {
        await _app.DisposeAsync();
    }

    public TestServer GetTestServer()
    {
        return _app.GetTestServer();
    }

    public HttpClient CreateHttpClient()
    {
        return GetTestServer().CreateClient();
    }
}