From 50fe812cc21a969962fe9b3842ec9b8ab6dee85b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Thu, 12 May 2022 11:13:09 +0100 Subject: [PATCH 1/2] fix: pass correct protocol to functions server --- src/lib/functions/server.js | 3 ++- tests/integration/200.command.dev.test.js | 8 +++++--- tests/integration/300.command.dev.test.js | 12 ++++++------ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/lib/functions/server.js b/src/lib/functions/server.js index 9784215cd16..9eb1625a72d 100644 --- a/src/lib/functions/server.js +++ b/src/lib/functions/server.js @@ -89,7 +89,8 @@ const createHandler = function (options) { {}, ) const rawQuery = new URLSearchParams(request.query).toString() - const url = new URL(requestPath, `${request.protocol}://${request.get('host') || 'localhost'}`) + const protocol = options.config.dev.https ? 'https' : 'http' + const url = new URL(requestPath, `${protocol}://${request.get('host') || 'localhost'}`) url.search = rawQuery const rawUrl = url.toString() const event = { diff --git a/tests/integration/200.command.dev.test.js b/tests/integration/200.command.dev.test.js index 82b29175a60..592e1a1171e 100644 --- a/tests/integration/200.command.dev.test.js +++ b/tests/integration/200.command.dev.test.js @@ -213,9 +213,9 @@ export const handler = async function () { }) .withFunction({ path: 'hello.js', - handler: async () => ({ + handler: async (event) => ({ statusCode: 200, - body: 'Hello World', + body: JSON.stringify({ rawUrl: event.rawUrl }), }), }) .withEdgeFunction({ @@ -242,7 +242,9 @@ export const handler = async function () { const options = { https: { rejectUnauthorized: false } } t.is(await got(`https://localhost:${port}`, options).text(), 'index') t.is(await got(`https://localhost:${port}?ef=true`, options).text(), 'INDEX') - t.is(await got(`https://localhost:${port}/api/hello`, options).text(), 'Hello World') + t.deepEqual(await got(`https://localhost:${port}/api/hello`, options).json(), { + rawUrl: `https://localhost:${port}/api/hello`, + }) }) }) }) diff --git a/tests/integration/300.command.dev.test.js b/tests/integration/300.command.dev.test.js index 98dd4025d76..c50b9e30827 100644 --- a/tests/integration/300.command.dev.test.js +++ b/tests/integration/300.command.dev.test.js @@ -131,9 +131,9 @@ test('should serve function from a subdirectory', async (t) => { await withSiteBuilder('site-with-from-subdirectory', async (builder) => { builder.withNetlifyToml({ config: { functions: { directory: 'functions' } } }).withFunction({ path: path.join('echo', 'echo.js'), - handler: async () => ({ + handler: async (event) => ({ statusCode: 200, - body: 'ping', + body: JSON.stringify({ rawUrl: event.rawUrl }), metadata: { builder_function: true }, }), }) @@ -141,10 +141,10 @@ test('should serve function from a subdirectory', async (t) => { await builder.buildAsync() await withDevServer({ cwd: builder.directory }, async (server) => { - const response = await got(`${server.url}/.netlify/functions/echo`).text() - t.is(response, 'ping') - const builderResponse = await got(`${server.url}/.netlify/builders/echo`).text() - t.is(builderResponse, 'ping') + const response = await got(`${server.url}/.netlify/functions/echo`).json() + t.deepEqual(response, { rawUrl: `${server.url}/.netlify/functions/echo` }) + const builderResponse = await got(`${server.url}/.netlify/builders/echo`).json() + t.deepEqual(builderResponse, { rawUrl: `${server.url}/.netlify/builders/echo` }) }) }) }) From a0375e952fabfa4f13054433a67ec9bd8cc851a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Thu, 12 May 2022 11:29:54 +0100 Subject: [PATCH 2/2] refactor: safely get config property --- src/lib/functions/server.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/functions/server.js b/src/lib/functions/server.js index 9eb1625a72d..019ae459178 100644 --- a/src/lib/functions/server.js +++ b/src/lib/functions/server.js @@ -1,4 +1,5 @@ // @ts-check +const { get } = require('dot-prop') const jwtDecode = require('jwt-decode') const { @@ -89,7 +90,7 @@ const createHandler = function (options) { {}, ) const rawQuery = new URLSearchParams(request.query).toString() - const protocol = options.config.dev.https ? 'https' : 'http' + const protocol = get(options, 'config.dev.https') ? 'https' : 'http' const url = new URL(requestPath, `${protocol}://${request.get('host') || 'localhost'}`) url.search = rawQuery const rawUrl = url.toString()