Skip to content

Commit

Permalink
test: further test coverage improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
refactorthis committed Apr 18, 2024
1 parent 79640b3 commit 51f00fc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
3 changes: 2 additions & 1 deletion packages/api/src/middleware/api-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ export default <TResponseStruct, TEvent>(opts?: FuncyApiOptions) => {
.use(httpJsonBodyParserMiddleware({ disableContentTypeError: true }))
.use(httpMultipartBodyParserMiddleware({ disableContentTypeError: true }))
.use(httpUrlencodeBodyParserMiddleware({ disableContentTypeError: true }))
.use(httpSecurityHeadersMiddleware(opts?.http?.security))
.use(httpContentEncodingMiddleware(opts?.http?.encoding))
.use(httpResponseSerializerMiddleware(opts?.http?.content?.response))
.use(warmupMiddleware(opts?.function?.warmup))

if (opts?.http?.security) pipe.use(httpSecurityHeadersMiddleware(opts?.http?.security))

if (opts?.monitoring?.logLevel === 'debug')
pipe.use(inputOutputLoggerMiddleware({ logger: logger.debug }))

Expand Down
79 changes: 49 additions & 30 deletions packages/api/test/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,86 @@ import { ctx } from './data/lambda-context'
import { logger } from './mocks/logger.mock'

describe('pipeline', () => {
it('should enable cloudwatch metrics if specified', () => {
it('should enable security headers if specified', async () => {
const event: any = { ...events.payloadV2 }
const fn = api({
monitoring: {
cloudWatchMetrics: {
namespace: 'my-namespace',
},
},
handler: vi.fn<any>(({ context }) => {
expect(context.metrics).toBeDefined()
return res.ok()
}),
const disabled = api({
handler: () => res.ok(),
})

const r1 = await disabled(event, ctx())
expect(r1.headers?.['X-Powered-By']).toBeUndefined()

const enabled = api({
http: { security: { poweredBy: { server: 'myserver' } } },
handler: () => res.ok(),
})

fn(event, ctx())
const r2 = await enabled(event, ctx())
expect(r2.headers?.['X-Powered-By']).toBe('myserver')
})

it('should enable full input/output logging if debug mode', async () => {
const event: any = { ...events.payloadV2 }
const log = logger()
const fn = api({
monitoring: {
logger: () => log,
logLevel: 'debug',
},
const disabled = api({
monitoring: { logger: () => log, logLevel: 'info' },
handler: () => res.ok(),
})

await disabled({ ...events.payloadV2 } as any, ctx())
expect(log.debug).not.toHaveBeenCalled()

const enabled = api({
monitoring: { logger: () => log, logLevel: 'debug' },
handler: () => res.ok(),
})

await fn(event, ctx())
await enabled({ ...events.payloadV2 } as any, ctx())
expect(log.debug).toHaveBeenCalled()
})

// TODO Need to mock the embedded metrics provider
it.skip('should enable cloudwatch metrics if specified', async () => {
const disabled = api({
handler: ({ context }) => res.ok({ metrics: !!context.metrics }),
})

const r1 = await disabled({ ...events.payloadV2 } as any, ctx())
expect(r1?.body?.metrics).toBeUndefined()

const enabled = api({
monitoring: { cloudWatchMetrics: { namespace: 'my-namespace' } },
handler: ({ context }) => {
return res.ok({ metrics: !!context.metrics })
},
})

const r2 = await enabled({ ...events.payloadV2 } as any, ctx())
expect(r2?.body?.metrics).toBeDefined()
})

it('should add extra middleware to the pipeline if specified', async () => {
const event: any = { ...events.payloadV2 }
const middleware = { before: vi.fn() }
const fn = api({
function: {
middleware: [middleware],
},
function: { middleware: [middleware] },
handler: () => res.ok(),
})

await fn(event, ctx())
await fn({ ...events.payloadV2 } as any, ctx())
expect(middleware.before).toHaveBeenCalled()
})

it('should enable CORS middleware if specified', async () => {
const event: any = { ...events.payloadV2 }
const disabled = api({
handler: () => res.ok(),
})
const disabled = api({ handler: () => res.ok() })

const r1 = await disabled(event, ctx())
const r1 = await disabled({ ...events.payloadV2 } as any, ctx())
expect(r1.headers?.['Access-Control-Allow-Origin']).toBeUndefined()

const enabled = api({
http: { cors: { origin: 'web.mysite.com' } },
handler: () => res.ok(),
})

const r2 = await enabled(event, ctx())
const r2 = await enabled({ ...events.payloadV2 } as any, ctx())
expect(r2.headers?.['Access-Control-Allow-Origin']).toBe('web.mysite.com')
})
})
1 change: 0 additions & 1 deletion packages/api/test/profiling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('Monitoring -> Profiling', () => {
it('should not profile if not enabled', async () => {
const fn = api({ handler: () => res.ok() })
const response = await fn(sampleEvent as any, ctx())
console.log(response)
expect(response.statusCode).toBe(200)
})

Expand Down

0 comments on commit 51f00fc

Please sign in to comment.