Skip to content

Commit 20e3594

Browse files
committed
test(WRITING-11435): test lambda function
1 parent 20a4fec commit 20e3594

File tree

11 files changed

+2855
-0
lines changed

11 files changed

+2855
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ etc/docs/build
8787
!docs/**/*.png
8888
!docs/**/*.css
8989
!docs/**/*.js
90+
91+
# AWS SAM generated
92+
test/lambda/.aws-sam
93+
test/lambda/env.json

test/lambda/.gitignore

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
2+
# Created by https://www.toptal.com/developers/gitignore/api/osx,node,linux,windows,sam
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=osx,node,linux,windows,sam
4+
5+
### Linux ###
6+
*~
7+
8+
# temporary files which can be created if a process still has a handle open of a deleted file
9+
.fuse_hidden*
10+
11+
# KDE directory preferences
12+
.directory
13+
14+
# Linux trash folder which might appear on any partition or disk
15+
.Trash-*
16+
17+
# .nfs files are created when an open file is removed but is still being accessed
18+
.nfs*
19+
20+
### Node ###
21+
# Logs
22+
logs
23+
*.log
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
lerna-debug.log*
28+
29+
# Diagnostic reports (https://nodejs.org/api/report.html)
30+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
31+
32+
# Runtime data
33+
pids
34+
*.pid
35+
*.seed
36+
*.pid.lock
37+
38+
# Directory for instrumented libs generated by jscoverage/JSCover
39+
lib-cov
40+
41+
# Coverage directory used by tools like istanbul
42+
coverage
43+
*.lcov
44+
45+
# nyc test coverage
46+
.nyc_output
47+
48+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
49+
.grunt
50+
51+
# Bower dependency directory (https://bower.io/)
52+
bower_components
53+
54+
# node-waf configuration
55+
.lock-wscript
56+
57+
# Compiled binary addons (https://nodejs.org/api/addons.html)
58+
build/Release
59+
60+
# Dependency directories
61+
node_modules/
62+
jspm_packages/
63+
64+
# TypeScript v1 declaration files
65+
typings/
66+
67+
# TypeScript cache
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
.npm
72+
73+
# Optional eslint cache
74+
.eslintcache
75+
76+
# Optional stylelint cache
77+
.stylelintcache
78+
79+
# Microbundle cache
80+
.rpt2_cache/
81+
.rts2_cache_cjs/
82+
.rts2_cache_es/
83+
.rts2_cache_umd/
84+
85+
# Optional REPL history
86+
.node_repl_history
87+
88+
# Output of 'npm pack'
89+
*.tgz
90+
91+
# Yarn Integrity file
92+
.yarn-integrity
93+
94+
# dotenv environment variables file
95+
.env
96+
.env.test
97+
.env*.local
98+
99+
# parcel-bundler cache (https://parceljs.org/)
100+
.cache
101+
.parcel-cache
102+
103+
# Next.js build output
104+
.next
105+
106+
# Nuxt.js build / generate output
107+
.nuxt
108+
dist
109+
110+
# Storybook build outputs
111+
.out
112+
.storybook-out
113+
storybook-static
114+
115+
# rollup.js default build output
116+
dist/
117+
118+
# Gatsby files
119+
.cache/
120+
# Comment in the public line in if your project uses Gatsby and not Next.js
121+
# https://nextjs.org/blog/next-9-1#public-directory-support
122+
# public
123+
124+
# vuepress build output
125+
.vuepress/dist
126+
127+
# Serverless directories
128+
.serverless/
129+
130+
# FuseBox cache
131+
.fusebox/
132+
133+
# DynamoDB Local files
134+
.dynamodb/
135+
136+
# TernJS port file
137+
.tern-port
138+
139+
# Stores VSCode versions used for testing VSCode extensions
140+
.vscode-test
141+
142+
# Temporary folders
143+
tmp/
144+
temp/
145+
146+
### OSX ###
147+
# General
148+
.DS_Store
149+
.AppleDouble
150+
.LSOverride
151+
152+
# Icon must end with two \r
153+
Icon
154+
155+
# Thumbnails
156+
._*
157+
158+
# Files that might appear in the root of a volume
159+
.DocumentRevisions-V100
160+
.fseventsd
161+
.Spotlight-V100
162+
.TemporaryItems
163+
.Trashes
164+
.VolumeIcon.icns
165+
.com.apple.timemachine.donotpresent
166+
167+
# Directories potentially created on remote AFP share
168+
.AppleDB
169+
.AppleDesktop
170+
Network Trash Folder
171+
Temporary Items
172+
.apdisk
173+
174+
### SAM ###
175+
# Ignore build directories for the AWS Serverless Application Model (SAM)
176+
# Info: https://aws.amazon.com/serverless/sam/
177+
# Docs: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html
178+
179+
**/.aws-sam
180+
181+
### Windows ###
182+
# Windows thumbnail cache files
183+
Thumbs.db
184+
Thumbs.db:encryptable
185+
ehthumbs.db
186+
ehthumbs_vista.db
187+
188+
# Dump file
189+
*.stackdump
190+
191+
# Folder config file
192+
[Dd]esktop.ini
193+
194+
# Recycle Bin used on file shares
195+
$RECYCLE.BIN/
196+
197+
# Windows Installer files
198+
*.cab
199+
*.msi
200+
*.msix
201+
*.msm
202+
*.msp
203+
204+
# Windows shortcuts
205+
*.lnk
206+
207+
# End of https://www.toptal.com/developers/gitignore/api/osx,node,linux,windows,sam

test/lambda/events/event.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"body": "{\"message\": \"hello world\"}",
3+
"resource": "/{proxy+}",
4+
"path": "/path/to/resource",
5+
"httpMethod": "POST",
6+
"isBase64Encoded": false,
7+
"queryStringParameters": {
8+
"foo": "bar"
9+
},
10+
"pathParameters": {
11+
"proxy": "/path/to/resource"
12+
},
13+
"stageVariables": {
14+
"baz": "qux"
15+
},
16+
"headers": {
17+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
18+
"Accept-Encoding": "gzip, deflate, sdch",
19+
"Accept-Language": "en-US,en;q=0.8",
20+
"Cache-Control": "max-age=0",
21+
"CloudFront-Forwarded-Proto": "https",
22+
"CloudFront-Is-Desktop-Viewer": "true",
23+
"CloudFront-Is-Mobile-Viewer": "false",
24+
"CloudFront-Is-SmartTV-Viewer": "false",
25+
"CloudFront-Is-Tablet-Viewer": "false",
26+
"CloudFront-Viewer-Country": "US",
27+
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
28+
"Upgrade-Insecure-Requests": "1",
29+
"User-Agent": "Custom User Agent String",
30+
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
31+
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
32+
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
33+
"X-Forwarded-Port": "443",
34+
"X-Forwarded-Proto": "https"
35+
},
36+
"requestContext": {
37+
"accountId": "123456789012",
38+
"resourceId": "123456",
39+
"stage": "prod",
40+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
41+
"requestTime": "09/Apr/2015:12:34:56 +0000",
42+
"requestTimeEpoch": 1428582896000,
43+
"identity": {
44+
"cognitoIdentityPoolId": null,
45+
"accountId": null,
46+
"cognitoIdentityId": null,
47+
"caller": null,
48+
"accessKey": null,
49+
"sourceIp": "127.0.0.1",
50+
"cognitoAuthenticationType": null,
51+
"cognitoAuthenticationProvider": null,
52+
"userArn": null,
53+
"userAgent": "Custom User Agent String",
54+
"user": null
55+
},
56+
"path": "/prod/path/to/resource",
57+
"resourcePath": "/{proxy+}",
58+
"httpMethod": "POST",
59+
"apiId": "1234567890",
60+
"protocol": "HTTP/1.1"
61+
}
62+
}

test/lambda/mongodb/.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.aws-sam

test/lambda/mongodb/.eslintrc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
parser: "@typescript-eslint/parser",
3+
parserOptions: {
4+
ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
5+
sourceType: "module"
6+
},
7+
extends: [
8+
"plugin:@typescript-eslint/recommended", // recommended rules from the @typescript-eslint/eslint-plugin
9+
"plugin:prettier/recommended" // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
10+
],
11+
rules: {
12+
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
13+
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
14+
}
15+
};

test/lambda/mongodb/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/*

test/lambda/mongodb/.prettierrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
semi: true,
3+
trailingComma: "all",
4+
singleQuote: true,
5+
printWidth: 120,
6+
tabWidth: 4
7+
};

test/lambda/mongodb/app.mjs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { MongoClient } from 'mongodb';
2+
3+
// Creates the client that is cached for all requests, subscribes to
4+
// relevant events, and forces the connection pool to get populated.
5+
const mongoClient = new MongoClient(process.env.MONGODB_URI, {
6+
monitorCommands: true
7+
});
8+
9+
mongoClient.on('commandStarted', (event) => {
10+
console.log('commandStarted', event);
11+
});
12+
13+
mongoClient.on('commandSucceeded', (event) => {
14+
console.log('commandSucceeded', event);
15+
});
16+
17+
mongoClient.on('commandFailed', (event) => {
18+
console.log('commandFailed', event);
19+
});
20+
21+
mongoClient.on('serverHeartbeatStarted', (event) => {
22+
console.log('serverHeartbeatStarted', event);
23+
});
24+
25+
mongoClient.on('serverHeartbeatSucceeded', (event) => {
26+
console.log('serverHeartbeatSucceeded', event);
27+
});
28+
29+
mongoClient.on('serverHeartbeatFailed', (event) => {
30+
console.log('serverHeartbeatFailed', event);
31+
});
32+
33+
mongoClient.on('connectionCreated', (event) => {
34+
console.log('connectionCreated', event);
35+
});
36+
37+
mongoClient.on('connectionClosed', (event) => {
38+
console.log('connectionClosed', event);
39+
});
40+
41+
// Populate the connection pool.
42+
await mongoClient.connect();
43+
44+
/**
45+
* The handler function itself performs an insert/delete and returns the
46+
* id of the document in play.
47+
*
48+
* @param {Object} event - API Gateway Lambda Proxy Input Format
49+
* @returns {Object} object - API Gateway Lambda Proxy Output Format
50+
*/
51+
export const lambdaHandler = async (event) => {
52+
const db = mongoClient.db('lambdaTest');
53+
const collection = db.collection('test');
54+
const { insertedId } = await collection.insertOne({ n: 1 });
55+
await collection.deleteOne({ _id: insertedId });
56+
try {
57+
return {
58+
statusCode: 200,
59+
body: JSON.stringify({
60+
insertedId: insertedId,
61+
}),
62+
};
63+
} catch (err) {
64+
return {
65+
statusCode: 500,
66+
body: JSON.stringify({
67+
message: err.message,
68+
}),
69+
};
70+
}
71+
};

0 commit comments

Comments
 (0)