From 62d8b34e0584cf7575177eb61313c61071c2009f Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 16 Oct 2024 18:20:02 +0200 Subject: [PATCH] fix: import with extension, add bin test, remove body-parser --- .github/workflows/ci.yml | 1 + README.md | 7 ++- bin/dynamock.js | 6 ++- package.json | 4 +- src/createServer.js | 9 ++-- src/fixtures.js | 2 +- src/properties.js | 2 +- test/bin.js | 94 ++++++++++++++++++++++++++++++++++++++++ yarn.lock | 1 - 9 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 test/bin.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 32f7d9e..429725e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,6 +17,7 @@ jobs: - run: yarn install - run: yarn format - run: yarn lint + - run: yarn test:bin - run: yarn test --coverage - run: yarn check-git - run: chmod -R 777 ./coverage diff --git a/README.md b/README.md index 77074e8..298096e 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,10 @@ dynamock 3001 ```js fetch('http://localhost:3001/___fixtures', { method: 'POST', - body: { + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ request: { method: 'GET', path: '/products/1' @@ -44,7 +47,7 @@ fetch('http://localhost:3001/___fixtures', { id: 1 } } - } + }) }) ``` diff --git a/bin/dynamock.js b/bin/dynamock.js index 570ca4e..e5611a2 100755 --- a/bin/dynamock.js +++ b/bin/dynamock.js @@ -1,9 +1,13 @@ #!/usr/bin/env node -const createServer = require('../src/createServer') +import { createServer } from "../src/createServer.js" const [, , port, host = '127.0.0.1'] = process.argv +if (!port) { + throw new Error('Missing argument ') +} + const server = createServer() server.listen(port, host, () => { diff --git a/package.json b/package.json index a08e881..5fcea7d 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "format": "npx @biomejs/biome format --write src", "lint": "npx @biomejs/biome lint --write src", "release": "semantic-release", - "test": "NODE_OPTIONS='--enable-source-maps --experimental-vm-modules' jest src --coverage" + "test": "NODE_OPTIONS='--enable-source-maps --experimental-vm-modules' jest src --coverage", + "test:bin": "node test/bin.js" }, "engines": { "node": ">=20" @@ -31,7 +32,6 @@ }, "dependencies": { "@hapi/joi": "17.1.1", - "body-parser": "1.20.3", "cookie-parser": "1.4.7", "express": "4.21.1" }, diff --git a/src/createServer.js b/src/createServer.js index 95de9e2..446c8ea 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,9 +1,8 @@ import express from 'express' -import bodyParser from 'body-parser' import cookieParser from 'cookie-parser' -import { REQUEST_PROPERTIES, requestPropertyMatch, RESPONSE_PROPERTIES, useResponseProperties } from './properties' -import { getFixtureIterator, registerFixture, removeFixture, removeFixtures, validateFixture } from './fixtures' -import { createConfiguration, updateConfiguration, validateConfiguration } from './configuration' +import { REQUEST_PROPERTIES, requestPropertyMatch, RESPONSE_PROPERTIES, useResponseProperties } from './properties.js' +import { getFixtureIterator, registerFixture, removeFixture, removeFixtures, validateFixture } from './fixtures.js' +import { createConfiguration, updateConfiguration, validateConfiguration } from './configuration.js' import { createServer as createHTTPServer } from 'node:http' function resError(res, status, message) { @@ -27,7 +26,7 @@ export function createServer() { 'Access-Control-Allow-Headers': '*', } - app.use(bodyParser.json({ limit: '10mb' })) + app.use(express.json({ limit: '10mb' })) app.use(cookieParser()) let configuration = createConfiguration() diff --git a/src/fixtures.js b/src/fixtures.js index f8aa6fc..c344307 100644 --- a/src/fixtures.js +++ b/src/fixtures.js @@ -1,4 +1,4 @@ -import { hash, isObjectEmpty, sortObjectKeysRecurs } from './utils' +import { hash, isObjectEmpty, sortObjectKeysRecurs } from './utils.js' import querystring from 'node:querystring' // TODO: remove Joi with a lightweight composable validation library import Joi from '@hapi/joi' diff --git a/src/properties.js b/src/properties.js index 0a10d5a..64a42f4 100644 --- a/src/properties.js +++ b/src/properties.js @@ -1,5 +1,5 @@ import { deepStrictEqual } from 'node:assert' -import { isIncluded, matchRegex } from './utils' +import { isIncluded, matchRegex } from './utils.js' export const REQUEST_PROPERTIES = ['headers', 'body', 'query', 'cookies'] export const RESPONSE_PROPERTIES = ['headers', 'cookies', 'filepath', 'body'] diff --git a/test/bin.js b/test/bin.js new file mode 100644 index 0000000..83fb14a --- /dev/null +++ b/test/bin.js @@ -0,0 +1,94 @@ +import { spawn } from 'node:child_process'; +import { createServer } from 'node:net'; +import assert from "node:assert"; + +const isPortUsed = async (port) => { + return new Promise((resolve) => { + const server = createServer(); + server.once('error', (err) => { + server.close(); + resolve(err["code"] === "EADDRINUSE"); + }); + server.once('listening', () => { + server.close(); + resolve(false); + }); + server.listen(port); + }); +} + +function waitPortUsed (port, retry = 1000) { + return new Promise(async (resolve) => { + do { + await new Promise(resolve => setTimeout(resolve, retry)) + console.log(`Waiting for port ${port}...`) + } while (!await isPortUsed(port)) + resolve() + }) +} + +async function test (port) { + let response = await fetch(`http://localhost:${port}/___fixtures`, { + method: `POST`, + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + request: { + method: `GET`, + path: `/pandas/1` + }, + response: { + body: { + id: 1 + } + } + }) + }) + + assert.strictEqual(response.status, 201) + let result = await response.json() + assert.deepStrictEqual(result, { id: `db7d555f28bce97730a0589add9e2ba42b395a74` }) + + response = await fetch(`http://localhost:5000/pandas/1`, { + method: `GET`, + }) + + assert.strictEqual(response.status, 200) + result = await response.json() + assert.deepStrictEqual(result, { id: 1 }) +} + +async function main() { + const PORT = 5000 + const process = spawn(`dynamock`, [PORT], { stdio: 'inherit' }) + + const kill = () => { + process.kill('SIGHUP'); + } + + process.on('SIGTERM', kill) + process.on('SIGINT', kill) + process.on('SIGQUIT', kill) + + try { + await waitPortUsed(PORT) + await test(PORT) + kill() + } catch (error) { + kill() + throw error + } +} + +main().then( + () => { + console.log("Tests succeeded") + process.exit(0) + }, + (error) => { + console.error("Tests failed.") + console.error(error) + process.exit(1) + }, +) \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index cccc6ba..27c423a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3280,7 +3280,6 @@ __metadata: "@semantic-release/github": "npm:^11.0.0" "@semantic-release/npm": "npm:^12.0.1" "@semantic-release/release-notes-generator": "npm:^14.0.1" - body-parser: "npm:1.20.3" cookie-parser: "npm:1.4.7" coveralls: "npm:^3.1.1" express: "npm:4.21.1"