This repository has been archived on 2026-04-30. You can view files and clone it, but cannot push or open issues or pull requests.
next-deploy/packages/aws-lambda-builder/__tests__/api-handler/api-handler.test.ts
2020-07-05 16:56:58 -07:00

58 lines
1.6 KiB
TypeScript

import { createCloudFrontEvent } from '../test-utils';
import { handler } from '../../src/api-handler';
import { CloudFrontResponseResult } from 'next-aws-cloudfront/node_modules/@types/aws-lambda';
jest.mock('../../src/manifest.json', () => require('./api-build-manifest.json'), {
virtual: true,
});
const mockPageRequire = (mockPagePath: string): void => {
jest.mock(
`../../src/${mockPagePath}`,
() => require(`../shared-fixtures/built-artifact/${mockPagePath}`),
{
virtual: true,
}
);
};
describe('API lambda handler', () => {
it('serves api request', async () => {
const event = createCloudFrontEvent({
uri: '/api/getCustomers',
host: 'mydistribution.cloudfront.net',
origin: {
s3: {
domainName: 'my-bucket.s3.amazonaws.com',
},
},
});
mockPageRequire('pages/api/getCustomers.js');
const response = (await handler(event)) as CloudFrontResponseResult;
const decodedBody = new Buffer(response.body, 'base64').toString('utf8');
expect(decodedBody).toEqual('pages/api/getCustomers');
expect(response.status).toEqual(200);
});
it('returns 404 for not-found api routes', async () => {
const event = createCloudFrontEvent({
uri: '/foo/bar',
host: 'mydistribution.cloudfront.net',
origin: {
s3: {
domainName: 'my-bucket.s3.amazonaws.com',
},
},
});
mockPageRequire('pages/api/getCustomers.js');
const response = (await handler(event)) as CloudFrontResponseResult;
expect(response.status).toEqual('404');
});
});