Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
add collectionVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
kwokfu committed Sep 20, 2020
1 parent 68b094b commit eea0d3e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/shim/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,22 @@ const pm = Object.freeze({
},
}),

collectionVariables: Object.freeze({
clear() {
scope.collection = new Dict();
},
get(name) {
return scope.collection[name];
},
set(name, value) {
requireRequest();
scope.collection = scope.collection[Write](name, value);
},
unset(name) {
scope.collection = scope.collection[Clear](name);
},
}),

/**
* Evaluate variable
*
Expand All @@ -564,6 +580,7 @@ function exposePm() {
request: pm.request,
sendRequest: pm.sendRequest,
variables: pm.variables,
collectionVariables: pm.collectionVariables,
[Var]: pm[Var],
};
if (state.data) {
Expand Down
27 changes: 27 additions & 0 deletions test/com/shim/var.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,33 @@ test.serial('pm.variables.set set', t => {
});
});

test.serial('pm.collectionVariables.set scoped', t => {
t.throws(() => {
pm.collectionVariables.set('test', 'a');
});
});

test.serial('pm.collectionVariables.set clear', t => {
postman[Request]({
pre() {
t.is(pm.collectionVariables.get('test'), undef);
pm.collectionVariables.set('test', 'a');
t.is(pm.collectionVariables.get('test'), 'a');
}
});
});

test.serial('pm.collectionVariables.set set', t => {
postman[Request]({
pre() {
pm.collectionVariables.set('test', 'a');
t.is(pm.collectionVariables.get('test'), 'a');
pm.collectionVariables.set('test', 'b');
t.is(pm.collectionVariables.get('test'), 'b');
}
});
});

test.serial('pm[Var] simple', t => {
postman[Initial]({ global: { test: 'a' } });
t.is(pm[Var]('test'), 'a');
Expand Down

0 comments on commit eea0d3e

Please sign in to comment.