-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtoSatisfyApiSpec.ts
188 lines (172 loc) · 6.83 KB
/
toSatisfyApiSpec.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
EXPECTED_COLOR,
matcherHint,
MatcherHintOptions,
RECEIVED_COLOR,
} from 'jest-matcher-utils';
import {
ActualResponse,
ErrorCode,
makeResponse,
OpenApi2Spec,
OpenApi3Spec,
OpenApiSpec,
RawResponse,
ValidationError,
} from 'openapi-validator';
import { joinWithNewLines, stringify } from '../utils';
export default function (
this: jest.MatcherContext,
received: unknown,
openApiSpec: OpenApiSpec,
): jest.CustomMatcherResult {
const actualResponse = makeResponse(received as RawResponse);
const validationError = openApiSpec.validateResponse(actualResponse);
const pass = !validationError;
const matcherHintOptions: MatcherHintOptions = {
comment:
"Matches 'received' to a response defined in your API spec, then validates 'received' against it",
isNot: this.isNot,
promise: this.promise,
};
const hint = matcherHint(
'toSatisfyApiSpec',
undefined,
'',
matcherHintOptions,
);
const message = pass
? () =>
getExpectReceivedNotToSatisfyApiSpecMsg(
actualResponse,
openApiSpec,
hint,
)
: () =>
getExpectReceivedToSatisfyApiSpecMsg(
actualResponse,
openApiSpec,
validationError,
hint,
);
return {
pass,
message,
};
}
function getExpectReceivedToSatisfyApiSpecMsg(
actualResponse: ActualResponse,
openApiSpec: OpenApiSpec,
validationError: ValidationError,
hint: string,
): string {
const { status, req } = actualResponse;
const { method, path: requestPath } = req;
const unmatchedEndpoint = `${method} ${requestPath}`;
if (validationError.code === ErrorCode.ServerNotFound) {
// prettier-ignore
return joinWithNewLines(
hint,
`expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
`${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has no matching servers`,
`Servers found in API spec: ${EXPECTED_COLOR((openApiSpec as OpenApi3Spec).getServerUrls().join(', '))}`,
);
}
if (validationError.code === ErrorCode.BasePathNotFound) {
// prettier-ignore
return joinWithNewLines(
hint,
`expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
`${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has basePath ${EXPECTED_COLOR((openApiSpec as OpenApi2Spec).spec.basePath)}`,
);
}
if (validationError.code === ErrorCode.PathNotFound) {
// prettier-ignore
const pathNotFoundErrorMessage = joinWithNewLines(
hint,
`expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${unmatchedEndpoint}' in your API spec`,
`${RECEIVED_COLOR('received')} had request path ${RECEIVED_COLOR(requestPath)}, but your API spec has no matching path`,
`Paths found in API spec: ${EXPECTED_COLOR(openApiSpec.paths().join(', '))}`,
);
if (
'didUserDefineBasePath' in openApiSpec &&
openApiSpec.didUserDefineBasePath
) {
// prettier-ignore
return joinWithNewLines(
pathNotFoundErrorMessage,
`'${requestPath}' matches basePath \`${openApiSpec.spec.basePath}\` but no <basePath/endpointPath> combinations`,
);
}
if (
'didUserDefineServers' in openApiSpec &&
openApiSpec.didUserDefineServers
) {
return joinWithNewLines(
pathNotFoundErrorMessage,
`'${requestPath}' matches servers ${stringify(
openApiSpec.getMatchingServerUrls(requestPath),
)} but no <server/endpointPath> combinations`,
);
}
return pathNotFoundErrorMessage;
}
const path = openApiSpec.findOpenApiPathMatchingRequest(req);
const endpoint = `${method} ${path}`;
if (validationError.code === ErrorCode.MethodNotFound) {
const expectedPathItem = openApiSpec.findExpectedPathItem(req);
const expectedRequestOperations = Object.keys(expectedPathItem)
.map((operation) => operation.toUpperCase())
.join(', ');
// prettier-ignore
return joinWithNewLines(
hint,
`expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,
`${RECEIVED_COLOR('received')} had request method ${RECEIVED_COLOR(method)}, but your API spec has no ${RECEIVED_COLOR(method)} operation defined for path '${path}'`,
`Request operations found for path '${path}' in API spec: ${EXPECTED_COLOR(expectedRequestOperations)}`,
);
}
if (validationError.code === ErrorCode.StatusNotFound) {
const expectedResponseOperation =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
openApiSpec.findExpectedResponseOperation(req)!;
const expectedResponseStatuses = Object.keys(
expectedResponseOperation.responses,
).join(', ');
// prettier-ignore
return joinWithNewLines(
hint,
`expected ${RECEIVED_COLOR('received')} to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,
`${RECEIVED_COLOR('received')} had status ${RECEIVED_COLOR(status)}, but your API spec has no ${RECEIVED_COLOR(status)} or 'default' response defined for endpoint '${endpoint}'`,
`Response statuses found for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(expectedResponseStatuses)}`,
);
}
// validationError.code === ErrorCode.InvalidBody
const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);
// prettier-ignore
return joinWithNewLines(
hint,
`expected ${RECEIVED_COLOR('received')} to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,
`${RECEIVED_COLOR('received')} did not satisfy it because: ${validationError}`,
`${RECEIVED_COLOR('received')} contained: ${RECEIVED_COLOR(actualResponse.toString())}`,
`The '${status}' response defined for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(stringify(responseDefinition))}`,
);
}
function getExpectReceivedNotToSatisfyApiSpecMsg(
actualResponse: ActualResponse,
openApiSpec: OpenApiSpec,
hint: string,
): string {
const { status, req } = actualResponse;
const responseDefinition = openApiSpec.findExpectedResponse(actualResponse);
const endpoint = `${req.method} ${openApiSpec.findOpenApiPathMatchingRequest(
req,
)}`;
// prettier-ignore
return joinWithNewLines(
hint,
`expected ${RECEIVED_COLOR('received')} not to satisfy the '${status}' response defined for endpoint '${endpoint}' in your API spec`,
`${RECEIVED_COLOR('received')} contained: ${RECEIVED_COLOR(actualResponse.toString())}`,
`The '${status}' response defined for endpoint '${endpoint}' in API spec: ${EXPECTED_COLOR(stringify(responseDefinition))}`,
);
}