UNPKG

1.69 kBPlain TextView Raw
1import http from 'http'
2import tap from 'tap'
3
4import { requestToEvent, normalizeHeaders, getQueryStringParameters } from '../lib/requestToEvent'
5
6function createRequest(props: Partial<http.IncomingMessage>): http.IncomingMessage {
7 // @ts-ignore
8 const req = new http.IncomingMessage(null)
9
10 Object.assign(req, props)
11
12 return req
13}
14
15tap.only('normalizeHeaders', async (t) => {
16 const { headers, multiValueHeaders } = normalizeHeaders({
17 'Content-Type': 'text/html',
18 'Set-Cookie': ['foo=1', 'bar=2'],
19 'X-Version': undefined,
20 })
21
22 t.same(headers, {
23 'content-type': 'text/html',
24 })
25 t.same(multiValueHeaders, {
26 'set-cookie': ['foo=1', 'bar=2'],
27 })
28})
29
30tap.only('getQueryStringParameters', async (t) => {
31 const { queryStringParameters, multiValueQueryStringParameters } = getQueryStringParameters('foo=bar&bar=a,b')
32
33 t.same(queryStringParameters, {
34 foo: 'bar',
35 })
36 t.same(multiValueQueryStringParameters, {
37 bar: ['a', 'b'],
38 })
39})
40
41tap.only('requestToEvent', async (t) => {
42 const event = await requestToEvent(
43 createRequest({
44 url: '/',
45 method: 'GET',
46 })
47 )
48
49 t.same(event, {
50 rawUrl: '/',
51 path: '/',
52 httpMethod: 'GET',
53 headers: { 'client-ip': '0.0.0.0' },
54 multiValueHeaders: {},
55 rawQuery: '',
56 queryStringParameters: {},
57 multiValueQueryStringParameters: {},
58 body: null,
59 isBase64Encoded: false,
60 })
61})
62
63tap.only('requestToEvent - shouldBase64Encode', async (t) => {
64 const event = await requestToEvent(
65 createRequest({
66 url: '/',
67 method: 'GET',
68 headers: {
69 'content-type': 'image/png',
70 },
71 })
72 )
73
74 t.equal(event.isBase64Encoded, true)
75})