-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestServerFixture.cs
86 lines (76 loc) · 3.44 KB
/
TestServerFixture.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Martin Costello, 2020. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MartinCostello.Logging.XUnit;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
namespace TodoApp
{
/// <summary>
/// A class representing a factory for creating instances of the application.
/// </summary>
public class TestServerFixture : WebApplicationFactory<Startup>
{
/// <summary>
/// Initializes a new instance of the <see cref="TestServerFixture"/> class.
/// </summary>
public TestServerFixture()
: base()
{
ClientOptions.AllowAutoRedirect = false;
ClientOptions.BaseAddress = new Uri("https://localhost");
ClientOptions.HandleCookies = true;
// HACK Force HTTP server startup
using (CreateDefaultClient())
{
}
}
/// <summary>
/// Gets a set of valid antiforgery tokens for the application as an asynchronous operation.
/// </summary>
/// <param name="httpClientFactory">
/// An optional delegate to a method to provide an <see cref="HttpClient"/> to use to obtain the response.
/// </param>
/// <param name="cancellationToken">
/// The optional <see cref="CancellationToken"/> to use for the HTTP request to obtain the response.
/// </param>
/// <returns>
/// A <see cref="Task{TResult}"/> representing the asynchronous operation to get a set of valid
/// antiforgery (CSRF/XSRF) tokens to use for HTTP POST requests to the test server.
/// </returns>
public async Task<AntiforgeryTokens> GetAntiforgeryTokensAsync(
Func<HttpClient>? httpClientFactory = null,
CancellationToken cancellationToken = default)
{
using var httpClient = httpClientFactory?.Invoke() ?? CreateClient();
using var response = await httpClient.GetAsync(AntiforgeryTokenController.GetTokensUri, cancellationToken).ConfigureAwait(false);
string json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return JsonSerializer.Deserialize<AntiforgeryTokens>(json);
}
/// <summary>
/// Clears the current <see cref="ITestOutputHelper"/>.
/// </summary>
public virtual void ClearOutputHelper()
=> Server.Services.GetRequiredService<ITestOutputHelperAccessor>().OutputHelper = null;
/// <summary>
/// Sets the <see cref="ITestOutputHelper"/> to use.
/// </summary>
/// <param name="value">The <see cref="ITestOutputHelper"/> to use.</param>
public virtual void SetOutputHelper(ITestOutputHelper value)
=> Server.Services.GetRequiredService<ITestOutputHelperAccessor>().OutputHelper = value;
/// <inheritdoc />
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAntiforgeryTokenResource()
.ConfigureLogging((loggingBuilder) => loggingBuilder.ClearProviders().AddXUnit());
}
}
}