Skip to content

Maintenance: Simplify Stream Testing in Event Handler HTTP APIs #4752

@svozza

Description

@svozza

Summary

Currently, to test the stream functionality we create we need to implement a mock streaming class in our tests. However, this helper mock file will not be available to users who wish to test their streaming functions so they will have to implement this mock themselves. We should create a ResponseStream class that is used by the fallback version of streamifyHandler that can be used in our tests in utils module. Customers will also be able to use this class in their own tests too.

Why is this needed?

This simplifies testing as we are using the same implementation in the fallabck and the tests.

Which area does this relate to?

Event Handler

Solution

Unfortunately, the types in aws-lambda package are not correct so we will have to do some workarounds to make everything type check. But ultimately we should be able to do this:

import {ResponseStream} from "../../../../src/rest/utils.js";

it('streams a simple JSON response', async () => {
    // Prepare
    const app = new Router();
    app.get('/test', async () => ({ message: 'Hello, World!' }));

    const handler = streamify(app);
    const responseStream = new ResponseStream();

    // Act
    const result = await handler(
      createEvent('/test', 'GET'),
      responseStream,
      context
    );

    // Assess
    expect(result.statusCode).toBe(200);
    expect(JSON.parse(result.body)).toEqual({ message: 'Hello, World!' });
  });

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedevent-handlerThis item relates to the Event Handler UtilityinternalPRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.)

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions