Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1047 from AlexHayton/patch-1
Handle undefined environment variables in resolveJoins
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/endToEnd/environmentVariables/environmentVariables.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { resolve } from 'path' | ||
import fetch from 'node-fetch' | ||
import { | ||
joinUrl, | ||
setup, | ||
teardown, | ||
} from '../../integration/_testHelpers/index.js' | ||
|
||
jest.setTimeout(30000) | ||
|
||
describe('environment variables', () => { | ||
const ENV_VAR_QUOTED = 'I am ENV_VAR_1' | ||
const ENV_VAR_UNQUOTED = 'I am ENV_VAR_2' | ||
const ENV_VAR_MAPPED = 'I am ENV_VAR_3' | ||
|
||
// init | ||
let json | ||
beforeAll(async () => { | ||
process.env.ENV_VAR_QUOTED = ENV_VAR_QUOTED | ||
process.env.ENV_VAR_UNQUOTED = ENV_VAR_UNQUOTED | ||
process.env.ENV_VAR_MAPPED_FROM_ANOTHER = ENV_VAR_MAPPED | ||
await setup({ | ||
servicePath: resolve(__dirname), | ||
}) | ||
const url = joinUrl(TEST_BASE_URL, '/dev/hello') | ||
const response = await fetch(url) | ||
json = await response.json() | ||
}) | ||
|
||
// cleanup | ||
afterAll(async () => { | ||
process.env.ENV_VAR_QUOTED = undefined | ||
process.env.ENV_VAR_UNQUOTED = undefined | ||
process.env.ENV_VAR_MAPPED_FROM_ANOTHER = undefined | ||
await teardown() | ||
}) | ||
|
||
test('it should handle a quoted environment variable', async () => { | ||
expect(json).toMatchObject({ | ||
ENV_VAR_QUOTED, | ||
}) | ||
}) | ||
|
||
test('it should handle an unquoted environment variable', async () => { | ||
expect(json).toMatchObject({ | ||
ENV_VAR_UNQUOTED, | ||
}) | ||
}) | ||
|
||
test('it should handle a mapped environment variable', async () => { | ||
expect(json).toMatchObject({ | ||
ENV_VAR_MAPPED, | ||
}) | ||
}) | ||
|
||
test('it should handle an undefined quoted environment variable', async () => { | ||
expect(json).toHaveProperty('ENV_VAR_EMPTY_STRING', undefined) | ||
}) | ||
|
||
test('it should handle an undefined unquoted environment variable', async () => { | ||
expect(json).toHaveProperty('ENV_VAR_UNDEFINED', undefined) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
exports.hello = async () => { | ||
const { | ||
ENV_VAR_QUOTED, | ||
ENV_VAR_UNQUOTED, | ||
ENV_VAR_MAPPED, | ||
ENV_VAR_EMPTY_STRING, | ||
ENV_VAR_UNDEFINED, | ||
} = process.env | ||
|
||
const body = JSON.stringify({ | ||
ENV_VAR_QUOTED, | ||
ENV_VAR_UNQUOTED, | ||
ENV_VAR_MAPPED, | ||
ENV_VAR_EMPTY_STRING, // This should be undefined | ||
ENV_VAR_UNDEFINED, // This should be undefined | ||
}) | ||
|
||
return { | ||
statusCode: 200, | ||
body, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
service: uncategorized-tests | ||
|
||
plugins: | ||
- ../../../ | ||
|
||
provider: | ||
memorySize: 128 | ||
name: aws | ||
region: us-east-1 # default | ||
runtime: nodejs12.x | ||
stage: dev | ||
versionFunctions: false | ||
environment: | ||
ENV_VAR_QUOTED: "${env:ENV_VAR_QUOTED}" | ||
ENV_VAR_UNQUOTED: ${env:ENV_VAR_UNQUOTED} | ||
ENV_VAR_MAPPED: "${env:ENV_VAR_MAPPED_FROM_ANOTHER}" | ||
ENV_VAR_EMPTY_STRING: "${env:ENV_VAR_EMPTY_STRING}" | ||
ENV_VAR_UNDEFINED: ${env:ENV_VAR_UNDEFINED} | ||
|
||
functions: | ||
hello: | ||
handler: handler.hello | ||
events: | ||
- http: | ||
method: get | ||
path: /hello |