|
1 | 1 | const sdk = require('postman-collection'), |
| 2 | + sinon = require('sinon').createSandbox(), |
2 | 3 | collectionRunner = require('../../../lib/runner'); |
3 | 4 |
|
4 | 5 | describe('pm.execution.runRequest handling', function () { |
@@ -30,7 +31,7 @@ describe('pm.execution.runRequest handling', function () { |
30 | 31 | new collectionRunner().run(collection, |
31 | 32 | { |
32 | 33 | script: { |
33 | | - requestResolver (_requestId, callback) { |
| 34 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
34 | 35 | callback(null, { |
35 | 36 | item: { |
36 | 37 | id: 'nested-request-id', |
@@ -89,6 +90,66 @@ describe('pm.execution.runRequest handling', function () { |
89 | 90 | }); |
90 | 91 | }); |
91 | 92 |
|
| 93 | + it('should provide req metadata from event.requests object to resolver along with root req id', function (done) { |
| 94 | + const requestsMeta = { |
| 95 | + // Refer to postman-collection package to know more about this field |
| 96 | + 'nested-request-id': { |
| 97 | + location: ['Collection Name', 'Folder Name', 'Request Name'] |
| 98 | + } |
| 99 | + }, |
| 100 | + collection = new sdk.Collection({ |
| 101 | + item: [{ |
| 102 | + id: 'root-request-id', |
| 103 | + event: [{ |
| 104 | + listen: 'prerequest', |
| 105 | + script: { |
| 106 | + exec: ` |
| 107 | + await pm.execution.runRequest("nested-request-id"); |
| 108 | + await pm.execution.runRequest("nested-request-id-without-fallback"); |
| 109 | + `, |
| 110 | + requests: requestsMeta |
| 111 | + } |
| 112 | + }], |
| 113 | + request: { |
| 114 | + url: 'https://postman-echo.com/get', |
| 115 | + method: 'GET' |
| 116 | + } |
| 117 | + }] |
| 118 | + }); |
| 119 | + |
| 120 | + new collectionRunner().run(collection, { |
| 121 | + script: { |
| 122 | + requestResolver: (nestedRequestId, nestedRequestContext, callback) => { |
| 123 | + expect(typeof callback).to.eql('function'); |
| 124 | + expect(nestedRequestContext).to.be.ok; |
| 125 | + expect(nestedRequestContext.rootItemId).to.eql('root-request-id'); |
| 126 | + |
| 127 | + if (nestedRequestId === 'nested-request-id') { |
| 128 | + expect(nestedRequestContext.nestedRequestMeta).to.eql(requestsMeta['nested-request-id']); |
| 129 | + } |
| 130 | + |
| 131 | + if (nestedRequestId === 'nested-request-id-without-fallback') { |
| 132 | + // Should be empty for this request |
| 133 | + expect(nestedRequestContext.nestedRequestMeta).to.not.be.ok; |
| 134 | + } |
| 135 | + |
| 136 | + return callback(null, { |
| 137 | + item: { |
| 138 | + id: 'nested-request-id', |
| 139 | + request: { url: 'https://postman-echo.com/post', method: 'POST' } |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + } |
| 144 | + }, function (_err, run) { |
| 145 | + run.start({ |
| 146 | + done (err) { |
| 147 | + done(err); |
| 148 | + } |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
92 | 153 | it('should handle for exceptions thrown from nested request parsing or uncaught errors', function (done) { |
93 | 154 | const collection = new sdk.Collection({ |
94 | 155 | item: [{ |
@@ -116,7 +177,7 @@ describe('pm.execution.runRequest handling', function () { |
116 | 177 | new collectionRunner().run(collection, |
117 | 178 | { |
118 | 179 | script: { |
119 | | - requestResolver (_requestId, callback) { |
| 180 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
120 | 181 | callback(null, { |
121 | 182 | item: { |
122 | 183 | id: 'nested-request-id', |
@@ -171,7 +232,7 @@ describe('pm.execution.runRequest handling', function () { |
171 | 232 | new collectionRunner().run(collection, |
172 | 233 | { |
173 | 234 | script: { |
174 | | - requestResolver (_requestId, callback) { |
| 235 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
175 | 236 | callback(null, { |
176 | 237 | item: { |
177 | 238 | id: 'nested-request-id', |
@@ -236,7 +297,7 @@ describe('pm.execution.runRequest handling', function () { |
236 | 297 | new collectionRunner().run(collection, |
237 | 298 | { |
238 | 299 | script: { |
239 | | - requestResolver (_requestId, callback) { |
| 300 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
240 | 301 | callback(null, { |
241 | 302 | item: { |
242 | 303 | id: 'nested-request-id', |
@@ -330,7 +391,7 @@ describe('pm.execution.runRequest handling', function () { |
330 | 391 | new collectionRunner().run(collection, |
331 | 392 | { |
332 | 393 | script: { |
333 | | - requestResolver (_requestId, callback) { |
| 394 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
334 | 395 | callback(null, { |
335 | 396 | item: { |
336 | 397 | id: 'nested-request-id', |
@@ -411,7 +472,7 @@ describe('pm.execution.runRequest handling', function () { |
411 | 472 | new collectionRunner().run(collection, |
412 | 473 | { |
413 | 474 | script: { |
414 | | - requestResolver (_requestId, callback) { |
| 475 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
415 | 476 | callback(null, { |
416 | 477 | item: { |
417 | 478 | id: 'nested-request-id', |
@@ -476,7 +537,7 @@ describe('pm.execution.runRequest handling', function () { |
476 | 537 | new collectionRunner().run(collection, |
477 | 538 | { |
478 | 539 | script: { |
479 | | - requestResolver (_requestId, callback) { |
| 540 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
480 | 541 | callback(null, { |
481 | 542 | item: { |
482 | 543 | id: 'nested-request-id', |
@@ -526,7 +587,7 @@ describe('pm.execution.runRequest handling', function () { |
526 | 587 | new collectionRunner().run(collection, |
527 | 588 | { |
528 | 589 | script: { |
529 | | - requestResolver (_requestId, callback) { |
| 590 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
530 | 591 | callback(null, { |
531 | 592 | item: { |
532 | 593 | id: 'nested-request-id', |
@@ -594,7 +655,7 @@ describe('pm.execution.runRequest handling', function () { |
594 | 655 | new collectionRunner().run(collection, |
595 | 656 | { |
596 | 657 | script: { |
597 | | - requestResolver (_requestId, callback) { |
| 658 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
598 | 659 | callback(null, { |
599 | 660 | item: { |
600 | 661 | id: 'nested-request-id', |
@@ -682,7 +743,7 @@ describe('pm.execution.runRequest handling', function () { |
682 | 743 | { |
683 | 744 | vaultSecrets: vaultSecrets, |
684 | 745 | script: { |
685 | | - requestResolver (_requestId, callback) { |
| 746 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
686 | 747 | callback(null, { |
687 | 748 | item: { |
688 | 749 | id: 'nested-request-id', |
@@ -760,7 +821,7 @@ describe('pm.execution.runRequest handling', function () { |
760 | 821 | new collectionRunner().run(collection, |
761 | 822 | { |
762 | 823 | script: { |
763 | | - requestResolver (_requestId, callback) { |
| 824 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
764 | 825 | callback(null, { |
765 | 826 | item: { |
766 | 827 | id: 'nested-request-id', |
@@ -832,7 +893,7 @@ describe('pm.execution.runRequest handling', function () { |
832 | 893 | new collectionRunner().run(collection, |
833 | 894 | { |
834 | 895 | script: { |
835 | | - requestResolver (_requestId, callback) { |
| 896 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
836 | 897 | callback(null, { |
837 | 898 | item: { |
838 | 899 | id: 'nested-request-id', |
@@ -885,4 +946,62 @@ describe('pm.execution.runRequest handling', function () { |
885 | 946 | }); |
886 | 947 | }); |
887 | 948 | }); |
| 949 | + |
| 950 | + it('should not invoke any events if root request is cancelled', function (done) { |
| 951 | + const collection = new sdk.Collection({ |
| 952 | + item: [{ |
| 953 | + event: [ |
| 954 | + { |
| 955 | + listen: 'prerequest', |
| 956 | + script: { exec: 'await pm.execution.runRequest("nested-request-id");' } |
| 957 | + } |
| 958 | + ], |
| 959 | + request: { |
| 960 | + url: 'https://postman-echo.com/get', |
| 961 | + method: 'GET' |
| 962 | + } |
| 963 | + }] |
| 964 | + }); |
| 965 | + |
| 966 | + new collectionRunner().run(collection, |
| 967 | + { |
| 968 | + script: { |
| 969 | + requestResolver (_requestId, _nestedRequestContext, callback) { |
| 970 | + callback(null, { |
| 971 | + item: { |
| 972 | + id: 'nested-request-id', |
| 973 | + request: { |
| 974 | + url: 'https://postman-echo.com/post', |
| 975 | + method: 'POST' |
| 976 | + } |
| 977 | + } |
| 978 | + }); |
| 979 | + } |
| 980 | + } |
| 981 | + }, |
| 982 | + function (_err, run) { |
| 983 | + const callbacks = { |
| 984 | + start: sinon.spy(), |
| 985 | + abort: sinon.spy(), |
| 986 | + request: sinon.spy() |
| 987 | + }; |
| 988 | + |
| 989 | + callbacks.done = sinon.spy(function () { |
| 990 | + expect(run.state.nestedRequest).to.be.undefined; |
| 991 | + expect(callbacks).to.be.ok; |
| 992 | + expect(callbacks.done.getCall(0).args[0]).to.be.null; |
| 993 | + expect(callbacks).to.nested.include({ |
| 994 | + 'start.calledOnce': true, |
| 995 | + 'abort.calledOnce': true, |
| 996 | + 'request.calledOnce': false |
| 997 | + }); |
| 998 | + |
| 999 | + return done(); |
| 1000 | + }); |
| 1001 | + |
| 1002 | + run.start(callbacks); |
| 1003 | + // Abort root run |
| 1004 | + run.abort(); |
| 1005 | + }); |
| 1006 | + }); |
888 | 1007 | }); |
0 commit comments