From a4701e50408693b0c28a7b90a736a08c26ffe92e Mon Sep 17 00:00:00 2001 From: Marcus Weiner Date: Mon, 13 Sep 2021 17:02:11 +0200 Subject: [PATCH] feat: Support rawUrl and rawQuery in local lambda executions (#3310) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Support rawUrl and rawQuery in local lambda executions * refactor: use URLSearchParams * chore: add test Co-authored-by: Eduardo Bouças Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- src/lib/functions/server.js | 6 +++++- tests/serving-functions.test.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/lib/functions/server.js b/src/lib/functions/server.js index 9a4715c64a6..228eaf27489 100644 --- a/src/lib/functions/server.js +++ b/src/lib/functions/server.js @@ -73,7 +73,9 @@ const createHandler = function ({ functionsRegistry }) { (prev, [key, value]) => ({ ...prev, [key]: Array.isArray(value) ? value : [value] }), {}, ) - + const host = request.get('host') || 'localhost' + const rawUrl = `${request.protocol}://${host}${request.originalUrl}` + const rawQuery = new URLSearchParams(request.query).toString() const event = { path: requestPath, httpMethod: request.method, @@ -86,6 +88,8 @@ const createHandler = function ({ functionsRegistry }) { multiValueHeaders: headers, body, isBase64Encoded, + rawUrl, + rawQuery, } const clientContext = buildClientContext(request.headers) || {} diff --git a/tests/serving-functions.test.js b/tests/serving-functions.test.js index ac7b129680f..f55d0708fa2 100644 --- a/tests/serving-functions.test.js +++ b/tests/serving-functions.test.js @@ -734,4 +734,37 @@ test('Uses sourcemaps to show correct paths and locations in stack trace', async }) }) }) + +test('Populates the `event` argument', async (t) => { + await withSiteBuilder('function-event', async (builder) => { + await builder + .withFunction({ + path: 'hello.js', + handler: async (event) => ({ + statusCode: 200, + body: JSON.stringify(event), + }), + }) + .withNetlifyToml({ + config: { + build: { publish: 'public' }, + functions: { directory: 'functions' }, + }, + }) + .buildAsync() + + await withDevServer({ cwd: builder.directory }, async ({ port, outputBuffer }) => { + await tryAndLogOutput(async () => { + const { httpMethod, path, rawQuery, rawUrl } = await got( + `http://localhost:${port}/.netlify/functions/hello?net=lify&jam=stack`, + ).json() + + t.is(httpMethod, 'GET') + t.is(path, '/.netlify/functions/hello') + t.is(rawQuery, 'net=lify&jam=stack') + t.is(rawUrl, `http://localhost:${port}/.netlify/functions/hello?net=lify&jam=stack`) + }, outputBuffer) + }) + }) +}) /* eslint-enable require-await */