Skip to content

Commit

Permalink
fix(http-function-runtime-v3): fix undeclared variable and added impr…
Browse files Browse the repository at this point in the history
…ove tests for set-cookie (#534)
  • Loading branch information
H4ad committed Jul 26, 2022
1 parent 2764c09 commit 34bd7da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions __tests__/integration.js
Expand Up @@ -395,7 +395,16 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
})

test('set-cookie', async () => {
const now = new Date(2022, 7, 11, 3, 30, 30)
const maxAge = 3000
const expires = new Date(+now + maxAge)
const expectedExpires = expires.toUTCString()

jest.useFakeTimers('modern')
jest.setSystemTime(now)

router.get('/cookie', (req, res) => {
res.cookie('Zoo', 'boo', { domain: 'mafoo.com', secure: true, httpOnly: true, sameSite: 'Strict', maxAge })
res.cookie('Foo', 'bar', { domain: 'example.com', secure: true, httpOnly: true, sameSite: 'Strict' })
res.cookie('Fizz', 'buzz')
res.json({})
Expand All @@ -408,6 +417,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
const response = await serverlessExpressInstance(event)

const expectedSetCookieHeaders = [
`Zoo=boo; Max-Age=3; Domain=mafoo.com; Path=/; Expires=${expectedExpires}; HttpOnly; Secure; SameSite=Strict`,
'Foo=bar; Domain=example.com; Path=/; HttpOnly; Secure; SameSite=Strict',
'Fizz=buzz; Path=/'
]
Expand All @@ -423,10 +433,23 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
statusCode: 200
})

jest.useRealTimers()

switch (eventSourceName) {
case 'azureHttpFunctionV4':
case 'azureHttpFunctionV3':
expectedResponse.cookies = [
{
domain: 'mafoo.com',
httpOnly: true,
name: 'Zoo',
path: '/',
sameSite: 'Strict',
secure: true,
value: 'boo',
maxAge: maxAge / 1000,
expires
},
{
domain: 'example.com',
httpOnly: true,
Expand Down
4 changes: 2 additions & 2 deletions src/event-sources/azure/http-function-runtime-v3.js
Expand Up @@ -64,7 +64,7 @@ function getResponseToHttpFunction ({ statusCode, body, headers = {}, isBase64En
}

if (parsedCookie['max-age']) {
cookie.maxAge = parsedCookie['max-age']
cookie.maxAge = +parsedCookie['max-age']
}

if (parsedCookie.samesite) {
Expand All @@ -73,7 +73,7 @@ function getResponseToHttpFunction ({ statusCode, body, headers = {}, isBase64En

if (parsedCookie.expires && typeof parsedCookie.expires === 'string') {
cookie.expires = new Date(parsedCookie.expires)
} else if (parsedCookie.expires && typeof value === 'number') {
} else if (parsedCookie.expires && typeof parsedCookie.expires === 'number') {
cookie.expires = parsedCookie.expires
}

Expand Down

0 comments on commit 34bd7da

Please sign in to comment.