From db0f1ed0a74fa4dbee41d47f499de1dfd762a3aa Mon Sep 17 00:00:00 2001 From: Karagulamos Date: Fri, 4 Feb 2022 15:02:52 +0100 Subject: [PATCH] fix: function:serve shows undefined port in url (#4146) * fix: function:serve shows undefined port in url * fix: restored missing test * fix: wrapped function url in URL object * fix: wrapped function url in URL object Co-authored-by: Lukas Holzer Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Erez Rokah --- src/lib/functions/netlify-function.js | 8 +++++++- tests/integration/20.command.functions.test.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib/functions/netlify-function.js b/src/lib/functions/netlify-function.js index 9e6956b5ae7..29a4a06320c 100644 --- a/src/lib/functions/netlify-function.js +++ b/src/lib/functions/netlify-function.js @@ -131,7 +131,13 @@ class NetlifyFunction { } get url() { - return `http://localhost:${this.settings.port}/.netlify/functions/${this.name}` + // This line fixes the issue here https://github.com/netlify/cli/issues/4116 + // Not sure why `settings.port` was used here nor does a valid reference exist. + // However, it remains here to serve whatever purpose for which it was added. + const port = this.settings.port || this.settings.functionsPort + + const url = new URL(`/.netlify/functions/${this.name}`, `http://localhost:${port}`) + return url.href } } diff --git a/tests/integration/20.command.functions.test.js b/tests/integration/20.command.functions.test.js index aa2a41512dd..94096f8c3ac 100644 --- a/tests/integration/20.command.functions.test.js +++ b/tests/integration/20.command.functions.test.js @@ -8,6 +8,7 @@ const getPort = require('get-port') const waitPort = require('wait-port') const fs = require('../../src/lib/fs') +const { NetlifyFunction } = require('../../src/lib/functions/netlify-function') const callCli = require('./utils/call-cli') const cliPath = require('./utils/cli-path') @@ -21,6 +22,20 @@ const { withSiteBuilder } = require('./utils/site-builder') const test = isCI ? avaTest.serial.bind(avaTest) : avaTest +test('should return the correct function url for a NetlifyFunction object', (t) => { + const port = 7331 + const functionName = 'test-function' + + const functionUrl = `http://localhost:${port}/.netlify/functions/${functionName}` + + const ntlFunction = new NetlifyFunction({ + name: functionName, + settings: { functionsPort: port }, + }) + + t.is(ntlFunction.url, functionUrl) +}) + test('should return function response when invoked with no identity argument', async (t) => { await withSiteBuilder('function-invoke-with-no-identity-argument', async (builder) => { builder.withNetlifyToml({ config: { functions: { directory: 'functions' } } }).withFunction({