@zemke/http-mock
Advanced tools
Comparing version
54
index.js
const http = require('http'); | ||
const fs = require('fs'); | ||
module.exports = function (port, pathToMocks) { | ||
const mocks = []; | ||
const urlMatchesPattern = (url, pattern) => | ||
typeof pattern[0] !== 'string' ? url.match(pattern) != null : url === pattern; | ||
/** | ||
* Start the server and get returned methods to add mocks. | ||
* | ||
* @param port The port to start the server | ||
* @returns {{add: (function(*, *): number), clear: (function(): *[])}} | ||
*/ | ||
module.exports = (port) => { | ||
http.createServer((req, res) => { | ||
@@ -9,16 +20,41 @@ req.on('error', err => console.error(err.stack)); | ||
const file = `${pathToMocks}/${req.url.substr(1).replace(/\//g, '_')}.json`; | ||
if (req.url === '/favicon.ico') { | ||
res.statusCode = 204; | ||
const matchingMocks = mocks.filter(m => urlMatchesPattern(req.url, m[0])); | ||
if (matchingMocks.length > 1) { | ||
console.warn("More than one pattern matches the URL:"); | ||
matchingMocks.forEach(matchingMocks, idx => console.warn(`${idx + 1}. ${matchingMocks[0]}`)); | ||
console.warn("This results in a 400 HTTP Bad Request"); | ||
res.statusCode = 400; | ||
res.end(); | ||
} else if (!fs.existsSync(file)) { | ||
console.warn(`${file} does not exist.`); | ||
} else if (matchingMocks.length === 0) { | ||
console.warn(`No pattern matches URL of ${req.url}`); | ||
console.warn("This results in a 404 HTTP Not Found"); | ||
res.statusCode = 404; | ||
res.end(); | ||
} else { | ||
console.log(`Serving ${file}`); | ||
res.setHeader('Content-Type', 'application/json'); | ||
fs.createReadStream(file).pipe(res); | ||
const mock = matchingMocks[0][1]; | ||
if (mock.trim().startsWith("{")) { | ||
console.log(`Serving inline mock for ${req.url}`); | ||
res.end(mock); | ||
} else { | ||
if (!fs.existsSync(mock)) { | ||
console.warn(`${mock} does not exist.`); | ||
console.warn("This results in a 500 HTTP Internal Server Error"); | ||
res.statusCode = 500; | ||
res.end(); | ||
} else { | ||
console.log(`Serving mock file ${mock} for ${req.url}`); | ||
fs.createReadStream(mock).pipe(res); | ||
} | ||
} | ||
} | ||
}).listen(port); | ||
} | ||
return { | ||
add: (stringOrRegExpUrlMatcher, jsonOrFilePath) => mocks.push([stringOrRegExpUrlMatcher, jsonOrFilePath]), | ||
clear: () => mocks.splice(0, mocks.length), | ||
}; | ||
}; | ||
{ | ||
"name": "@zemke/http-mock", | ||
"version": "1.0.3", | ||
"version": "2.0.1", | ||
"description": "Serve JSON mocks conveniently.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,10 +8,17 @@ # `http-mock` | ||
```js | ||
require('@zemke/http-mock')(3333, __dirname + '/mocks'); | ||
// Start server on port 3333. | ||
const httpMock = require('@zemke/http-mock')(3333); | ||
// When http://localhost:3333/api/tournamnet is requested | ||
// respond with contents of the JSON file under the given path. | ||
httpMock.add('/api/tournament', __dirname + '/mocks/api_tournament.json'); | ||
``` | ||
Starts a server on port `3333` with JSON files to serve located in the current directory under `mocks/`. | ||
If you create a file in `mocks/` named `api_users_1_details.json` it will be served when `http://localhost:3333/api/users/1/details` is requested. | ||
## API | ||
## Appendix | ||
### `add(urlMatcher: string | RegExp, mock: string)` | ||
You might also roll your own version of this. Peek into `index.js`—it’s kids’ stuff. | ||
`urlMatcher` — May be a string to match the exact path or a regular expression. | ||
`mock` — A path to a JSON file or inline stringified JSON. | ||
3327
79.74%51
142.86%24
41.18%