|
| 1 | +import { HttpStatusCode } from '@hello.nrfcloud.com/proto/hello' |
1 | 2 | import middy from '@middy/core'
|
2 | 3 | import { Type } from '@sinclair/typebox'
|
3 | 4 | import type { Context } from 'aws-lambda'
|
4 | 5 | import assert from 'node:assert'
|
5 | 6 | import { describe, it } from 'node:test'
|
| 7 | +import { aResponse } from './aResponse.js' |
6 | 8 | import {
|
7 | 9 | ResponseValidationFailedError,
|
8 | 10 | validateResponse,
|
9 | 11 | } from './validateResponse.js'
|
10 | 12 |
|
11 | 13 | void describe('validateResponse()', () => {
|
12 | 14 | void it('should validate the response', async () =>
|
13 |
| - assert.equal( |
| 15 | + assert.deepEqual( |
14 | 16 | await middy()
|
15 |
| - .use(validateResponse(Type.Boolean({ title: 'A boolean' }))) |
16 |
| - .handler(async () => true)('Some event', {} as Context), |
17 |
| - true, |
| 17 | + .use(validateResponse(Type.Object({ value: Type.Boolean() }))) |
| 18 | + .handler(async () => |
| 19 | + aResponse(HttpStatusCode.OK, { |
| 20 | + '@context': new URL('https://example.com'), |
| 21 | + value: true, |
| 22 | + }), |
| 23 | + )('Some event', {} as Context), |
| 24 | + { |
| 25 | + body: '{"@context":"https://example.com/","value":true}', |
| 26 | + headers: { |
| 27 | + 'Cache-Control': 'public, max-age=60', |
| 28 | + 'content-length': '48', |
| 29 | + 'content-type': 'application/json', |
| 30 | + }, |
| 31 | + statusCode: 200, |
| 32 | + }, |
18 | 33 | ))
|
19 | 34 |
|
20 | 35 | void it('should throw an Error in case the response is invalid', async () =>
|
21 | 36 | assert.rejects(
|
22 | 37 | async () =>
|
23 | 38 | middy()
|
24 |
| - .use(validateResponse(Type.Boolean())) |
25 |
| - .handler(async () => 42)('Some event', {} as Context), |
| 39 | + .use(validateResponse(Type.Object({ value: Type.Boolean() }))) |
| 40 | + .handler(async () => |
| 41 | + aResponse(HttpStatusCode.OK, { |
| 42 | + '@context': new URL('https://example.com'), |
| 43 | + value: 42, |
| 44 | + }), |
| 45 | + )('Some event', {} as Context), |
26 | 46 | ResponseValidationFailedError,
|
27 | 47 | ))
|
28 | 48 | })
|
0 commit comments