-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e.spec.ts
66 lines (61 loc) · 1.9 KB
/
e2e.spec.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { describe, it } from 'node:test'
import { listRequests } from './src/requests.js'
import { registerResponse } from './src/responses.js'
const { responsesTableName, apiURL, requestsTableName } = JSON.parse(
await readFile(path.join(process.cwd(), 'http-api-mock.json'), 'utf-8'),
)
const db = new DynamoDBClient({})
void describe('end-to-end tests', () => {
void it('should respond with 404 if no response is configured', async () => {
const res = await fetch(new URL('/prod/foo', apiURL), {
method: 'POST',
body: JSON.stringify({ some: 'data' }),
headers: {
'content-type': 'application/json; charset=utf-8',
},
})
assert.equal(res.ok, false)
assert.equal(res.status, 404)
})
void it('should store all requests', async () => {
const pathSegment = crypto.randomUUID()
await fetch(new URL(`/prod/${pathSegment}`, apiURL))
const request = (await listRequests(db, requestsTableName)).find(
({ path }) => path === pathSegment,
)
assert.notEqual(request, undefined)
})
void it('should return a configured response', async () => {
const pathSegment = crypto.randomUUID()
await registerResponse(db, responsesTableName, {
method: 'PUT',
path: pathSegment,
queryParams: new URLSearchParams({
foo: 'bar',
}),
body: [
`Content-Type: application/json`,
'',
JSON.stringify({ success: true }),
].join('\n'),
statusCode: 201,
})
const res = await fetch(
new URL(
`/prod/${pathSegment}?${new URLSearchParams({ foo: 'bar' }).toString()}`,
apiURL,
),
{
method: 'PUT',
},
)
assert.equal(res.ok, true)
assert.equal(res.status, 201)
assert.equal(res.headers.get('Content-Type'), 'application/json')
assert.deepEqual(await res.json(), { success: true })
})
})