[RFC] Add `FunctionHttpCallSpyEvent`
With @MaximeVivier, we are working on a lib to intercept HTTP calls made by a lambda and mock them: https://github.com/MaximeVivier/lambda-http-interceptor. The aim is to be able to mock external services in integration tests.
Our next feature is to be able to assert an HTTP call has been made. But we are facing some issues you have already resolved and I like having a unified interface to assert something happened.
What do you think about intercepting HTTP calls made by lambdas and emitting FunctionHttpCallSpyEvent?
I'll be happy to implement it based on what we did on lambda-http-interceptor
:ok_hand: I will look into it.
Hi Corentin,
I think that is an amazing feature you suggested :star_struck: It would be really easy to add.
I will add it in a week or so. But if you have time, I would be really grateful for your contribution.
Here are some tips if you intend to do it:
You have to alter or add the following files:
- Add:
common/spyEvents/FunctionHttpSpyEvent.tsandcommon/spyEvents/FunctionHttp.tsAdd this type with a structure that describes http request (url, method, body....) and response (http code, body ...). There are similar type:FunctionConsole,FunctionConsoleSpyEvent. - Change:
extension/interceptor.tsAddmswlibrary in the same wayinterceptConsole()works. Send an event in the structure you defined :point_up: - Change:
listener/ServerlessSpyListener.ts,listener/SpyHandlers.ts.ts,listener/WsListener.tsJest listener uses this. You add the same code as is for the Console feature. - Add one line
src/ServerlessSpy.tsin after line 691this.serviceKeys.push('Function#${functionName}#Http');This generates the TypeScript files used by test.
That is all.
We also need a test:
You can add code to this test:
test/cdk/test/lambda.test.ts that use this Lambda function test/cdk/functions/lambda.ts where you have to add one HTTP request.
Note. Tests need to be run with:
npm run deploy
npr run test
in the ROOT folder!
You can also optimize the command just to deploy and run your test. You can also use the VsCode extension for Jest for that.
That would be an incredible feature!