@zemke/http-mock
Advanced tools
Comparing version 2.0.2 to 2.0.3
24
index.js
@@ -23,10 +23,8 @@ const http = require('http'); | ||
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"); | ||
console.warn("HTTP400 - More than one pattern matches the URL:"); | ||
matchingMocks.forEach((matchingMock, idx) => console.warn(` ${idx + 1}. ${matchingMock[0]}`)); | ||
res.statusCode = 400; | ||
res.end(); | ||
} else if (matchingMocks.length === 0) { | ||
console.warn(`No pattern matches URL of ${req.url}`); | ||
console.warn("This results in a 404 HTTP Not Found"); | ||
console.warn(`HTTP404 - No pattern matches URL of ${req.url}`); | ||
res.statusCode = 404; | ||
@@ -38,9 +36,8 @@ res.end(); | ||
if (mock.trim().startsWith("{")) { | ||
try { | ||
res.end(JSON.parse(JSON.stringify(mock))); | ||
console.log(`Serving inline mock for ${req.url}`); | ||
res.end(mock); | ||
} else { | ||
} catch (e) { | ||
if (!fs.existsSync(mock)) { | ||
console.warn(`${mock} does not exist.`); | ||
console.warn("This results in a 500 HTTP Internal Server Error"); | ||
console.warn(`HTTP500 - ${mock} does not exist.`); | ||
res.statusCode = 500; | ||
@@ -57,3 +54,8 @@ res.end(); | ||
return { | ||
add: (stringOrRegExpUrlMatcher, jsonOrFilePath) => mocks.push([stringOrRegExpUrlMatcher, jsonOrFilePath]), | ||
add: (stringOrRegExpUrlMatcher, jsonOrFilePath) => { | ||
const indexOfUrlPattern = mocks.findIndex(mock => mock[0].toString() === stringOrRegExpUrlMatcher.toString()); | ||
indexOfUrlPattern === -1 | ||
? mocks.push([stringOrRegExpUrlMatcher, jsonOrFilePath]) | ||
: (mocks[indexOfUrlPattern][1] = jsonOrFilePath); | ||
}, | ||
clear: () => mocks.splice(0, mocks.length), | ||
@@ -60,0 +62,0 @@ }; |
{ | ||
"name": "@zemke/http-mock", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Serve JSON mocks conveniently.", | ||
@@ -18,3 +18,4 @@ "main": "index.js", | ||
"protractor", | ||
"backend" | ||
"backend", | ||
"rest" | ||
], | ||
@@ -21,0 +22,0 @@ "author": "Zemke", |
@@ -20,2 +20,4 @@ # `http-mock` | ||
Add a new mock for the given URL pattern. Performs a replace, if the URL pattern already exists. | ||
`urlMatcher` — May be a string to match the exact path or a regular expression. | ||
@@ -25,1 +27,4 @@ | ||
### `clean()` | ||
Remove all mocks. |
3541
53
29