Skip to content

Commit

Permalink
refactor: call now from Date namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Nov 1, 2022
1 parent f472d89 commit 5342bec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/events/http/createJWTAuthScheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { log } from '@serverless/utils/log.js'
import { decodeJwt } from 'jose'

const { isArray } = Array
const { now } = Date

export default function createAuthScheme(jwtOptions) {
const authorizerName = jwtOptions.name
Expand Down Expand Up @@ -38,7 +39,7 @@ export default function createAuthScheme(jwtOptions) {
const claims = decodeJwt(jwtToken)

const expirationDate = new Date(claims.exp * 1000)
if (expirationDate.valueOf() < Date.now()) {
if (expirationDate.valueOf() < now()) {
return Boom.unauthorized('JWT Token expired')
}

Expand Down
10 changes: 7 additions & 3 deletions tests/manual/websocket/RouteSelection/test/e2e/ws.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const WebSocketTester = require('../support/WebSocketTester.js')
const endpoint = env.npm_config_endpoint || 'ws://localhost:3005'
const timeout = env.npm_config_timeout ? +env.npm_config_timeout : 1000

const { now } = Date
const { stringify } = JSON

describe('serverless', () => {
Expand Down Expand Up @@ -53,12 +54,15 @@ describe('serverless', () => {

it("should call action 'echo' handler located at service.do", async () => {
const ws = await createWebSocket()
const now = `${Date.now()}`
const payload = stringify({ message: now, service: { do: 'echo' } })
const timestamp = `${now()}`
const payload = stringify({
message: timestamp,
service: { do: 'echo' },
})

ws.send(payload)

expect(await ws.receive1()).to.equal(`${now}`)
expect(await ws.receive1()).to.equal(`${timestamp}`)
}).timeout(timeout)
})
})
39 changes: 20 additions & 19 deletions tests/manual/websocket/main/test/e2e/ws.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const timeout = env.npm_config_timeout
const WebSocketTester = require('../support/WebSocketTester.js')

const { expect } = chai
const { now } = Date
const { parse, stringify } = JSON
const { keys } = Object

Expand Down Expand Up @@ -99,11 +100,11 @@ describe('serverless', () => {
.replace('wss://', 'https://')}`,
)
.keepOpen()
let res = await request.get(`/${Date.now()}`) // .set('Authorization', user.accessToken);
let res = await request.get(`/${now()}`) // .set('Authorization', user.accessToken);

expect(res).to.have.status(426)

res = await request.get(`/${Date.now()}/${Date.now()}`) // .set('Authorization', user.accessToken);
res = await request.get(`/${now()}/${now()}`) // .set('Authorization', user.accessToken);

expect(res).to.have.status(426)
}).timeout(timeout)
Expand All @@ -127,7 +128,7 @@ describe('serverless', () => {

it('should call default handler when no such action exists', async () => {
const ws = await createWebSocket()
const payload = stringify({ action: `action${Date.now()}` })
const payload = stringify({ action: `action${now()}` })
ws.send(payload)

expect(await ws.receive1()).to.equal(
Expand Down Expand Up @@ -408,7 +409,7 @@ describe('serverless', () => {
// connect
const c = await createClient()
const connect = parse(await ws.receive1())
let now = Date.now()
let timestamp = now()
let expectedCallInfo = {
context: createExpectedContext(connect.info.context),
event: {
Expand Down Expand Up @@ -438,12 +439,12 @@ describe('serverless', () => {
connect.info.event.requestContext.requestTimeEpoch + 10,
)
expect(connect.info.event.requestContext.connectedAt).to.be.within(
now - timeout,
now,
timestamp - timeout,
timestamp,
)
expect(connect.info.event.requestContext.requestTimeEpoch).to.be.within(
now - timeout,
now,
timestamp - timeout,
timestamp,
)
expect(
moment
Expand All @@ -453,7 +454,7 @@ describe('serverless', () => {
)
.toDate()
.getTime(),
).to.be.within(now - timeout, now)
).to.be.within(timestamp - timeout, timestamp)

if (endpoint.startsWith('ws://locahost')) {
expect(connect.info.event.headers['X-Forwarded-For']).to.be.equal(
Expand All @@ -464,7 +465,7 @@ describe('serverless', () => {
// getCallInfo
c.ws.send(stringify({ action: 'getCallInfo' }))
const callInfo = parse(await c.ws.receive1())
now = Date.now()
timestamp = now()
expectedCallInfo = {
context: createExpectedContext(callInfo.info.context),
event: {
Expand All @@ -489,12 +490,12 @@ describe('serverless', () => {
callInfo.info.event.requestContext.requestTimeEpoch,
)
expect(callInfo.info.event.requestContext.connectedAt).to.be.within(
now - timeout,
now,
timestamp - timeout,
timestamp,
)
expect(callInfo.info.event.requestContext.requestTimeEpoch).to.be.within(
now - timeout,
now,
timestamp - timeout,
timestamp,
)
expect(
moment
Expand All @@ -504,12 +505,12 @@ describe('serverless', () => {
)
.toDate()
.getTime(),
).to.be.within(now - timeout, now)
).to.be.within(timestamp - timeout, timestamp)

// disconnect
c.ws.close()
const disconnect = parse(await ws.receive1())
now = Date.now()
timestamp = now()
expectedCallInfo = {
context: createExpectedContext(disconnect.info.context),
event: {
Expand Down Expand Up @@ -538,21 +539,21 @@ describe('serverless', () => {
}).timeout(timeout)

it('should be able to parse query string', async () => {
const now = `${Date.now()}`
const timestamp = `${now()}`
const ws = await createWebSocket()
ws.send(stringify({ action: 'registerListener' }))
await ws.receive1()

await createClient()
await createClient(`now=${now}&before=123456789`)
await createClient(`now=${timestamp}&before=123456789`)

expect(parse(await ws.receive1()).info.event.queryStringParameters).to.be
.undefined
expect(
parse(await ws.receive1()).info.event.queryStringParameters,
).to.deep.equal({
before: '123456789',
now,
timestamp,
})
}).timeout(timeout)

Expand Down

0 comments on commit 5342bec

Please sign in to comment.