-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathOpenApiTestContext.cs
More file actions
110 lines (88 loc) · 3.62 KB
/
OpenApiTestContext.cs
File metadata and controls
110 lines (88 loc) · 3.62 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System.Reflection;
using System.Text.Json;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TestBuildingBlocks;
using Xunit.Abstractions;
namespace OpenApiTests;
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public class OpenApiTestContext<TStartup, TDbContext> : IntegrationTestContext<TStartup, TDbContext>
where TStartup : class
where TDbContext : TestableDbContext
{
private readonly Lazy<Task<JsonElement>> _lazySwaggerDocument;
private ITestOutputHelper? _testOutputHelper;
internal string? SwaggerDocumentOutputDirectory { get; set; }
public OpenApiTestContext()
{
_lazySwaggerDocument = new Lazy<Task<JsonElement>>(CreateSwaggerDocumentAsync, LazyThreadSafetyMode.ExecutionAndPublication);
}
internal async Task<JsonElement> GetSwaggerDocumentAsync()
{
return await _lazySwaggerDocument.Value;
}
internal async Task<JsonElement> CreateSwaggerDocumentAsync()
{
string content = await GetAsync("/swagger/v1/swagger.json");
JsonElement rootElement = ParseSwaggerDocument(content);
if (SwaggerDocumentOutputDirectory != null)
{
string absoluteOutputPath = GetSwaggerDocumentAbsoluteOutputPath(SwaggerDocumentOutputDirectory);
await WriteToDiskAsync(absoluteOutputPath, rootElement);
}
return rootElement;
}
internal void SetTestOutputHelper(ITestOutputHelper testOutputHelper)
{
ArgumentNullException.ThrowIfNull(testOutputHelper);
_testOutputHelper = testOutputHelper;
ConfigureLogging(AddXUnitProvider);
}
private void AddXUnitProvider(ILoggingBuilder loggingBuilder)
{
if (_testOutputHelper != null)
{
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
loggingBuilder.Services.AddSingleton<ILoggerProvider>(_ => new XUnitLoggerProvider(_testOutputHelper, "JsonApiDotNetCore.OpenApi.Swashbuckle"));
}
}
private static string GetSwaggerDocumentAbsoluteOutputPath(string relativePath)
{
string testRootDirectory = Path.Combine(Assembly.GetExecutingAssembly().Location, "../../../../../");
string outputPath = Path.Combine(testRootDirectory, relativePath, "swagger.g.json");
return Path.GetFullPath(outputPath);
}
private async Task<string> GetAsync(string requestUrl)
{
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
using HttpClient client = Factory.CreateClient();
using HttpResponseMessage responseMessage = await client.SendAsync(request);
return await responseMessage.Content.ReadAsStringAsync();
}
private static JsonElement ParseSwaggerDocument(string content)
{
using JsonDocument jsonDocument = JsonDocument.Parse(content);
return jsonDocument.RootElement.Clone();
}
private static async Task WriteToDiskAsync(string path, JsonElement jsonElement)
{
while (true)
{
try
{
string directory = Path.GetDirectoryName(path)!;
Directory.CreateDirectory(directory);
string contents = jsonElement.ToString();
await File.WriteAllTextAsync(path, contents);
return;
}
catch (IOException)
{
// This sometimes happens when running tests locally.
// Multi-targeted projects should not use the same output path.
await Task.Delay(TimeSpan.FromMilliseconds(50));
}
}
}
}