Skip to content

Commit

Permalink
Add support for pm.connection and pm.message
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Jun 13, 2023
1 parent 2759dd3 commit e919335
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lib/sandbox/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ module.exports = function (bridge, glob) {

bridge.dispatch(assertionEventName, options.cursor, assertions);
bridge.dispatch(EXECUTION_ASSERTION_EVENT, options.cursor, assertions);
},

// TODO: Handle timers
dispatchConnectionEvents = {
sendMessage (...args) {
bridge.dispatch('connection.sendMessage.' + id, ...args);
},
close (...args) {
bridge.dispatch('connection.close.' + id, ...args);
}
};

let waiting,
Expand Down Expand Up @@ -214,7 +224,8 @@ module.exports = function (bridge, glob) {
var eventId = timers.setEvent(callback);

bridge.dispatch(executionRequestEventName, options.cursor, id, eventId, request);
}, dispatchAssertions, new PostmanCookieStore(id, bridge, timers), {
}, dispatchAssertions, dispatchConnectionEvents,
new PostmanCookieStore(id, bridge, timers), {
disabledAPIs: initializationOptions.disabledAPIs
})
),
Expand Down
3 changes: 2 additions & 1 deletion lib/sandbox/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ class Execution {
});

if (options.initializeExecution) {
const { request, response } = options.initializeExecution(this.target, context) || {};
const { request, response, message } = options.initializeExecution(this.target, context) || {};

this.request = request;
this.response = response;
this.message = message;
}
else {
if (TARGETS_WITH_REQUEST[this.target] || _.has(context, PROPERTY.REQUEST)) {
Expand Down
19 changes: 17 additions & 2 deletions lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ const _ = require('lodash'),
* @param {Execution} execution -
* @param {Function} onRequest -
* @param {Function} onAssertion -
* @param {Object} onConnection -
* @param {Object} cookieStore -
* @param {Object} [options] -
* @param {Array.<String>} [options.disabledAPIs] -
*/
function Postman (execution, onRequest, onAssertion, cookieStore, options = {}) {
function Postman (execution, onRequest, onAssertion, onConnection, cookieStore, options = {}) {
// @todo - ensure runtime passes data in a scope format
let iterationData = new VariableScope();

Expand Down Expand Up @@ -167,6 +168,8 @@ function Postman (execution, onRequest, onAssertion, cookieStore, options = {})
*/
response: execution.response,

message: execution.message,

/**
* The cookies object contains a list of cookies that are associated with the domain
* to which the request was made.
Expand Down Expand Up @@ -250,7 +253,9 @@ function Postman (execution, onRequest, onAssertion, cookieStore, options = {})
});

return self;
}
},

connection: onConnection
}, options.disabledAPIs);

// extend pm api with test runner abilities
Expand All @@ -265,6 +270,16 @@ function Postman (execution, onRequest, onAssertion, cookieStore, options = {})
}
});
}

if (this.message) {
// these are removed before serializing see `purse.js`
Object.defineProperty(this.message, 'to', {
get () {
return chai.expect(this).to;
}
});
}

// add request assertions
if (this.request) {
// these are removed before serializing see `purse.js`
Expand Down

0 comments on commit e919335

Please sign in to comment.