UNPKG

@mikeycoxon/aws-lambda-event-http-router

Version:

AWS Lambda handler utility for HTTP REST request routing based on details in the APIGatewayProxyEvent

187 lines 8 kB
import { test, expect } from 'vitest'; import { TEST_DELETE_EVENT_WITH_ID, TEST_GET_EVENT, TEST_PATCH_EVENT, TEST_POST_EVENT_WITH_ID, TEST_PUT_EVENT_WITH_ID, } from '../__fixtures__/events.js'; import { DELETE, EventMap, GET, isDelete, isGet, isPatch, isPathFile, isPathUuid, isPost, isPut, NotFoundError, OK, PATCH, POST, PUT, resource, ROUTE_KEY, withEvent, } from '../index.js'; test('and gets the goods', () => { const event = TEST_GET_EVENT; event.pathParameters = { proxy: 'test/6c468016-00fa-4e23-9ff4-d7c99a7affc3' }; const req = withEvent(event); console.log(req); expect(req).toBeTruthy(); expect(req.keys()).toContain(GET); expect(req.keys()).toContain(POST); expect(req.keys()).toContain(PATCH); expect(req.keys()).toContain(PUT); expect(req.keys()).toContain(DELETE); expect(req.keys()).toContain('isPathUuid'); expect(req.keys()).toContain('isPathFile'); console.log(req.entries()); expect(req.entries().filter((e) => e[0] === GET && e[1] === 'GET')).toBeTruthy(); expect(req.entries().filter((e) => e[0] === POST && e[1] === undefined)).toBeTruthy(); expect(req.entries().filter((e) => e[0] === PATCH && e[1] === undefined)).toBeTruthy(); expect(req.entries().filter((e) => e[0] === PUT && e[1] === undefined)).toBeTruthy(); expect(req.entries().filter((e) => e[0] === DELETE && e[1] === undefined)).toBeTruthy(); expect(req.entries().filter((e) => e[0] === 'isPathUuid' && e[1] === '/6c468016-00fa-4e23-9ff4-d7c99a7affc3')).toBeTruthy(); expect(req.entries().filter((e) => e[0] === 'isPathFile' && e[1] === undefined)).toBeTruthy(); }); test('and gets the goods with a more complex path registered', () => { const event = TEST_GET_EVENT; event.pathParameters = { proxy: 'test/second/6c468016-00fa-4e23-9ff4-d7c99a7affc3' }; const req = withEvent(event).andResource(event, 'test/second'); expect(req.keys()).toContain('test/second'); const req2 = withEvent(event).andResource(event, '/test/second'); expect(req2.keys()).toContain('/test/second'); const req3 = withEvent(event).andResource(event, '/test/third'); expect(req3.keys()).not.toContain('/test/second'); }); test('and gets the goods with pathy bits', () => { const event = TEST_GET_EVENT; event.pathParameters = { proxy: 'test/6c468016-00fa-4e23-9ff4-d7c99a7affc3' }; const req = withEvent(event).andResource(event, 'test'); console.log(req); expect(req).toBeTruthy(); expect(req.keys()).toContain('test'); expect(req.keys()).toContain(GET); console.log(req.entries()); expect(req.entries().filter((e) => e[0] === 'test' && e[1] === 'test')).toBeTruthy(); // GET /api/test :::if the request was a GET for the /test endpoint if (req.get(GET) && req.get('test')) { let pass = true; expect(pass).toBeTruthy(); } }); test('and gets the goods primitively', () => { const event = TEST_GET_EVENT; event.pathParameters = { proxy: 'test/6c468016-00fa-4e23-9ff4-d7c99a7affc3' }; const response = [isGet(event), resource(event, 'test'), isPathUuid(event)]; expect(response).toBeTruthy(); expect(response).toEqual(['GET', 'test', '/6c468016-00fa-4e23-9ff4-d7c99a7affc3']); }); test('finds the path "6c468016-00fa-4e23-9ff4-d7c99a7affc3" resource in a get', () => { const event = TEST_GET_EVENT; event.pathParameters = { proxy: 'test/6c468016-00fa-4e23-9ff4-d7c99a7affc3' }; const response = isPathUuid(event); expect(response).toBeTruthy(); expect(response).toEqual('/6c468016-00fa-4e23-9ff4-d7c99a7affc3'); }); test('finds the path "978-1-76358-980-3.png" resource in a get', () => { const event = TEST_GET_EVENT; event.pathParameters = { proxy: 'test/978-1-76358-980-3.png' }; const response = isPathFile(event); expect(response).toBeTruthy(); expect(response).toEqual('/978-1-76358-980-3.png'); }); test('does not find the path "978-1-76358-980-3.png" resource in a get', () => { const event = TEST_GET_EVENT; event.pathParameters = { proxy: 'test/6c468016-00fa-4e23-9ff4-d7c99a7affc3' }; const response = isPathFile(event); expect(response).toBeFalsy(); }); test('api get', () => { const event = TEST_GET_EVENT; const response = isGet(event); expect(response).toBeTruthy(); expect(response).toEqual('GET'); }); test('finds "get" method in a get', () => { const event = TEST_GET_EVENT; const response = isGet(event); expect(response).toBeTruthy(); expect(response).toEqual('GET'); }); test('does not find "get" method in a post', () => { const event = TEST_POST_EVENT_WITH_ID; const response = isGet(event); expect(response).toBeFalsy(); }); test('finds "put" method in a put', () => { const event = TEST_PUT_EVENT_WITH_ID; const response = isPut(event); expect(response).toBeTruthy(); expect(response).toEqual('PUT'); }); test('does not find "put" method in a post', () => { const event = TEST_POST_EVENT_WITH_ID; const response = isPut(event); expect(response).toBeFalsy(); }); test('finds "patch" method in a patch', () => { const event = TEST_PATCH_EVENT; const response = isPatch(event); expect(response).toBeTruthy(); expect(response).toEqual('PATCH'); }); test('does not find "patch" method in a post', () => { const event = TEST_POST_EVENT_WITH_ID; const response = isPatch(event); expect(response).toBeFalsy(); }); test('finds "delete" method in a delete', () => { const event = TEST_DELETE_EVENT_WITH_ID; const response = isDelete(event); expect(response).toBeTruthy(); expect(response).toEqual('DELETE'); }); test('does not find "delete" method in a post', () => { const event = TEST_POST_EVENT_WITH_ID; const response = isDelete(event); expect(response).toBeFalsy(); }); test('finds "post" method in a post', () => { const event = TEST_POST_EVENT_WITH_ID; const response = isPost(event); expect(response).toBeTruthy(); expect(response).toEqual('POST'); }); test('does not find "post" method in a patch', () => { const event = TEST_PATCH_EVENT; const response = isPost(event); expect(response).toBeFalsy(); }); test('finds the "test" resource in a get', () => { const event = TEST_GET_EVENT; const response = resource(event, 'test'); expect(response).toBeTruthy(); expect(response).toEqual('test'); }); test('finds the "test/category" resource in a get', () => { const event = TEST_GET_EVENT; event.pathParameters = { proxy: 'test/category' }; const response = resource(event, 'test'); expect(response).toBeTruthy(); expect(response).toContain('test'); expect(response).not.toContain('test/category'); }); test('finds the "test" resource in a put', () => { const event = TEST_PUT_EVENT_WITH_ID; const response = resource(event, 'test'); expect(response).toBeTruthy(); expect(response).toEqual('test'); }); test('finds the "test" resource in a patch', () => { const event = TEST_PATCH_EVENT; const response = resource(event, 'test'); expect(response).toBeTruthy(); expect(response).toEqual('test'); }); test('finds the "test" resource in a post', () => { const event = TEST_POST_EVENT_WITH_ID; const response = resource(event, 'test'); expect(response).toBeTruthy(); expect(response).toEqual('test'); }); test('finds the "test" resource in a delete', () => { const event = TEST_DELETE_EVENT_WITH_ID; const response = resource(event, 'test'); expect(response).toBeTruthy(); expect(response).toEqual('test'); }); test('makes an ok, ok', () => { const res = new OK('all good'); console.log(res); expect(res.message).toBe('all good'); expect(res.statusCode).toBe(200); const event = TEST_DELETE_EVENT_WITH_ID; const req = withEvent(event).andResource(event, 'test'); new NotFoundError(`No handler for routeKey ${req.get(ROUTE_KEY)}.`).format(); }); //# sourceMappingURL=index.test.js.map