-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlambda-utils-tap.js
More file actions
97 lines (90 loc) · 2.43 KB
/
lambda-utils-tap.js
File metadata and controls
97 lines (90 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict";
const lambdaUtils = require("./lambda-utils");
const schemas = require("./schemas");
const tap = require("tap");
const sinon = require("sinon");
function stubRes() {
return {
send: sinon.spy(),
error: sinon.spy()
};
}
tap.test("lambdaUtils.validateEvent invalid event", test => {
const schema = schemas.define({
type: "object",
required: ["count"],
properties: {
count: {
type: "number"
}
}
});
const res = stubRes();
const middleware = lambdaUtils.validateEvent(schema);
const next = sinon.spy();
middleware({event: {}}, res, next);
test.ok(res.send.calledOnce);
const result = res.send.firstCall.args[0];
test.ok(result);
test.match(result.body, "required property 'count'");
test.same(result.statusCode, 400);
test.end();
});
tap.test("lambdaUtils.validateConfig invalid", test => {
const res = stubRes();
const middleware = lambdaUtils.validateConfig({error: "unit-test-error"});
const next = sinon.spy();
middleware({event: {}}, res, next);
test.notOk(res.send.called);
const error = next.firstCall.args[0];
test.ok(error);
test.match(error.message, "unit-test-error");
test.end();
});
tap.test("lambdaUtils.validateEvent invalid body JSON", test => {
const schema = schemas.define({
type: "object",
required: ["body"],
properties: {
body: {
type: "object"
}
}
});
const middleware = lambdaUtils.validateEvent(schema);
middleware({event: {body: "{not json..!"}}, {}, error => {
test.ok(error);
test.match(error.message, "Invalid JSON in request body");
test.same(error.statusCode, 400);
test.end();
});
});
tap.test("httpError should provide default message", {skip: false}, test => {
const error = lambdaUtils.httpError();
test.match(error, {
statusCode: 500,
message: "Internal Server Error"
});
test.end();
});
tap.test("errorHandler no error case", {skip: false}, test => {
const res = {
send(response) {
test.match(response, {statusCode: 500});
const body = JSON.parse(response.body);
test.match(body, {message: "Internal Server Error"});
test.end();
}
};
lambdaUtils.errorHandler(null, null, res);
});
tap.test("sendBody no body provided", {skip: false}, test => {
const res = {
send(response) {
test.match(response, {statusCode: 200});
test.same(response.body, "{}");
test.end();
}
};
lambdaUtils.sendBody({}, res);
});