|
| 1 | +import { stringify } from 'querystring'; |
| 2 | +import createEvent from '@serverless/event-mocks'; |
| 3 | +import { Request, Body } from './api-parser'; |
| 4 | + |
| 5 | +describe('Body parsing', () => { |
| 6 | + it('Parses json body', () => { |
| 7 | + const json = { hello: 'world' }; |
| 8 | + const headers = { 'content-type': 'application/json' }; |
| 9 | + const body = new Body(JSON.stringify(json), headers).getParsedBody(); |
| 10 | + expect(body).toEqual(json); |
| 11 | + }); |
| 12 | + |
| 13 | + it('Parses form url encoded body', () => { |
| 14 | + const form = { hello: 'world' }; |
| 15 | + const headers = { 'content-type': 'application/x-www-form-urlencoded' }; |
| 16 | + const body = new Body(stringify(form), headers).getParsedBody(); |
| 17 | + expect(body).toEqual(form); |
| 18 | + }); |
| 19 | + |
| 20 | + it("Passes body through when content type isn't specified", () => { |
| 21 | + const json = { hello: 'world' }; |
| 22 | + const headers = {}; |
| 23 | + const body = new Body(JSON.stringify(json), headers).getParsedBody(); |
| 24 | + expect(body).toEqual(JSON.stringify(json)); |
| 25 | + }); |
| 26 | + |
| 27 | + it("Errors when encoding and content-type don't match", () => { |
| 28 | + const invalid = '2["test" : 123]4}{'; |
| 29 | + const headers = { 'content-type': 'application/json' }; |
| 30 | + const body = new Body(invalid, headers).getParsedBody(); |
| 31 | + expect(body).toEqual(invalid); |
| 32 | + }); |
| 33 | + |
| 34 | + it('Returns empty body when none given', () => { |
| 35 | + let empty; |
| 36 | + const headers = { 'content-type': 'application/x-www-form-urlencoded' }; |
| 37 | + const body = new Body(empty, headers).getParsedBody(); |
| 38 | + expect(body).toEqual(empty); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('Request parsing', () => { |
| 43 | + it('Gets all fields with optional parameters', () => { |
| 44 | + // @ts-ignore |
| 45 | + const event = createEvent('aws:apiGateway', { |
| 46 | + body: JSON.stringify({ hello: 'world' }), |
| 47 | + pathParameters: { proxy: 'not today' }, |
| 48 | + queryStringParameters: { name: 'a test' }, |
| 49 | + headers: { 'content-type': 'application/json', 'Test-Request': 'true' } |
| 50 | + }); |
| 51 | + const { body, path, query, auth, headers, testRequest } = new Request(event).getProperties(); |
| 52 | + |
| 53 | + expect(body).toEqual({ hello: 'world' }); |
| 54 | + expect(path).toEqual({ proxy: 'not today' }); |
| 55 | + expect(query).toEqual({ name: 'a test' }); |
| 56 | + expect(headers['content-type']).toEqual('application/json'); |
| 57 | + expect(testRequest).toEqual(true); |
| 58 | + expect(auth).toBeFalsy(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("Get's falsy fields when optional parameters not used", () => { |
| 62 | + // @ts-ignore |
| 63 | + const event = createEvent('aws:apiGateway', {}); |
| 64 | + delete event.body; |
| 65 | + delete event.headers; |
| 66 | + delete event.pathParameters; |
| 67 | + delete event.queryStringParameters; |
| 68 | + const { body, path, query, auth, headers, testRequest } = new Request(event).getProperties(); |
| 69 | + |
| 70 | + expect(body).toBeFalsy(); |
| 71 | + expect(path).toBeFalsy(); |
| 72 | + expect(query).toBeFalsy(); |
| 73 | + expect(auth).toBeFalsy(); |
| 74 | + expect(headers).toBeFalsy(); |
| 75 | + expect(testRequest).toBeFalsy(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments