Skip to content
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
2 changes: 2 additions & 0 deletions src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { EXIT_CODES } from '../utils/exit-codes.js'
import type { FeatureFlags } from '../utils/feature-flags.js'
import { getFrameworksAPIPaths } from '../utils/frameworks-api.js'
import { getSiteByName } from '../utils/get-site.js'
import { getLocalApiTestOpts } from '../utils/local-api-test-opts.js'
import { buildAuthorizeUrl } from '../utils/login-url.js'
import openBrowser from '../utils/open-browser.js'
import { isInteractive } from '../utils/scripted-commands.js'
Expand Down Expand Up @@ -845,6 +846,7 @@ export default class BaseCommand extends Command {
host: host,
pathPrefix: pathPrefix,
scheme: scheme,
testOpts: getLocalApiTestOpts({ host, scheme }),
offline: offline ?? flags.offline,
siteFeatureFlagPrefix: 'cli',
featureFlags: this.featureFlags,
Expand Down
20 changes: 20 additions & 0 deletions src/utils/local-api-test-opts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const LOOPBACK_HOSTNAMES = new Set(['localhost', '127.0.0.1', '[::1]'])

export const getLocalApiTestOpts = ({
host,
scheme,
}: {
host?: string
scheme?: string
}): { host: string; scheme?: string } | undefined => {
if (host === undefined) {
return undefined
}

try {
const { hostname } = new URL(`http://${host}`)
return LOOPBACK_HOSTNAMES.has(hostname) ? { host, scheme } : undefined
} catch {
return undefined
}
}
4 changes: 1 addition & 3 deletions tests/integration/commands/dev/v2-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ describe.concurrent('v2 api', async () => {
expect(context).toHaveProperty('ip', expect.stringMatching(/::1|127\.0\.0\.1/))
expect(context).toHaveProperty('geo.city', 'Mock City')
expect(context).toHaveProperty('cookies', { foo: 'bar' })
// TODO: re-enable once the extensions API is mocked. The real one rejects this test's fake token,
// so config resolution falls back to offline and the account is unknown.
// expect(context).toHaveProperty('account.id', 'mock-account-id')
expect(context).toHaveProperty('account.id', 'mock-account-id')
expect(context).toHaveProperty('deploy.context', 'dev')
expect(context).toHaveProperty('deploy.id', '0')
expect(context).toHaveProperty('deploy.published', false)
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/utils/mock-api-vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ export const startMockApi = ({ routes, silent }: MockApiOptions): Promise<MockAp
)
})

app.get('/site/:siteId/integrations/safe', function onRequest(req, res) {
addRequest(requests, req)
res.status(200)
res.json([])
})

app.get('/team/:accountId/integrations/installations/meta/:siteId', function onRequest(req, res) {
addRequest(requests, req)
res.status(200)
res.json([])
})

app.all('{*splat}', function onRequest(req, res) {
addRequest(requests, req)
if (!silent) {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/utils/mock-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const startMockApi = ({ routes, silent }: MockApiOptions): Promise<MockAp
)
})

app.get('/site/site_id/integrations/safe', function onRequest(req, res) {
app.get('/site/:siteId/integrations/safe', function onRequest(req, res) {
if (process.env.DEBUG_TESTS) {
console.debug('[mock-api] ', req.method, req.path, req.body)
}
Expand All @@ -81,7 +81,7 @@ export const startMockApi = ({ routes, silent }: MockApiOptions): Promise<MockAp
res.json([])
})

app.get('/team/account_id/integrations/installations/meta/site_id', function onRequest(req, res) {
app.get('/team/:accountId/integrations/installations/meta/:siteId', function onRequest(req, res) {
if (process.env.DEBUG_TESTS) {
console.debug('[mock-api] ', req.method, req.path, req.body)
}
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/utils/local-api-test-opts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, test } from 'vitest'

import { getLocalApiTestOpts } from '../../../src/utils/local-api-test-opts.js'

describe('getLocalApiTestOpts', () => {
test('returns host and scheme for localhost', () => {
expect(getLocalApiTestOpts({ host: 'localhost:1234', scheme: 'http' })).toEqual({
host: 'localhost:1234',
scheme: 'http',
})
})

test('returns host and scheme for 127.0.0.1', () => {
expect(getLocalApiTestOpts({ host: '127.0.0.1:1234', scheme: 'http' })).toEqual({
host: '127.0.0.1:1234',
scheme: 'http',
})
})

test('returns host and scheme for [::1]', () => {
expect(getLocalApiTestOpts({ host: '[::1]:1234', scheme: 'http' })).toEqual({
host: '[::1]:1234',
scheme: 'http',
})
})

test.each(['api.netlify.com', 'api-staging.netlify.com', 'example.com:8080', 'localhost.example.com'])(
'returns undefined for non-loopback host %s',
(host) => {
expect(getLocalApiTestOpts({ host, scheme: 'https' })).toBeUndefined()
},
)

test('returns undefined when host is undefined', () => {
expect(getLocalApiTestOpts({ host: undefined, scheme: 'http' })).toBeUndefined()
})

test('returns undefined when host cannot be parsed', () => {
expect(getLocalApiTestOpts({ host: 'not a host', scheme: 'http' })).toBeUndefined()
})
})
Loading