Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #226 Support default response status #271

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commonTestResources/exampleApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ app.get('/no/content-type/header/and/no/response/body', (_req, res) =>

export default app;

export const port = 5000;
export const port = 5001;
10 changes: 10 additions & 0 deletions commonTestResources/exampleOpenApiFiles/valid/openapi2.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@
}
}
},
"/responseStatus/default": {
"get": {
"parameters": [],
"responses": {
"default": {
"description": "No response body"
}
}
}
},
"/responseReferencesResponseDefinitionObject": {
"get": {
"produces": ["application/json"],
Expand Down
5 changes: 5 additions & 0 deletions commonTestResources/exampleOpenApiFiles/valid/openapi3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ paths:
description: No response body
204:
description: No response body
/responseStatus/default:
get:
responses:
default:
description: No response body
/HTTPMethod:
get:
responses:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function getExpectedResToSatisfyApiSpecMsg(
return joinWithNewLines(
hint,
`expected res to satisfy a '${status}' response defined for endpoint '${endpoint}' in your API spec`,
`res had status '${status}', but your API spec has no '${status}' response defined for endpoint '${endpoint}'`,
`res had status '${status}', but your API spec has no '${status}' or 'default' response defined for endpoint '${endpoint}'`,
`Response statuses found for endpoint '${endpoint}' in API spec: ${expectedResponseStatuses}`,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ openApiSpecs.forEach((spec) => {
expect(assertion).to.throw(
joinWithNewLines(
"expected res to satisfy a '204' response defined for endpoint 'GET /endpointPath' in your API spec",
"res had status '204', but your API spec has no '204' response defined for endpoint 'GET /endpointPath'",
"res had status '204', but your API spec has no '204' or 'default' response defined for endpoint 'GET /endpointPath'",
),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ openApiSpecs.forEach((spec) => {
joinWithNewLines(
'expected res to satisfy API spec',
"expected res to satisfy a '418' response defined for endpoint 'GET /responseStatus' in your API spec",
"res had status '418', but your API spec has no '418' response defined for endpoint 'GET /responseStatus'",
"res had status '418', but your API spec has no '418' or 'default' response defined for endpoint 'GET /responseStatus'",
"Response statuses found for endpoint 'GET /responseStatus' in API spec: 200, 204",
),
);
Expand All @@ -466,6 +466,30 @@ openApiSpecs.forEach((spec) => {
});
});

describe('res.status caught by default response', () => {
const res = {
status: 418,
req: {
method: 'GET',
path: '/responseStatus/default',
},
};
it('passes', () => {
expect(res).to.satisfyApiSpec;
});
it('fails when using .not', () => {
const assertion = () => expect(res).to.not.satisfyApiSpec;
expect(assertion).to.throw(
joinWithNewLines(
'expected res not to satisfy API spec',
"expected res not to satisfy the '418' response defined for endpoint 'GET /responseStatus/default' in your API spec",
'res contained: { body: undefined }',
"The '418' response defined for endpoint 'GET /responseStatus/default' in API spec: { '418': { description: 'No response body' } }",
),
);
});
});

describe('wrong res.body (multiple errors)', () => {
const res = {
status: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ openApiSpecs.forEach((spec) => {
// prettier-ignore
joinWithNewLines(
`expected ${red('received')} to satisfy a '204' response defined for endpoint 'GET /endpointPath' in your API spec`,
`${red('received')} had status ${red('204')}, but your API spec has no ${red('204')} response defined for endpoint 'GET /endpointPath'`,
`${red('received')} had status ${red('204')}, but your API spec has no ${red('204')} or 'default' response defined for endpoint 'GET /endpointPath'`,
),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ openApiSpecs.forEach((spec) => {
joinWithNewLines(
expectReceivedToSatisfyApiSpec,
`expected ${red('received')} to satisfy a '418' response defined for endpoint 'GET /responseStatus' in your API spec`,
`${red('received')} had status ${red('418')}, but your API spec has no ${red('418')} response defined for endpoint 'GET /responseStatus'`,
`${red('received')} had status ${red('418')}, but your API spec has no ${red('418')} or 'default' response defined for endpoint 'GET /responseStatus'`,
`Response statuses found for endpoint 'GET /responseStatus' in API spec: ${green('200, 204')}`,
),
);
Expand All @@ -508,6 +508,30 @@ openApiSpecs.forEach((spec) => {
});
});

describe('res.status caught by default response', () => {
const res = {
status: 418,
req: {
method: 'GET',
path: '/responseStatus/default',
},
};
it('passes', () => {
expect(res).toSatisfyApiSpec();
});
it('fails when using .not', () => {
const assertion = () => expect(res).not.toSatisfyApiSpec();
// prettier-ignore
expect(assertion).toThrow(
joinWithNewLines(
expectReceivedNotToSatisfyApiSpec,
`expected ${red('received')} not to satisfy the '418' response defined for endpoint 'GET /responseStatus/default' in your API spec`,
`${red('received')} contained: ${red(`{ body: undefined }`)}`,
`The '418' response defined for endpoint 'GET /responseStatus/default' in API spec: ${green(`{ '418': { description: 'No response body' } }`)}`,
))
});
});

describe('wrong res.body (multiple errors)', () => {
const res = {
status: 200,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-openapi/src/matchers/toSatisfyApiSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function getExpectReceivedToSatisfyApiSpecMsg(
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)} response defined for endpoint '${endpoint}'`,
`${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)}`,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export default abstract class OpenApiSpec {
responseOperation: Operation,
status: ActualResponse['status'],
): ResponseObjectWithSchema | undefined {
const response = responseOperation.responses[status];
const response =
responseOperation.responses[status] ||
responseOperation.responses.default;
if (!response) {
return undefined;
}
Expand Down