@yousolution/node-red-contrib-you-sap-service-layer 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,934 @@
1
+ const should = require('should');
2
+ const expect = require('chai').expect;
3
+ const sinon = require('sinon');
4
+ const Support = require('../nodes/support');
5
+ // import * as Support from '../nodes/support';
6
+
7
+ describe('support library', () => {
8
+ // beforeEach((done) => {
9
+ // // helper.startServer(done);
10
+ // });
11
+
12
+ // afterEach(() => {
13
+ // // Restore the default sandbox here
14
+ // sinon.restore();
15
+ // });
16
+
17
+ describe('generateRequest() ', () => {
18
+ it('should generate a correct request', () => {
19
+ const node = {
20
+ context: () => {
21
+ return {
22
+ flow: {
23
+ get: (param) => {
24
+ if (param == '_YOU_SapServiceLayer_1.host') {
25
+ return 'host';
26
+ }
27
+ if (param == '_YOU_SapServiceLayer_1.port') {
28
+ return 'port';
29
+ }
30
+ if (param == '_YOU_SapServiceLayer_1.version') {
31
+ return 'version';
32
+ }
33
+ if (param == '_YOU_SapServiceLayer_1.headers') {
34
+ return ['header:1', 'header:2'];
35
+ }
36
+ },
37
+ },
38
+ };
39
+ },
40
+ };
41
+
42
+ const msg = {
43
+ _YOU_SapServiceLayer: {
44
+ idAuth: 1,
45
+ },
46
+ };
47
+ const config = {
48
+ entity: 'businessPartner',
49
+ query: { select: ['ItemCode', 'ItemName'] },
50
+ };
51
+ let options = { method: 'GET', hasRawQuery: true };
52
+ const expectedValue = {
53
+ axiosOptions: {
54
+ headers: {
55
+ Cookie: 'header:1;header:2',
56
+ },
57
+ method: 'GET',
58
+ rejectUnauthorized: false,
59
+ url: 'https://host:port/b1s/version/businessPartner?$select=ItemCode,ItemName',
60
+ withCredentials: true,
61
+ },
62
+ idAuthNode: 1,
63
+ };
64
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue);
65
+
66
+ // Headers
67
+ const config2 = {
68
+ entity: 'businessPartner',
69
+ headers: 'myHeaders',
70
+ query: { select: ['ItemCode', 'ItemName'] },
71
+ };
72
+ const msg2 = {
73
+ _YOU_SapServiceLayer: {
74
+ idAuth: 1,
75
+ },
76
+ myHeaders: { Prefer: 'odata.maxpagesize=5' },
77
+ };
78
+ const options2 = { method: 'GET', hasRawQuery: false };
79
+ const expectedValue2 = {
80
+ axiosOptions: {
81
+ headers: {
82
+ Cookie: 'header:1;header:2',
83
+ Prefer: 'odata.maxpagesize=5',
84
+ },
85
+ method: 'GET',
86
+ rejectUnauthorized: false,
87
+ url: 'https://host:port/b1s/version/businessPartner',
88
+ withCredentials: true,
89
+ },
90
+ idAuthNode: 1,
91
+ };
92
+ should.deepEqual(Support.generateRequest(node, msg2, config2, options2), expectedValue2);
93
+ });
94
+
95
+ it('should generate a correct request with entityId', () => {
96
+ const node = {
97
+ context: () => {
98
+ return {
99
+ flow: {
100
+ get: (param) => {
101
+ if (param == '_YOU_SapServiceLayer_1.host') {
102
+ return 'host';
103
+ }
104
+ if (param == '_YOU_SapServiceLayer_1.port') {
105
+ return 'port';
106
+ }
107
+ if (param == '_YOU_SapServiceLayer_1.version') {
108
+ return 'version';
109
+ }
110
+ if (param == '_YOU_SapServiceLayer_1.headers') {
111
+ return ['header:1', 'header:2'];
112
+ }
113
+ },
114
+ },
115
+ };
116
+ },
117
+ };
118
+
119
+ const msg = {
120
+ _YOU_SapServiceLayer: {
121
+ idAuth: 1,
122
+ },
123
+ entityId: 1,
124
+ };
125
+ let config = {
126
+ entity: 'BusinessPartner',
127
+ entityId: 'entityId',
128
+ };
129
+ const options = { method: 'GET', hasRawQuery: true, hasEntityId: true };
130
+ const expectedValue = {
131
+ axiosOptions: {
132
+ headers: {
133
+ Cookie: 'header:1;header:2',
134
+ },
135
+ method: 'GET',
136
+ rejectUnauthorized: false,
137
+ url: 'https://host:port/b1s/version/BusinessPartner(1)',
138
+ withCredentials: true,
139
+ },
140
+ idAuthNode: 1,
141
+ };
142
+
143
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue);
144
+
145
+ config.entity = 'EmailGroups';
146
+ const expectedValue1 = {
147
+ axiosOptions: {
148
+ headers: {
149
+ Cookie: 'header:1;header:2',
150
+ },
151
+ method: 'GET',
152
+ rejectUnauthorized: false,
153
+ url: `https://host:port/b1s/version/EmailGroups('1')`,
154
+ withCredentials: true,
155
+ },
156
+ idAuthNode: 1,
157
+ };
158
+
159
+ // thick SAP hack
160
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue1);
161
+ });
162
+ it('should generate a correct request change HTTP VERB', () => {
163
+ const node = {
164
+ context: () => {
165
+ return {
166
+ flow: {
167
+ get: (param) => {
168
+ if (param == '_YOU_SapServiceLayer_1.host') {
169
+ return 'host';
170
+ }
171
+ if (param == '_YOU_SapServiceLayer_1.port') {
172
+ return 'port';
173
+ }
174
+ if (param == '_YOU_SapServiceLayer_1.version') {
175
+ return 'version';
176
+ }
177
+ if (param == '_YOU_SapServiceLayer_1.headers') {
178
+ return ['header:1', 'header:2'];
179
+ }
180
+ },
181
+ },
182
+ };
183
+ },
184
+ };
185
+
186
+ const msg = {
187
+ _YOU_SapServiceLayer: {
188
+ idAuth: 1,
189
+ },
190
+ };
191
+ let config = {
192
+ entity: 'BusinessPartner',
193
+ };
194
+ const options = { method: 'POST', hasRawQuery: true, hasEntityId: false };
195
+ const expectedValue = {
196
+ axiosOptions: {
197
+ headers: {
198
+ Cookie: 'header:1;header:2',
199
+ },
200
+ method: 'POST',
201
+ rejectUnauthorized: false,
202
+ url: 'https://host:port/b1s/version/BusinessPartner',
203
+ withCredentials: true,
204
+ },
205
+ idAuthNode: 1,
206
+ };
207
+
208
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue);
209
+ });
210
+ it('should generate a correct request with nextLink', () => {
211
+ const node = {
212
+ context: () => {
213
+ return {
214
+ flow: {
215
+ get: (param) => {
216
+ if (param == '_YOU_SapServiceLayer_1.host') {
217
+ return 'host';
218
+ }
219
+ if (param == '_YOU_SapServiceLayer_1.port') {
220
+ return 'port';
221
+ }
222
+ if (param == '_YOU_SapServiceLayer_1.version') {
223
+ return 'version';
224
+ }
225
+ if (param == '_YOU_SapServiceLayer_1.headers') {
226
+ return ['header:1', 'header:2'];
227
+ }
228
+ },
229
+ },
230
+ };
231
+ },
232
+ };
233
+
234
+ const msg = {
235
+ _YOU_SapServiceLayer: {
236
+ idAuth: 1,
237
+ },
238
+ nextLink: 'BusinessPartners?$select=CardCode&$skip=50005',
239
+ };
240
+ const config = {
241
+ entity: 'businessPartner',
242
+ nextLink: 'nextLink',
243
+ query: { select: ['ItemCode', 'ItemName'] },
244
+ };
245
+ let options = { method: 'GET', hasRawQuery: true };
246
+ const expectedValue = {
247
+ axiosOptions: {
248
+ headers: {
249
+ Cookie: 'header:1;header:2',
250
+ },
251
+ method: 'GET',
252
+ rejectUnauthorized: false,
253
+ url: 'https://host:port/b1s/version/BusinessPartners?$select=CardCode&$skip=50005',
254
+ withCredentials: true,
255
+ },
256
+ idAuthNode: 1,
257
+ };
258
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue);
259
+ });
260
+
261
+ it('should generate a correct request Close', () => {
262
+ const node = {
263
+ context: () => {
264
+ return {
265
+ flow: {
266
+ get: (param) => {
267
+ if (param == '_YOU_SapServiceLayer_1.host') {
268
+ return 'host';
269
+ }
270
+ if (param == '_YOU_SapServiceLayer_1.port') {
271
+ return 'port';
272
+ }
273
+ if (param == '_YOU_SapServiceLayer_1.version') {
274
+ return 'version';
275
+ }
276
+ if (param == '_YOU_SapServiceLayer_1.headers') {
277
+ return ['header:1', 'header:2'];
278
+ }
279
+ },
280
+ },
281
+ };
282
+ },
283
+ };
284
+
285
+ const msg = {
286
+ _YOU_SapServiceLayer: {
287
+ idAuth: 1,
288
+ },
289
+ entityId: 1,
290
+ };
291
+ const config = {
292
+ entity: 'BusinessPartners',
293
+ entityId: 'entityId',
294
+ };
295
+ let options = { method: 'POST', hasRawQuery: false, isClose: true, hasEntityId: true };
296
+ const expectedValue = {
297
+ axiosOptions: {
298
+ headers: {
299
+ Cookie: 'header:1;header:2',
300
+ },
301
+ method: 'POST',
302
+ rejectUnauthorized: false,
303
+ url: `https://host:port/b1s/version/BusinessPartners('1')/Close`,
304
+ withCredentials: true,
305
+ },
306
+ idAuthNode: 1,
307
+ };
308
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue);
309
+ });
310
+
311
+ it('should generate a correct request with UDO', () => {
312
+ const node = {
313
+ context: () => {
314
+ return {
315
+ flow: {
316
+ get: (param) => {
317
+ if (param == '_YOU_SapServiceLayer_1.host') {
318
+ return 'host';
319
+ }
320
+ if (param == '_YOU_SapServiceLayer_1.port') {
321
+ return 'port';
322
+ }
323
+ if (param == '_YOU_SapServiceLayer_1.version') {
324
+ return 'version';
325
+ }
326
+ if (param == '_YOU_SapServiceLayer_1.headers') {
327
+ return ['header:1', 'header:2'];
328
+ }
329
+ },
330
+ },
331
+ };
332
+ },
333
+ };
334
+
335
+ const msg = {
336
+ _YOU_SapServiceLayer: {
337
+ idAuth: 1,
338
+ },
339
+ DocEntry: 1,
340
+ };
341
+ let config = {
342
+ entity: 'UDO',
343
+ udo: 'YOU_SAP_CUSTOM_ENTITY',
344
+ docEntry: 'DocEntry',
345
+ };
346
+ const options = { method: 'GET', hasRawQuery: true, hasEntityId: true };
347
+ const expectedValue = {
348
+ axiosOptions: {
349
+ headers: {
350
+ Cookie: 'header:1;header:2',
351
+ },
352
+ method: 'GET',
353
+ rejectUnauthorized: false,
354
+ url: 'https://host:port/b1s/version/YOU_SAP_CUSTOM_ENTITY(1)',
355
+ withCredentials: true,
356
+ },
357
+ idAuthNode: 1,
358
+ };
359
+
360
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue);
361
+ });
362
+
363
+ it('should generate a request without DocEntry (UDO)', () => {
364
+ const node = {
365
+ context: () => {
366
+ return {
367
+ flow: {
368
+ get: (param) => {
369
+ if (param == '_YOU_SapServiceLayer_1.host') {
370
+ return 'host';
371
+ }
372
+ if (param == '_YOU_SapServiceLayer_1.port') {
373
+ return 'port';
374
+ }
375
+ if (param == '_YOU_SapServiceLayer_1.version') {
376
+ return 'version';
377
+ }
378
+ if (param == '_YOU_SapServiceLayer_1.headers') {
379
+ return ['header:1', 'header:2'];
380
+ }
381
+ },
382
+ },
383
+ };
384
+ },
385
+ };
386
+
387
+ const msg = {
388
+ _YOU_SapServiceLayer: {
389
+ idAuth: 1,
390
+ },
391
+ };
392
+ let config = {
393
+ entity: 'UDO',
394
+ udo: 'YOU_SAP_CUSTOM_ENTITY',
395
+ docEntry: 'docEntry',
396
+ };
397
+ const options = { method: 'GET', hasRawQuery: true, hasEntityId: true };
398
+
399
+ expect(() => {
400
+ Support.generateRequest(node, msg, config, options);
401
+ }).to.throw('Missing docEntry');
402
+ });
403
+
404
+ it('should generate a correct request with UDT', () => {
405
+ const node = {
406
+ context: () => {
407
+ return {
408
+ flow: {
409
+ get: (param) => {
410
+ if (param == '_YOU_SapServiceLayer_1.host') {
411
+ return 'host';
412
+ }
413
+ if (param == '_YOU_SapServiceLayer_1.port') {
414
+ return 'port';
415
+ }
416
+ if (param == '_YOU_SapServiceLayer_1.version') {
417
+ return 'version';
418
+ }
419
+ if (param == '_YOU_SapServiceLayer_1.headers') {
420
+ return ['header:1', 'header:2'];
421
+ }
422
+ },
423
+ },
424
+ };
425
+ },
426
+ };
427
+
428
+ const msg = {
429
+ _YOU_SapServiceLayer: {
430
+ idAuth: 1,
431
+ },
432
+ Code: '0001',
433
+ };
434
+ let config = {
435
+ entity: 'UDT',
436
+ udt: 'YOU_SAP_CUSTOM_ENTITY',
437
+ code: 'Code',
438
+ };
439
+ const options = { method: 'GET', hasRawQuery: true, hasEntityId: true };
440
+ const expectedValue = {
441
+ axiosOptions: {
442
+ headers: {
443
+ Cookie: 'header:1;header:2',
444
+ },
445
+ method: 'GET',
446
+ rejectUnauthorized: false,
447
+ url: `https://host:port/b1s/version/YOU_SAP_CUSTOM_ENTITY('0001')`,
448
+ withCredentials: true,
449
+ },
450
+ idAuthNode: 1,
451
+ };
452
+
453
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue);
454
+ });
455
+
456
+ it('should generate a request without Code (UDT)', () => {
457
+ const node = {
458
+ context: () => {
459
+ return {
460
+ flow: {
461
+ get: (param) => {
462
+ if (param == '_YOU_SapServiceLayer_1.host') {
463
+ return 'host';
464
+ }
465
+ if (param == '_YOU_SapServiceLayer_1.port') {
466
+ return 'port';
467
+ }
468
+ if (param == '_YOU_SapServiceLayer_1.version') {
469
+ return 'version';
470
+ }
471
+ if (param == '_YOU_SapServiceLayer_1.headers') {
472
+ return ['header:1', 'header:2'];
473
+ }
474
+ },
475
+ },
476
+ };
477
+ },
478
+ };
479
+
480
+ const msg = {
481
+ _YOU_SapServiceLayer: {
482
+ idAuth: 1,
483
+ },
484
+ };
485
+ let config = {
486
+ entity: 'UDT',
487
+ udt: 'YOU_SAP_CUSTOM_ENTITY',
488
+ code: 'Code',
489
+ };
490
+ const options = { method: 'GET', hasRawQuery: true, hasEntityId: true };
491
+
492
+ expect(() => {
493
+ Support.generateRequest(node, msg, config, options);
494
+ }).to.throw('Missing Code');
495
+ });
496
+
497
+ it('should generate a correct cross join request', () => {
498
+ const node = {
499
+ context: () => {
500
+ return {
501
+ flow: {
502
+ get: (param) => {
503
+ if (param == '_YOU_SapServiceLayer_1.host') {
504
+ return 'host';
505
+ }
506
+ if (param == '_YOU_SapServiceLayer_1.port') {
507
+ return 'port';
508
+ }
509
+ if (param == '_YOU_SapServiceLayer_1.version') {
510
+ return 'version';
511
+ }
512
+ if (param == '_YOU_SapServiceLayer_1.headers') {
513
+ return ['header:1', 'header:2'];
514
+ }
515
+ },
516
+ },
517
+ };
518
+ },
519
+ };
520
+
521
+ const rawQuery = {
522
+ expand: {
523
+ Orders: { select: ['DocEntry', 'DocNum'] },
524
+ BusinessPartners: { select: ['CardCode', 'CardName'] },
525
+ },
526
+ filter: {
527
+ 'Orders/CardCode': { eq: { type: 'raw', value: 'BusinessPartners/CardCode' } },
528
+ },
529
+ orderBy: ['CardCode desc'],
530
+ };
531
+
532
+ const msg = {
533
+ _YOU_SapServiceLayer: {
534
+ idAuth: 1,
535
+ },
536
+ };
537
+ const config = {
538
+ entity: 'BusinessPartners,Orders',
539
+ query: rawQuery,
540
+ };
541
+ const options = { method: 'GET', hasRawQuery: true, hasEntityId: false, isCrossJoin: true };
542
+ const expectedValue = {
543
+ axiosOptions: {
544
+ headers: {
545
+ Cookie: 'header:1;header:2',
546
+ },
547
+ method: 'GET',
548
+ rejectUnauthorized: false,
549
+ url: 'https://host:port/b1s/version/$crossjoin(BusinessPartners,Orders)?$filter=Orders/CardCode eq BusinessPartners/CardCode&$expand=Orders($select=DocEntry,DocNum),BusinessPartners($select=CardCode,CardName)&$orderby=CardCode desc',
550
+ withCredentials: true,
551
+ },
552
+ idAuthNode: 1,
553
+ };
554
+
555
+ should.deepEqual(Support.generateRequest(node, msg, config, options), expectedValue);
556
+ });
557
+
558
+ it('should have error missing object', () => {
559
+ const node = {
560
+ context: () => {
561
+ return {
562
+ flow: {
563
+ get: (param) => {
564
+ if (param == '_YOU_SapServiceLayer_1.host') {
565
+ return 'host';
566
+ }
567
+ if (param == '_YOU_SapServiceLayer_1.port') {
568
+ return 'port';
569
+ }
570
+ if (param == '_YOU_SapServiceLayer_1.version') {
571
+ return 'version';
572
+ }
573
+ if (param == '_YOU_SapServiceLayer_1.headers') {
574
+ return ['header:1', 'header:2'];
575
+ }
576
+ },
577
+ },
578
+ };
579
+ },
580
+ };
581
+
582
+ const msg = {
583
+ _YOU_SapServiceLayer: {
584
+ idAuth: 1,
585
+ },
586
+ };
587
+ const config = {};
588
+ const options = { method: 'GET', hasRawQuery: true };
589
+
590
+ expect(() => {
591
+ Support.generateRequest(node, msg, config, options);
592
+ }).to.throw('Missing entity');
593
+ });
594
+
595
+ it('should have error missing idAuthNode', () => {
596
+ const node = {
597
+ context: () => {
598
+ return {
599
+ flow: {
600
+ get: (param) => {},
601
+ },
602
+ };
603
+ },
604
+ };
605
+
606
+ const msg = {};
607
+ const config = {};
608
+ const options = { method: 'GET', hasRawQuery: true };
609
+
610
+ expect(() => {
611
+ Support.generateRequest(node, msg, config, options);
612
+ }).to.throw('Authentication failed');
613
+ });
614
+ });
615
+
616
+ describe('sendRequest()', () => {
617
+ it('should send a correct request', async () => {
618
+ const node = {
619
+ context: () => {
620
+ return {
621
+ flow: {
622
+ get: (param) => {
623
+ if (param == '_YOU_SapServiceLayer_1.host') {
624
+ return 'host';
625
+ }
626
+ if (param == '_YOU_SapServiceLayer_1.port') {
627
+ return 'port';
628
+ }
629
+ if (param == '_YOU_SapServiceLayer_1.version') {
630
+ return 'version';
631
+ }
632
+ if (param == '_YOU_SapServiceLayer_1.headers') {
633
+ return ['header:1', 'header:2'];
634
+ }
635
+ },
636
+ },
637
+ };
638
+ },
639
+ };
640
+
641
+ const login = async () => Promise.resolve();
642
+
643
+ const msg = {
644
+ _YOU_SapServiceLayer: {
645
+ idAuth: 1,
646
+ },
647
+ };
648
+ const config = {
649
+ entity: 'businessPartner',
650
+ };
651
+ const options = { method: 'GET', hasRawQuery: true };
652
+ const axios = async () => {
653
+ return true;
654
+ };
655
+
656
+ const actual = await Support.sendRequest({ node, msg, config, login, axios, options });
657
+
658
+ should.equal(actual, true);
659
+ });
660
+
661
+ it('should send a request without mandatory arguments', async () => {
662
+ const node = {
663
+ context: () => {
664
+ return {
665
+ flow: {
666
+ get: (param) => {
667
+ if (param == '_YOU_SapServiceLayer_1.host') {
668
+ return 'host';
669
+ }
670
+ if (param == '_YOU_SapServiceLayer_1.port') {
671
+ return 'port';
672
+ }
673
+ if (param == '_YOU_SapServiceLayer_1.version') {
674
+ return 'version';
675
+ }
676
+ if (param == '_YOU_SapServiceLayer_1.headers') {
677
+ return ['header:1', 'header:2'];
678
+ }
679
+ },
680
+ },
681
+ };
682
+ },
683
+ };
684
+
685
+ const msg = {
686
+ _YOU_SapServiceLayer: {
687
+ idAuth: 1,
688
+ },
689
+ };
690
+ const config = {
691
+ entity: 'businessPartner',
692
+ };
693
+ const options = { method: 'GET', hasRawQuery: true };
694
+
695
+ const expect = new Error(`Missing mandatory params: config,axios,login.`);
696
+ try {
697
+ await Support.sendRequest({ node, msg, options });
698
+ } catch (error) {
699
+ should.deepEqual(error, expect);
700
+ }
701
+ });
702
+
703
+ it('should send session error', async () => {
704
+ const node = {
705
+ context: () => {
706
+ return {
707
+ flow: {
708
+ get: (param) => {
709
+ if (param == '_YOU_SapServiceLayer_1.host') {
710
+ return 'host';
711
+ }
712
+ if (param == '_YOU_SapServiceLayer_1.port') {
713
+ return 'port';
714
+ }
715
+ if (param == '_YOU_SapServiceLayer_1.version') {
716
+ return 'version';
717
+ }
718
+ if (param == '_YOU_SapServiceLayer_1.headers') {
719
+ return ['header:1', 'header:2'];
720
+ }
721
+ },
722
+ set: (param) => {},
723
+ },
724
+ };
725
+ },
726
+ };
727
+
728
+ const msg = {
729
+ _YOU_SapServiceLayer: {
730
+ idAuth: 1,
731
+ },
732
+ };
733
+ const config = {
734
+ entity: 'businessPartner',
735
+ };
736
+ const options = { method: 'GET', hasRawQuery: true };
737
+
738
+ const stubLogin = sinon.stub();
739
+ stubLogin.resolves({
740
+ headers: {
741
+ 'Content-Type': 'application/json',
742
+ 'Content-Length': 100,
743
+ 'set-cookie': 'cookie1=1;cookie2',
744
+ },
745
+ });
746
+
747
+ const axios = sinon.stub();
748
+ axios.onCall(0).returns(
749
+ Promise.reject({
750
+ response: {
751
+ status: 301,
752
+ },
753
+ })
754
+ );
755
+ axios.onCall(1).returns(true);
756
+
757
+ const actual = await Support.sendRequest({ node, msg, config, login: stubLogin, axios, options });
758
+ should.equal(actual, true);
759
+ });
760
+
761
+ it('should send a generic error', async () => {
762
+ const node = {
763
+ context: () => {
764
+ return {
765
+ flow: {
766
+ get: (param) => {
767
+ if (param == '_YOU_SapServiceLayer_1.host') {
768
+ return 'host';
769
+ }
770
+ if (param == '_YOU_SapServiceLayer_1.port') {
771
+ return 'port';
772
+ }
773
+ if (param == '_YOU_SapServiceLayer_1.version') {
774
+ return 'version';
775
+ }
776
+ if (param == '_YOU_SapServiceLayer_1.headers') {
777
+ return ['header:1', 'header:2'];
778
+ }
779
+ },
780
+ },
781
+ };
782
+ },
783
+ };
784
+
785
+ const msg = {
786
+ _YOU_SapServiceLayer: {
787
+ idAuth: 1,
788
+ },
789
+ };
790
+ const config = {
791
+ entity: 'businessPartner',
792
+ };
793
+ const options = { method: 'GET', hasRawQuery: true };
794
+ const login = async () => Promise.resolve();
795
+ const axios = async () => {
796
+ return Promise.reject(new Error('Custom error'));
797
+ };
798
+
799
+ let actual = null;
800
+ try {
801
+ await Support.sendRequest({ node, msg, config, axios, login, options });
802
+ } catch (error) {
803
+ actual = error;
804
+ }
805
+
806
+ should.deepEqual(actual, new Error('Custom error'));
807
+ });
808
+
809
+ it('should handle axios response error #1', async () => {
810
+ const node = {
811
+ send: () => {},
812
+ context: () => {
813
+ return {
814
+ flow: {
815
+ get: (param) => {
816
+ if (param == '_YOU_SapServiceLayer_1.host') {
817
+ return 'host';
818
+ }
819
+ if (param == '_YOU_SapServiceLayer_1.port') {
820
+ return 'port';
821
+ }
822
+ if (param == '_YOU_SapServiceLayer_1.version') {
823
+ return 'version';
824
+ }
825
+ if (param == '_YOU_SapServiceLayer_1.headers') {
826
+ return ['header:1', 'header:2'];
827
+ }
828
+ },
829
+ },
830
+ };
831
+ },
832
+ };
833
+
834
+ const msg = {
835
+ _YOU_SapServiceLayer: {
836
+ idAuth: 1,
837
+ },
838
+ };
839
+ const config = {
840
+ entity: 'businessPartner',
841
+ };
842
+ const options = { method: 'GET', hasRawQuery: true };
843
+
844
+ const axiosError = {
845
+ response: {
846
+ data: 'Custom Error',
847
+ },
848
+ };
849
+
850
+ const expect = new Error(JSON.stringify(axiosError.response.data));
851
+
852
+ const login = async () => Promise.resolve({});
853
+ const axios = async () => {
854
+ return Promise.reject(axiosError);
855
+ };
856
+
857
+ let actual = null;
858
+ try {
859
+ await Support.sendRequest({ node, msg, config, axios, login, options });
860
+ } catch (error) {
861
+ actual = error;
862
+ }
863
+
864
+ should.deepEqual(actual, expect);
865
+ });
866
+
867
+ it('should handle axios response error 401 #2', async () => {
868
+ const node = {
869
+ send: () => {},
870
+ context: () => {
871
+ return {
872
+ flow: {
873
+ get: (param) => {
874
+ if (param == '_YOU_SapServiceLayer_1.host') {
875
+ return 'host';
876
+ }
877
+ if (param == '_YOU_SapServiceLayer_1.port') {
878
+ return 'port';
879
+ }
880
+ if (param == '_YOU_SapServiceLayer_1.version') {
881
+ return 'version';
882
+ }
883
+ if (param == '_YOU_SapServiceLayer_1.headers') {
884
+ return ['header:1', 'header:2'];
885
+ }
886
+ },
887
+ set: () => {},
888
+ },
889
+ };
890
+ },
891
+ };
892
+
893
+ const msg = {
894
+ _YOU_SapServiceLayer: {
895
+ idAuth: 1,
896
+ },
897
+ };
898
+ const config = {
899
+ entity: 'businessPartner',
900
+ };
901
+ const options = { method: 'GET', hasRawQuery: true };
902
+
903
+ const axiosError = {
904
+ response: {
905
+ status: 401,
906
+ data: 'Custom Error',
907
+ },
908
+ };
909
+
910
+ const expect = new Error(JSON.stringify(axiosError.response.data));
911
+
912
+ const login = async () =>
913
+ Promise.resolve({
914
+ headers: {
915
+ 'Content-Type': 'application/json',
916
+ 'Content-Length': 100,
917
+ 'set-cookie': 'cookie1=1;cookie2',
918
+ },
919
+ });
920
+ const axios = async () => {
921
+ return Promise.reject(axiosError);
922
+ };
923
+
924
+ let actual = null;
925
+ try {
926
+ await Support.sendRequest({ node, msg, config, axios, login, options });
927
+ } catch (error) {
928
+ actual = error;
929
+ }
930
+
931
+ should.deepEqual(actual, expect);
932
+ });
933
+ });
934
+ });