Skip to content

Commit

Permalink
fix: add a missing assertion chain for the response entity (#7474)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihexxa committed Jun 3, 2024
1 parent 1152458 commit 42cd078
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
7 changes: 6 additions & 1 deletion packages/insomnia-sdk/src/objects/__tests__/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('test request and response objects', () => {
body: '{"key": 888}',
stream: undefined,
responseTime: 100,
status: 'OK',
originalRequest: req,
});

Expand All @@ -69,5 +68,11 @@ 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.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}');
});
});
3 changes: 1 addition & 2 deletions packages/insomnia-sdk/src/objects/insomnia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ export async function initInsomniaObject(
const baseEnvironment = new Environment(rawObj.baseEnvironmentName || '', rawObj.baseEnvironment);
// TODO: update "iterationData" name when it is supported
const iterationData = new Environment('iterationData', rawObj.iterationData);
const collectionVariables = new Environment(rawObj.baseEnvironmentName || '', rawObj.baseEnvironment);
const cookies = new CookieObject(rawObj.cookieJar);
// TODO: update follows when post-request script and iterating are introduced
const requestInfo = new RequestInfo({
Expand All @@ -140,7 +139,7 @@ export async function initInsomniaObject(
const variables = new Variables({
globals,
environment,
collection: collectionVariables,
collection: baseEnvironment,
data: iterationData,
});

Expand Down
52 changes: 35 additions & 17 deletions packages/insomnia-sdk/src/objects/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,34 +186,52 @@ export class Response extends Property {

// Besides chai.expect, "to" is extended to support cases like:
// insomnia.response.to.have.status(200);
// insomnia.response.to.not.have.status(200);
get to() {
type valueType = boolean | number | string | object | undefined;
const verify = (got: valueType, expected: valueType) => {
if (['boolean', 'number', 'string', 'undefined'].includes(typeof got) && expected === got) {
return;
} else if (deepEqual(got, expected, { strict: true })) {

const verify = (got: valueType, expected: valueType, checkEquality: boolean = true) => {
if (['boolean', 'number', 'string', 'undefined'].includes(typeof got)) {
if ((checkEquality && expected === got) || (!checkEquality && expected !== got)) {
return;
}
} else if (
(checkEquality && deepEqual(got, expected, { strict: true })) ||
(!checkEquality && !deepEqual(got, expected, { strict: true }))
) {
return;
}
throw Error(`"${got}" is not equal to the expected value: "${expected}"`);
};
const haveStatus = (expected: number | string, checkEquality: boolean) => {
if (typeof expected === 'string') {
verify(this.status, expected, checkEquality);
} else {
verify(this.code, expected, checkEquality);
}
};
const haveHeader = (expected: string, checkEquality: boolean) => verify(
this.headers.toObject().find(header => header.key === expected) !== undefined,
checkEquality,
);
const haveBody = (expected: string, checkEquality: boolean) => verify(this.text(), expected, checkEquality);
const haveJsonBody = (expected: object, checkEquality: boolean) => verify(this.json(), expected, checkEquality);

return {
// follows extend chai's chains for compatibility
have: {
status: (expected: number | string) => {
if (typeof expected === 'string') {
verify(this.status, expected);
} else {
verify(this.code, expected);
}
status: (expected: number | string) => haveStatus(expected, true),
header: (expected: string) => haveHeader(expected, true),
body: (expected: string) => haveBody(expected, true),
jsonBody: (expected: object) => haveJsonBody(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),
},
header: (expected: string) => verify(
this.headers.toObject().find(header => header.key === expected) !== undefined,
true,
),

body: (expected: string) => verify(this.text(), expected),
jsonBody: (expected: object) => verify(this.json(), expected),
},
};
}
Expand Down

0 comments on commit 42cd078

Please sign in to comment.