-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added profiler - added tests for profiler as well as handlers - changed trace to debug in logger - removed void response from handler, since it made handler responses harder to test / work with resolves: #6
- Loading branch information
1 parent
99f0a80
commit 999376e
Showing
10 changed files
with
115 additions
and
20 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
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
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
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
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
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,65 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { api } from '../src/api' | ||
import * as events from './data/api-proxy-events' | ||
import { ctx } from './data/lambda-context' | ||
|
||
describe('Handlers', () => { | ||
it('should support an anonymous JSON body and stringify', async () => { | ||
const event: any = { | ||
...events.payloadV2, | ||
body: JSON.stringify({ | ||
hi: 'test', | ||
}), | ||
} | ||
|
||
const fn = api({ | ||
handler: () => ({ | ||
statusCode: 200, | ||
body: { | ||
hi: 'hello', | ||
}, | ||
}), | ||
}) | ||
|
||
const response = await fn(event, ctx()) | ||
expect(response.body).toBe('{"hi":"hello"}') | ||
expect(response.statusCode).toBe(200) | ||
}) | ||
|
||
it('should support an undefined body response', async () => { | ||
const event: any = { | ||
...events.payloadV2, | ||
body: JSON.stringify({ | ||
hi: 'test', | ||
}), | ||
} | ||
|
||
const fn = api({ | ||
handler: () => ({ statusCode: 200 }), | ||
}) | ||
const response = await fn(event as any, ctx()) | ||
expect(response.statusCode).toBe(200) | ||
}) | ||
|
||
it('should support a null body response', async () => { | ||
const event: any = { | ||
...events.payloadV2, | ||
body: JSON.stringify({ | ||
hi: 'test', | ||
}), | ||
} | ||
|
||
const fn = api({ | ||
handler: () => ({ statusCode: 200, body: null }), | ||
}) | ||
const response = await fn(event as any, ctx()) | ||
expect(response.statusCode).toBe(200) | ||
}) | ||
|
||
// supporting void makes the interface harder to use, so skipping this. | ||
// it.skip('should support a void response', async () => { | ||
// const fn = api({ handler: () => {} }) | ||
// const response = await fn(events.payloadV2 as any, ctx()) | ||
// expect(response.statusCode).toBe(200) | ||
// }) | ||
}) |
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,33 @@ | ||
import { describe, it, expect, vi } from 'vitest' | ||
import { api } from '../src/api' | ||
import { res } from '../src/res' | ||
import * as events from './data/api-proxy-events' | ||
import { ctx } from './data/lambda-context' | ||
import { Logger } from '@funcy/core' | ||
|
||
const sampleEvent = { | ||
...events.payloadV2, | ||
headers: {}, | ||
body: undefined, | ||
} | ||
|
||
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) | ||
}) | ||
|
||
it('should output profile information if enabled', async () => { | ||
const logger = { debug: vi.fn(), error: vi.fn(), info: vi.fn() } as unknown as Logger | ||
const fn = api({ | ||
monitoring: { logger: () => logger, enableProfiling: true }, | ||
handler: () => res.ok(), | ||
}) | ||
const response = await fn(sampleEvent as any, ctx()) | ||
expect(response.statusCode).toBe(200) | ||
expect(logger.info).toHaveBeenCalledWith('[Funcy] Profiling Enabled') | ||
expect(logger.debug).toHaveBeenCalled() | ||
}) | ||
}) |
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
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
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