Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -44,7 +47,7 @@ fetch('http://localhost:3001/___fixtures', {
id: 1
}
}
}
})
})
```

Expand Down
6 changes: 5 additions & 1 deletion bin/dynamock.js
Original file line number Diff line number Diff line change
@@ -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 <port>')
}

const server = createServer()

server.listen(port, host, () => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,7 +32,6 @@
},
"dependencies": {
"@hapi/joi": "17.1.1",
"body-parser": "1.20.3",
"cookie-parser": "1.4.7",
"express": "4.21.1"
},
Expand Down
9 changes: 4 additions & 5 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/fixtures.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/properties.js
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
94 changes: 94 additions & 0 deletions test/bin.js
Original file line number Diff line number Diff line change
@@ -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)
},
)
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading