Skip to content

Commit a4c9dbb

Browse files
fix: pass correct protocol to functions server (#4604)
* fix: pass correct protocol to functions server * refactor: safely get config property
1 parent b6d76e8 commit a4c9dbb

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/lib/functions/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @ts-check
2+
const { get } = require('dot-prop')
23
const jwtDecode = require('jwt-decode')
34

45
const {
@@ -89,7 +90,8 @@ const createHandler = function (options) {
8990
{},
9091
)
9192
const rawQuery = new URLSearchParams(request.query).toString()
92-
const url = new URL(requestPath, `${request.protocol}://${request.get('host') || 'localhost'}`)
93+
const protocol = get(options, 'config.dev.https') ? 'https' : 'http'
94+
const url = new URL(requestPath, `${protocol}://${request.get('host') || 'localhost'}`)
9395
url.search = rawQuery
9496
const rawUrl = url.toString()
9597
const event = {

tests/integration/200.command.dev.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ export const handler = async function () {
213213
})
214214
.withFunction({
215215
path: 'hello.js',
216-
handler: async () => ({
216+
handler: async (event) => ({
217217
statusCode: 200,
218-
body: 'Hello World',
218+
body: JSON.stringify({ rawUrl: event.rawUrl }),
219219
}),
220220
})
221221
.withEdgeFunction({
@@ -242,7 +242,9 @@ export const handler = async function () {
242242
const options = { https: { rejectUnauthorized: false } }
243243
t.is(await got(`https://localhost:${port}`, options).text(), 'index')
244244
t.is(await got(`https://localhost:${port}?ef=true`, options).text(), 'INDEX')
245-
t.is(await got(`https://localhost:${port}/api/hello`, options).text(), 'Hello World')
245+
t.deepEqual(await got(`https://localhost:${port}/api/hello`, options).json(), {
246+
rawUrl: `https://localhost:${port}/api/hello`,
247+
})
246248
})
247249
})
248250
})

tests/integration/300.command.dev.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,20 @@ test('should serve function from a subdirectory', async (t) => {
131131
await withSiteBuilder('site-with-from-subdirectory', async (builder) => {
132132
builder.withNetlifyToml({ config: { functions: { directory: 'functions' } } }).withFunction({
133133
path: path.join('echo', 'echo.js'),
134-
handler: async () => ({
134+
handler: async (event) => ({
135135
statusCode: 200,
136-
body: 'ping',
136+
body: JSON.stringify({ rawUrl: event.rawUrl }),
137137
metadata: { builder_function: true },
138138
}),
139139
})
140140

141141
await builder.buildAsync()
142142

143143
await withDevServer({ cwd: builder.directory }, async (server) => {
144-
const response = await got(`${server.url}/.netlify/functions/echo`).text()
145-
t.is(response, 'ping')
146-
const builderResponse = await got(`${server.url}/.netlify/builders/echo`).text()
147-
t.is(builderResponse, 'ping')
144+
const response = await got(`${server.url}/.netlify/functions/echo`).json()
145+
t.deepEqual(response, { rawUrl: `${server.url}/.netlify/functions/echo` })
146+
const builderResponse = await got(`${server.url}/.netlify/builders/echo`).json()
147+
t.deepEqual(builderResponse, { rawUrl: `${server.url}/.netlify/builders/echo` })
148148
})
149149
})
150150
})

0 commit comments

Comments
 (0)