Skip to content

Commit

Permalink
add pm.message
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhaviv committed Mar 10, 2025
1 parent 7d26802 commit 9ce6077
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
22 changes: 20 additions & 2 deletions lib/sandbox/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const _ = require('lodash'),
SCRIPT: 'script',
DATA: 'data',
COOKIES: 'cookies',
RESPONSE: 'response'
RESPONSE: 'response',
MESSAGE: 'message'
},

TARGETS_WITH_REQUEST = {
Expand All @@ -18,6 +19,11 @@ const _ = require('lodash'),
test: true
},

TARGETS_WITH_MESSAGE = {
onIncomingMessage: true,
onOutgoingMessage: true
},

CONTEXT_VARIABLE_SCOPES = ['_variables', 'environment', 'collectionVariables', 'globals'],

trackingOptions = { autoCompact: true };
Expand All @@ -42,10 +48,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;

Check warning on line 55 in lib/sandbox/execution.js

View check run for this annotation

Codecov / codecov/patch

lib/sandbox/execution.js#L55

Added line #L55 was not covered by tests
}
else {
if (TARGETS_WITH_REQUEST[this.target] || _.has(context, PROPERTY.REQUEST)) {
Expand All @@ -68,6 +75,17 @@ class Execution {
this.response = sdk.Response.isResponse(context.response) ?
context.response : new sdk.Response(context.response);
}

if (TARGETS_WITH_MESSAGE[this.target] || _.has(context, PROPERTY.MESSAGE)) {
/**
* @note:
* this reference is passed on as `pm.message`, pm api adds helper functions like `to` to `pm.response`
* sandbox overrides collection Response.prototype.toJSON to remove helpers before toJSON,
* see `purse.js`
*/
this.message = sdk.message?.isMessage(context.message) ?

Check warning on line 86 in lib/sandbox/execution.js

View check run for this annotation

Codecov / codecov/patch

lib/sandbox/execution.js#L86

Added line #L86 was not covered by tests
context.message : sdk.Message && new sdk.Message(context.message);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @license Copyright 2016 Postdot Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in complian ce with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
Expand Down
18 changes: 18 additions & 0 deletions lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
*/
response: execution.response,

/**
* Only available in on message sent and received scripts. pm.message contains information about
* an individual message (varies by protocol) received from a server or sent from the client
* that the script is being run on.
*
* @type {Object}
*/
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 @@ -398,6 +407,15 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
}
});
}
// add message assertions
if (this.message) {
// these are removed before serializing see `purse.js`
Object.defineProperty(this.message, 'to', {
get () {
return chai.expect(this).to;

Check warning on line 415 in lib/sandbox/pmapi.js

View check run for this annotation

Codecov / codecov/patch

lib/sandbox/pmapi.js#L413-L415

Added lines #L413 - L415 were not covered by tests
}
});
}

iterationData = null; // precautionary
}
Expand Down
10 changes: 10 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ declare class Postman {
* to the response that was received.
*/
response: Response;
/**
* Only available in on message sent and received scripts. pm.message contains information about
* an individual message (varies by protocol) received from a server or sent from the client
* that the script is being run on.
*
* TODO: define message type
* @type {Object}
*/
message: any;
/**
* The cookies object contains a list of cookies that are associated with the domain
* to which the request was made.
Expand Down Expand Up @@ -284,6 +293,7 @@ declare interface Visualizer {
declare interface Execution {
request: any;
response: any;
message: any;
/**
* Stops the current request and its scripts from executing.
*/
Expand Down

0 comments on commit 9ce6077

Please sign in to comment.