Skip to content

Commit

Permalink
feat: rename skipRequest to stopExecution
Browse files Browse the repository at this point in the history
  • Loading branch information
vedkribhu committed Sep 7, 2023
1 parent e2e9c80 commit c82c0d1
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .cache/bootcode.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .cache/bootcode.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,6 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
});

return self;
},

skipRequest: function () {
onSkipRequest();
}

}, options.disabledAPIs);
Expand All @@ -279,6 +275,10 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
return chai.expect(this).to;
}
});

Object.defineProperty(this.request, 'stopExecution', {
value: onSkipRequest
});
}

iterationData = null; // precautionary
Expand Down
113 changes: 113 additions & 0 deletions test/unit/sandbox-libraries/pm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,119 @@ describe('sandbox library - pm api', function () {
}, done);
});

it('should not execute any line after pm.request.stopExecution in pre-request script', function (done) {
context.on('console', function (level, ...args) {
// console.log(args);
expect(args[1]).to.equal('pre-request log 1');
});
context.execute(`
preRequestScript: {
console.log('pre-request log 1');
pm.request.stopExecution();
console.log('pre-request log 2');
}
`, {
timeout: 200,
context: {
request: 'https://postman-echo.com/get?foo=bar'
}
}, function (err, execution) {
if (err) { return done(err); }
expect(execution).to.include({ shouldSkipExecution: true });

return done();
});
});

it(`should not execute any line after pm.request.stopExecution in pre-request script,
even if the pm.request.stopExecution invoked inside a try catch block`, function (done) {
context.on('console', function (level, ...args) {
// console.log(args);
expect(args[1]).to.equal('pre-request log 1');
});
context.execute(`
preRequestScript: {
console.log('pre-request log 1');
try {
pm.request.stopExecution();
} catch (err) {
// ignore
}
console.log('pre-request log 2');
}
`, {
timeout: 200,
context: {
request: 'https://postman-echo.com/get?foo=bar'
}
}, function (err, execution) {
if (err) { return done(err); }
expect(execution).to.include({ shouldSkipExecution: true });

return done();
});
});

it(`should not execute any line after pm.request.stopExecution in pre-request script,
even if the pm.request.stopExecution invoked inside an async function`, function (done) {
context.on('console', function (level, ...args) {
// console.log(args);
expect(args[1]).to.equal('pre-request log 1');
});
context.execute(`
preRequestScript: {
console.log('pre-request log 1');
async function myAsyncFunction() {
pm.request.stopExecution();
}
myAsyncFunction();
console.log('pre-request log 2');
}
`, {
timeout: 200,
context: {
request: 'https://postman-echo.com/get?foo=bar'
}
}, function (err, execution) {
if (err) { return done(err); }
expect(execution).to.include({ shouldSkipExecution: true });

return done();
});
});

it('should not reflect any variable change line after pm.request.stopExecution in pre-request script',
function (done) {
context.on('console', function (level, ...args) {
// console.log(args);
expect(args[1]).to.equal('pre-request log 1');
});
context.execute(`
preRequestScript: {
console.log('pre-request log 1');
pm.variables.set('foo', 'bar');
pm.request.stopExecution();
pm.variables.set('foo', 'nobar');
console.log('pre-request log 2');
}
`, {
timeout: 200,
context: {
request: 'https://postman-echo.com/get?foo=bar'
}
}, function (err, execution) {
if (err) { return done(err); }
expect(execution).to.include({ shouldSkipExecution: true });
expect(execution).to.deep.nested.include({ '_variables.values': [
{ value: 'bar', key: 'foo', type: 'any' }
] });
print(execution);

return done();
});
});

it('when serialized should not have assertion helpers added by sandbox', function (done) {
context.execute(`
var assert = require('assert'),
Expand Down

0 comments on commit c82c0d1

Please sign in to comment.