@@ -107,6 +107,98 @@ describe("createNodeMiddleware(webhooks)", () => {
107107 server . close ( ) ;
108108 } ) ;
109109
110+ test ( "request.body is already an Object (e.g. GCF)" , async ( ) => {
111+ expect . assertions ( 3 ) ;
112+
113+ const webhooks = new Webhooks ( {
114+ secret : "mySecret" ,
115+ } ) ;
116+ const dataChunks : any [ ] = [ ] ;
117+ const middleware = createNodeMiddleware ( webhooks ) ;
118+
119+ const server = createServer ( ( req , res ) => {
120+ req . once ( "data" , ( chunk ) => dataChunks . push ( chunk ) ) ;
121+ req . once ( "end" , ( ) => {
122+ // @ts -expect-error - TS2339: Property 'body' does not exist on type 'IncomingMessage'.
123+ req . body = JSON . parse ( Buffer . concat ( dataChunks ) ) ;
124+ middleware ( req , res ) ;
125+ } ) ;
126+ } ) . listen ( ) ;
127+
128+ webhooks . on ( "push" , ( event ) => {
129+ expect ( event . id ) . toBe ( "123e4567-e89b-12d3-a456-426655440000" ) ;
130+ } ) ;
131+
132+ // @ts -expect-error complains about { port } although it's included in returned AddressInfo interface
133+ const { port } = server . address ( ) ;
134+
135+ const response = await fetch (
136+ `http://localhost:${ port } /api/github/webhooks` ,
137+ {
138+ method : "POST" ,
139+ headers : {
140+ "Content-Type" : "application/json" ,
141+ "X-GitHub-Delivery" : "123e4567-e89b-12d3-a456-426655440000" ,
142+ "X-GitHub-Event" : "push" ,
143+ "X-Hub-Signature-256" : signatureSha256 ,
144+ } ,
145+ body : pushEventPayload ,
146+ } ,
147+ ) ;
148+
149+ expect ( response . status ) . toEqual ( 200 ) ;
150+ expect ( await response . text ( ) ) . toEqual ( "ok\n" ) ;
151+
152+ server . close ( ) ;
153+ } ) ;
154+
155+ test ( "request.body is already an Object and has request.rawBody as Buffer (e.g. GCF)" , async ( ) => {
156+ expect . assertions ( 3 ) ;
157+
158+ const webhooks = new Webhooks ( {
159+ secret : "mySecret" ,
160+ } ) ;
161+ const dataChunks : any [ ] = [ ] ;
162+ const middleware = createNodeMiddleware ( webhooks ) ;
163+
164+ const server = createServer ( ( req , res ) => {
165+ req . once ( "data" , ( chunk ) => dataChunks . push ( chunk ) ) ;
166+ req . once ( "end" , ( ) => {
167+ // @ts -expect-error - TS2339: Property 'rawBody' does not exist on type 'IncomingMessage'.
168+ req . rawBody = Buffer . concat ( dataChunks ) ;
169+ // @ts -expect-error - TS2339: Property 'body' does not exist on type 'IncomingMessage'.
170+ req . body = JSON . parse ( req . rawBody ) ;
171+ middleware ( req , res ) ;
172+ } ) ;
173+ } ) . listen ( ) ;
174+
175+ webhooks . on ( "push" , ( event ) => {
176+ expect ( event . id ) . toBe ( "123e4567-e89b-12d3-a456-426655440000" ) ;
177+ } ) ;
178+
179+ // @ts -expect-error complains about { port } although it's included in returned AddressInfo interface
180+ const { port } = server . address ( ) ;
181+
182+ const response = await fetch (
183+ `http://localhost:${ port } /api/github/webhooks` ,
184+ {
185+ method : "POST" ,
186+ headers : {
187+ "Content-Type" : "application/json" ,
188+ "X-GitHub-Delivery" : "123e4567-e89b-12d3-a456-426655440000" ,
189+ "X-GitHub-Event" : "push" ,
190+ "X-Hub-Signature-256" : signatureSha256 ,
191+ } ,
192+ body : pushEventPayload ,
193+ } ,
194+ ) ;
195+
196+ expect ( response . status ) . toEqual ( 200 ) ;
197+ expect ( await response . text ( ) ) . toEqual ( "ok\n" ) ;
198+
199+ server . close ( ) ;
200+ } ) ;
201+
110202 test ( "Handles invalid Content-Type" , async ( ) => {
111203 const webhooks = new Webhooks ( {
112204 secret : "mySecret" ,
0 commit comments