From 4f6877705fb2094d67e5f299001bfd02fe3290a8 Mon Sep 17 00:00:00 2001 From: Josh Morel Date: Thu, 19 Apr 2018 08:03:34 -0400 Subject: [PATCH 1/6] update README and add tests for body in json file --- README.md | 20 +++++++++ test/mocks/request-json/POST.mock | 6 +++ .../mocks/request-json/POST@payload.json.mock | 6 +++ test/mocks/request-json/not-payload.json | 6 +++ test/mocks/request-json/payload.json | 6 +++ test/mockserver.js | 45 ++++++++++++++++++- 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/mocks/request-json/POST.mock create mode 100644 test/mocks/request-json/POST@payload.json.mock create mode 100644 test/mocks/request-json/not-payload.json create mode 100644 test/mocks/request-json/payload.json diff --git a/README.md b/README.md index a84ef2b..e2eb5cf 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,8 @@ In the same way, if your POST body is a json like `{"json": "yesPlease"}`, mockserver will look for a file called `POST--{"json": "yesPlease"}.mock`. *Warning! This feature is* __NOT compatible with Windows__*. This is because Windows doesn't accept curly brackets as filenames.* +To overcome this limitation or to keep the file names tidy, you can put the body in [a json file](#body-in-json-file). + If no parametrized mock file is found, mockserver will default to the nearest headers based .mock file @@ -204,6 +206,24 @@ Authorization: 12345 ``` if there's no `hello/GET_Authorization=12345--a=b.mock`, we'll default to `hello/GET_Authorization=12345.mock` or to `hello/GET.mock` +## Body in json file + +To support Windows and tidier file naming, the expected body of the request can be saved in a separate `.json` file. If the request contains a body in json format, mockserver will look for that body in json files in the same `$REQUEST-PATH` directory. + +For example, if a POST body is `{"json": "yesPlease"}`, and a file in the path called `payload.json` has the same content (order is important, but spacing between keys/values is not), mockserver will look for a file called `POST@payload.json.mock`. + +The general naming convention is: + +``` +$REQUEST-PATH/$HTTP-METHOD@$JSON-FILENAME.mock +``` + +The precedence for matching requests containing a json body is: + +1) Contained within mock file name +2) Contained within .json file +3) No match - nearest headers based .mock file + ## Wildcard slugs If you want to match against a route with a wildcard - say in the case of an ID or other parameter in the URL, you can diff --git a/test/mocks/request-json/POST.mock b/test/mocks/request-json/POST.mock new file mode 100644 index 0000000..888c1e4 --- /dev/null +++ b/test/mocks/request-json/POST.mock @@ -0,0 +1,6 @@ +HTTP/1.1 404 OK +Content-Type: application/json; charset=utf-8 + +{ + "error": "User not found" +} diff --git a/test/mocks/request-json/POST@payload.json.mock b/test/mocks/request-json/POST@payload.json.mock new file mode 100644 index 0000000..efc30d8 --- /dev/null +++ b/test/mocks/request-json/POST@payload.json.mock @@ -0,0 +1,6 @@ +HTTP/1.1 200 OK +Content-Type: application/json; charset=utf-8 + +{ + "token": "longJWT" +} diff --git a/test/mocks/request-json/not-payload.json b/test/mocks/request-json/not-payload.json new file mode 100644 index 0000000..16308db --- /dev/null +++ b/test/mocks/request-json/not-payload.json @@ -0,0 +1,6 @@ +{ + "user": { + "username": "notTheUser", + "password": "123456" + } +} \ No newline at end of file diff --git a/test/mocks/request-json/payload.json b/test/mocks/request-json/payload.json new file mode 100644 index 0000000..56247ae --- /dev/null +++ b/test/mocks/request-json/payload.json @@ -0,0 +1,6 @@ +{ + "user": { + "username": "theUser", + "password": "123456" + } +} \ No newline at end of file diff --git a/test/mockserver.js b/test/mockserver.js index 4f26451..615162b 100644 --- a/test/mockserver.js +++ b/test/mockserver.js @@ -7,7 +7,7 @@ var res; var req; var mocksDirectory = './test/mocks'; -var verbose = process.env.DEBUG === 'true' || false; +var verbose = process.env.DEBUG === 'true' || true; /** * Processes request @@ -294,6 +294,48 @@ describe('mockserver', function() { }); }); + it('should be able to include POST json body in separate file', function(done) { + var jsonBody = {user: {username: 'theUser', password: '123456'}}; + var req = new MockReq({ + method: 'POST', + url: '/request-json', + headers: { + 'Accept': 'application/json' + } + }); + req.write(jsonBody); + req.end(); + + mockserver(mocksDirectory, verbose)(req, res); + + req.on('end', function() { + assert.deepEqual(JSON.parse(res.body), {token: 'longJWT'}); + assert.equal(res.status, 200); + done(); + }); + }); + + it('should default to POST.mock if json body not found in any files', function(done) { + var jsonBody = {user: {username: 'notFoundUser', password: '123456'}}; + var req = new MockReq({ + method: 'POST', + url: '/request-json', + headers: { + 'Accept': 'application/json' + } + }); + req.write(jsonBody); + req.end(); + + mockserver(mocksDirectory, verbose)(req, res); + + req.on('end', function() { + assert.deepEqual(JSON.parse(res.body), {error: 'User not found'}); + assert.equal(res.status, 404); + done(); + }); + }); + it('Should return 404 when no default .mock files are found', function() { mockserver.headers = ['authorization']; req.headers['authorization'] = 12; @@ -343,6 +385,7 @@ describe('mockserver', function() { assert.equal(res.status, 404); }); + }); }); }); From 8ac889c51bcaa9f17feb81093afb142d78a752d0 Mon Sep 17 00:00:00 2001 From: Josh Morel Date: Sat, 21 Apr 2018 07:35:46 -0400 Subject: [PATCH 2/6] Ability to match request json to file contents --- mockserver.js | 73 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/mockserver.js b/mockserver.js index 29be3b8..034c830 100644 --- a/mockserver.js +++ b/mockserver.js @@ -104,7 +104,7 @@ function getWildcardPath(path) { /** * Returns the body or query string to be used in - * the mock name. + * the mock fileName. * * In any case we will prepend the value with a double * dash so that the mock files will look like: @@ -151,24 +151,73 @@ function getBody(req, callback) { }); } -function getMockedContent(path, prefix, body, query) { - var mockName = prefix + (getBodyOrQueryString(body, query) || '') + '.mock'; - var mockFile = join(mockserver.directory, path, mockName); - var content; +function isJsonString(str) { + if (typeof(str) !== 'string') { + return false; + } + try { + JSON.parse(str); + } catch (err) { + return false; + } + return true; +} - try { - content = fs.readFileSync(mockFile, {encoding: 'utf8'}); +function getMatchingJsonFile(files, fullPath, jsonBody) { + for (var file of files) { + if (file.endsWith('.json')) { + var data = fs.readFileSync(join(fullPath, file), {encoding: 'utf8'}); + try { + if (jsonBody === JSON.stringify(JSON.parse(data))) { + return file; + } + } catch (err) { if (mockserver.verbose) { - console.log('Reading from '+ mockFile.yellow +' file: ' + 'Matched'.green); + console.log('Tried to match json body with ' + file.yellow + '. File has invalid JSON'.red); } - } catch(err) { + } + } + } + return null; +} + +function getMockedContent(path, prefix, body, query) { + var fullPath = join(mockserver.directory, path); + var mockName = prefix + (getBodyOrQueryString(body, query) || '') + '.mock'; + var prefixFallback = prefix + '.mock'; + + try { + var files = fs.readdirSync(fullPath); + + // 1st try to match on body or query within file name + if (files.includes(mockName)) { + if (mockserver.verbose) { + console.log('Reading from '+ mockName.yellow +' file: ' + 'Matched'.green); + } + return fs.readFileSync(join(fullPath, mockName), {encoding: 'utf8'}); + } + + // 2nd (for json body only) try to match on json body within file contents + if (body && isJsonString(body)) { + var matchingJsonFile = getMatchingJsonFile(files, fullPath, body); + if (matchingJsonFile) { + var mockNameFromJson = prefix + '@' + matchingJsonFile + '.mock'; if (mockserver.verbose) { - console.log('Reading from '+ mockFile.yellow +' file: ' + 'Not matched'.red); + console.log('Reading from '+ mockNameFromJson.yellow +' file: ' + 'Matched'.green); } - content = (body || query) && getMockedContent(path, prefix); + return fs.readFileSync(join(fullPath, mockNameFromJson), {encoding: 'utf8'}) + } } - return content; + // 3rd try fallback with only prefix + if (files.includes(prefixFallback)) { + if (mockserver.verbose) { + console.log('Reading from '+ mockName.yellow +' file: ' + 'Not matched'.red); + } + return fs.readFileSync(join(fullPath, prefixFallback), {encoding: 'utf8'}); + } + } catch (err) {} + return null; } function getContentFromPermutations(path, method, body, query, permutations) { From 85b679f6679ec34db56297f217e5859d3ec1b98d Mon Sep 17 00:00:00 2001 From: Josh Morel Date: Sun, 22 Apr 2018 07:55:04 -0400 Subject: [PATCH 3/6] support node < 6 substitute Array.includes with Array.indexOf --- mockserver.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mockserver.js b/mockserver.js index 034c830..da8fc6a 100644 --- a/mockserver.js +++ b/mockserver.js @@ -190,7 +190,7 @@ function getMockedContent(path, prefix, body, query) { var files = fs.readdirSync(fullPath); // 1st try to match on body or query within file name - if (files.includes(mockName)) { + if (files.indexOf(mockName) !== -1) { if (mockserver.verbose) { console.log('Reading from '+ mockName.yellow +' file: ' + 'Matched'.green); } @@ -210,7 +210,7 @@ function getMockedContent(path, prefix, body, query) { } // 3rd try fallback with only prefix - if (files.includes(prefixFallback)) { + if (files.indexOf(prefixFallback) !== -1) { if (mockserver.verbose) { console.log('Reading from '+ mockName.yellow +' file: ' + 'Not matched'.red); } From 8a8487ff0636d5c3ff18ec874232d374704c1280 Mon Sep 17 00:00:00 2001 From: Matthew_Pfister Date: Fri, 6 Dec 2019 12:49:32 -0600 Subject: [PATCH 4/6] Adding vscode config to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0f37c36..2d5f796 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules test.js .nyc_output *.code-workspace +.vscode/ From 3a224a47c1f9121bd3a03bb4c098c850f3864ce3 Mon Sep 17 00:00:00 2001 From: Matthew_Pfister Date: Fri, 6 Dec 2019 13:19:29 -0600 Subject: [PATCH 5/6] Upading previous PR to allow for POST with dynamic body. Using format POST@[filename].mock --- mockserver.js | 133 ++++++++++++++++---------------------------------- 1 file changed, 42 insertions(+), 91 deletions(-) diff --git a/mockserver.js b/mockserver.js index 287654b..0a9563c 100644 --- a/mockserver.js +++ b/mockserver.js @@ -25,15 +25,14 @@ function parseStatus(header) { * Parses an HTTP header, splitting * by colon. */ -const parseHeader = function (header, context, request) { +const parseHeader = function(header, context, request) { header = header.split(': '); return { key: normalizeHeader(header[0]), value: parseValue(header[1], context, request) }; }; const parseValue = function(value, context, request) { - return Monad - .of(value) + return Monad.of(value) .map((value) => importHandler(value, context, request)) .map((value) => headerHandler(value, request)) .map((value) => evalHandler(value, request)) @@ -45,8 +44,7 @@ const parseValue = function(value, context, request) { * Priority exports over ENV definition. */ const prepareWatchedHeaders = function() { - const exportHeaders = - module.exports.headers && module.exports.headers.toString(); + const exportHeaders = module.exports.headers && module.exports.headers.toString(); const headers = (exportHeaders || process.env.MOCK_HEADERS || '').split(','); return headers.filter(function(item, pos, self) { @@ -65,7 +63,7 @@ const addHeader = function(headers, line) { } else { headers[key] = value; } -} +}; /** * Parser the content of a mockfile @@ -78,14 +76,12 @@ const parse = function(content, file, request) { let body; const bodyContent = []; content = content.split(/\r?\n/); - const status = Monad - .of(content[0]) + const status = Monad.of(content[0]) .map((value) => importHandler(value, context, request)) .map((value) => evalHandler(value, context, request)) .map(parseStatus) .join(); - let headerEnd = false; delete content[0]; @@ -103,9 +99,7 @@ const parse = function(content, file, request) { } }); - - body = Monad - .of(bodyContent.join('\n')) + body = Monad.of(bodyContent.join('\n')) .map((value) => importHandler(value, context, request)) .map((value) => evalHandler(value, context, request)) .join(); @@ -119,7 +113,6 @@ function removeBlanks(array) { }); } - /** * This method will look for a header named Response-Delay. When set it * delay the response in that number of milliseconds simulating latency @@ -157,7 +150,7 @@ function getWildcardPath(dir) { } const res = getDirectoriesRecursive(mockserver.directory) - .filter(dir => { + .filter((dir) => { const directories = dir.split(path.sep); return directories.includes('__'); }) @@ -170,7 +163,7 @@ function getWildcardPath(dir) { // Order from longest file path to shortest. return aLength > bLength ? -1 : 1; }) - .map(dir => { + .map((dir) => { const steps = dir.split(path.sep); const baseDir = mockserver.directory.split(path.sep); steps.splice(0, baseDir.length); @@ -211,8 +204,7 @@ function matchWildcardPath(steps, dirSteps) { function flattenDeep(directories) { return directories.reduce( - (acc, val) => - Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), + (acc, val) => (Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val)), [] ); } @@ -220,14 +212,12 @@ function flattenDeep(directories) { function getDirectories(srcpath) { return fs .readdirSync(srcpath) - .map(file => path.join(srcpath, file)) - .filter(path => fs.statSync(path).isDirectory()); + .map((file) => path.join(srcpath, file)) + .filter((path) => fs.statSync(path).isDirectory()); } function getDirectoriesRecursive(srcpath) { - const nestedDirectories = getDirectories(srcpath).map( - getDirectoriesRecursive - ); + const nestedDirectories = getDirectories(srcpath).map(getDirectoriesRecursive); const directories = flattenDeep(nestedDirectories); directories.push(srcpath); return directories; @@ -235,13 +225,8 @@ function getDirectoriesRecursive(srcpath) { /** * Returns the body or query string to be used in -<<<<<<< HEAD - * the mock name. - * -======= * the mock fileName. - * ->>>>>>> 85b679f6679ec34db56297f217e5859d3ec1b98d + * * In any case we will prepend the value with a double * dash so that the mock files will look like: * @@ -287,21 +272,8 @@ function getBody(req, callback) { }); } -<<<<<<< HEAD -function getMockedContent(path, prefix, body, query) { - const mockName = prefix + (getBodyOrQueryString(body, query) || '') + '.mock'; - const mockFile = join(mockserver.directory, path, mockName); - let content; - - try { - content = fs.readFileSync(mockFile, { encoding: 'utf8' }); - if (mockserver.verbose) { - console.log( - 'Reading from ' + mockFile.yellow + ' file: ' + 'Matched'.green - ); -======= function isJsonString(str) { - if (typeof(str) !== 'string') { + if (typeof str !== 'string') { return false; } try { @@ -315,14 +287,17 @@ function isJsonString(str) { function getMatchingJsonFile(files, fullPath, jsonBody) { for (var file of files) { if (file.endsWith('.json')) { - var data = fs.readFileSync(join(fullPath, file), {encoding: 'utf8'}); + var data = fs.readFileSync(join(fullPath, file), { encoding: 'utf8' }); + try { if (jsonBody === JSON.stringify(JSON.parse(data))) { return file; } } catch (err) { if (mockserver.verbose) { - console.log('Tried to match json body with ' + file.yellow + '. File has invalid JSON'.red); + console.log( + 'Tried to match json body with ' + file.yellow + '. File has invalid JSON'.red + ); } } } @@ -332,7 +307,7 @@ function getMatchingJsonFile(files, fullPath, jsonBody) { function getMockedContent(path, prefix, body, query) { var fullPath = join(mockserver.directory, path); - var mockName = prefix + (getBodyOrQueryString(body, query) || '') + '.mock'; + var mockName = prefix + (getBodyOrQueryString(body, query) || '') + '.mock'; var prefixFallback = prefix + '.mock'; try { @@ -341,45 +316,40 @@ function getMockedContent(path, prefix, body, query) { // 1st try to match on body or query within file name if (files.indexOf(mockName) !== -1) { if (mockserver.verbose) { - console.log('Reading from '+ mockName.yellow +' file: ' + 'Matched'.green); + console.log('Reading from ' + mockName.yellow + ' file: ' + 'Matched'.green); } - return fs.readFileSync(join(fullPath, mockName), {encoding: 'utf8'}); + return fs.readFileSync(join(fullPath, mockName), { encoding: 'utf8' }); } // 2nd (for json body only) try to match on json body within file contents if (body && isJsonString(body)) { var matchingJsonFile = getMatchingJsonFile(files, fullPath, body); + if (matchingJsonFile) { - var mockNameFromJson = prefix + '@' + matchingJsonFile + '.mock'; + var fileWithoutExtension = matchingJsonFile.replace('.json', ''); + var mockNameFromJson = prefix + '@' + fileWithoutExtension + '.mock'; if (mockserver.verbose) { - console.log('Reading from '+ mockNameFromJson.yellow +' file: ' + 'Matched'.green); + console.log('Reading from ' + mockNameFromJson.yellow + ' file: ' + 'Matched'.green); } - return fs.readFileSync(join(fullPath, mockNameFromJson), {encoding: 'utf8'}) + return fs.readFileSync(join(fullPath, mockNameFromJson), { encoding: 'utf8' }); } ->>>>>>> 85b679f6679ec34db56297f217e5859d3ec1b98d } - } catch (err) { - if (mockserver.verbose) { - console.log( - 'Reading from ' + mockFile.yellow + ' file: ' + 'Not matched'.red - ); - } - content = (body || query) && getMockedContent(path, prefix); - } -<<<<<<< HEAD - return content; -======= // 3rd try fallback with only prefix if (files.indexOf(prefixFallback) !== -1) { if (mockserver.verbose) { - console.log('Reading from '+ mockName.yellow +' file: ' + 'Not matched'.red); + console.log('Reading from ' + mockName.yellow + ' file: ' + 'Not matched'.red); } - return fs.readFileSync(join(fullPath, prefixFallback), {encoding: 'utf8'}); + return fs.readFileSync(join(fullPath, prefixFallback), { encoding: 'utf8' }); } - } catch (err) {} + } catch (err) { + if (mockserver.verbose) { + console.log('Reading from ' + mockName.yellow + ' file: ' + 'Not matched'.red); + } + content = (body || query) && getMockedContent(path, prefix); + } + return null; ->>>>>>> 85b679f6679ec34db56297f217e5859d3ec1b98d } function getContentFromPermutations(path, method, body, query, permutations) { @@ -409,8 +379,7 @@ const mockserver = { let path = url; const queryIndex = url.indexOf('?'), - query = - queryIndex >= 0 ? url.substring(queryIndex).replace(/\?/g, '') : '', + query = queryIndex >= 0 ? url.substring(queryIndex).replace(/\?/g, '') : '', method = req.method.toUpperCase(), headers = []; @@ -422,9 +391,7 @@ const mockserver = { mockserver.headers.forEach(function(header) { header = header.toLowerCase(); if (req.headers[header]) { - headers.push( - '_' + normalizeHeader(header) + '=' + req.headers[header] - ); + headers.push('_' + normalizeHeader(header) + '=' + req.headers[header]); } }); } @@ -443,30 +410,14 @@ const mockserver = { permutations.push([]); } - matched = getContentFromPermutations( - path, - method, - body, - query, - permutations.slice(0) - ); + matched = getContentFromPermutations(path, method, body, query, permutations.slice(0)); if (!matched.content && (path = getWildcardPath(path))) { - matched = getContentFromPermutations( - path, - method, - body, - query, - permutations.slice(0) - ); + matched = getContentFromPermutations(path, method, body, query, permutations.slice(0)); } if (matched.content) { - const mock = parse( - matched.content, - join(mockserver.directory, path, matched.prefix), - req - ); + const mock = parse(matched.content, join(mockserver.directory, path, matched.prefix), req); const delay = getResponseDelay(mock.headers); Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay); res.writeHead(mock.status, mock.headers); @@ -476,7 +427,7 @@ const mockserver = { res.end('Not Mocked'); } }); - }, + } }; module.exports = function(directory, silent) { From 1f60f922dadf9d2fedc388785889d97d9d907ec7 Mon Sep 17 00:00:00 2001 From: Matthew_Pfister Date: Fri, 6 Dec 2019 13:19:46 -0600 Subject: [PATCH 6/6] cleaning up tests --- .../multi-level-url-2}/GET.mock | 0 .../{test => query-params}/GET--a=b.mock | 0 test/mocks/query-params/GET.mock | 4 + ...ST@payload.json.mock => POST@payload.mock} | 0 .../mocks/{test => response-default}/GET.mock | 0 test/mockserver.js | 141 ++++++------------ 6 files changed, 51 insertions(+), 94 deletions(-) rename test/mocks/{test1/test2 => multi-level-url/multi-level-url-2}/GET.mock (100%) rename test/mocks/{test => query-params}/GET--a=b.mock (100%) create mode 100644 test/mocks/query-params/GET.mock rename test/mocks/request-json/{POST@payload.json.mock => POST@payload.mock} (100%) rename test/mocks/{test => response-default}/GET.mock (100%) diff --git a/test/mocks/test1/test2/GET.mock b/test/mocks/multi-level-url/multi-level-url-2/GET.mock similarity index 100% rename from test/mocks/test1/test2/GET.mock rename to test/mocks/multi-level-url/multi-level-url-2/GET.mock diff --git a/test/mocks/test/GET--a=b.mock b/test/mocks/query-params/GET--a=b.mock similarity index 100% rename from test/mocks/test/GET--a=b.mock rename to test/mocks/query-params/GET--a=b.mock diff --git a/test/mocks/query-params/GET.mock b/test/mocks/query-params/GET.mock new file mode 100644 index 0000000..eeb4636 --- /dev/null +++ b/test/mocks/query-params/GET.mock @@ -0,0 +1,4 @@ +HTTP/1.1 200 OK +Content-Type: text + +Passed! diff --git a/test/mocks/request-json/POST@payload.json.mock b/test/mocks/request-json/POST@payload.mock similarity index 100% rename from test/mocks/request-json/POST@payload.json.mock rename to test/mocks/request-json/POST@payload.mock diff --git a/test/mocks/test/GET.mock b/test/mocks/response-default/GET.mock similarity index 100% rename from test/mocks/test/GET.mock rename to test/mocks/response-default/GET.mock diff --git a/test/mockserver.js b/test/mockserver.js index b93c494..1ee4bec 100644 --- a/test/mockserver.js +++ b/test/mockserver.js @@ -2,17 +2,12 @@ const MockReq = require('mock-req'); const assert = require('assert'); const mockserver = require('./../mockserver'); const path = require('path'); -const Monad = require('../monad'); let res; let req; const mocksDirectory = path.join('.', 'test', 'mocks'); -<<<<<<< HEAD -const verbose = process.env.DEBUG === 'true' || false; -======= var verbose = process.env.DEBUG === 'true' || true; ->>>>>>> 85b679f6679ec34db56297f217e5859d3ec1b98d /** * Processes request @@ -78,7 +73,7 @@ describe('mockserver', function() { describe('mockserver()', function() { it('should return a valid response', function() { - processRequest('/test', 'GET'); + processRequest('/response-default', 'GET'); assert.equal(res.body, 'Welcome!'); assert.equal(res.status, 200); @@ -93,7 +88,7 @@ describe('mockserver', function() { }); it('should be able to handle trailing slashes without changing the name of the mockfile', function() { - processRequest('/test/', 'GET'); + processRequest('/response-default/', 'GET'); assert.equal(res.status, 200); assert.equal(res.body, 'Welcome!'); @@ -142,19 +137,19 @@ describe('mockserver', function() { }); it('should be able to map multi-level urls', function() { - processRequest('/test1/test2', 'GET'); + processRequest('/multi-level-url/multi-level-url-2', 'GET'); assert.equal(res.body, 'multi-level url'); }); it('should be able to handle GET parameters', function() { - processRequest('/test?a=b', 'GET'); + processRequest('/query-params?a=b', 'GET'); assert.equal(res.status, 200); }); it('should default to GET.mock if no matching parameter file is found', function() { - processRequest('/test?a=c', 'GET'); + processRequest('/query-params?a=c', 'GET'); assert.equal(res.status, 200); }); @@ -322,60 +317,52 @@ describe('mockserver', function() { assert.equal(res.body, JSON.stringify({ foo: 'bar' }, null, 4)); }); -<<<<<<< HEAD - it('should be able to handle eval', function() { - processRequest('/eval', 'GET'); -======= - it('should be able to include POST json body in separate file', function(done) { - var jsonBody = {user: {username: 'theUser', password: '123456'}}; - var req = new MockReq({ - method: 'POST', - url: '/request-json', - headers: { - 'Accept': 'application/json' - } - }); - req.write(jsonBody); - req.end(); - - mockserver(mocksDirectory, verbose)(req, res); + it('should be able to include POST json body in separate file', function(done) { + var jsonBody = {user: {username: 'theUser', password: '123456'}}; + var req = new MockReq({ + method: 'POST', + url: '/request-json', + headers: { + 'Accept': 'application/json' + } + }); + req.write(jsonBody); + req.end(); - req.on('end', function() { - assert.deepEqual(JSON.parse(res.body), {token: 'longJWT'}); - assert.equal(res.status, 200); - done(); - }); - }); + mockserver(mocksDirectory, verbose)(req, res); - it('should default to POST.mock if json body not found in any files', function(done) { - var jsonBody = {user: {username: 'notFoundUser', password: '123456'}}; - var req = new MockReq({ - method: 'POST', - url: '/request-json', - headers: { - 'Accept': 'application/json' - } - }); - req.write(jsonBody); - req.end(); + req.on('end', function() { + assert.deepEqual(JSON.parse(res.body), {token: 'longJWT'}); + assert.equal(res.status, 200); + done(); + }); + }); - mockserver(mocksDirectory, verbose)(req, res); + it('should default to POST.mock if json body not found in any files', function(done) { + var jsonBody = {user: {username: 'notFoundUser', password: '123456'}}; + var req = new MockReq({ + method: 'POST', + url: '/request-json', + headers: { + 'Accept': 'application/json' + } + }); + req.write(jsonBody); + req.end(); - req.on('end', function() { - assert.deepEqual(JSON.parse(res.body), {error: 'User not found'}); - assert.equal(res.status, 404); - done(); - }); - }); + mockserver(mocksDirectory, verbose)(req, res); - it('Should return 404 when no default .mock files are found', function() { - mockserver.headers = ['authorization']; - req.headers['authorization'] = 12; - processRequest('/return-200?a=c', 'GET'); ->>>>>>> 85b679f6679ec34db56297f217e5859d3ec1b98d + req.on('end', function() { + assert.deepEqual(JSON.parse(res.body), {error: 'User not found'}); + assert.equal(res.status, 404); + done(); + }); + }); - assert.equal(res.status, 200); - assert.deepEqual(JSON.parse(res.body), { foo: 'bar' }); + it('Should return 404 when no default .mock files are found', function() { + mockserver.headers = ['authorization']; + req.headers['authorization'] = 12; + processRequest('/return-200?a=c', 'GET'); }); it('should be able to handle imports with content around import', function() { @@ -484,6 +471,7 @@ describe('mockserver', function() { assert.equal(res.status, 404); }); }); + describe('.getResponseDelay', function() { it('should return a value greater than zero when valid', function() { const ownValueHeaders = [ @@ -545,43 +533,8 @@ describe('mockserver', function() { assert.equal(res.status, '200'); done(); }); -<<<<<<< HEAD - }); - }) - }); -}); - -describe('Monad methods', function() { - let m; - function fn(val) { - return { - ...val, - b: 2 - }; - } - const testData = { a: 1 }; - const expectData = { a: 1, b: 2 }; - beforeEach(function() { - m = Monad.of(testData); - }); - - it('Monad have static method `of`', function() { - assert.equal(typeof Monad.of, 'function'); - }); - it('Monad method `of` should return Object type Monad', function() { - assert.equal(m instanceof Monad, true); - }); - it('Monad method `map` should recive value and return Object type Monad', function() { - assert.equal(m.map(fn) instanceof Monad, true); - }); - it('Monad method `join` should return value', function () { - assert.strictEqual(m.join(), testData); - }); - it('Monad method `chain` should return value', function () { - assert.deepEqual(m.chain(fn), expectData); -======= }); ->>>>>>> 85b679f6679ec34db56297f217e5859d3ec1b98d }); + }); });