11import * as fs from 'node:fs' ;
22import { Buffer } from 'node:buffer' ;
33
4- import type { RequestBody } from '../../../types' ;
4+ import type { RequestBody , TestContext } from '../../../types' ;
55
66import { parseRequestBody , stripFileDecorator } from '../../config-parser' ;
77
88jest . mock ( 'node:fs' ) ;
99
1010describe ( 'parseRequestBody' , ( ) => {
11+ const ctx = {
12+ options : {
13+ workflowPath : 'test.yaml' ,
14+ } ,
15+ } as unknown as TestContext ;
16+
1117 it ( 'should return empty object if no body' , async ( ) => {
12- expect ( await parseRequestBody ( undefined ) ) . toEqual ( { } ) ;
18+ expect ( await parseRequestBody ( undefined , ctx ) ) . toEqual ( { } ) ;
1319 } ) ;
1420
1521 it ( 'should return body with no ctx provided' , async ( ) => {
1622 expect (
17- await parseRequestBody ( {
18- payload : {
19- test : 'test' ,
23+ await parseRequestBody (
24+ {
25+ payload : {
26+ test : 'test' ,
27+ } ,
2028 } ,
21- } )
29+ ctx
30+ )
2231 ) . toEqual ( {
2332 payload : { test : 'test' } ,
2433 contentType : undefined ,
@@ -28,16 +37,19 @@ describe('parseRequestBody', () => {
2837
2938 it ( 'should return body with ctx provided' , async ( ) => {
3039 expect (
31- await parseRequestBody ( {
32- payload : 'clientId={$input.clientID}&grant_type=12' ,
33- contentType : 'application/x-www-form-urlencoded' ,
34- replacements : [
35- {
36- target : '/clientId' ,
37- value : '123' ,
38- } ,
39- ] ,
40- } )
40+ await parseRequestBody (
41+ {
42+ payload : 'clientId={$input.clientID}&grant_type=12' ,
43+ contentType : 'application/x-www-form-urlencoded' ,
44+ replacements : [
45+ {
46+ target : '/clientId' ,
47+ value : '123' ,
48+ } ,
49+ ] ,
50+ } ,
51+ ctx
52+ )
4153 ) . toEqual ( {
4254 payload : {
4355 clientId : '123' ,
@@ -50,13 +62,16 @@ describe('parseRequestBody', () => {
5062
5163 it ( 'should return body with string replacement applied' , async ( ) => {
5264 expect (
53- await parseRequestBody ( {
54- payload : {
55- test : 'test' ,
65+ await parseRequestBody (
66+ {
67+ payload : {
68+ test : 'test' ,
69+ } ,
70+ contentType : 'application/json' ,
71+ encoding : 'utf-8' ,
5672 } ,
57- contentType : 'application/json' ,
58- encoding : 'utf-8' ,
59- } )
73+ ctx
74+ )
6075 ) . toEqual ( {
6176 payload : { test : 'test' } ,
6277 contentType : 'application/json' ,
@@ -66,13 +81,16 @@ describe('parseRequestBody', () => {
6681
6782 it ( 'should handle multipart/form-data' , async ( ) => {
6883 expect (
69- await parseRequestBody ( {
70- payload : {
71- test : 'test' ,
84+ await parseRequestBody (
85+ {
86+ payload : {
87+ test : 'test' ,
88+ } ,
89+ contentType : 'multipart/form-data' ,
90+ encoding : 'utf-8' ,
7291 } ,
73- contentType : 'multipart/form-data' ,
74- encoding : 'utf-8' ,
75- } )
92+ ctx
93+ )
7694 ) . toEqual ( {
7795 payload : expect . any ( Object ) ,
7896 contentType : expect . stringMatching (
@@ -89,14 +107,17 @@ describe('parseRequestBody', () => {
89107 callback ( ) ;
90108 } ) ;
91109 expect (
92- await parseRequestBody ( {
93- payload : {
94- test : 'test' ,
95- file : "$file('file1.txt')" ,
110+ await parseRequestBody (
111+ {
112+ payload : {
113+ test : 'test' ,
114+ file : "$file('file1.txt')" ,
115+ } ,
116+ contentType : 'multipart/form-data' ,
117+ encoding : 'utf-8' ,
96118 } ,
97- contentType : 'multipart/form-data' ,
98- encoding : 'utf-8' ,
99- } )
119+ ctx
120+ )
100121 ) . toEqual ( {
101122 payload : expect . any ( Object ) ,
102123 contentType : expect . stringMatching (
@@ -109,16 +130,19 @@ describe('parseRequestBody', () => {
109130 } ) ;
110131
111132 it ( 'should handle multipart/form-data with nested object' , async ( ) => {
112- const { payload, contentType, encoding } = await parseRequestBody ( {
113- payload : {
114- commit : {
115- message : 'test' ,
116- author : 'John Doe' ,
133+ const { payload, contentType, encoding } = await parseRequestBody (
134+ {
135+ payload : {
136+ commit : {
137+ message : 'test' ,
138+ author : 'John Doe' ,
139+ } ,
117140 } ,
141+ contentType : 'multipart/form-data' ,
142+ encoding : 'utf-8' ,
118143 } ,
119- contentType : 'multipart/form-data' ,
120- encoding : 'utf-8' ,
121- } ) ;
144+ ctx
145+ ) ;
122146 expect ( contentType ) . toMatch ( 'multipart/form-data; boundary=--------------------------' ) ;
123147 expect ( encoding ) . toBe ( 'utf-8' ) ;
124148 expect ( typeof payload ) . toBe ( 'object' ) ;
@@ -131,14 +155,17 @@ describe('parseRequestBody', () => {
131155
132156 it ( 'should handle multipart/form-data with array' , async ( ) => {
133157 expect (
134- await parseRequestBody ( {
135- payload : {
136- test : 'test' ,
137- array : [ 'test' , 'test2' ] ,
158+ await parseRequestBody (
159+ {
160+ payload : {
161+ test : 'test' ,
162+ array : [ 'test' , 'test2' ] ,
163+ } ,
164+ contentType : 'multipart/form-data' ,
165+ encoding : 'utf-8' ,
138166 } ,
139- contentType : 'multipart/form-data' ,
140- encoding : 'utf-8' ,
141- } )
167+ ctx
168+ )
142169 ) . toEqual ( {
143170 payload : expect . any ( Object ) ,
144171 contentType : expect . stringMatching (
@@ -155,14 +182,17 @@ describe('parseRequestBody', () => {
155182 callback ( ) ;
156183 } ) ;
157184 expect (
158- await parseRequestBody ( {
159- payload : {
160- test : 'test' ,
161- array : [ 'test' , "$file('file2.txt')" ] ,
185+ await parseRequestBody (
186+ {
187+ payload : {
188+ test : 'test' ,
189+ array : [ 'test' , "$file('file2.txt')" ] ,
190+ } ,
191+ contentType : 'multipart/form-data' ,
192+ encoding : 'utf-8' ,
162193 } ,
163- contentType : 'multipart/form-data' ,
164- encoding : 'utf-8' ,
165- } )
194+ ctx
195+ )
166196 ) . toEqual ( {
167197 payload : expect . any ( Object ) ,
168198 contentType : expect . stringMatching (
@@ -180,14 +210,17 @@ describe('parseRequestBody', () => {
180210 callback ( new Error ( 'error' ) ) ;
181211 } ) ;
182212 try {
183- await parseRequestBody ( {
184- payload : {
185- test : 'test' ,
186- array : [ 'test' , "$file('file2.txt')" ] ,
213+ await parseRequestBody (
214+ {
215+ payload : {
216+ test : 'test' ,
217+ array : [ 'test' , "$file('file2.txt')" ] ,
218+ } ,
219+ contentType : 'multipart/form-data' ,
220+ encoding : 'utf-8' ,
187221 } ,
188- contentType : 'multipart/form-data' ,
189- encoding : 'utf-8' ,
190- } ) ;
222+ ctx
223+ ) ;
191224 } catch ( error ) {
192225 expect ( error . message ) . toContain ( "file2.txt doesn't exist or isn't readable." ) ;
193226 }
@@ -200,11 +233,14 @@ describe('parseRequestBody', () => {
200233 callback ( ) ;
201234 } ) ;
202235 expect (
203- await parseRequestBody ( {
204- payload : "$file('file3.txt')" ,
205- contentType : 'application/octet-stream' ,
206- encoding : 'utf-8' ,
207- } )
236+ await parseRequestBody (
237+ {
238+ payload : "$file('file3.txt')" ,
239+ contentType : 'application/octet-stream' ,
240+ encoding : 'utf-8' ,
241+ } ,
242+ ctx
243+ )
208244 ) . toEqual ( {
209245 payload : 'readStream' ,
210246 contentType : 'application/octet-stream' ,
@@ -215,11 +251,14 @@ describe('parseRequestBody', () => {
215251
216252 it ( 'should handle application/octet-stream with string payload' , async ( ) => {
217253 expect (
218- await parseRequestBody ( {
219- payload : new Buffer ( 'test' ) as unknown as RequestBody [ 'payload' ] ,
220- contentType : 'application/octet-stream' ,
221- encoding : 'utf-8' ,
222- } )
254+ await parseRequestBody (
255+ {
256+ payload : new Buffer ( 'test' ) as unknown as RequestBody [ 'payload' ] ,
257+ contentType : 'application/octet-stream' ,
258+ encoding : 'utf-8' ,
259+ } ,
260+ ctx
261+ )
223262 ) . toEqual ( {
224263 payload : new Buffer ( 'test' ) ,
225264 contentType : 'application/octet-stream' ,
@@ -234,11 +273,14 @@ describe('parseRequestBody', () => {
234273 callback ( new Error ( 'error' ) ) ;
235274 } ) ;
236275 await expect (
237- parseRequestBody ( {
238- payload : "$file('file3.txt')" ,
239- contentType : 'application/octet-stream' ,
240- encoding : 'utf-8' ,
241- } )
276+ parseRequestBody (
277+ {
278+ payload : "$file('file3.txt')" ,
279+ contentType : 'application/octet-stream' ,
280+ encoding : 'utf-8' ,
281+ } ,
282+ ctx
283+ )
242284 ) . rejects . toThrow ( "file3.txt doesn't exist or isn't readable." ) ;
243285 } ) ;
244286} ) ;
0 commit comments