Skip to content
Merged
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
16 changes: 16 additions & 0 deletions packages/insomnia-sdk/src/objects/__tests__/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,27 @@ describe('test request and response objects', () => {
resp.to.have.header('header1');
resp.to.have.jsonBody({ 'key': 888 });
resp.to.have.body('{"key": 888}');
resp.to.have.jsonSchema({
type: 'object',
properties: {
key: { type: 'integer' },
},
required: ['key'],
additionalProperties: false,
});

resp.to.not.have.status(201);
resp.to.not.have.status('NOT FOUND');
resp.to.not.have.header('header_nonexist');
resp.to.not.have.jsonBody({ 'key': 777 });
resp.to.not.have.body('{"key": 777}');
resp.to.not.have.jsonSchema({
type: 'object',
properties: {
keyNoExist: { type: 'integer' },
},
required: ['keyNoExist'],
additionalProperties: false,
});
});
});
17 changes: 17 additions & 0 deletions packages/insomnia-sdk/src/objects/response.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Ajv from 'ajv';
import deepEqual from 'deep-equal';
import { RESPONSE_CODE_REASONS } from 'insomnia/src/common/constants';
import { sendCurlAndWriteTimelineError, type sendCurlAndWriteTimelineResponse } from 'insomnia/src/network/network';
Expand Down Expand Up @@ -216,6 +217,20 @@ export class Response extends Property {
);
const haveBody = (expected: string, checkEquality: boolean) => verify(this.text(), expected, checkEquality);
const haveJsonBody = (expected: object, checkEquality: boolean) => verify(this.json(), expected, checkEquality);
const haveJsonSchema = (expected: object, checkEquality: boolean) => {
const ajv = new Ajv();

try {
const jsonBody = JSON.parse(this.body);
const schemaMatched = ajv.validate(expected, jsonBody);
if ((schemaMatched && checkEquality) || (!schemaMatched && !checkEquality)) {
return;
}
} catch (e) {
throw Error(`Failed to verify response body schema, response could not be a valid json: "${e}"`);
}
throw Error("Response's schema is not equal to the expected value");
};

return {
// follows extend chai's chains for compatibility
Expand All @@ -224,13 +239,15 @@ export class Response extends Property {
header: (expected: string) => haveHeader(expected, true),
body: (expected: string) => haveBody(expected, true),
jsonBody: (expected: object) => haveJsonBody(expected, true),
jsonSchema: (expected: object) => haveJsonSchema(expected, true),
},
not: {
have: {
status: (expected: number | string) => haveStatus(expected, false),
header: (expected: string) => haveHeader(expected, false),
body: (expected: string) => haveBody(expected, false),
jsonBody: (expected: object) => haveJsonBody(expected, false),
jsonSchema: (expected: object) => haveJsonSchema(expected, false),
},
},
};
Expand Down