-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
218 lines (198 loc) · 7.41 KB
/
index.js
File metadata and controls
218 lines (198 loc) · 7.41 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const Serverless_Events = require("./sampleEvents.json");
const data = require("./animals.json");
const firetailWrapper = require("../dist");
const timer = t => new Promise(r => setTimeout(() => r(), t));
//=====================================================
//====================================== test GET calls
//=====================================================
describe("test Firetail:Serverless", () => {
test("should work when wrapping a Promise", done => {
const time = 200;
const next = firetailWrapper(
new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
statusCode: 200,
body: JSON.stringify(data),
});
}, time);
}),
);
const cLog = console.log;
console.log = txt => {
expect(txt.startsWith("firetail:log-ext:")).toBe(true);
const base64 = txt?.slice(17);
const json = JSON.parse(atob(base64));
expect(json.execution_time).toBeGreaterThan(time - 10);
};
next(Serverless_Events["lambda function url"]).then(
({ statusCode, body }) => {
expect(statusCode).toBe(200);
expect(body).toBe(
'[{"id":1,"name":"Bubbles","tag":"fish"},' +
'{"id":2,"name":"Jax","tag":"cat"},' +
'{"id":3,"name":"Tiger Lily","tag":"cat"},' +
'{"id":4,"name":"Buzz","tag":"dog"},' +
'{"id":5,"name":"Duke","owner":"Tom"}]',
);
console.log = cLog.bind(console);
done();
},
);
});
test("should work when wrapping an async function", done => {
const time = 300;
const next = firetailWrapper(async event => {
await timer(time);
const statusCode = 200;
if (
event.queryStringParameters &&
event.queryStringParameters.limit
) {
return {
statusCode,
body: JSON.stringify(
data.slice(0, event.queryStringParameters.limit),
),
};
}
return {
statusCode,
body: JSON.stringify(data),
};
});
const cLog = console.log;
console.log = txt => {
expect(txt.startsWith("firetail:log-ext:")).toBe(true);
const base64 = txt?.slice(17);
const json = JSON.parse(atob(base64));
expect(json.execution_time).toBeGreaterThan(time - 10);
};
next(Serverless_Events["lambda function url"])
.then(({ statusCode, body }) => {
expect(statusCode).toBe(200);
expect(body).toBe(
'[{"id":1,"name":"Bubbles","tag":"fish"},' +
'{"id":2,"name":"Jax","tag":"cat"},' +
'{"id":3,"name":"Tiger Lily","tag":"cat"},' +
'{"id":4,"name":"Buzz","tag":"dog"},' +
'{"id":5,"name":"Duke","owner":"Tom"}]',
);
return next(Serverless_Events["api Gateway Proxy Event"]);
})
.then(({ statusCode, body }) => {
expect(statusCode).toBe(200);
expect(body).toBe(
'[{"id":1,"name":"Bubbles","tag":"fish"},' +
'{"id":2,"name":"Jax","tag":"cat"}]',
);
console.log = cLog.bind(console);
done();
});
});
test("should work when wrapping a sync function", done => {
const next = firetailWrapper(event => {
const statusCode = 200;
if (
event.queryStringParameters &&
event.queryStringParameters.limit
) {
return {
statusCode,
body: JSON.stringify(
data.slice(0, event.queryStringParameters.limit),
),
};
}
return {
statusCode,
body: JSON.stringify(data),
};
});
const cLog = console.log;
console.log = txt => {
expect(txt.startsWith("firetail:log-ext:")).toBe(true);
const base64 = txt?.slice(17);
const json = JSON.parse(atob(base64));
expect(json.execution_time).toBeLessThan(10);
};
next(Serverless_Events["lambda function url"])
.then(({ statusCode, body }) => {
expect(statusCode).toBe(200);
expect(body).toBe(
'[{"id":1,"name":"Bubbles","tag":"fish"},' +
'{"id":2,"name":"Jax","tag":"cat"},' +
'{"id":3,"name":"Tiger Lily","tag":"cat"},' +
'{"id":4,"name":"Buzz","tag":"dog"},' +
'{"id":5,"name":"Duke","owner":"Tom"}]',
);
return next(Serverless_Events["api Gateway Proxy Event"]);
})
.then(({ statusCode, body }) => {
expect(statusCode).toBe(200);
expect(body).toBe(
'[{"id":1,"name":"Bubbles","tag":"fish"},' +
'{"id":2,"name":"Jax","tag":"cat"}]',
);
console.log = cLog.bind(console);
done();
});
});
test("should not throw when wrapping a non-function", done => {
const next = firetailWrapper({
statusCode: 200,
body: "some body",
});
const cLog = console.log;
console.log = txt => {
expect(txt.startsWith("firetail:log-ext:")).toBe(true);
const base64 = txt?.slice(17);
const json = JSON.parse(atob(base64));
expect(json.execution_time).toBeLessThan(10);
};
next(Serverless_Events["lambda function url"]).then(
({ statusCode, body }) => {
expect(statusCode).toBe(200);
expect(body).toBe("some body");
done();
},
);
});
test("should not throw when Date() does not work or status code is not found", done => {
const next = firetailWrapper({
statusCode: null,
body: "some body",
});
const d = Date;
class C {
constructor(x, y) {
this.x = x;
this.y = y;
}
foo() {
return this.x + this.y;
}
}
Date = class D {
constructor() {}
getTime() {
return "not a date";
}
};
const cLog = console.log;
console.log = txt => {
expect(txt.startsWith("firetail:log-ext:")).toBe(true);
const base64 = txt?.slice(17);
const json = JSON.parse(atob(base64));
expect(json.execution_time).toBe(0);
expect(json.response.statusCode).toBe(200);
};
next(Serverless_Events["lambda function url"]).then(
({ statusCode, body }) => {
expect(statusCode).toBe(null);
expect(body).toBe("some body");
done();
},
);
});
});