-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathResponseValidator.cs
More file actions
103 lines (88 loc) · 3.57 KB
/
ResponseValidator.cs
File metadata and controls
103 lines (88 loc) · 3.57 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
using System.Collections.Specialized;
using Microsoft.OpenApi;
#if NET11_0_OR_GREATER
using OpenApiMediaType = Microsoft.OpenApi.IOpenApiMediaType;
#endif
namespace Swashbuckle.AspNetCore.ApiTesting;
public sealed class ResponseValidator(IEnumerable<IContentValidator> contentValidators)
{
private readonly IEnumerable<IContentValidator> _contentValidators = contentValidators;
public void Validate(
HttpResponseMessage response,
OpenApiDocument openApiDocument,
string pathTemplate,
HttpMethod operationType,
string expectedStatusCode)
{
var operationSpec = openApiDocument.GetOperationByPathAndType(pathTemplate, operationType, out _);
if (!operationSpec.Responses.TryGetValue(expectedStatusCode, out var responseSpec))
{
throw new InvalidOperationException($"Response for status '{expectedStatusCode}' not found for operation '{operationSpec.OperationId}'");
}
var statusCode = (int)response.StatusCode;
if (statusCode.ToString() != expectedStatusCode)
{
throw new ResponseDoesNotMatchSpecException($"Status code '{statusCode}' does not match expected value '{expectedStatusCode}'");
}
ValidateHeaders(responseSpec.Headers, response.Headers.ToNameValueCollection());
if (responseSpec.Content != null && responseSpec.Content.Keys.Count != 0)
{
ValidateContent(responseSpec.Content, openApiDocument, response.Content);
}
}
private static void ValidateHeaders(
IDictionary<string, IOpenApiHeader> headerSpecs,
NameValueCollection headerValues)
{
if (headerSpecs is null)
{
return;
}
foreach (var entry in headerSpecs)
{
var value = headerValues[entry.Key];
var headerSpec = entry.Value;
if (headerSpec.Required && value == null)
{
throw new ResponseDoesNotMatchSpecException($"Required header '{entry.Key}' is not present");
}
if (value == null || headerSpec.Schema == null)
{
continue;
}
if (headerSpec.Schema is OpenApiSchema schema &&
!schema.TryParse(value, out object typedValue))
{
throw new ResponseDoesNotMatchSpecException($"Header '{entry.Key}' is not of type '{schema.TypeIdentifier()}'");
}
}
}
private void ValidateContent(
IDictionary<string, OpenApiMediaType> contentSpecs,
OpenApiDocument openApiDocument,
HttpContent content)
{
if (content == null || content?.Headers?.ContentLength == 0)
{
throw new RequestDoesNotMatchSpecException("Expected content is not present");
}
if (!contentSpecs.TryGetValue(content.Headers.ContentType.MediaType, out var mediaTypeSpec))
{
throw new ResponseDoesNotMatchSpecException($"Content media type '{content.Headers.ContentType.MediaType}' is not specified");
}
try
{
foreach (var contentValidator in _contentValidators)
{
if (contentValidator.CanValidate(content.Headers.ContentType.MediaType))
{
contentValidator.Validate(mediaTypeSpec, openApiDocument, content);
}
}
}
catch (ContentDoesNotMatchSpecException contentException)
{
throw new ResponseDoesNotMatchSpecException($"Content does not match spec. {contentException.Message}");
}
}
}