Skip to content

Commit

Permalink
Refactor remaining function helpers, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Aug 13, 2019
1 parent 0c127d9 commit 85e0c2c
Show file tree
Hide file tree
Showing 10 changed files with 673 additions and 742 deletions.
213 changes: 213 additions & 0 deletions __tests__/integration/offline-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
'use strict'

const { stringify } = JSON

exports.basicAuthentication1 = (event, context, cb) => {
const response = {
body: stringify({
message: 'Private Function Executed Correctly',
}),
statusCode: 200,
}
cb(null, response)
}

exports.basicAuthentication2 = (event, context, cb) => {
const response = {
body: stringify({
message: 'Private Function Executed Correctly',
}),
statusCode: 200,
}
cb(null, response)
}

exports.usersIndex1 = (event, context, cb) => cb(null, 'Hello World')

exports.usersIndex2 = (event, context, cb) =>
cb(new Error('Internal Server Error'))

exports.usersIndex3 = (event, context, cb) =>
cb(new Error('[401] Unauthorized'))

exports.fn1 = (event, context, cb) =>
cb(null, {
body: stringify({ data: 'data' }),
statusCode: 200,
})

exports.fn2 = (event, context, cb) =>
cb(null, {
body: stringify({ data: 'data' }),
headers: {
'content-type': 'application/json',
},
statusCode: 200,
})

exports.fn3 = (event, context, cb) =>
cb(null, {
body: stringify({ data: 'data' }),
headers: {
'content-type': 'application/vnd.api+json',
},
statusCode: 200,
})

exports.fn4 = (event, context, cb) =>
cb(null, {
body: stringify({ data: 'data' }),
statusCode: 200,
})

exports.fn5 = (event, context, cb) =>
cb(null, {
body: null,
statusCode: 201,
})

exports.fn6 = (event, context, cb) =>
cb(null, {
body: null,
statusCode: 201,
})

exports.unstringifiedBody = (event, context, cb) => {
if (typeof event.body !== 'string') {
const response = {
body: stringify({
message:
'According to the API Gateway specs, the body content must be stringified. Check your Lambda response and make sure you are invoking stringify(YOUR_CONTENT) on your body object',
}),
statusCode: 500,
}

cb(null, response)
}
}

exports.fn7 = (event, context, cb) =>
cb(null, {
headers: { 'set-cookie': 'foo=bar', 'set-COOKIE': 'floo=baz' },
statusCode: 200,
})

exports.hello = (event, context, cb) =>
cb(null, {
statusCode: 201,
body: event.stageVariables.hello,
})

exports.test = (event, context, cb) =>
cb(null, {
body: 'Hello',
statusCode: 200,
})

const rawBody = `{
\t"type": "notification_event",
\t"app_id": "q8sn4hth",
\t"data": {
\t\t"type": "notification_event_data",
\t\t\t"item": {
\t\t\t\t"type": "ping",
\t\t\t\t"message": "something something interzen"
\t\t\t}
\t\t},
\t"links": {},
\t"id": null,
\t"topic": "ping",
\t"delivery_status": null,
\t"delivery_attempts": 1,
\t"delivered_at": 0,
\t"first_sent_at": 1513466985,
\t"created_at": 1513466985,
\t"self": null
}`

exports.rawJsonBody = (event, context, cb) => {
if (event.body === rawBody) {
const response = {
body: stringify({
message: 'JSON body was not stripped of newlines or tabs',
}),
statusCode: 200,
}

cb(null, response)
} else {
cb('JSON body was mangled')
}
}

exports.promise = () =>
Promise.resolve({
body: stringify({ message: 'Hello World' }),
statusCode: 200,
})

exports.promiseDeferred = () =>
new Promise((resolve) =>
setTimeout(
() =>
resolve({
statusCode: 200,
body: stringify({ message: 'Hello World' }),
}),
10,
),
)

exports.doneDeferred = (request, context, cb) =>
setTimeout(
() =>
cb(null, {
body: stringify({ message: 'Hello World' }),
statusCode: 200,
}),
10,
)

exports.throwDone = () => {
throw new Error('This is an error')
}

exports.asyncFunction = async () => ({
body: stringify({ message: 'Hello World' }),
statusCode: 200,
})

exports.asyncFunctionThrows = async () => {
throw new Error('This is an error')
}

exports.fn8 = (event, context, cb) =>
cb(null, {
body: null,
statusCode: 204,
})

exports.headers = (event, context, cb) => cb(null, {})

exports.cookie = (event, context, cb) => cb(null, {})

exports.fn9 = (event, context, cb) =>
cb(null, {
headers: {
'Set-Cookie': 'mycookie=123',
},
})

exports.fn10 = (event, context, cb) =>
cb(null, {
headers: {
'Set-Cookie': 'mycookie=123',
},
})

exports.fn11 = (event, context, cb) =>
cb(null, {
headers: {
'Set-Cookie': 'mycookie=123',
},
})
Loading

0 comments on commit 85e0c2c

Please sign in to comment.