-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
1206 lines (1076 loc) · 40.6 KB
/
utils.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// DESCRIPTION
// A library of utility functions for calling and implementing tests,
// checking results, handling errors, and performing a variety of
// miscellaneous operations in Postman.
//
// EXPECTATIONS
// The REST API being tested is expected to be consistent in returning
// successful results and error details. In particular, errors are
// expected to return an HTTP status code in the 4xx or 5xx range along
// with the data object coplying with RFC7807 (see description at:
// https://tools.ietf.org/html/rfc7807). The default implementation
// assumes that the error details object contains a custom property,
// such as 'serviceCode' identifying error; if your problem details object
// relies on a different property, pass the appropriate name or change
// the default).
//
// REQUIREMENTS
// All functions implemented in this library accept the global 'pm'
// object as the first parameter (not all need it, but it makes
// the API consistent).
//
// EXAMPLES
//
// 1. A simple positive test checking for HTTP response with status
// code 200.
//
// utils.test.positive(pm);
//
// 2. A simple negative test checking for HTTP respons with status
// code 400 and JSON error result containing 'serviceCode'
// property with the value of 'BadRequest'.
//
// utils.test.negative(pm, null, 400, "BadRequest");
//
// 2. A negative test checking for HTTP respons with status
// code 400 and JSON error result containing 'errorCode'
// property with the value of 'BadRequest'.
//
// utils.test.negative(pm, null, 400, "errorCode:BadRequest");
//
// 4. A more typical positive test checking for HTTP response with
// status code 200 and performing additional validations.
//
// utils.test.positive(pm, null, 200, function() {
// utils.expect.response.text(pm);
// pm.expect(pm.response.text()).to.be.a("string");
// });
//
// 5. A positive test with special error handling logic performed
// on test failure:
//
// utils.test.positive(pm, null, 200,
// function() {
// pm.expect(pm.response.text()).to.be.a("string");
// },
// function() {
// console.warn("EXITING TEST RUN DUE TO ERROR");
// utils.stop();
// }
// );
//
// 6. Two tests performed for one request.
//
// var i = 1;
//
// utils.test.positive(pm, utils.name(pm, null, i++), 200, function() {
// pm.expect(pm.response.text()).to.be.a("string");
// });
//
// utils.test.neutral(pm, utils.name(pm, null, i++), function() {
// pm.expect(pm.response.text()).not.to.be.an("array");
// });
//
// 7. Initialization logic in a test pre-request.
//
// utils.test.initialize(pm, null,
// function() {
// console.log("Executing pre-request script");
// },
// function() {
// console.error("EXITING TEST RUN DUE TO ERROR IN PRE-REQUEST");
// utils.stop();
// }
// );
//
// 8. Folder pre-request code that gets executed once per test run.
//
// utils.run.once(pm, "Name-Of-The-Folder-X", function() {
// utils.trace.set.all(pm);
// console.log("Code in this folder gets called only once.")
// });
//
// 9. Folder pre-request code that gets executed for every request
// during each test run.
//
// utils.run.always(pm, "Name-Of-The-Folder-Y", function() {
// utils.trace.set.all(pm);
// console.log("Code in this folder gets called only once.")
// });
//
utils = {
// Version of this library.
version: "1.0.3",
// Default name of the problem details object property returning
// application specific error code.
defaultServiceCodePropertyName: "serviceCode",
// DESCRIPTION
// Primary test functions that can be used for implementing
// positive, negative, and neutral tests.
test: {
// DESCRIPTION
// Use this function to implement custom pre-request logic
// intended for test pre-requests only.
//
// PARAMETERS
// - name (string, optional)
// Unique (across the test collection) name of the test.
// If not specified, the name will be set to the value of
// 'pm.info.requestname' property (which is the recommended value
// to be passed to 'pm.test' unless the request has multiple tests).
//
// - process (function)
// Inline function that will be executed on every explicit call.
//
// - onerror
// Same as in the 'once' function.
initialize: function(pm, name, process, onerror) {
if (process === undefined ||
process === null ||
(typeof process !== 'function')) {
return;
}
name = utils.name(pm, name);
if (onerror === undefined ||
onerror === null ||
(typeof onerror !== 'function')) {
utils.run.always(pm, name, process, null, false);
} else {
utils.run.always(pm, name, process, onerror, false);
}
},
// DESCRITION
// Implements a positive test. By default, this function
// will check the returned HTTP status code. It can also
// call the code from the inline custom function if one is
// specified and invoke special code in case of error (also
// if error handling code is provided).
//
// PARAMETERS
// - name
// Same as in the 'test.initialize' function.
//
// - status (integer, optional, default=200)
// Expected HTTP status code identifying success.
//
// - process (function, optional)
// Implements custom processing logic.
//
// - onerror (function, optional)
// Implements custom error handling logic.
positive: function(pm, name, status, process, onerror) {
name = utils.name(pm, name);
pm.test(name, function() {
if (status === undefined || status === null) {
status = 200;
}
try {
// Invoke pre-test code (console message, etc).
utils.prologue(pm, name);
// If returned HTTP status code matches the expected value...
if (pm.response.code === status) {
// If a custom function with additional tests is specified...
if (process !== undefined &&
process !== null &&
(typeof process === 'function')) {
// Perform custom tests.
process();
}
} else {
// Process error based on either problem details object
// or HTTP status code returned in the response.
utils.error(pm, status);
}
} catch (e) {
// Call custom error handler (if one is specified)
// before performing standard error handling.
if (onerror !== undefined) {
utils.failure(pm, onerror);
}
// Call standard error handler.
utils.exception(pm, e, name);
} finally {
// Invoke post-test code (console message, etc).
utils.epilogue(pm, name);
}
});
},
// DESCRITION
// Implements a negative test. By default, this function
// will check the returned HTTP status code and an optional
// service code returned via the problem details object.
// Just as with the 'positive' function, you can pass
// custom functions with additional logic and error handling.
//
// PARAMETERS
// - name
// Same as in the 'test.initialize' function.
//
// - status (integer, optional, default=400)
// Expected HTTP status identifying error.
//
// - serviceCode (string, optional)
// Extended property of the problem details object returned by
// the REST API on error that needs to be checked for a specific
// value. By default, the problem details property name is
// assumed to be 'serviceCode'. To use a different property name
// add it in front of the expected value and separete the name
// from the value by a colon (or equal sign), e.g.
// 'errorCode:InvalidInput' (or 'errorCode=InvalidInput').
//
// - process
// Same as in the 'test.initialize' function.
//
// - onerror
// Same as in the 'test.initialize' function.
negative: function(pm, name, status, serviceCode, process, onerror) {
name = utils.name(pm, name);
pm.test(name, function() {
// If HTTP status code is not specified, set it to 400.
if (status === undefined || status === null) {
status = 400;
}
try {
// Invoke pre-test code (console message, etc).
utils.prologue(pm, name);
// First compare returned HTTP status code to the expected.
if (pm.response.code === status) {
// Since negative test case assumes an error,
// get the problem details object from the response.
var response = pm.response.json();
// If we also need to check service code, validate it as well.
if (serviceCode !== undefined &&
serviceCode !== null &&
serviceCode !== "") {
var errorCodePropertyName;
var errorCodePropertyValue;
// If the service code property contains colon or equal sign,
// it means that we mwy be using a non-default property name,
// so we will get it from the string following the separator
// character (the value can contain the separator character,
// because only the first occurrence of the separator character
// is considered to be a separator).
if (serviceCode.includes(":")) {
var keyValuePair = serviceCode.split(":");
errorCodePropertyName = keyValuePair[0];
// Remove first item (name) from the array and join the rest.
// to restore the original value in case it contained the
// separator character.
errorCodePropertyValue = keyValuePair.slice(1).join(":");
} else if (serviceCode.includes("=")) {
var keyValuePair = serviceCode.split("=");
errorCodePropertyName = keyValuePair[0];
// Remove first item (name) from the array and join the rest
// to restore the original value in case it contained the
// separator character.
errorCodePropertyValue = keyValuePair.slice(1).join("=");
} else {
errorCodePropertyName = utils.defaultServiceCodePropertyName;
errorCodePropertyValue = serviceCode;
}
// Validate returned service code.
pm.expect(response).to.have.property(errorCodePropertyName);
if (response[errorCodePropertyName] !== errorCodePropertyValue) {
pm.expect.fail(
"Expected response '" + errorCodePropertyName + "' property to be '" +
errorCodePropertyValue + "' but got '" +
response[errorCodePropertyName] + "'")
}
}
// If a custom function with additional tests is specified...
if (process !== undefined &&
process !== null &&
(typeof process === 'function')) {
// Perform custom tests.
process();
}
} else {
pm.response.to.have.status(status);
}
} catch (e) {
// Call custom error handler (if one is specified)
// before performing standard error handling.
if (onerror !== undefined) {
utils.failure(pm, onerror);
}
// Call standard error handler.
utils.exception(pm, e, name);
} finally {
// Invoke post-test code (console message, etc).
utils.epilogue(pm, name);
}
});
},
// DESCRITION
// Implements test that is not dependent on the value of
// the returned HTTP status code. Use this function if you
// need to implement more than one tests for a request.
//
// PARAMETERS
// - name
// Same as in the 'test.initialize' function.
//
// - process
// Same as in the 'test.initialize' function.
//
// - onerror
// Same as in the 'test.initialize' function.
neutral: function(pm, name, process, onerror) {
name = utils.name(pm, name);
pm.test(name, function() {
try {
// Invoke pre-test code (console message, etc).
utils.prologue(pm, name);
if (process !== undefined &&
process !== null &&
(typeof process === 'function')) {
// Perform custom tests.
process();
}
} catch (e) {
// Call custom error handler (if one is specified)
// before performing standard error handling.
if (onerror !== undefined) {
utils.failure(pm, onerror);
}
// Call standard error handler.
utils.exception(pm, e, name);
} finally {
// Invoke post-test code (console message, etc).
utils.epilogue(pm, name);
}
});
}
// End of 'utils.test' functions.
},
// DESCRIPTION
// Functions to be used in request workflow.
run: {
// DESCRITION
// Executes code in the inline function once per collection run,
// which can be handy for pre-request scripts defined in folders
// for one-time initializations.
//
// PARAMETERS
// - name (string)
// Descriptive name describing the collection being tested.
// This name will be used as the name of the environment variable
// to prevent consecutive test runs.
//
// - process (function)
// Inline function with the code that must be executed once.
//
// - onerror
// Inline function with the code that must be executed on error.
once: function(pm, name, process, onerror) {
if (process === undefined ||
process === null ||
(typeof process !== 'function')) {
return;
}
if (name === undefined ||
name === null ||
(typeof name !== 'string')) {
return;
}
var firstRun = pm.variables.get(name);
if (firstRun !== undefined) {
return;
}
var failed = false;
try {
utils.trace.log(pm, name + ": Script started", 1);
pm.variables.set(name, false);
if (process !== undefined &&
process !== null &&
(typeof process === 'function')) {
process();
}
}
catch (e) {
failed = true;
// Call custom error handler (if one is specified)
// before performing standard error handling.
if (onerror !== undefined) {
utils.failure(pm, onerror);
}
// Call standard error handler.
utils.exception(pm, e, name);
} finally {
if (failed) {
console.error(name + ": Script failed");
} else {
utils.trace.log(pm, name + ": Script ended", 2);
}
}
},
// DESCRIPTION
// Executes code in the inline function for every request during
// test collection run, which can be handy for pre-request scripts
// defined for folders in repeateed initializations.
//
// PARAMETERS
// - name
// Same as in the 'run.once' function.
//
// - process (function)
// Inline function that will be executed on every explicit call.
//
// - onerror
// Same as in the 'run.once' function.
always: function(pm, name, process, onerror, folder=true) {
if (process === undefined ||
process === null ||
(typeof process !== 'function')) {
return;
}
var failed = false;
var type = folder ? "Script" : "Pre-request";
try
{
utils.trace.log(pm, name + ": " + type + " started", 1);
process();
}
catch (e) {
failed = true;
// Call custom error handler (if one is specified)
// before performing standard error handling.
if (onerror !== undefined) {
utils.failure(pm, onerror);
}
// Call standard error handler.
utils.exception(pm, e, name);
} finally {
if (failed) {
console.error(name + ": " + type + " failed");
} else {
utils.trace.log(pm, name + ": " + type + " ended", 2);
}
}
}
// End of 'utils.run' functions.
},
// DESCRIPTION
// Data validation fuinctions that are split in subgroups targeting
// specific objects being validated: response, property, string, etc.
expect: {
// DESCRIPTION
// Functions validating HTTP response.
response: {
// DESCRITION
// Expects response to return any text element.
text: function(pm) {
try
{
response = pm.response.text();
}
catch (e)
{
pm.expect.fail("Response must return a valid text object: " + e.message);
}
},
// DESCRITION
// Expects response to return any JSON element.
json: function(pm) {
try
{
response = pm.response.json();
}
catch (e)
{
pm.expect.fail("Response must return a valid JSON object: " + e.message);
}
},
// DESCRITION
// Expects response to return a single JSON element (not an array,
// and, in particular, not a JSON array containing a single item).
one: function(pm) {
try
{
response = pm.response.json();
}
catch (e)
{
pm.expect.fail("Response must return a valid JSON object: " + e.message);
}
if ("length" in response) {
pm.expect.fail("Response must return a single object and not an array");
}
},
// DESCRITION
// Expects response to return a JSON array.
// The array can be empty or contain the expected number of items
// identified by the 'min' and 'max' parameters (if 'max' is -1,
// it means there is no maximum).
// The array can also contain a single item which is treated
// NOT the same as a single JSON element.
//
// PARAMETERS
// - min (non-negative integer, optional, default=0)
// Minimal expected number of elements in the collection.
//
// - max (integer, optional, default=-1)
// Maximum expected number of elements in the collection.
// Use -1 for unlimited maximum number.
many: function (pm, min = 0, max = -1) {
var response = null;
try
{
response = pm.response.json();
}
catch (e)
{
pm.expect.fail("Response must return a valid JSON object: " + e.message);
}
pm.expect(response).to.be.an("array", "Response must return an array");
if (min == 0 && max == 0) {
pm.expect(response).to.be.empty("Response must return an empty array");
} else if (min == 1 && max == 1) {
pm.expect(response).to.have.lengthOf(1, "Response must return a single item");
} else if (min == max) {
pm.expect(response.length).to.equal(min, "Response must return exactly " + min + " item(s)");
} else if (max > 0) {
if (min > 0) {
pm.expect(response.length).to.be.at.least(min, "Response must return at least " + min + " item(s)");
}
pm.expect(response.length).to.be.at.most(max, "Response must return at most " + max + " item(s)");
} else if (min == 1) {
pm.expect(response.length).to.be.at.least(1, "Response must return at least one item");
} else if (min > 0) {
pm.expect(response.length).to.be.at.least(min, "Response must return at least " + min + " item(s)");
}
},
// DESCRITION
// Expects response to contain an empty array.
empty: function(pm) {
utils.expect.response.many(pm, 0, 0);
},
// DESCRITION
// Expects response to contain an array holding a single item.
unique: function(pm) {
utils.expect.response.many(pm, 1, 1);
},
// DESCRIPTION
// Negative functions checking response.
not: {
// DESCRITION
// Expects response to contain an non-empty array.
empty: function(pm) {
utils.expect.response.many(pm, 1, -1);
},
// DESCRITION
// Expects response to contain an array with at least two items.
unique: function(pm) {
utils.expect.response.many(pm, 2, -1);
}
}
// End of 'utils.expect.response' functions.
},
// DESCRIPTION
// Functions validating object properties.
// String-specific validation functions are grouped under the
// 'utils.expect.property.string' subcluss.
property: {
// DESCRIPTION
// Expects object to have a named property holding any value
// including null.
//
// PARAMETERS
// - data (object)
// Data object which property is being checked.
//
// - name (string)
// Property name.
exist: function(pm, data, name) {
pm.expect(data).to.have.property(name);
},
// DESCRIPTION
// Expects object property to exist and equal the specified value.
//
// PARAMETERS
// - data
// Same as in 'utils.expect.property.exist'.
//
// - name
// Same as in 'utils.expect.property.exist'.
//
// - value (object)
// Expected value (can be null).
equal: function(pm, data, name, value) {
utils.expect.property.exist(pm, data, name);
if (value !== undefined) {
if (value === null) {
pm.expect(data[name]).to.be.null;
} else {
var msg = "Expected '" + name +
"' property to equal '" + value +
"' but got '" + data[name] +
"'";
pm.expect(data[name]).to.equal(value, msg);
}
}
},
// DESCRIPTION
// Negative property check functions.
not: {
// DESCRIPTION
// Expects object to not have a named property holding any value
// including null.
//
// PARAMETERS
// - data (object)
// Data object which property is being checked.
//
// - name (string)
// Property name.
exist: function(pm, data, name) {
pm.expect(data).to.not.have.property(name);
},
// DESCRIPTION
// Expects object property to exist and equal the specified value.
//
// PARAMETERS
// - data
// Same as in 'utils.expect.property.exist'.
//
// - name
// Same as in 'utils.expect.property.exist'.
//
// - value (object)
// Expected value (can be null).
equal: function(pm, data, name, value) {
utils.expect.property.exist(pm, data, name);
if (value !== undefined) {
if (value === null) {
pm.expect(data[name]).to.not.be.null;
} else {
var msg = "Expected '" + name +
"' property to not equal '" + value +
"' but got '" + data[name] +
"'";
pm.expect(data[name]).to.not.equal(value, msg);
}
}
}
},
// DESCRIPTION
// Functions validating string properties.
string: {
// DESCRIPTION
// Expects the string object property to exist and
// be exactly the same as the specified value.
//
// PARAMETERS
// - data
// Same as in 'utils.expect.property.exist'.
//
// - name
// Same as in 'utils.expect.property.exist'.
//
// - value (string)
// Expected string value (can be null).
//
// - ignoreCase (boolean, default=false)
// Set to 'true' for case-insensitive comparisons.
exact: function(pm, data, name, value, ignoreCase = false) {
utils.expect.property.exist(pm, data, name);
if (value === null) {
pm.expect(data[name]).to.be.null;
} else {
var msg = "Expected '" + name +
"' property to match '" + value +
"' but got '" + data[name] +
"'";
if (ignoreCase) {
pm.expect(data[name].toUpperCase()).to.equal(value.toUpperCase(),
msg + " (case-insensitive)");
} else {
pm.expect(data[name]).to.equal(value, msg + " (case-sensitive)");
}
}
},
// DESCRIPTION
// Expects the string object property to exist and contain the specified value.
//
// PARAMETERS
// - data
// Same as in 'utils.expect.property.exist'.
//
// - name
// Same as in 'utils.expect.property.exist'.
//
// - value (string)
// Expected substring value (cannot be null).
//
// - ignoreCase (boolean, default=false)
// Set to 'true' for case-insensitive comparisons.
partial: function(pm, data, name, value, ignoreCase = false) {
utils.expect.property.exist(pm, data, name);
var msg = "Expected '" + name +
"' property to contain '" + value +
"' but got '" + data[name] +
"'";
if (ignoreCase) {
pm.expect(data[name].toUpperCase()).to.have.string(value.toUpperCase(), msg + " (case-insensitive)");
} else {
pm.expect(data[name]).to.have.string(value, msg + " (case-sensitive)");
}
},
// DESCRIPTION
// Expects the string object property to exist and start with the specified value.
//
// PARAMETERS
// - data
// Same as in 'utils.expect.property.exist'.
//
// - name
// Same as in 'utils.expect.property.exist'.
//
// - value (string)
// Expected beginning of the string value (cannot be null).
//
// - ignoreCase (boolean, default=false)
// Set to 'true' for case-insensitive comparisons.
start: function(pm, data, name, value, ignoreCase = false) {
utils.expect.property.exist(pm, data, name);
var msg = "Expected '" + name +
"' property to start with '" + value +
"' but got '" + data[name] +
"'";
if (data[name] === null) {
pm.fail(msg);
}
if (ignoreCase) {
if (!data[name].toUpperCase().startsWith(value.toUpperCase())) {
pm.fail(msg + " (case-insensitive)");
}
} else {
if (!data[name].startsWith(value)) {
pm.fail(msg + " (case-sensitive)");
}
}
},
// DESCRIPTION
// Expects the string object property to exist and end with the specified value.
//
// PARAMETERS
// - data
// Same as in 'utils.expect.property.exist'.
//
// - name
// Same as in 'utils.expect.property.exist'.
//
// - value (string)
// Expected ending of the string value (cannot be null).
//
// - ignoreCase (boolean, default=false)
// Set to 'true' for case-insensitive comparisons.
end: function(pm, data, name, value, ignoreCase = false) {
utils.expect.property.exist(pm, data, name);
if (value !== undefined) {
if (value === null) {
pm.expect(data[name]).to.be.null;
} else {
var msg = "Expected '" + name +
"' property to end with '" + value +
"' but got '" + data[name] +
"'";
if (data[name] === null) {
pm.fail(msg);
}
if (ignoreCase) {
if (!data[name].toUpperCase().endsWith(value.toUpperCase())) {
pm.fail(msg + " (case-insensitive)");
}
} else {
if (!data[name].startsWith(value)) {
pm.fail(msg + " (case-sensitive)");
}
}
}
}
},
// DESCRIPTION
// Expects the string object property to exist and
// match the specified regular expression.
//
// PARAMETERS
// - data
// Same as in 'utils.expect.property.exist'.
//
// - name
// Same as in 'utils.expect.property.exist'.
//
// - value (regular expression)
// Regular expression (can be null).
match: function(pm, data, name, value) {
utils.expect.property.exist(pm, data, name);
if (value !== undefined) {
if (value === null) {
pm.expect(data[name]).to.be.null;
} else {
var msg = "Expected '" + name +
"' property to match regular expression '" + value +
"' but got '" + data[name] +
"'";
pm.expect(data[name]).to.match(value, msg);
}
}
},
// DESCRIPTION
// Negative string property check functions.
not: {
// DESCRIPTION
// Expects the string object property to exist and
// not match the specified regular expression.
//
// PARAMETERS
// - data
// Same as in 'utils.expect.property.exist'.
//
// - name
// Same as in 'utils.expect.property.exist'.
//
// - value (regular expression)
// Regular expression (can be null).
match: function(pm, data, name, value) {
utils.expect.property.exist(pm, data, name);
if (value !== undefined) {
if (value === null) {
pm.expect(data[name]).to.not.be.null;
} else {
var msg = "Expected '" + name +
"' property to not match regular expression '" + value +
"' but got '" + data[name] +
"'";
pm.expect(data[name]).to.not.match(value, msg);
}
}
}
}
// End of 'utils.expect.property.string' functions.
}
// End of 'utils.expect.property' functions.
}
// End of 'utils.expect' functions.
},
// DESCRIPTION
// Functions implementing trace logging.
trace: {
// Variable used for setting and checking trace level.
variableName: "TRACE_LEVEL",
// Default trace level (start and end of functions).
defaultLevel: 2,
// DESCRIPTION
// Functions setting trace level (must be called at beginning of test run).
set: {
// DESCRITION
// Do not print trace messages to console
// (trace level = 0).
none: function(pm) {
utils.trace.set.custom(pm, 0);
},
// DESCRITION
// Only print trace messages indicating start of the operation to console
// (trace level = 1).
minimal: function(pm) {
utils.trace.set.custom(pm, 1);
},
// DESCRITION
// Print both start and end of operation trace messages to console.
// (trace level = 2).
all: function(pm) {
utils.trace.set.custom(pm, 2);
},
// DESCRITION
// Default is to print all.
// (trace level = 2).
default: function(pm) {
utils.trace.set.custom(pm, utils.trace.defaultLevel);
},
// DESCRITION
// You can set your own trace level higher than 2 and selectively
// print your trace messages.
// (trace level = level).
custom: function(pm, level) {
pm.collectionVariables.set(utils.trace.variableName, level)