Skip to content

Commit

Permalink
test: don't destroy lambda env vars until response stream finished
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jun 17, 2024
1 parent c6a8699 commit 6d66d8f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
22 changes: 20 additions & 2 deletions tests/utils/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,24 @@ export async function invokeFunction(
NETLIFY_BLOBS_CONTEXT: createBlobContext(ctx),
...(env || {}),
}

const envVarsToRestore = {}

// We are not using lambda-local's environment variable setting because it cleans up
// environment vars to early (before stream is closed)
Object.keys(environment).forEach(function (key) {
if (typeof process.env[key] !== 'undefined') {
envVarsToRestore[key] = process.env[key]
}
process.env[key] = environment[key]
})

const response = (await execute({
event: {
headers: headers || {},
httpMethod: httpMethod || 'GET',
rawUrl: new URL(url || '/', 'https://example.netlify').href,
},
environment,
envdestroy: true,
lambdaFunc: { handler },
timeoutMs: 4_000,
})) as LambdaResponse
Expand All @@ -386,6 +396,14 @@ export async function invokeFunction(

const bodyBuffer = await streamToBuffer(response.body)

Object.keys(environment).forEach(function (key) {
if (typeof envVarsToRestore[key] !== 'undefined') {
process.env[key] = envVarsToRestore[key]
} else {
delete process.env[key]
}
})

return {
statusCode: response.statusCode,
bodyBuffer,
Expand Down
19 changes: 17 additions & 2 deletions tests/utils/sandbox-child.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ process.on('message', async (msg) => {
...(env || {}),
}

// We are not using lambda-local's environment variable setting because it cleans up
// environment vars to early (before stream is closed)
Object.keys(environment).forEach(function (key) {
if (typeof process.env[key] !== 'undefined') {
envVarsToRestore[key] = process.env[key]
}
process.env[key] = environment[key]
})

const response = await execute({
event: {
headers: headers || {},
httpMethod: httpMethod || 'GET',
rawUrl: new URL(url || '/', 'https://example.netlify').href,
},
environment,
envdestroy: true,
lambdaFunc: { handler },
timeoutMs: 4_000,
})
Expand All @@ -70,6 +77,14 @@ process.on('message', async (msg) => {

const bodyBuffer = await streamToBuffer(response.body)

Object.keys(environment).forEach(function (key) {
if (typeof envVarsToRestore[key] !== 'undefined') {
process.env[key] = envVarsToRestore[key]
} else {
delete process.env[key]
}
})

const result = {
statusCode: response.statusCode,
bodyBuffer,
Expand Down

0 comments on commit 6d66d8f

Please sign in to comment.