-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateResponse.spec.ts
More file actions
48 lines (46 loc) · 1.37 KB
/
validateResponse.spec.ts
File metadata and controls
48 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import middy from '@middy/core'
import { HttpStatusCode } from '@nrfcloud/problem-detail'
import { Type } from '@sinclair/typebox'
import type { Context } from 'aws-lambda'
import assert from 'node:assert'
import { describe, it } from 'node:test'
import { aResponse } from './aResponse.ts'
import {
ResponseValidationFailedError,
validateResponse,
} from './validateResponse.ts'
void describe('validateResponse()', () => {
void it('should validate the response', async () =>
assert.deepEqual(
await middy()
.use(validateResponse(Type.Object({ value: Type.Boolean() })))
.handler(async () =>
aResponse(HttpStatusCode.OK, {
'@context': new URL('https://example.com'),
value: true,
}),
)('Some event', {} as Context),
{
body: '{"@context":"https://example.com/","value":true}',
headers: {
'Cache-Control': 'public, max-age=60',
'content-length': '48',
'content-type': 'application/json',
},
statusCode: 200,
},
))
void it('should throw an Error in case the response is invalid', async () =>
assert.rejects(
async () =>
middy()
.use(validateResponse(Type.Object({ value: Type.Boolean() })))
.handler(async () =>
aResponse(HttpStatusCode.OK, {
'@context': new URL('https://example.com'),
value: 42,
}),
)('Some event', {} as Context),
ResponseValidationFailedError,
))
})