Skip to content

Commit

Permalink
fix: pass correct protocol to functions server (#4604)
Browse files Browse the repository at this point in the history
* fix: pass correct protocol to functions server

* refactor: safely get config property
  • Loading branch information
eduardoboucas authored May 12, 2022
1 parent b6d76e8 commit a4c9dbb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/lib/functions/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
const { get } = require('dot-prop')
const jwtDecode = require('jwt-decode')

const {
Expand Down Expand Up @@ -89,7 +90,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 = get(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 = {
Expand Down
8 changes: 5 additions & 3 deletions tests/integration/200.command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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`,
})
})
})
})
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/300.command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,20 @@ 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 },
}),
})

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` })
})
})
})
Expand Down

1 comment on commit a4c9dbb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 286 MB

Please sign in to comment.