Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass correct protocol to functions server #4604

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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