forked from orval-labs/orval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
1691 lines (1530 loc) · 59.5 KB
/
Copy pathindex.test.ts
File metadata and controls
1691 lines (1530 loc) · 59.5 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
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
import type {
GeneratorOptions,
GeneratorVerbOptions,
NormalizedOverrideOutput,
} from '@orval/core';
import { OutputMockType } from '@orval/core';
import { describe, expect, it } from 'vitest';
import { generateMSW } from './index';
describe('generateMSW', () => {
const mockVerbOptions = {
operationId: 'getUser',
operationName: 'getUser',
verb: 'get',
tags: [],
response: {
imports: [],
definition: { success: 'User' },
types: { success: [{ key: '200', value: 'User' }] },
contentTypes: ['application/json'],
},
} as unknown as GeneratorVerbOptions;
const baseOptions = {
route: '/users/{id}',
pathRoute: '/users/{id}',
output: 'test',
override: { operations: {}, tags: {} } as NormalizedOverrideOutput,
context: {
target: 'test',
workspace: '',
spec: {
openapi: '3.0.0',
info: { title: 'Test', version: '1.0.0' },
paths: {},
},
output: {
target: 'test',
namingConvention: 'camelCase',
fileExtension: '.ts',
mode: 'single',
override: { operations: {}, tags: {} } as NormalizedOverrideOutput,
client: 'axios-functions',
httpClient: 'fetch',
clean: false,
docs: false,
formatter: undefined,
headers: false,
indexFiles: true,
allParamsOptional: false,
urlEncodeParameters: false,
unionAddMissingProperties: false,
optionsParamRequired: false,
propertySortOrder: 'specification',
},
},
} as unknown as GeneratorOptions;
const generate = (overrides: Partial<GeneratorOptions> = {}) =>
generateMSW(mockVerbOptions, { ...baseOptions, ...overrides });
describe('delay functionality', () => {
it('should not include delay call when no delay is provided', () => {
const result = generate();
expect(result.implementation.handler).not.toContain('await delay');
});
it('should include delay call when delay is a number', () => {
const result = generate({
mock: { type: OutputMockType.MSW, delay: 100 },
});
expect(result.implementation.handler).toContain('await delay(100);');
});
it('should not include delay call when delay is false', () => {
const result = generate({
mock: { type: OutputMockType.MSW, delay: false },
});
expect(result.implementation.handler).not.toContain('await delay');
});
it('should execute delay function immediately when delayFunctionLazyExecute is false', () => {
const result = generate({
mock: {
type: OutputMockType.MSW,
delay: () => 200,
delayFunctionLazyExecute: false,
},
});
expect(result.implementation.handler).toContain('await delay(200);');
});
it('should preserve delay function when delayFunctionLazyExecute is true', () => {
const result = generate({
mock: {
type: OutputMockType.MSW,
delay: () => 10,
delayFunctionLazyExecute: true,
},
});
expect(result.implementation.handler).toContain(
'await delay((() => 10)());',
);
});
it('should handle delay function in override.mock', () => {
const result = generate({
override: {
...baseOptions.override,
mock: { delay: () => 300, delayFunctionLazyExecute: true },
},
});
expect(result.implementation.handler).toContain(
'await delay((() => 300)());',
);
});
it('should prioritize override.mock.delay over options.mock.delay', () => {
const result = generate({
mock: { type: OutputMockType.MSW, delay: 100 },
override: { ...baseOptions.override, mock: { delay: 500 } },
});
expect(result.implementation.handler).toContain('await delay(500);');
expect(result.implementation.handler).not.toContain('await delay(100);');
});
it('should prioritize override.mock.delayFunctionLazyExecute', () => {
const result = generate({
mock: {
type: OutputMockType.MSW,
delay: () => 100,
delayFunctionLazyExecute: false,
},
override: {
...baseOptions.override,
mock: { delay: () => 200, delayFunctionLazyExecute: true },
},
});
expect(result.implementation.handler).toContain(
'await delay((() => 200)());',
);
});
});
describe('handler generation', () => {
it('should generate a valid MSW handler', () => {
const result = generate({
mock: { type: OutputMockType.MSW, delay: 100 },
});
expect(result.implementation.handlerName).toBe('getGetUserMockHandler');
expect(result.implementation.handler).toContain(
'export const getGetUserMockHandler',
);
expect(result.implementation.handler).toContain('return http.get');
});
it('should not include JSON.stringify in handler (uses HttpResponse.json or null body)', () => {
const result = generate();
// When there is no mock data, handler returns HttpResponse with null body
// When there IS mock data, handler should use HttpResponse.json (not JSON.stringify)
expect(result.implementation.handler).not.toContain('JSON.stringify');
});
it('should handle binary/Blob response types without JSON.stringify', () => {
const blobVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Blob' },
types: {
success: [{ key: '200', value: 'Blob' }],
},
contentTypes: ['application/octet-stream'],
},
} as GeneratorVerbOptions;
const result = generateMSW(blobVerbOptions, baseOptions);
expect(result.implementation.handler).not.toContain('JSON.stringify');
expect(result.implementation.handler).toContain(
'application/octet-stream',
);
// Mock function signature should use ArrayBuffer instead of Blob
expect(result.implementation.handler).not.toContain(
'overrideResponse?: Blob',
);
expect(result.implementation.handler).toContain(
'HttpResponse.arrayBuffer',
);
// The mock function should return an ArrayBuffer
expect(result.implementation.function).toContain('ArrayBuffer');
expect(result.implementation.function).not.toContain('Blob');
});
it('should handle image content types as binary', () => {
const imageVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Blob' },
types: {
success: [{ key: '200', value: 'Blob' }],
},
contentTypes: ['image/png'],
},
} as GeneratorVerbOptions;
const result = generateMSW(imageVerbOptions, baseOptions);
expect(result.implementation.handler).not.toContain('JSON.stringify');
expect(result.implementation.handler).toContain('image/png');
});
it('should handle font content types as binary', () => {
const fontVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Blob' },
types: {
success: [{ key: '200', value: 'Blob' }],
},
contentTypes: ['font/woff2'],
},
} as GeneratorVerbOptions;
const result = generateMSW(fontVerbOptions, baseOptions);
expect(result.implementation.handler).not.toContain('JSON.stringify');
expect(result.implementation.handler).toContain('font/woff2');
expect(result.implementation.handler).toContain(
'HttpResponse.arrayBuffer',
);
});
// Regression: issue #3065
// When the OpenAPI upgrader converts `format: binary` to
// `contentMediaType: application/octet-stream` (OAS 3.0 → 3.1), the mock
// generator must still emit an ArrayBuffer — not `faker.string.alpha(...)` —
// even when the declared content type is JSON-like (e.g. application/json).
it('should generate ArrayBuffer mock for binary schema under JSON content types', () => {
const binaryJsonVerbOptions = {
...mockVerbOptions,
response: {
imports: [],
definition: { success: 'Blob' },
types: {
success: [
{
key: '200',
value: 'Blob',
contentType: 'application/json',
originalSchema: {
type: 'string',
contentMediaType: 'application/octet-stream',
},
imports: [],
schemas: [],
type: 'string',
isEnum: false,
isRef: false,
hasReadonlyProps: false,
},
{
key: '200',
value: 'Blob',
contentType: 'text/json',
originalSchema: {
type: 'string',
contentMediaType: 'application/octet-stream',
},
imports: [],
schemas: [],
type: 'string',
isEnum: false,
isRef: false,
hasReadonlyProps: false,
},
],
},
contentTypes: ['application/json', 'text/json'],
},
} as unknown as GeneratorVerbOptions;
const result = generateMSW(binaryJsonVerbOptions, baseOptions);
expect(result.implementation.function).toContain('ArrayBuffer');
expect(result.implementation.function).not.toContain(
'faker.string.alpha',
);
expect(result.implementation.function).not.toContain(': Blob =>');
expect(result.implementation.handler).toContain(
'HttpResponse.arrayBuffer',
);
});
it('should handle application/pdf as binary', () => {
const pdfVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Blob' },
types: {
success: [{ key: '200', value: 'Blob' }],
},
contentTypes: ['application/pdf'],
},
} as GeneratorVerbOptions;
const result = generateMSW(pdfVerbOptions, baseOptions);
expect(result.implementation.handler).not.toContain('JSON.stringify');
expect(result.implementation.handler).toContain('application/pdf');
expect(result.implementation.handler).toContain(
'HttpResponse.arrayBuffer',
);
});
it('should generate ArrayBuffer mock for $ref to a format: binary schema', () => {
const refBinaryVerbOptions = {
...mockVerbOptions,
response: {
imports: [],
definition: { success: 'TestPdfFile' },
types: {
success: [
{
key: '200',
value: 'TestPdfFile',
contentType: '*/*',
originalSchema: { type: 'string', format: 'binary' },
imports: [{ name: 'TestPdfFile' }],
schemas: [],
type: 'string',
isEnum: false,
isRef: true,
hasReadonlyProps: false,
},
],
},
contentTypes: ['*/*'],
},
} as unknown as GeneratorVerbOptions;
const result = generateMSW(refBinaryVerbOptions, baseOptions);
expect(result.implementation.handler).toContain(
'HttpResponse.arrayBuffer',
);
expect(result.implementation.handler).not.toContain('JSON.stringify');
// The ref alias return type must be rewritten to ArrayBuffer to stay consistent with the arrayBuffer body.
expect(result.implementation.function).toContain(': ArrayBuffer');
expect(result.implementation.function).not.toContain(': TestPdfFile');
});
it('should rewrite aliased ref imports for $ref binary schemas', () => {
const aliasedVerbOptions = {
...mockVerbOptions,
response: {
imports: [],
definition: { success: '__TestPdfFile' },
types: {
success: [
{
key: '200',
value: '__TestPdfFile',
contentType: '*/*',
originalSchema: { type: 'string', format: 'binary' },
imports: [{ name: 'TestPdfFile', alias: '__TestPdfFile' }],
schemas: [],
type: 'string',
isEnum: false,
isRef: true,
hasReadonlyProps: false,
},
],
},
contentTypes: ['*/*'],
},
} as unknown as GeneratorVerbOptions;
const result = generateMSW(aliasedVerbOptions, baseOptions);
expect(result.implementation.function).toContain(': ArrayBuffer');
expect(result.implementation.function).not.toContain(': __TestPdfFile');
});
it('should not force binary path when preferredContentType narrows to a non-binary success variant', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
imports: [],
definition: { success: 'Pet | Blob' },
types: {
success: [
{
key: '200',
value: 'Pet',
contentType: 'application/json',
originalSchema: {
type: 'object',
properties: { name: { type: 'string' } },
},
imports: [],
schemas: [],
type: 'object',
isEnum: false,
isRef: true,
hasReadonlyProps: false,
},
{
key: '200',
value: 'Blob',
contentType: 'application/octet-stream',
originalSchema: { type: 'string', format: 'binary' },
imports: [],
schemas: [],
type: 'string',
isEnum: false,
isRef: false,
hasReadonlyProps: false,
},
],
},
contentTypes: ['application/json', 'application/octet-stream'],
},
} as unknown as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
mock: { preferredContentType: 'application/json' },
} as unknown as GeneratorOptions);
expect(result.implementation.handler).not.toContain(
'HttpResponse.arrayBuffer',
);
expect(result.implementation.handler).toContain('HttpResponse.json');
});
it('should type the info parameter in the handler callback', () => {
const result = generate({
mock: { type: OutputMockType.MSW, delay: 100 },
});
// The handler callback's info param should be explicitly typed
expect(result.implementation.handler).toContain(
'async (info: Parameters<Parameters<typeof http.get>[1]>[0])',
);
});
it('should use HttpResponse.text for text/plain responses', () => {
const textVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['text/plain'],
},
} as GeneratorVerbOptions;
const result = generateMSW(textVerbOptions, baseOptions);
expect(result.implementation.handler).toContain('HttpResponse.text');
expect(result.implementation.handler).toContain('textBody');
expect(result.implementation.handler).toContain('JSON.stringify');
expect(result.implementation.handler).not.toContain('HttpResponse.json');
});
it('should use HttpResponse.html for text/html responses', () => {
const htmlVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['text/html'],
},
} as GeneratorVerbOptions;
const result = generateMSW(htmlVerbOptions, baseOptions);
expect(result.implementation.handler).toContain('HttpResponse.html');
expect(result.implementation.handler).not.toContain('HttpResponse.json');
expect(result.implementation.handler).not.toContain('HttpResponse.text(');
});
it('should use HttpResponse.xml for application/xml responses', () => {
const xmlVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['application/xml'],
},
} as GeneratorVerbOptions;
const result = generateMSW(xmlVerbOptions, baseOptions);
expect(result.implementation.handler).toContain('HttpResponse.xml');
expect(result.implementation.handler).not.toContain('HttpResponse.json');
expect(result.implementation.handler).not.toContain('HttpResponse.text(');
});
// Regression test for https://github.com/orval-labs/orval/issues/3270.
// When the response definition resolves to `unknown` (as with a 302
// redirect returning text/html that is not treated as a success body),
// no `*ResponseMock` helper is generated. The handler must not emit a
// `resolvedBody`/`textBody` prelude that references the missing helper.
it('should not reference a missing *ResponseMock for unknown text responses', () => {
const unknownHtmlVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'unknown' },
types: { success: [{ key: '302', value: 'unknown' }] },
contentTypes: ['text/html'],
},
} as GeneratorVerbOptions;
const result = generateMSW(unknownHtmlVerbOptions, baseOptions);
expect(result.implementation.function).toBe('');
expect(result.implementation.handler).not.toContain('ResponseMock()');
expect(result.implementation.handler).not.toContain('resolvedBody');
expect(result.implementation.handler).not.toContain('textBody');
expect(result.implementation.handler).toContain('new HttpResponse(null,');
});
it('should use HttpResponse.xml for vendor +xml responses', () => {
const vendorXmlVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['application/vnd.api+xml'],
},
} as GeneratorVerbOptions;
const result = generateMSW(vendorXmlVerbOptions, baseOptions);
expect(result.implementation.handler).toContain('HttpResponse.xml');
expect(result.implementation.handler).not.toContain('HttpResponse.json');
expect(result.implementation.handler).not.toContain('HttpResponse.text(');
});
it('should avoid undefined array min/max in general JS types', () => {
const numberArrayVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'number[]' },
types: { success: [{ key: '200', value: 'number[]' }] },
contentTypes: ['application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(numberArrayVerbOptions, baseOptions);
expect(result.implementation.function).toContain(
'Array.from({length: faker.number.int()}',
);
expect(result.implementation.function).not.toContain('undefined');
});
it('should include array min/max when provided for general JS types', () => {
const numberArrayVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'number[]' },
types: { success: [{ key: '200', value: 'number[]' }] },
contentTypes: ['application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(numberArrayVerbOptions, {
...baseOptions,
override: {
...baseOptions.override,
mock: { arrayMin: 1, arrayMax: 3 },
},
});
expect(result.implementation.function).toContain(
'faker.number.int({min: 1, max: 3})',
);
});
it('should evaluate text response override expression once via temp variable', () => {
const textVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['text/plain'],
},
} as GeneratorVerbOptions;
const result = generateMSW(textVerbOptions, baseOptions);
// The handler should assign the resolved response to a temp var
// and then use it, rather than inlining the await expression multiple times
expect(result.implementation.handler).toContain('const resolvedBody =');
expect(result.implementation.handler).toContain(
"typeof resolvedBody === 'string'",
);
});
it('should handle binary responses with ArrayBuffer fallback', () => {
const blobVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Blob' },
types: { success: [{ key: '200', value: 'Blob' }] },
contentTypes: ['application/octet-stream'],
},
} as GeneratorVerbOptions;
const result = generateMSW(blobVerbOptions, baseOptions);
// Should use the mock function and fall back to empty ArrayBuffer
expect(result.implementation.handler).toContain(
'binaryBody instanceof ArrayBuffer',
);
expect(result.implementation.handler).toContain('new ArrayBuffer(0)');
// The binary handler should call the mock function as fallback
expect(result.implementation.handler).toContain(
'getGetUserResponseMock()',
);
});
it('should honor preferredContentType for binary content headers', () => {
const blobVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Blob' },
types: {
success: [
{
key: '200',
value: 'Blob',
contentType: 'application/octet-stream',
},
{ key: '200', value: 'Blob', contentType: 'image/png' },
],
},
contentTypes: ['application/octet-stream', 'image/png'],
},
} as GeneratorVerbOptions;
const result = generateMSW(blobVerbOptions, {
...baseOptions,
mock: {
type: OutputMockType.MSW,
preferredContentType: 'image/png',
},
});
expect(result.implementation.handler).toContain(
"headers: { 'Content-Type': 'image/png' }",
);
});
it('should use HttpResponse.text when text/plain comes before application/xml in mixed content types', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['text/plain', 'application/xml', 'application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
override: {
...baseOptions.override,
operations: {
...baseOptions.override.operations,
getUser: {
mock: {
data: { id: 1, name: 'Milo' },
},
},
},
},
});
// text/plain is the first text-like content type, so HttpResponse.text should be used
expect(result.implementation.handler).toContain('HttpResponse.text(');
expect(result.implementation.handler).not.toContain('HttpResponse.xml(');
expect(result.implementation.handler).not.toContain('HttpResponse.json(');
});
it('should use HttpResponse.xml when application/xml comes first in mixed content types', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['application/xml', 'application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
override: {
...baseOptions.override,
operations: {
...baseOptions.override.operations,
getUser: {
mock: {
data: { id: 1, name: 'Milo' },
},
},
},
},
});
// application/xml is the first text-like content type
expect(result.implementation.handler).toContain('HttpResponse.xml(');
expect(result.implementation.handler).not.toContain('HttpResponse.text(');
expect(result.implementation.handler).not.toContain('HttpResponse.json(');
});
it('should keep text helper for exact string return types even when preferredContentType is application/json', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: {
success: [
{ key: '200', value: 'string', contentType: 'application/xml' },
{ key: '200', value: 'string', contentType: 'application/json' },
],
},
contentTypes: ['application/xml', 'application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
mock: {
type: OutputMockType.MSW,
preferredContentType: 'application/json',
},
});
expect(result.implementation.handler).toContain('HttpResponse.xml(');
expect(result.implementation.handler).not.toContain('HttpResponse.json(');
expect(result.implementation.handler).not.toContain('HttpResponse.text(');
});
it('should prefer HttpResponse.json for structured return types when json and xml are both available', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Pet' },
types: { success: [{ key: '200', value: 'Pet' }] },
contentTypes: ['application/xml', 'application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
override: {
...baseOptions.override,
operations: {
...baseOptions.override.operations,
getUser: {
mock: {
data: { id: 1, name: 'Milo' },
},
},
},
},
});
expect(result.implementation.handler).toContain('HttpResponse.json(');
expect(result.implementation.handler).not.toContain('HttpResponse.xml(');
expect(result.implementation.handler).not.toContain('JSON.stringify');
});
it('should honor preferredContentType for text helpers when multiple text types exist', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: {
success: [
{ key: '200', value: 'string', contentType: 'text/plain' },
{ key: '200', value: 'string', contentType: 'text/html' },
],
},
contentTypes: ['text/plain', 'text/html'],
},
} as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
mock: {
type: OutputMockType.MSW,
preferredContentType: 'text/html',
},
});
expect(result.implementation.handler).toContain('HttpResponse.html(');
expect(result.implementation.handler).not.toContain('HttpResponse.text(');
});
it('should use HttpResponse.json when application/json is the only content type (no text-like)', () => {
const result = generate();
// Default mockVerbOptions has application/json only
expect(result.implementation.handler).not.toContain('HttpResponse.text(');
expect(result.implementation.handler).not.toContain('HttpResponse.xml(');
expect(result.implementation.handler).not.toContain('HttpResponse.html(');
});
it('should use text prelude (resolvedBody → textBody) for xml and html responses', () => {
const xmlVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['application/xml'],
},
} as GeneratorVerbOptions;
const result = generateMSW(xmlVerbOptions, baseOptions);
// XML responses should still use the text prelude for string conversion
expect(result.implementation.handler).toContain('const resolvedBody =');
expect(result.implementation.handler).toContain('textBody');
expect(result.implementation.handler).toContain(
'HttpResponse.xml(textBody',
);
});
it('should accept RequestHandlerOptions in all handler types', () => {
// JSON handler
const jsonResult = generate();
expect(jsonResult.implementation.handler).toContain(
'options?: RequestHandlerOptions',
);
expect(jsonResult.implementation.handler).toContain('}, options)');
// Text handler
const textVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string' },
types: { success: [{ key: '200', value: 'string' }] },
contentTypes: ['text/plain'],
},
} as GeneratorVerbOptions;
const textResult = generateMSW(textVerbOptions, baseOptions);
expect(textResult.implementation.handler).toContain(
'options?: RequestHandlerOptions',
);
expect(textResult.implementation.handler).toContain('}, options)');
// Binary handler
const blobVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Blob' },
types: { success: [{ key: '200', value: 'Blob' }] },
contentTypes: ['application/octet-stream'],
},
} as GeneratorVerbOptions;
const blobResult = generateMSW(blobVerbOptions, baseOptions);
expect(blobResult.implementation.handler).toContain(
'options?: RequestHandlerOptions',
);
expect(blobResult.implementation.handler).toContain('}, options)');
});
});
describe('mixed content-type runtime branching (issue #2950)', () => {
it('Test A: should generate runtime branching for text/plain + xml + json with union return type string | Pet', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string | Pet' },
types: { success: [{ key: '200', value: 'string | Pet' }] },
contentTypes: ['text/plain', 'application/xml', 'application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
override: {
...baseOptions.override,
operations: {
...baseOptions.override.operations,
getUser: { mock: { data: { id: 1, name: 'Milo' } } },
},
},
});
// Should generate runtime typeof check to branch on resolved value
expect(result.implementation.handler).toContain(
"typeof resolvedBody === 'string'",
);
// String values should use HttpResponse.text (first text-like content type)
expect(result.implementation.handler).toContain('HttpResponse.text(');
// Object values should use HttpResponse.json
expect(result.implementation.handler).toContain('HttpResponse.json(');
// Should NOT use JSON.stringify — objects go to .json(), strings go to .text()
expect(result.implementation.handler).not.toContain('JSON.stringify');
// Should resolve the body once into a temp variable
expect(result.implementation.handler).toContain('const resolvedBody =');
// Should NOT have the textBody prelude (that's only for static text branch)
expect(result.implementation.handler).not.toContain('const textBody =');
});
it('Test B: should generate runtime branching for xml + json with union return type string | Pet', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'string | Pet' },
types: { success: [{ key: '200', value: 'string | Pet' }] },
contentTypes: ['application/xml', 'application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
override: {
...baseOptions.override,
operations: {
...baseOptions.override.operations,
getUser: { mock: { data: { id: 1, name: 'Milo' } } },
},
},
});
// Should generate runtime typeof check
expect(result.implementation.handler).toContain(
"typeof resolvedBody === 'string'",
);
// String values should use HttpResponse.xml (first text-like = application/xml)
expect(result.implementation.handler).toContain('HttpResponse.xml(');
// Object values should use HttpResponse.json
expect(result.implementation.handler).toContain('HttpResponse.json(');
// Should NOT use JSON.stringify
expect(result.implementation.handler).not.toContain('JSON.stringify');
});
it('Test C: should NOT use runtime branching for xml + json with non-union return type Pet', () => {
const mixedVerbOptions = {
...mockVerbOptions,
response: {
...mockVerbOptions.response,
definition: { success: 'Pet' },
types: { success: [{ key: '200', value: 'Pet' }] },
contentTypes: ['application/xml', 'application/json'],
},
} as GeneratorVerbOptions;
const result = generateMSW(mixedVerbOptions, {
...baseOptions,
override: {
...baseOptions.override,
operations: {
...baseOptions.override.operations,
getUser: { mock: { data: { id: 1, name: 'Milo' } } },
},
},
});
// shouldPreferJsonResponse is true (non-string return type + json available)
// so it should use HttpResponse.json directly — no runtime branching needed
expect(result.implementation.handler).toContain('HttpResponse.json(');
expect(result.implementation.handler).not.toContain('HttpResponse.xml(');
expect(result.implementation.handler).not.toContain('HttpResponse.text(');
expect(result.implementation.handler).not.toContain('JSON.stringify');
// No runtime typeof check
expect(result.implementation.handler).not.toContain(
"typeof resolvedBody === 'string'",