-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
332 lines (285 loc) · 9.51 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict';
var runtime = require('postman-runtime');
var sdk = require('postman-collection');
var Collection = require('postman-collection').Collection,
myCollection;
var collectionJson = {
"variables": [],
"info": {
"name": "Test API",
"_postman_id": "48430400-3a9b-28dc-4f16-c1c404eee237",
"description": "version=0.0.1 - Description for Test API",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "http:///order?status={{status}}&page={{page}}",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"var jsonData = JSON.parse(responseBody);",
"console.log(\"jsonData: \" +JSON.stringify(jsonData));",
]
}
},
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
"",
]
}
}
],
"request": {
"url": {
"raw": "https://httpbin.org/get",
"protocol": "https",
"host": [
"httpbin.org"
],
"port": "443",
"path": [
"get"
],
"query": [],
"variable": []
},
"method": "GET",
"header": [],
"body": {},
"description": "Simple get to httpbin.org"
}
}
]
}
myCollection = new Collection(collectionJson);
console.log('Collection is now created !')
var callbacks = {
// Called any time we see a new assertion in the test scripts
// *note* Not used yet.
assertion: function (name, result) {
// name: string
// result: Boolean
},
// Called when the run begins
start: function (err, cursor) {
// err: null or Error
// cursor = {
// position: Number,
// iteration: Number,
// length: Number,
// cycles: Number,
// eof: Boolean,
// empty: Boolean,
// bof: Boolean,
// cr: Boolean,
// ref: String
// }
},
// Called before starting a new iteration
beforeIteration: function (err, cursor) {
/* Same as arguments for "start" */
},
// Called when an iteration is completed
iteration: function (err, cursor) {
/* Same as arguments for "start" */
},
// Called before running a new Item (check the postman collection v2 format for what Item means)
beforeItem: function (err, cursor, item) {
// err, cursor: Same as arguments for "start"
// item: sdk.Item
},
// Called after completion of an Item
item: function (err, cursor, item) {
/* Same as arguments for "beforeItem" */
},
// Called before running pre-request script(s) (Yes, Runtime supports multiple pre-request scripts!)
beforePrerequest: function (err, cursor, events, item) {
// err, cursor: Same as arguments for "start"
// events: Array of sdk.Event objects
// item: sdk.Item
},
// Called after running pre-request script(s)
prerequest: function (err, cursor, results, item) {
// err, cursor: Same as arguments for "start"
// item: sdk.Item
// results: Array of objects. Each object looks like this:
// {
// error: Error,
// event: sdk.Event,
// script: sdk.Script,
// result: {
// target: 'prerequest'
//
// -- Updated environment
// environment: <VariableScope>
//
// -- Updated globals
// globals: <VariableScope>
//
// data: <Object of data variables>
// return: <Object, contains set next request params, etc>
// }
// }
console.log('prerequest: ' + JSON.stringify(err));
},
// Called before running test script(s)
beforeTest: function (err, cursor, events, item) {
// err, cursor: Same as arguments for "start"
// events: Array of sdk.Event objects
// item: sdk.Item
console.log('beforeTest: ' + JSON.stringify(err));
},
// Called just after running test script (s)
test: function (err, cursor, results, item) {
// results: Array of objects. Each object looks like this:
// {
// error: Error,
// event: sdk.Event,
// script: sdk.Script,
// result: {
// target: 'test'
//
// -- Updated environment
// environment: <VariableScope>
//
// -- Updated globals
// globals: <VariableScope>
//
// response: <sdk.Response>
// request: <sdk.Request>
// data: <Object of data variables>
// cookies: <Array of "sdk.Cookie" objects>
// tests: <Object>
// return: <Object, contains set next request params, etc>
// }
// }
console.log('test: ' + JSON.stringify(err));
},
// Called just before sending a request
beforeRequest: function (err, cursor, request, item) {
// err, cursor: Same as arguments for "start"
// item: sdk.Item
// request: sdk.request
console.log('beforeRequest: ' + JSON.stringify(err));
},
// Called just after sending a request
request: function (err, cursor, response, request, item, cookies) {
// err, cursor: Same as arguments for "start"
// item: sdk.Item
// response: sdk.Response
// request: sdk.request
console.log('request: ' + JSON.stringify(err));
},
// Called at the end of a run
done: function (err) {
// err: null or Error
console.log('done');
},
// Called any time a console.* function is called in test/pre-request scripts
console: function (cursor, level, ...logs) {
console.log('console: "' + logs + '"');
},
io: function (err, cursor, trace, ...otherArgs) {
// err, cursor: Same as arguments for "start"
// trace: An object which looks like this:
// {
// -- Indicates the type of IO event, may be HTTP, File, etc. Any requests sent out as a part of
// -- auth flows, replays, etc will show up here.
// type: 'http',
//
// -- Indicates what this IO event originated from, (collection, auth flows, etc)
// source: 'collection'
// }
// otherArgs: Variable number of arguments, specific to the type of the IO event.
// For http type, the otherArgs are:
// response: sdk.Response()
// request: sdk.Request()
// cookies: Array of sdk.Cookie()
console.log('io: ' + JSON.stringify(err) + ' || ' + JSON.stringify(trace));
}
}
console.log('Callbacks are now ready !')
var runner = new runtime.Runner();
runner.run(myCollection, {
// Iteration Data
data: [],
// Timeouts (in ms)
timeout: {
request: 30000,
},
// Number of iterations
iterationCount: 1,
// Control flags (you can only specify one of these):
// - gracefully halts on errors (errors in request scripts or while sending the request)
// calls the `item` and `iteration` callbacks and does not run any further items (requests)
stopOnError: true,
// - abruptly halts the run on errors, and directly calls the `done` callback
abortOnError: true,
// - gracefully halts on errors _or_ test failures.
// calls the `item` and `iteration` callbacks and does not run any further items (requests)
stopOnFailure: true,
// - abruptly halts the run on errors or test failures, and directly calls the `done` callback
abortOnFailure: true,
// Environment (a "VariableScope" from the SDK)
environment: new sdk.VariableScope(),
// Globals (a "VariableScope" from the SDK)
globals: new sdk.VariableScope(),
// Configure delays (in ms)
delay: {
// between each request
item: 500,
// between iterations
iteration: 500
},
// Used to fetch contents of files, certificates wherever needed
fileResolver: require('fs'),
// Options specific to the requester
requester: {
// An object compatible with the cookieJar provided by the 'postman-request' module
//cookieJar: jar,
// Controls redirect behavior (only supported on Node, ignored in the browser)
followRedirects: true,
// Enable or disable certificate verification (only supported on Node, ignored in the browser)
strictSSL: false,
// Enable sending of bodies with GET requests (only supported on Node, ignored in the browser)
sendBodyWithGetRequests: true,
},
// authorizer
authorizer: {
// Enables advanced mode only in these auths
interactive: {
ntlm: true,
basic: true
},
// Enables advanced mode for all auths
interactive: true
},
// A ProxyConfigList, from the SDK
proxies: new sdk.ProxyConfigList(),
// A function that fetches the system proxy for a given URL.
//systemProxy: function (url, callback) { return callback(null, {/* ProxyConfig object */}) },
systemProxy: null,
//systemProxy: function (url, callback) { return callback(null, {
//host: 'proxy.com',
//match: 'http+https://*/*',
//port: '8080',
//tunnel: true,
//disabled: false
//}) },
// A CertificateList from the SDK
certificates: new sdk.CertificateList(),
// *note* Not implemented yet.
// In the future, this will be used to read certificates from the OS keychain.
systemCertificate: function() {}
}, function (err, run) {
console.log('Creating a new Run !');
console.trace('======================')
// Check the section below for detailed documentation on what callbacks should be.
run.start(callbacks);
});