@@ -47,6 +47,23 @@ class NoOpNativeHttp {
4747  } 
4848} 
4949
50+ class  MockNativeClient  { 
51+   constructor ( response )  { 
52+     this . response  =  response ; 
53+     this . called  =  false ; 
54+     this . shouldThrowError  =  false ; 
55+   } 
56+ 
57+   next ( )  { 
58+     this . called  =  true ; 
59+     if  ( this . shouldThrowError )  { 
60+       return  Promise . reject ( new  Error ( 'Failed to get next invocation' ) ) ; 
61+     }  else  { 
62+       return  Promise . resolve ( this . response ) ; 
63+     } 
64+   } 
65+ } 
66+ 
5067class  EvilError  extends  Error  { 
5168  get  name ( )  { 
5269    throw  'gotcha' ; 
@@ -115,3 +132,88 @@ describe('invalid request id works', () => {
115132    } ) ; 
116133  } ) ; 
117134} ) ; 
135+ 
136+ describe ( 'next invocation with native client works' ,  ( )  =>  { 
137+   it ( 'should call the native client next() method' ,  async  ( )  =>  { 
138+     const  mockNative  =  new  MockNativeClient ( { 
139+       bodyJson : '' , 
140+       headers : { 
141+         'lambda-runtime-aws-request-id' : 'test-request-id' , 
142+       } , 
143+     } ) ; 
144+     const  client  =  new  RAPIDClient ( 'notUsed:1337' ,  undefined ,  mockNative ) ; 
145+     client . useAlternativeClient  =  false ; 
146+ 
147+     await  client . nextInvocation ( ) ; 
148+     // verify native client was called 
149+     mockNative . called . should . be . true ( ) ; 
150+   } ) ; 
151+   it ( 'should parse all required headers' ,  async  ( )  =>  { 
152+     const  mockResponse  =  { 
153+       bodyJson : '{"message":"Hello from Lambda!"}' , 
154+       headers : { 
155+         'lambda-runtime-aws-request-id' : 'test-request-id' , 
156+         'lambda-runtime-deadline-ms' : 1619712000000 , 
157+         'lambda-runtime-trace-id' : 'test-trace-id' , 
158+         'lambda-runtime-invoked-function-arn' : 'test-function-arn' , 
159+         'lambda-runtime-client-context' : '{"client":{"app_title":"MyApp"}}' , 
160+         'lambda-runtime-cognito-identity' :
161+           '{"identityId":"id123","identityPoolId":"pool123"}' , 
162+         'lambda-runtime-aws-tenant-id' : 'test-tenant-id' , 
163+       } , 
164+     } ; 
165+ 
166+     const  mockNative  =  new  MockNativeClient ( mockResponse ) ; 
167+     const  client  =  new  RAPIDClient ( 'notUsed:1337' ,  undefined ,  mockNative ) ; 
168+ 
169+     client . useAlternativeClient  =  false ; 
170+     const  response  =  await  client . nextInvocation ( ) ; 
171+ 
172+     // Verify all headers are present 
173+     response . headers . should . have . property ( 
174+       'lambda-runtime-aws-request-id' , 
175+       'test-request-id' , 
176+     ) ; 
177+     response . headers . should . have . property ( 
178+       'lambda-runtime-deadline-ms' , 
179+       1619712000000 , 
180+     ) ; 
181+     response . headers . should . have . property ( 
182+       'lambda-runtime-trace-id' , 
183+       'test-trace-id' , 
184+     ) ; 
185+     response . headers . should . have . property ( 
186+       'lambda-runtime-invoked-function-arn' , 
187+       'test-function-arn' , 
188+     ) ; 
189+     response . headers . should . have . property ( 
190+       'lambda-runtime-client-context' , 
191+       '{"client":{"app_title":"MyApp"}}' , 
192+     ) ; 
193+     response . headers . should . have . property ( 
194+       'lambda-runtime-cognito-identity' , 
195+       '{"identityId":"id123","identityPoolId":"pool123"}' , 
196+     ) ; 
197+     response . headers . should . have . property ( 
198+       'lambda-runtime-aws-tenant-id' , 
199+       'test-tenant-id' , 
200+     ) ; 
201+     // Verify body is correctly passed through 
202+     response . bodyJson . should . equal ( '{"message":"Hello from Lambda!"}' ) ; 
203+   } ) ; 
204+   it ( 'should handle native client errors' ,  async  ( )  =>  { 
205+     const  nativeClient  =  new  MockNativeClient ( { } ) ; 
206+     nativeClient . shouldThrowError  =  true ; 
207+ 
208+     const  client  =  new  RAPIDClient ( 'localhost:8080' ,  null ,  nativeClient ) ; 
209+     client . useAlternativeClient  =  false ; 
210+ 
211+     try  { 
212+       await  client . nextInvocation ( ) ; 
213+       // Should not reach here 
214+       false . should . be . true ( 'Expected an error but none was thrown' ) ; 
215+     }  catch  ( error )  { 
216+       error . message . should . equal ( 'Failed to get next invocation' ) ; 
217+     } 
218+   } ) ; 
219+ } ) ; 
0 commit comments