@@ -107,6 +107,53 @@ describe("createNodeMiddleware(webhooks)", () => {
107107 server . close ( ) ;
108108 } ) ;
109109
110+ test ( "request.body is already an Object and has request.rawBody as Buffer (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 'rawBody' does not exist on type 'IncomingMessage'.
123+ req . rawBody = Buffer . concat ( dataChunks ) ;
124+ // @ts -expect-error - TS2339: Property 'body' does not exist on type 'IncomingMessage'.
125+ req . body = JSON . parse ( req . rawBody ) ;
126+ middleware ( req , res ) ;
127+ } ) ;
128+ } ) . listen ( ) ;
129+
130+ webhooks . on ( "push" , ( event ) => {
131+ expect ( event . id ) . toBe ( "123e4567-e89b-12d3-a456-426655440000" ) ;
132+ } ) ;
133+
134+ // @ts -expect-error complains about { port } although it's included in returned AddressInfo interface
135+ const { port } = server . address ( ) ;
136+
137+ const response = await fetch (
138+ `http://localhost:${ port } /api/github/webhooks` ,
139+ {
140+ method : "POST" ,
141+ headers : {
142+ "Content-Type" : "application/json" ,
143+ "X-GitHub-Delivery" : "123e4567-e89b-12d3-a456-426655440000" ,
144+ "X-GitHub-Event" : "push" ,
145+ "X-Hub-Signature-256" : signatureSha256 ,
146+ } ,
147+ body : pushEventPayload ,
148+ } ,
149+ ) ;
150+
151+ expect ( response . status ) . toEqual ( 200 ) ;
152+ expect ( await response . text ( ) ) . toEqual ( "ok\n" ) ;
153+
154+ server . close ( ) ;
155+ } ) ;
156+
110157 test ( "Handles invalid Content-Type" , async ( ) => {
111158 const webhooks = new Webhooks ( {
112159 secret : "mySecret" ,
0 commit comments