serverless-spy icon indicating copy to clipboard operation
serverless-spy copied to clipboard

[RFC] Add `FunctionHttpCallSpyEvent`

Open CorentinDoue opened this issue 2 years ago • 3 comments

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

CorentinDoue avatar Sep 29 '23 16:09 CorentinDoue

:ok_hand: I will look into it.

ServerlessLife avatar Sep 29 '23 17:09 ServerlessLife

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.ts and common/spyEvents/FunctionHttp.ts Add 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.ts Add msw library in the same way interceptConsole() works. Send an event in the structure you defined :point_up:
  • Change: listener/ServerlessSpyListener.ts, listener/SpyHandlers.ts.ts, listener/WsListener.ts Jest listener uses this. You add the same code as is for the Console feature.
  • Add one line src/ServerlessSpy.ts in after line 691 this.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.

ServerlessLife avatar Oct 01 '23 07:10 ServerlessLife

That would be an incredible feature!

Lewenhaupt avatar Oct 06 '23 14:10 Lewenhaupt