-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoptions.test.ts
More file actions
736 lines (666 loc) · 28.1 KB
/
options.test.ts
File metadata and controls
736 lines (666 loc) · 28.1 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
import { describe, it, expect } from "vitest";
import { SenderOptions } from "../src/options";
import { Agent } from "undici";
describe("Configuration string parser suite", function () {
it("can parse a basic config string", function () {
const options = SenderOptions.fromConfig(
"https::addr=host;username=user1;password=pwd;",
);
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host");
expect(options.username).toBe("user1");
expect(options.password).toBe("pwd");
});
it("can parse a config string from environment variable", async function () {
process.env.QDB_CLIENT_CONF = "tcp::addr=host;";
const options = SenderOptions.fromEnv();
expect(options.protocol).toBe("tcp");
expect(options.addr).toBe("host");
});
it("accepts only lowercase protocols", function () {
let options = SenderOptions.fromConfig("tcp::addr=host;");
expect(options.protocol).toBe("tcp");
options = SenderOptions.fromConfig("tcps::addr=host;");
expect(options.protocol).toBe("tcps");
options = SenderOptions.fromConfig("http::addr=host;");
expect(options.protocol).toBe("http");
options = SenderOptions.fromConfig("https::addr=host;");
expect(options.protocol).toBe("https");
expect(() => SenderOptions.fromConfig("HTTP::")).toThrow(
"Invalid protocol: 'HTTP', accepted protocols: 'http', 'https', 'tcp', 'tcps'",
);
expect(() => SenderOptions.fromConfig("Http::")).toThrow(
"Invalid protocol: 'Http', accepted protocols: 'http', 'https', 'tcp', 'tcps'",
);
expect(() => SenderOptions.fromConfig("HtTps::")).toThrow(
"Invalid protocol: 'HtTps', accepted protocols: 'http', 'https', 'tcp', 'tcps'",
);
expect(() => SenderOptions.fromConfig("TCP::")).toThrow(
"Invalid protocol: 'TCP', accepted protocols: 'http', 'https', 'tcp', 'tcps'",
);
expect(() => SenderOptions.fromConfig("TcP::")).toThrow(
"Invalid protocol: 'TcP', accepted protocols: 'http', 'https', 'tcp', 'tcps'",
);
expect(() => SenderOptions.fromConfig("Tcps::")).toThrow(
"Invalid protocol: 'Tcps', accepted protocols: 'http', 'https', 'tcp', 'tcps'",
);
});
it("considers that keys and values are case-sensitive", function () {
const options = SenderOptions.fromConfig(
"tcps::addr=Host;username=useR1;token=TOKEN;",
);
expect(options.protocol).toBe("tcps");
expect(options.addr).toBe("Host");
expect(options.username).toBe("useR1");
expect(options.token).toBe("TOKEN");
expect(() =>
SenderOptions.fromConfig("tcps::addr=Host;UserNAME=useR1;PaSswOrD=pWd;"),
).toThrow("Unknown configuration key: 'UserNAME'");
expect(() =>
SenderOptions.fromConfig("tcps::addr=Host;PaSswOrD=pWd;"),
).toThrow("Unknown configuration key: 'PaSswOrD'");
});
it("can parse with or without the last semicolon", function () {
let options = SenderOptions.fromConfig("https::addr=host:9002");
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host:9002");
options = SenderOptions.fromConfig("https::addr=host:9002;");
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host:9002");
options = SenderOptions.fromConfig("https::addr=host:9002;token=abcde");
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host:9002");
expect(options.token).toBe("abcde");
options = SenderOptions.fromConfig("https::addr=host:9002;token=abcde;");
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host:9002");
expect(options.token).toBe("abcde");
options = SenderOptions.fromConfig("https::addr=host:9002;token=abcde;;");
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host:9002");
expect(options.token).toBe("abcde;");
options = SenderOptions.fromConfig("https::addr=host:9002;token=abcde;;;");
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host:9002");
expect(options.token).toBe("abcde;");
});
it("can parse escaped config string values", function () {
const options = SenderOptions.fromConfig(
"https::addr=host:9002;username=us;;;;;;er;;1;;;password=p;;wd;",
);
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host:9002");
expect(options.username).toBe("us;;;er;1;");
expect(options.password).toBe("p;wd");
});
it("can parse the address", function () {
let options = SenderOptions.fromConfig(
"https::addr=host1:9002;token=resttoken123;",
);
expect(options.protocol).toBe("https");
expect(options.addr).toBe("host1:9002");
expect(options.host).toBe("host1");
expect(options.port).toBe(9002);
expect(options.token).toBe("resttoken123");
options = SenderOptions.fromConfig(
"tcps::addr=host2:9005;username=user1;token=jwkprivkey123;",
);
expect(options.protocol).toBe("tcps");
expect(options.addr).toBe("host2:9005");
expect(options.host).toBe("host2");
expect(options.port).toBe(9005);
expect(options.username).toBe("user1");
expect(options.token).toBe("jwkprivkey123");
});
it("can default the port", function () {
let options = SenderOptions.fromConfig(
"https::addr=hostname;token=resttoken123;",
);
expect(options.protocol).toBe("https");
expect(options.addr).toBe("hostname");
expect(options.host).toBe("hostname");
expect(options.port).toBe(9000);
expect(options.token).toBe("resttoken123");
options = SenderOptions.fromConfig(
"http::addr=hostname;token=resttoken123;",
);
expect(options.protocol).toBe("http");
expect(options.addr).toBe("hostname");
expect(options.host).toBe("hostname");
expect(options.port).toBe(9000);
expect(options.token).toBe("resttoken123");
options = SenderOptions.fromConfig(
"tcps::addr=hostname;username=user1;token=jwkprivkey123;",
);
expect(options.protocol).toBe("tcps");
expect(options.addr).toBe("hostname");
expect(options.host).toBe("hostname");
expect(options.port).toBe(9009);
expect(options.username).toBe("user1");
expect(options.token).toBe("jwkprivkey123");
options = SenderOptions.fromConfig(
"tcp::addr=hostname;username=user1;token=jwkprivkey123;",
);
expect(options.protocol).toBe("tcp");
expect(options.addr).toBe("hostname");
expect(options.host).toBe("hostname");
expect(options.port).toBe(9009);
expect(options.username).toBe("user1");
expect(options.token).toBe("jwkprivkey123");
});
it("fails if port is not a positive integer", function () {
expect(() => SenderOptions.fromConfig("tcp::addr=host:;")).toThrow(
"Port is required",
);
expect(() => SenderOptions.fromConfig("tcp::addr=host:0")).toThrow(
"Invalid port: 0",
);
expect(() => SenderOptions.fromConfig("tcp::addr=host:0.2")).toThrow(
"Invalid port: 0.2",
);
expect(() => SenderOptions.fromConfig("tcp::addr=host:-2")).toThrow(
"Invalid port: -2",
);
expect(() => SenderOptions.fromConfig("tcp::addr=host:!;")).toThrow(
"Invalid port: '!'",
);
expect(() => SenderOptions.fromConfig("tcp::addr=host:9009x;")).toThrow(
"Invalid port: '9009x'",
);
expect(() => SenderOptions.fromConfig("tcp::addr=host:900 9;")).toThrow(
"Invalid port: '900 9'",
);
});
it("fails if init_buf_size is not a positive integer", function () {
expect(() =>
SenderOptions.fromConfig("tcp::addr=host;init_buf_size=;"),
).toThrow("Invalid configuration, value is not set for 'init_buf_size'");
expect(() =>
SenderOptions.fromConfig("tcp::addr=host;init_buf_size=1024a;"),
).toThrow("Invalid initial buffer size option, not a number: '1024a'");
expect(() =>
SenderOptions.fromConfig("tcp::addr=host;init_buf_size=102 4;"),
).toThrow("Invalid initial buffer size option, not a number: '102 4'");
expect(() =>
SenderOptions.fromConfig("tcp::addr=host;init_buf_size=0;"),
).toThrow("Invalid initial buffer size option: 0");
});
it("fails if max_buf_size is not a positive integer", function () {
expect(() =>
SenderOptions.fromConfig("tcp::addr=host;max_buf_size=;"),
).toThrow("Invalid configuration, value is not set for 'max_buf_size'");
expect(() =>
SenderOptions.fromConfig("tcp::addr=host;max_buf_size=1024a;"),
).toThrow("Invalid max buffer size option, not a number: '1024a'");
expect(() =>
SenderOptions.fromConfig("tcp::addr=host;max_buf_size=102 4;"),
).toThrow("Invalid max buffer size option, not a number: '102 4'");
expect(() =>
SenderOptions.fromConfig("tcp::addr=host;max_buf_size=0;"),
).toThrow("Invalid max buffer size option: 0");
});
it("rejects missing or empty hostname", function () {
expect(() => SenderOptions.fromConfig("http::")).toThrow(
"Invalid configuration, 'addr' is required",
);
expect(() => SenderOptions.fromConfig("http::;")).toThrow(
"Missing '=' sign in ''",
);
expect(() => SenderOptions.fromConfig("http::addr=;")).toThrow(
"Invalid configuration, value is not set for 'addr'",
);
expect(() =>
SenderOptions.fromConfig("http::addr=;username=user1;"),
).toThrow("Invalid configuration, value is not set for 'addr'");
expect(() =>
SenderOptions.fromConfig("http::username=user1;addr=;"),
).toThrow("Invalid configuration, value is not set for 'addr'");
expect(() => SenderOptions.fromConfig("http::addr=:9000;")).toThrow(
"Host name is required",
);
const options = SenderOptions.fromConfig("http::addr=x;");
expect(options.protocol).toBe("http");
expect(options.host).toBe("x");
expect(options.host).toBe("x");
});
it("does not default optional fields", function () {
const options = SenderOptions.fromConfig(
"https::addr=host:9000;token=abcdef123;",
);
expect(options.protocol).toBe("https");
expect(options.token).toBe("abcdef123");
expect(options.username).toBe(undefined);
expect(options.password).toBe(undefined);
});
it("rejects invalid config value", function () {
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;username=;"),
).toThrow("Invalid configuration, value is not set for 'username'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;username=user\t;"),
).toThrow(
"Invalid configuration, control characters are not allowed: 'user\t'",
);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;username=user\n;"),
).toThrow(
"Invalid configuration, control characters are not allowed: 'user\n'",
);
let options = SenderOptions.fromConfig(
"http::addr=host:9000;username=us\x7Eer;",
);
expect(options.protocol).toBe("http");
expect(options.addr).toBe("host:9000");
expect(options.username).toBe("us\x7Eer");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;username=us\x7Fer;"),
).toThrow(
"Invalid configuration, control characters are not allowed: 'us\x7Fer'",
);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;username=us\x9Fer;"),
).toThrow(
"Invalid configuration, control characters are not allowed: 'us\x9Fer'",
);
options = SenderOptions.fromConfig(
"http::addr=host:9000;username=us\xA0er;",
);
expect(options.protocol).toBe("http");
expect(options.addr).toBe("host:9000");
expect(options.username).toBe("us\xA0er");
});
it("reject invalid config keys", function () {
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;username=user1;pass=pwd;"),
).toThrow("Unknown configuration key: 'pass'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;user=user1;password=pwd;"),
).toThrow("Unknown configuration key: 'user'");
expect(() =>
SenderOptions.fromConfig(
"http::addr=host:9000;username =user1;password=pwd;",
),
).toThrow("Unknown configuration key: 'username '");
expect(() =>
SenderOptions.fromConfig(
"http::addr=host:9000; username=user1;password=pwd;",
),
).toThrow("Unknown configuration key: ' username'");
expect(() =>
SenderOptions.fromConfig(
"http::addr=host:9000;user name=user1;password=pwd;",
),
).toThrow("Unknown configuration key: 'user name'");
});
it("rejects keys without value", function () {
expect(() => SenderOptions.fromConfig("http::addr;username=user1")).toThrow(
"Missing '=' sign in 'addr'",
);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;username;"),
).toThrow("Missing '=' sign in 'username'");
});
it("throws error if protocol is invalid", function () {
expect(() =>
SenderOptions.fromConfig("::addr=host;username=user1;password=pwd;"),
).toThrow(
"Invalid protocol: '', accepted protocols: 'http', 'https', 'tcp', 'tcps'",
);
expect(() =>
SenderOptions.fromConfig("htt::addr=host;username=user1;password=pwd;"),
).toThrow(
"Invalid protocol: 'htt', accepted protocols: 'http', 'https', 'tcp', 'tcps'",
);
});
it("throws error if protocol is missing", function () {
expect(() =>
SenderOptions.fromConfig("addr=host;username=user1;password=pwd;"),
).toThrow(
"Missing protocol, configuration string format: 'protocol::key1=value1;key2=value2;key3=value3;'",
);
expect(() =>
SenderOptions.fromConfig("https:addr=host;username=user1;password=pwd;"),
).toThrow(
"Missing protocol, configuration string format: 'protocol::key1=value1;key2=value2;key3=value3;'",
);
expect(() =>
SenderOptions.fromConfig("https addr=host;username=user1;password=pwd;"),
).toThrow(
"Missing protocol, configuration string format: 'protocol::key1=value1;key2=value2;key3=value3;'",
);
});
it("throws error if configuration string is missing", function () {
// @ts-expect-error - Testing invalid input
expect(() => SenderOptions.fromConfig()).toThrow(
"Configuration string is missing",
);
expect(() => SenderOptions.fromConfig("")).toThrow(
"Configuration string is missing",
);
expect(() => SenderOptions.fromConfig(null)).toThrow(
"Configuration string is missing",
);
expect(() => SenderOptions.fromConfig(undefined)).toThrow(
"Configuration string is missing",
);
});
it("can parse auto_flush config", function () {
let options = SenderOptions.fromConfig(
"http::addr=host:9000;auto_flush=on;",
);
expect(options.protocol).toBe("http");
expect(options.host).toBe("host");
expect(options.port).toBe(9000);
expect(options.auto_flush).toBe(true);
options = SenderOptions.fromConfig("http::addr=host:9000;auto_flush=off;");
expect(options.protocol).toBe("http");
expect(options.host).toBe("host");
expect(options.port).toBe(9000);
expect(options.auto_flush).toBe(false);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush=ON;"),
).toThrow("Invalid auto flush option: 'ON'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush=On;"),
).toThrow("Invalid auto flush option: 'On'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush=true;"),
).toThrow("Invalid auto flush option: 'true'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush=OFF;"),
).toThrow("Invalid auto flush option: 'OFF'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush=Off;"),
).toThrow("Invalid auto flush option: 'Off'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush=false;"),
).toThrow("Invalid auto flush option: 'false'");
});
it("can parse auto_flush_rows config", function () {
let options = SenderOptions.fromConfig(
"http::addr=host:9000;auto_flush_rows=123;",
);
expect(options.protocol).toBe("http");
expect(options.auto_flush_rows).toBe(123);
options = SenderOptions.fromConfig(
"http::addr=host:9000;auto_flush_rows=0;",
);
expect(options.protocol).toBe("http");
expect(options.auto_flush_rows).toBe(0);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_rows=-123;"),
).toThrow("Invalid auto flush rows option: -123");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_rows=1.23;"),
).toThrow("Invalid auto flush rows option: 1.23");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_rows=123x;"),
).toThrow("Invalid auto flush rows option, not a number: '123x'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_rows=a123;"),
).toThrow("Invalid auto flush rows option, not a number: 'a123'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_rows=1w23;"),
).toThrow("Invalid auto flush rows option, not a number: '1w23'");
});
it("can parse auto_flush_interval config", function () {
let options = SenderOptions.fromConfig(
"http::addr=host:9000;auto_flush_interval=30",
);
expect(options.protocol).toBe("http");
expect(options.auto_flush_interval).toBe(30);
options = SenderOptions.fromConfig(
"http::addr=host:9000;auto_flush_interval=0",
);
expect(options.protocol).toBe("http");
expect(options.auto_flush_interval).toBe(0);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_interval=-60"),
).toThrow("Invalid auto flush interval option: -60");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_interval=-6.0"),
).toThrow("Invalid auto flush interval option: -6");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_interval=60x"),
).toThrow("Invalid auto flush interval option, not a number: '60x'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_interval=a60"),
).toThrow("Invalid auto flush interval option, not a number: 'a60'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;auto_flush_interval=6w0"),
).toThrow("Invalid auto flush interval option, not a number: '6w0'");
});
it("can parse tls_verify config", function () {
let options = SenderOptions.fromConfig(
"http::addr=host:9000;tls_verify=on",
);
expect(options.protocol).toBe("http");
expect(options.host).toBe("host");
expect(options.port).toBe(9000);
expect(options.tls_verify).toBe(true);
options = SenderOptions.fromConfig(
"http::addr=host:9000;tls_verify=unsafe_off",
);
expect(options.protocol).toBe("http");
expect(options.host).toBe("host");
expect(options.port).toBe(9000);
expect(options.tls_verify).toBe(false);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_verify=ON"),
).toThrow("Invalid TLS verify option: 'ON'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_verify=On"),
).toThrow("Invalid TLS verify option: 'On'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_verify=true"),
).toThrow("Invalid TLS verify option: 'true'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_verify=OFF"),
).toThrow("Invalid TLS verify option: 'OFF'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_verify=Off"),
).toThrow("Invalid TLS verify option: 'Off'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_verify=UNSAFE_OFF"),
).toThrow("Invalid TLS verify option: 'UNSAFE_OFF'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_verify=Unsafe_Off"),
).toThrow("Invalid TLS verify option: 'Unsafe_Off'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_verify=false"),
).toThrow("Invalid TLS verify option: 'false'");
});
it("fails with tls_roots or tls_roots_password config", function () {
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_roots=/whatever/path"),
).toThrow(
"'tls_roots' and 'tls_roots_password' options are not supported, please, use the 'tls_ca' option or the NODE_EXTRA_CA_CERTS environment variable instead",
);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;tls_roots_password=pwd"),
).toThrow(
"'tls_roots' and 'tls_roots_password' options are not supported, please, use the 'tls_ca' option or the NODE_EXTRA_CA_CERTS environment variable instead",
);
});
it("can parse request_min_throughput config", function () {
const options = SenderOptions.fromConfig(
"http::addr=host:9000;request_min_throughput=300",
);
expect(options.protocol).toBe("http");
expect(options.request_min_throughput).toBe(300);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;request_min_throughput=0"),
).toThrow("Invalid request min throughput option: 0");
expect(() =>
SenderOptions.fromConfig(
"http::addr=host:9000;request_min_throughput=0.5",
),
).toThrow("Invalid request min throughput option: 0.5");
expect(() =>
SenderOptions.fromConfig(
"http::addr=host:9000;request_min_throughput=-60",
),
).toThrow("Invalid request min throughput option: -60");
expect(() =>
SenderOptions.fromConfig(
"http::addr=host:9000;request_min_throughput=60x",
),
).toThrow("Invalid request min throughput option, not a number: '60x'");
expect(() =>
SenderOptions.fromConfig(
"http::addr=host:9000;request_min_throughput=a60",
),
).toThrow("Invalid request min throughput option, not a number: 'a60'");
expect(() =>
SenderOptions.fromConfig(
"http::addr=host:9000;request_min_throughput=6w0",
),
).toThrow("Invalid request min throughput option, not a number: '6w0'");
});
it("can parse request_timeout config", function () {
const options = SenderOptions.fromConfig(
"http::addr=host:9000;request_timeout=30",
);
expect(options.protocol).toBe("http");
expect(options.request_timeout).toBe(30);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;request_timeout=0"),
).toThrow("Invalid request timeout option: 0");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;request_timeout=10.32"),
).toThrow("Invalid request timeout option: 10.32");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;request_timeout=-60"),
).toThrow("Invalid request timeout option: -60");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;request_timeout=60x"),
).toThrow("Invalid request timeout option, not a number: '60x'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;request_timeout=a60"),
).toThrow("Invalid request timeout option, not a number: 'a60'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;request_timeout=6w0"),
).toThrow("Invalid request timeout option, not a number: '6w0'");
});
it("can parse retry_timeout config", function () {
let options = SenderOptions.fromConfig(
"http::addr=host:9000;retry_timeout=60",
);
expect(options.protocol).toBe("http");
expect(options.retry_timeout).toBe(60);
options = SenderOptions.fromConfig("http::addr=host:9000;retry_timeout=0");
expect(options.protocol).toBe("http");
expect(options.retry_timeout).toBe(0);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;retry_timeout=-60"),
).toThrow("Invalid retry timeout option: -60");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;retry_timeout=-60.444"),
).toThrow("Invalid retry timeout option: -60.444");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;retry_timeout=60x"),
).toThrow("Invalid retry timeout option, not a number: '60x'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;retry_timeout=a60"),
).toThrow("Invalid retry timeout option, not a number: 'a60'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;retry_timeout=6w0"),
).toThrow("Invalid retry timeout option, not a number: '6w0'");
});
it("can parse copy_buffer config", function () {
let options = SenderOptions.fromConfig(
"http::addr=host:9000;copy_buffer=on;",
);
expect(options.protocol).toBe("http");
expect(options.host).toBe("host");
expect(options.port).toBe(9000);
expect(options.copy_buffer).toBe(true);
options = SenderOptions.fromConfig("http::addr=host:9000;copy_buffer=off;");
expect(options.protocol).toBe("http");
expect(options.host).toBe("host");
expect(options.port).toBe(9000);
expect(options.copy_buffer).toBe(false);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;copy_buffer=ON;"),
).toThrow("Invalid copy buffer option: 'ON'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;copy_buffer=On;"),
).toThrow("Invalid copy buffer option: 'On'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;copy_buffer=true;"),
).toThrow("Invalid copy buffer option: 'true'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;copy_buffer=OFF;"),
).toThrow("Invalid copy buffer option: 'OFF'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;copy_buffer=Off;"),
).toThrow("Invalid copy buffer option: 'Off'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;copy_buffer=false;"),
).toThrow("Invalid copy buffer option: 'false'");
});
it("can parse max_name_len config", function () {
const options = SenderOptions.fromConfig(
"http::addr=host:9000;max_name_len=30",
);
expect(options.protocol).toBe("http");
expect(options.max_name_len).toBe(30);
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;max_name_len=0"),
).toThrow("Invalid max name length option: 0");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;max_name_len=10.32"),
).toThrow("Invalid max name length option: 10.32");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;max_name_len=-60"),
).toThrow("Invalid max name length option: -60");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;max_name_len=60x"),
).toThrow("Invalid max name length option, not a number: '60x'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;max_name_len=a60"),
).toThrow("Invalid max name length option, not a number: 'a60'");
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000;max_name_len=6w0"),
).toThrow("Invalid max name length option, not a number: '6w0'");
});
it("can take a custom logger", function () {
const options = SenderOptions.fromConfig("http::addr=host:9000", {
log: console.log,
});
expect(options.protocol).toBe("http");
expect(options.log).toBe(console.log);
expect(() =>
// @ts-expect-error - Testing invalid input
SenderOptions.fromConfig("http::addr=host:9000", { log: 1234 }),
).toThrow("Invalid logging function");
expect(() =>
// @ts-expect-error - Testing invalid input
SenderOptions.fromConfig("http::addr=host:9000", { log: "hoppa" }),
).toThrow("Invalid logging function");
});
it("can take a custom agent", function () {
const agent = new Agent({ connect: { keepAlive: true } });
const options = SenderOptions.fromConfig("http::addr=host:9000", {
agent: agent,
});
expect(options.protocol).toBe("http");
const symbols = Object.getOwnPropertySymbols(options.agent);
expect(agent[symbols[6]]).toEqual({ connect: { keepAlive: true } });
agent.destroy();
expect(() =>
SenderOptions.fromConfig("http::addr=host:9000", {
// @ts-expect-error - Testing invalid input
agent: { keepAlive: true },
}),
).toThrow("Invalid http/https agent");
expect(() =>
// @ts-expect-error - Testing invalid input
SenderOptions.fromConfig("http::addr=host:9000", { agent: 4567 }),
).toThrow("Invalid http/https agent");
expect(() =>
// @ts-expect-error - Testing invalid input
SenderOptions.fromConfig("http::addr=host:9000", { agent: "hopp" }),
).toThrow("Invalid http/https agent");
});
});