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: add a missing assertion chain for the response entity - INS-3917 #7474

Merged
merged 2 commits into from
Jun 3, 2024
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is collection accepting environments? If so maybe we should change the name to make more clear

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, it tries to keep one one base environment instance so that operations triggered from variables would also be applied to the base environment.
Let me start another PR to rename them.

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
Loading