-
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.
- Loading branch information
1 parent
653b871
commit 69e4f7e
Showing
9 changed files
with
180 additions
and
148 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules/ | ||
npm-debug.log | ||
dist/ | ||
cdk.out/ | ||
cdk.out/ | ||
http-api-mock.json |
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,66 @@ | ||
import { listRequests } from './src/requests.js' | ||
import { registerResponse } from './src/responses.js' | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import { DynamoDBClient } from '@aws-sdk/client-dynamodb' | ||
import { readFile } from 'node:fs/promises' | ||
import path from 'node:path' | ||
|
||
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: { | ||
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 }) | ||
}) | ||
}) |
Oops, something went wrong.