Skip to content

Commit e4ca01a

Browse files
ihexxastefancruz
authored andcommitted
fix: add jsonSchema assertion chain (Kong#7481)
1 parent ec23412 commit e4ca01a

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

packages/insomnia-sdk/src/objects/__tests__/response.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,27 @@ describe('test request and response objects', () => {
6868
resp.to.have.header('header1');
6969
resp.to.have.jsonBody({ 'key': 888 });
7070
resp.to.have.body('{"key": 888}');
71+
resp.to.have.jsonSchema({
72+
type: 'object',
73+
properties: {
74+
key: { type: 'integer' },
75+
},
76+
required: ['key'],
77+
additionalProperties: false,
78+
});
7179

7280
resp.to.not.have.status(201);
7381
resp.to.not.have.status('NOT FOUND');
7482
resp.to.not.have.header('header_nonexist');
7583
resp.to.not.have.jsonBody({ 'key': 777 });
7684
resp.to.not.have.body('{"key": 777}');
85+
resp.to.not.have.jsonSchema({
86+
type: 'object',
87+
properties: {
88+
keyNoExist: { type: 'integer' },
89+
},
90+
required: ['keyNoExist'],
91+
additionalProperties: false,
92+
});
7793
});
7894
});

packages/insomnia-sdk/src/objects/response.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Ajv from 'ajv';
12
import deepEqual from 'deep-equal';
23
import { RESPONSE_CODE_REASONS } from 'insomnia/src/common/constants';
34
import { sendCurlAndWriteTimelineError, type sendCurlAndWriteTimelineResponse } from 'insomnia/src/network/network';
@@ -216,6 +217,20 @@ export class Response extends Property {
216217
);
217218
const haveBody = (expected: string, checkEquality: boolean) => verify(this.text(), expected, checkEquality);
218219
const haveJsonBody = (expected: object, checkEquality: boolean) => verify(this.json(), expected, checkEquality);
220+
const haveJsonSchema = (expected: object, checkEquality: boolean) => {
221+
const ajv = new Ajv();
222+
223+
try {
224+
const jsonBody = JSON.parse(this.body);
225+
const schemaMatched = ajv.validate(expected, jsonBody);
226+
if ((schemaMatched && checkEquality) || (!schemaMatched && !checkEquality)) {
227+
return;
228+
}
229+
} catch (e) {
230+
throw Error(`Failed to verify response body schema, response could not be a valid json: "${e}"`);
231+
}
232+
throw Error("Response's schema is not equal to the expected value");
233+
};
219234

220235
return {
221236
// follows extend chai's chains for compatibility
@@ -224,13 +239,15 @@ export class Response extends Property {
224239
header: (expected: string) => haveHeader(expected, true),
225240
body: (expected: string) => haveBody(expected, true),
226241
jsonBody: (expected: object) => haveJsonBody(expected, true),
242+
jsonSchema: (expected: object) => haveJsonSchema(expected, true),
227243
},
228244
not: {
229245
have: {
230246
status: (expected: number | string) => haveStatus(expected, false),
231247
header: (expected: string) => haveHeader(expected, false),
232248
body: (expected: string) => haveBody(expected, false),
233249
jsonBody: (expected: object) => haveJsonBody(expected, false),
250+
jsonSchema: (expected: object) => haveJsonSchema(expected, false),
234251
},
235252
},
236253
};

0 commit comments

Comments
 (0)