-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathApiTestRunnerBase.cs
More file actions
96 lines (80 loc) · 3.44 KB
/
ApiTestRunnerBase.cs
File metadata and controls
96 lines (80 loc) · 3.44 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
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader;
using Microsoft.OpenApi.Readers;
using Microsoft.OpenApi.Writers;
namespace Swashbuckle.AspNetCore.ApiTesting
{
public abstract class ApiTestRunnerBase : IDisposable
{
static ApiTestRunnerBase()
{
// TODO Make an assembly fixture
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
}
private readonly ApiTestRunnerOptions _options;
private readonly RequestValidator _requestValidator;
private readonly ResponseValidator _responseValidator;
protected ApiTestRunnerBase()
{
_options = new ApiTestRunnerOptions();
_requestValidator = new RequestValidator(_options.ContentValidators);
_responseValidator = new ResponseValidator(_options.ContentValidators);
}
public void Configure(Action<ApiTestRunnerOptions> setupAction)
{
setupAction(_options);
}
public void ConfigureOperation(
string documentName,
string pathTemplate,
OperationType operationType,
OpenApiOperation operation)
{
var openApiDocument = _options.GetOpenApiDocument(documentName);
if (openApiDocument.Paths == null)
openApiDocument.Paths = new OpenApiPaths();
if (!openApiDocument.Paths.TryGetValue(pathTemplate, out OpenApiPathItem pathItem))
{
pathItem = new OpenApiPathItem();
openApiDocument.Paths.Add(pathTemplate, pathItem);
}
pathItem.AddOperation(operationType, operation);
}
public async Task TestAsync(
string documentName,
string operationId,
string expectedStatusCode,
HttpRequestMessage request,
HttpClient httpClient)
{
var openApiDocument = _options.GetOpenApiDocument(documentName);
if (!openApiDocument.TryFindOperationById(operationId, out string pathTemplate, out OperationType operationType))
throw new InvalidOperationException($"Operation with id '{operationId}' not found in OpenAPI document '{documentName}'");
if (expectedStatusCode.StartsWith("2"))
_requestValidator.Validate(request, openApiDocument, pathTemplate, operationType);
var response = await httpClient.SendAsync(request);
_responseValidator.Validate(response, openApiDocument, pathTemplate, operationType, expectedStatusCode);
}
public void Dispose()
{
if (!_options.GenerateOpenApiFiles) return;
if (_options.FileOutputRoot == null)
throw new Exception("GenerateOpenApiFiles set but FileOutputRoot is null");
foreach (var entry in _options.OpenApiDocs)
{
var outputDir = Path.Combine(_options.FileOutputRoot, entry.Key);
Directory.CreateDirectory(outputDir);
using (var streamWriter = new StreamWriter(Path.Combine(outputDir, "openapi.json")))
{
var openApiJsonWriter = new OpenApiJsonWriter(streamWriter);
entry.Value.SerializeAsV3(openApiJsonWriter);
streamWriter.Close();
}
}
}
}
}