Skip to content

Commit 0773623

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

File tree

10 files changed

+2648
-0
lines changed

10 files changed

+2648
-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/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)