-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathResponseValidator.cs
More file actions
109 lines (94 loc) · 4.21 KB
/
ResponseValidator.cs
File metadata and controls
109 lines (94 loc) · 4.21 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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net.Http;
using Microsoft.OpenApi.Models;
namespace Swashbuckle.AspNetCore.ApiTesting
{
public class ResponseValidator(IEnumerable<IContentValidator> contentValidators)
{
private readonly IEnumerable<IContentValidator> _contentValidators = contentValidators;
public void Validate(
HttpResponseMessage response,
OpenApiDocument openApiDocument,
string pathTemplate,
OperationType operationType,
string expectedStatusCode)
{
var operationSpec = openApiDocument.GetOperationByPathAndType(pathTemplate, operationType, out _);
if (!operationSpec.Responses.TryGetValue(expectedStatusCode, out OpenApiResponse 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, openApiDocument, response.Headers.ToNameValueCollection());
if (responseSpec.Content != null && responseSpec.Content.Keys.Count != 0)
{
ValidateContent(responseSpec.Content, openApiDocument, response.Content);
}
}
private static void ValidateHeaders(
IDictionary<string, OpenApiHeader> headerSpecs,
OpenApiDocument openApiDocument,
NameValueCollection headerValues)
{
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;
}
var schema = (headerSpec.Schema.Reference != null) ?
(OpenApiSchema)openApiDocument.ResolveReference(headerSpec.Schema.Reference)
: headerSpec.Schema;
if (value == null)
{
continue;
}
if (!schema.TryParse(value, out object typedValue))
{
throw new ResponseDoesNotMatchSpecException($"Header '{entry.Key}' is not of type '{headerSpec.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 OpenApiMediaType 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}");
}
}
}
public class ResponseDoesNotMatchSpecException(string message) : Exception(message);
}