-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathbrowser_context.js
More file actions
970 lines (865 loc) · 36.2 KB
/
Copy pathbrowser_context.js
File metadata and controls
970 lines (865 loc) · 36.2 KB
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
var utils = splunkjs.Utils;
splunkjs.Logger.setLevel("ALL");
const isBrowser = typeof window !== "undefined";
describe('Context tests', function() {
before(function(){
this.service = svc;
})
describe("General Context Test", () => {
before(function (done) {
this.service = svc;
done();
});
it("Service exists", function (done) {
assert.ok(this.service);
done();
});
it("Create test search", async function () {
// The search created here is used by several of the following tests, specifically those using get()
let searchID = "DELETEME_JSSDK_UNITTEST";
let res = await this.service.post("search/jobs", { search: "search index=_internal | head 1", exec_mode: "blocking", id: searchID });
assert.ok(res.data.sid);
});
it("Login", async function () {
let newService = new splunkjs.Service(svc.http, {
scheme: svc.scheme,
host: svc.host,
port: svc.port,
username: svc.username,
password: svc.password,
version: svc.version
});
//ASK assert.ok(await newService.login());
try {
await newService.login();
} catch (error) {
assert.ok(!error);
}
});
it("Login fail", async function () {
let newService = new splunkjs.Service(svc.http, {
scheme: svc.scheme,
host: svc.host,
port: svc.port,
username: svc.username,
password: svc.password + "wrong_password",
version: svc.version
});
if (!isBrowser) {
try {
let res = await newService.login();
assert.ok(!res);
} catch (error) {
assert.ok(error);
}
}
});
it("Get", async function () {
let res = await this.service.get("search/jobs", { count: 1 });
assert.strictEqual(res.data.paging.offset, 0);
assert.ok(res.data.entry.length <= res.data.paging.total);
assert.strictEqual(res.data.entry.length, 1);
assert.ok(res.data.entry[0].content.sid);
});
it("Get error", async function () {
try {
let res = await this.service.get("search/jobs/1234_nosuchjob", {});
assert.ok(!res);
} catch (error) {
assert.strictEqual(error.status, 404);
}
});
it("Get autologin - success", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
version: svc.version
}
);
let res = await service.get("search/jobs", { count: 1 });
assert.strictEqual(res.data.paging.offset, 0);
assert.ok(res.data.entry.length <= res.data.paging.total);
assert.strictEqual(res.data.entry.length, 1);
assert.ok(res.data.entry[0].content.sid);
});
it("Get autologin - error", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password + "ABC",
version: svc.version
}
);
try {
let res = await service.get("search/jobs", { count: 1 });
assert.ok(!res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 401);
}
});
it("Get autologin - disabled", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
autologin: false,
version: svc.version
});
let res;
try {
res = await service.get("search/jobs", { count: 1 });
} catch (error) {
assert.ok(!error);
}
assert.ok(res);
assert.strictEqual(res.data.paging.offset, 0);
assert.ok(res.data.entry.length <= res.data.paging.total);
assert.strictEqual(res.data.entry.length, 1);
assert.ok(res.data.entry[0].content.sid);
});
it("Get relogin - success", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
sessionKey: "ABCDEF-not-real",
version: svc.version
}
);
let res = await service.get("search/jobs", { count: 1 });
assert.strictEqual(res.data.paging.offset, 0);
assert.ok(res.data.entry.length <= res.data.paging.total);
assert.strictEqual(res.data.entry.length, 1);
assert.ok(res.data.entry[0].content.sid);
});
it("Get relogin - error", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password + "ABC",
sessionKey: "ABCDEF-not-real",
version: svc.version
}
);
try {
let res = await service.get("search/jobs", { count: 1 });
assert.ok(!res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 401);
}
});
it("Post", async function () {
let service = this.service;
let res = await this.service.post("search/jobs", { search: "search index=_internal | head 1" });
let sid = res.data.sid;
assert.ok(sid);
let endpoint = "search/jobs/" + sid + "/control";
res = await service.post(endpoint, { action: "cancel" });
});
it("Post error", async function () {
try {
let res = await this.service.post("search/jobs", { search: "index_internal | head 1" });
assert.ok(!res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 400);
}
});
it("Post autologin - success", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
version: svc.version
}
);
let res = await service.post("search/jobs", { search: "search index=_internal | head 1" });
let sid = res.data.sid;
assert.ok(sid);
let endpoint = "search/jobs/" + sid + "/control";
res = await service.post(endpoint, { action: "cancel" });
});
it("Post autologin - error", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password + "ABC",
version: svc.version
}
);
try {
let res = await service.post("search/jobs", { search: "search index=_internal | head 1" });
assert.ok(!res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 401);
}
});
it("Post autologin - disabled", async function () {
var service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
autologin: false,
version: svc.version
});
let res;
try {
res = await service.post("search/jobs", { search: "search index=_internal | head 1" });
} catch (error) {
assert.ok(!error);
}
assert.ok(res);
let sid = res.data.sid;
assert.ok(sid);
});
it("Post relogin - success", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
sessionKey: "ABCDEF-not-real",
version: svc.version
}
);
let res = await service.post("search/jobs", { search: "search index=_internal | head 1" });
let sid = res.data.sid;
assert.ok(sid);
let endpoint = "search/jobs/" + sid + "/control";
service.post(endpoint, { action: "cancel" });
});
it("Post relogin - error", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password + "ABC",
sessionKey: "ABCDEF-not-real",
version: svc.version
}
);
try {
let res = await service.post("search/jobs", { search: "search index=_internal | head 1" });
assert.ok(res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 401);
}
});
it("Delete", async function () {
let service = this.service;
let res = await this.service.post("search/jobs", { search: "search index=_internal | head 1" });
let sid = res.data.sid;
assert.ok(sid);
let endpoint = "search/jobs/" + sid;
await service.del(endpoint, {});
});
it("Delete error", async function () {
try {
let res = await this.service.del("search/jobs/1234_nosuchjob", {});
assert.ok(res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 404);
}
});
it("Delete autologin - success", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
version: svc.version
}
);
let res = await service.post("search/jobs", { search: "search index=_internal | head 1" });
let sid = res.data.sid;
assert.ok(sid);
service.sessionKey = null;
let endpoint = "search/jobs/" + sid;
await service.del(endpoint, {});
});
it("Delete autologin - error", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password + "ABC",
version: svc.version
}
);
try {
let res = await service.del("search/jobs/NO_SUCH_SID", {});
assert.ok(res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 401);
}
});
it("Delete autologin - disabled", async function () {
var service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
autologin: false,
version: svc.version
});
let res;
try {
res = await service.del("search/jobs/NO_SUCH_SID", {});
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 404);
}
assert.ok(!res);
});
it("Delete relogin - success", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
sessionKey: "ABCDEF-not-real",
version: svc.version
}
);
let res = await service.post("search/jobs", { search: "search index=_internal | head 1" });
var sid = res.data.sid;
assert.ok(sid);
service.sessionKey = "ABCDEF-not-real";
let endpoint = "search/jobs/" + sid;
await service.del(endpoint, {});
});
it("Delete relogin - error", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password + "ABC",
sessionKey: "ABCDEF-not-real",
version: svc.version
}
);
try {
let res = await service.del("search/jobs/NO_SUCH_SID", {});
assert.ok(res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 401);
}
});
it("Request get", async function () {
let get = { count: 1 };
let post = null;
let body = null;
let res = await this.service.request("search/jobs", "GET", get, post, body, { "X-TestHeader": 1 });
assert.strictEqual(res.data.paging.offset, 0);
assert.ok(res.data.entry.length <= res.data.paging.total);
assert.strictEqual(res.data.entry.length, 1);
assert.ok(res.data.entry[0].content.sid);
if (res.response.request) {
assert.strictEqual(res.response.request.headers["X-TestHeader"], 1);
}
});
it("Request post", async function () {
let body = "search=" + encodeURIComponent("search index=_internal | head 1");
let headers = {
"Content-Type": "application/x-www-form-urlencoded"
};
let service = this.service;
let res = await this.service.request("search/jobs", "POST", null, null, body, headers);
assert.ok(res);
let sid = res.data.sid;
assert.ok(sid);
let endpoint = "search/jobs/" + sid + "/control";
await service.post(endpoint, { action: "cancel" });
});
it("Request error", async function () {
let res;
try {
res = await this.service.request("search/jobs/1234_nosuchjob", "GET", null, null, null, { "X-TestHeader": 1 });
} catch (error) {
if (error.response.request) {
assert.strictEqual(error.response.request.headers["X-TestHeader"], 1);
}
assert.strictEqual(error.status, 404);
}
assert.ok(!res);
});
it("Request autologin - success", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
version: svc.version
}
);
let get = { count: 1 };
let post = null;
let body = null;
let res = await service.request("search/jobs", "GET", get, post, body, { "X-TestHeader": 1 });
assert.ok(res);
assert.strictEqual(res.data.paging.offset, 0);
assert.ok(res.data.entry.length <= res.data.paging.total);
assert.strictEqual(res.data.entry.length, 1);
assert.ok(res.data.entry[0].content.sid);
if (res.response.request) {
assert.strictEqual(res.response.request.headers["X-TestHeader"], 1);
}
});
it("Request autologin - error", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password + "ABC",
version: svc.version
}
);
let get = { count: 1 };
let post = null;
let body = null;
try {
let res = await service.request("search/jobs", "GET", get, post, body, { "X-TestHeader": 1 });
assert.ok(!res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 401);
}
});
it("Request autologin - disabled", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
autologin: false,
version: svc.version
}
);
let get = { count: 1 };
let post = null;
let body = null;
let res;
try {
res = await service.request("search/jobs", "GET", get, post, body, { "X-TestHeader": 1 });
} catch (error) {
assert.ok(!error);
}
assert.ok(res);
});
it("Request relogin - success", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
sessionKey: "ABCDEF-not-real",
version: svc.version
}
);
let get = { count: 1 };
let post = null;
let body = null;
let res = await service.request("search/jobs", "GET", get, post, body, { "X-TestHeader": 1 });
assert.ok(res);
assert.strictEqual(res.data.paging.offset, 0);
assert.ok(res.data.entry.length <= res.data.paging.total);
assert.strictEqual(res.data.entry.length, 1);
assert.ok(res.data.entry[0].content.sid);
if (res.response.request) {
assert.strictEqual(res.response.request.headers["X-TestHeader"], 1);
}
});
it("Request relogin - error", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password + "ABC",
sessionKey: "ABCDEF-not-real",
version: svc.version
}
);
let get = { count: 1 };
let post = null;
let body = null;
try {
let res = await service.request("search/jobs", "GET", get, post, body, { "X-TestHeader": 1 });
assert.ok(!res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.status, 401);
}
});
it("Abort", async function () {
try {
let res = await this.service.get("search/jobs", { count: 1 }, response_timeout = 1);
assert.ok(!res);
} catch (error) {
assert.ok(error);
assert.strictEqual(error.error, "abort");
assert.strictEqual(error.status, "abort");
}
});
it("Timeout default test", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
version: svc.version
}
);
assert.strictEqual(0, service.timeout);
let res = await service.request("search/jobs", "GET", { count: 1 }, null, null, { "X-TestHeader": 1 });
assert.ok(res);
});
it("Timeout timed test", async function () {
let service = new splunkjs.Service(svc.http,{
scheme: this.service.scheme,
host: this.service.host,
port: this.service.port,
username: this.service.username,
password: this.service.password,
version: svc.version,
timeout: 10000
}
);
assert.strictEqual(service.timeout, 10000);
let res = await service.request("search/jobs", "GET", { count: 1 }, null, null, { "X-TestHeader": 1 });
assert.ok(res);
});
// This test is not stable, commenting it out until we figure it out
// "Callback#timeout fail -- FAILS INTERMITTENTLY": function(done){
// var service = new splunkjs.Service(
// {
// scheme: this.service.scheme,
// host: this.service.host,
// port: this.service.port,
// username: this.service.username,
// password: this.service.password,
// version: svc.version,
// timeout: 3000
// }
// );
// // Having a timeout of 3 seconds, a max_time of 5 seconds with a blocking mode and searching realtime should involve a timeout error.
// service.get("search/jobs/export", {search:"search index=_internal", timeout:2, max_time:5, search_mode:"realtime", exec_mode:"blocking"}, function(err, res){
// assert.ok(err);
// // Prevent test suite from erroring out if `err` is null, just fail the test
// if (err) {
// assert.strictEqual(err.status, 600);
// }
// done();
// });
// },
it("Cancel test search", async function () {
// Here, the search created for several of the previous tests is terminated, it is no longer necessary
var endpoint = "search/jobs/DELETEME_JSSDK_UNITTEST/control";
await this.service.post(endpoint, { action: "cancel" });
});
it("Fullpath gets its owner/app from the right places", function (done) {
var http = DummyHttp;
var ctx = new splunkjs.Context(http, { /*nothing*/ });
// Absolute paths are unchanged
assert.strictEqual(ctx.fullpath("/a/b/c"), "/a/b/c");
// Fall through to /services if there is no app
assert.strictEqual(ctx.fullpath("meep"), "/services/meep");
// Are username and app set properly?
var ctx2 = new splunkjs.Context(http, { owner: "alpha", app: "beta" });
assert.strictEqual(ctx2.fullpath("meep"), "/servicesNS/alpha/beta/meep");
assert.strictEqual(ctx2.fullpath("meep", { owner: "boris" }), "/servicesNS/boris/beta/meep");
assert.strictEqual(ctx2.fullpath("meep", { app: "factory" }), "/servicesNS/alpha/factory/meep");
assert.strictEqual(ctx2.fullpath("meep", { owner: "boris", app: "factory" }), "/servicesNS/boris/factory/meep");
// Sharing settings
assert.strictEqual(ctx2.fullpath("meep", { sharing: "app" }), "/servicesNS/nobody/beta/meep");
assert.strictEqual(ctx2.fullpath("meep", { sharing: "global" }), "/servicesNS/nobody/beta/meep");
assert.strictEqual(ctx2.fullpath("meep", { sharing: "system" }), "/servicesNS/nobody/system/meep");
// Do special characters get encoded?
var ctx3 = new splunkjs.Context(http, { owner: "alpha@beta.com", app: "beta" });
assert.strictEqual(ctx3.fullpath("meep"), "/servicesNS/alpha%40beta.com/beta/meep");
done();
});
it("Version check", function (done) {
var http = DummyHttp;
var ctx;
ctx = new splunkjs.Context(http, { "version": "4.0" });
assert.ok(ctx.version === "4.0");
ctx = new splunkjs.Context(http, { "version": "4.0" });
assert.ok(ctx.versionCompare("5.0") === -1);
ctx = new splunkjs.Context(http, { "version": "4" });
assert.ok(ctx.versionCompare("5.0") === -1);
ctx = new splunkjs.Context(http, { "version": "4.0" });
assert.ok(ctx.versionCompare("5") === -1);
ctx = new splunkjs.Context(http, { "version": "4.1" });
assert.ok(ctx.versionCompare("4.9") === -1);
ctx = new splunkjs.Context(http, { "version": "4.0" });
assert.ok(ctx.versionCompare("4.0") === 0);
ctx = new splunkjs.Context(http, { "version": "4" });
assert.ok(ctx.versionCompare("4.0") === 0);
ctx = new splunkjs.Context(http, { "version": "4.0" });
assert.ok(ctx.versionCompare("4") === 0);
ctx = new splunkjs.Context(http, { "version": "5.0" });
assert.ok(ctx.versionCompare("4.0") === 1);
ctx = new splunkjs.Context(http, { "version": "5.0" });
assert.ok(ctx.versionCompare("4") === 1);
ctx = new splunkjs.Context(http, { "version": "5" });
assert.ok(ctx.versionCompare("4.0") === 1);
ctx = new splunkjs.Context(http, { "version": "4.9" });
assert.ok(ctx.versionCompare("4.1") === 1);
ctx = new splunkjs.Context(http, { /*nothing*/ });
assert.ok(ctx.versionCompare("5.0") === 0);
done();
});
}),
describe("Cookie Tests", () => {
before(async function () {
this.service = svc;
this.skip = false;
var that = this;
let info = await svc.serverInfo();
let majorVersion = parseInt(info.properties().version.split(".")[0], 10);
let minorVersion = parseInt(info.properties().version.split(".")[1], 10);
// Skip cookie tests if Splunk older than 6.2
if (majorVersion < 6 || (majorVersion === 6 && minorVersion < 2)) {
that.skip = true;
splunkjs.Logger.log("Skipping cookie tests...");
}
});
after(async function () {
await this.service.logout();
});
it("_getCookieString works as expected", function (done) {
let service = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port
});
service.http._cookieStore = {
'cookie': 'format',
'another': 'one'
};
let expectedCookieString = 'cookie=format; another=one; ';
let cookieString = service.http._getCookieString();
assert.strictEqual(cookieString, expectedCookieString);
done();
});
it("Login and store cookie", async function () {
if (this.skip) {
return;
}
let service = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port,
username: svc.username,
password: svc.password,
version: svc.version
});
// Check that there are no cookies
assert.ok(utils.isEmpty(service.http._cookieStore));
await service.login();
// Check that cookies were saved
assert.ok(!utils.isEmpty(service.http._cookieStore));
assert.notStrictEqual(service.http._getCookieString(), '');
});
it("Request with cookie", async function () {
if (this.skip) {
return;
}
let service = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port,
username: svc.username,
password: svc.password,
version: svc.version
});
// Create another service to put valid cookie into, give no other authentication information
let service2 = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port,
version: svc.version
});
try {
// Login to service to get a valid cookie
await service.login();
// Save the cookie store
let cookieStore = service.http._cookieStore;
// Test that there are cookies
assert.ok(!utils.isEmpty(cookieStore));
// Add the cookies to a service with no other authentication information
service2.http._cookieStore = cookieStore;
// Make a request that requires authentication
let resp = await service2.get("search/jobs", { count: 1 });
// Test that a response was returned
assert.ok(resp);
} catch (error) {
// Test that no errors were returned
assert.ok(!error);
}
});
it("Request fails with bad cookie", async function () {
if (this.skip) {
return;
}
// Create a service with no login information
let service = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port,
version: svc.version
});
// Put a bad cookie into the service
service.http._cookieStore = { "bad": "cookie" };
let resp;
// Try requesting something that requires authentication
try {
resp = await service.get("search/jobs", { count: 1 });
} catch (err) {
// Test if an error is returned
assert.ok(err);
// Check that it is an unauthorized error
assert.strictEqual(err.status, 401);
}
assert.ok(!resp);
});
it("Autologin with cookie", async function () {
if (this.skip) {
return;
}
let service = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port,
username: svc.username,
password: svc.password,
version: svc.version
});
// Test if service has no cookies
assert.ok(utils.isEmpty(service.http._cookieStore));
await service.get("search/jobs", { count: 1 });
// Test if service now has a cookie
assert.ok(service.http._cookieStore);
});
it("Login fails with no cookie and no sessionKey", async function () {
if (this.skip) {
return;
}
let service = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port,
version: svc.version
});
// Test there is no authentication information
assert.ok(utils.isEmpty(service.http._cookieStore));
assert.strictEqual(service.sessionKey, '');
assert.ok(!service.username);
assert.ok(!service.password);
try {
let res = await service.get("search/jobs", { count: 1 });
assert.ok(!res);
} catch (error) {
// Test if an error is returned
assert.ok(error);
}
});
it("Login with multiple cookies", async function () {
if (this.skip) {
return;
}
let service = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port,
username: svc.username,
password: svc.password,
version: svc.version
});
// Create another service to put valid cookie into, give no other authentication information
let service2 = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host,
port: svc.port,
version: svc.version
});
// Login to service to get a valid cookie
try {
await service.login();
// Save the cookie store
let cookieStore = service.http._cookieStore;
// Test that there are cookies
assert.ok(!utils.isEmpty(cookieStore));
// Add a bad cookie to the cookieStore
cookieStore['bad'] = 'cookie';
// Add the cookies to a service with no other authenitcation information
service2.http._cookieStore = cookieStore;
// Make a request that requires authentication
let res = await service2.get("search/jobs", { count: 1 });
// Test that a response was returned
assert.ok(res);
} catch (error) {
assert.ok(!error);
}
});
it("Autologin with cookie and bad sessionKey", async function () {
if (this.skip) {
return;
}
let service = new splunkjs.Service(svc.http,{
scheme: svc.scheme,
host: svc.host, port: svc.port,
username: svc.username,
password: svc.password,
sessionKey: 'ABC-BADKEY',
version: svc.version
});
// Test if service has no cookies
assert.ok(utils.isEmpty(service.http._cookieStore));
try {
await service.get("search/jobs", { count: 1 });
// Test if service now has a cookie
assert.ok(service.http._cookieStore);
} catch (error) {
assert.ok(!error);
}
});
})
});