Skip to content

Commit a44227d

Browse files
committed
Fix regression and test: --watch
1 parent c208719 commit a44227d

File tree

2 files changed

+101
-31
lines changed

2 files changed

+101
-31
lines changed

src/lambdalocal.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,44 +94,49 @@ function _getRequestPayload(req, callback) {
9494
body += chunk.toString();
9595
});
9696
req.on('end', () => {
97-
let payload;
97+
let payload, event;
9898
try {
9999
payload = JSON.parse(body || '{}');
100100
} catch(err) {
101101
callback(err);
102102
return;
103103
}
104-
// Format: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.proxy-format
105-
const url = new URL(req.url, `http://${req.headers.host}`);
106-
const event = {
107-
version: "2.0",
108-
routeKey: "$default",
109-
rawPath: url.pathname,
110-
rawQueryString: url.search,
111-
cookies: utils.parseCookies(req),
112-
headers: req.headers,
113-
queryStringParameters: Object.fromEntries(url.searchParams),
114-
requestContext: {
115-
accountId: "123456789012",
116-
apiId: "api-id",
117-
authentication: {},
118-
authorizer: {},
119-
http: {
120-
method: req.method,
121-
path: url.pathname,
122-
protocol: "HTTP/" + req.httpVersion,
123-
sourceIp: req.socket.localAddress,
124-
userAgent: req.headers['user-agent'],
125-
},
126-
requestId: "id",
104+
if (payload.event) {
105+
// compatibility: if "event" was provided.
106+
event = payload.event;
107+
} else {
108+
// Format: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.proxy-format
109+
const url = new URL(req.url, `http://${req.headers.host}`);
110+
event = {
111+
version: "2.0",
127112
routeKey: "$default",
128-
stage: "$default",
129-
time: new Date().toISOString(),
130-
timeEpoch: new Date().getTime(),
131-
},
132-
body: payload,
133-
isBase64Encoded: req.headers['content-type'] !== 'application/json',
134-
};
113+
rawPath: url.pathname,
114+
rawQueryString: url.search,
115+
cookies: utils.parseCookies(req),
116+
headers: req.headers,
117+
queryStringParameters: Object.fromEntries(url.searchParams),
118+
requestContext: {
119+
accountId: "123456789012",
120+
apiId: "api-id",
121+
authentication: {},
122+
authorizer: {},
123+
http: {
124+
method: req.method,
125+
path: url.pathname,
126+
protocol: "HTTP/" + req.httpVersion,
127+
sourceIp: req.socket.localAddress,
128+
userAgent: req.headers['user-agent'],
129+
},
130+
requestId: "id",
131+
routeKey: "$default",
132+
stage: "$default",
133+
time: new Date().toISOString(),
134+
timeEpoch: new Date().getTime(),
135+
},
136+
body: payload,
137+
isBase64Encoded: req.headers['content-type'] !== 'application/json',
138+
};
139+
}
135140
callback(null, event);
136141
});
137142
}

test/test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ describe("- Testing lambdalocal.js", function () {
504504
});
505505
describe("- Testing cli.js", function () {
506506
var spawnSync = require('child_process').spawnSync;
507+
var spawnAsync = require('child_process').spawn;
507508
describe("* Basic Run", function () {
508509
it("should end normally", function () {
509510
var command = get_shell("node ../build/cli.js -l ./functs/test-func.js -e ./events/test-event.js");
@@ -611,6 +612,70 @@ describe("- Testing cli.js", function () {
611612
});
612613
});
613614

615+
if (get_node_major_version() >= 18) {
616+
// we use fetch() (nodejs>=18) to not bother.
617+
const doRequestWhenReady = function(r, event, check, cb) {
618+
var result;
619+
r.stdout.on('data', (data) => {
620+
data = data.toString();
621+
if (data.includes("listening on")) {
622+
// started
623+
fetch("http://localhost:8008", {
624+
method: "POST",
625+
headers: {
626+
"Content-Type": "application/json",
627+
},
628+
body: JSON.stringify(event),
629+
}).then((res) => {
630+
res.json().then((datres) => {
631+
result = datres;
632+
r.kill('SIGINT');
633+
});
634+
});
635+
}
636+
});
637+
r.on('error', (err) => {
638+
cb(err);
639+
});
640+
r.on('close', () => {
641+
check(result);
642+
cb();
643+
});
644+
// add timeout
645+
setTimeout(() => {
646+
r.kill();
647+
}, 1000);
648+
}
649+
describe("* --watch run", function () {
650+
it("test watch with old event format", function (cb) {
651+
var command = get_shell("node ../build/cli.js -l ./functs/test-func-echo.js --watch");
652+
var r = spawnAsync(command[0], command[1]);
653+
doRequestWhenReady(r,
654+
{
655+
"event": {"hey": "data"}
656+
},
657+
(result) => {
658+
assert.deepEqual(result, {"hey": "data"});
659+
},
660+
cb
661+
);
662+
});
663+
it("test watch with gateway event format", function (cb) {
664+
var command = get_shell("node ../build/cli.js -l ./functs/test-func-echo.js --watch");
665+
var r = spawnAsync(command[0], command[1]);
666+
doRequestWhenReady(r,
667+
{
668+
"hey": "data",
669+
},
670+
(result) => {
671+
assert.deepEqual(result["body"], {"hey": "data"});
672+
},
673+
cb
674+
);
675+
});
676+
});
677+
}
678+
614679
describe("* Crashing run", function () {
615680
it("should fail", function () {
616681
var command = get_shell("node ../build/cli.js -l ./functs/test-func-error.js -e ./events/test-event.js");

0 commit comments

Comments
 (0)