Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend support of wildcard directories #47

Merged
@@ -229,6 +229,13 @@ GET /users/1000
GET /users/__/GET.mock
```

ex:
```
GET /users/1000/detail
GET /users/__/detail/GET.mock
```

## Custom imports
Say you have some json you want to use in your unit tests, and also serve as the body of the call. You can use this import syntax:

@@ -86,22 +86,71 @@ function removeBlanks(array) {
return array.filter(function (i) { return i; });
}

function getWildcardPath(path) {
var steps = removeBlanks(path.split('/')),
function getWildcardPath(dir) {
var steps = removeBlanks(dir.split('/')),
testPath,
newPath,
exists = false;

while(steps.length && !newPath) {
while(steps.length) {
steps.pop();
testPath = join(steps.join('/'), '/__');
exists = fs.existsSync(join(mockserver.directory, testPath));
if(exists) { newPath = testPath; }
}

var res = getDirectoriesRecursive(mockserver.directory).filter(dir => {
var directories = dir.split(path.sep)
return directories.indexOf('__') >= 0
}).sort((a, b) => {
var aLength = a.split(path.sep)
var bLength = b.split(path.sep)

if (aLength == bLength)
return 0

// Order from longest file path to shortest.
return aLength > bLength ? -1 : 1
}).map(dir => {
var steps = dir.split(path.sep)
var baseDir = mockserver.directory.split(path.sep)
steps.splice(0, baseDir.length)
return steps.join(path.sep)
})

steps = removeBlanks(dir.split('/'))
var length = steps.length
for (var index = 0; index < length; index++) {
var dupeSteps = removeBlanks(dir.split('/'))
dupeSteps[index] = '__'
var testPath = dupeSteps.join(path.sep)
var matchFound = res.indexOf(testPath) >= 0;
if (matchFound) {
newPath = testPath
break
}
}

return newPath;
}

function flattenDeep(directories){
return directories.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
}

function getDirectories(srcpath) {
return fs.readdirSync(srcpath)
.map(file => path.join(srcpath, file))
.filter(path => fs.statSync(path).isDirectory());
}

function getDirectoriesRecursive(srcpath) {
var nestedDirectories = getDirectories(srcpath).map(getDirectoriesRecursive);
var directories = flattenDeep(nestedDirectories);
directories.push(srcpath)
return directories;
}

/**
* Returns the body or query string to be used in
* the mock name.
@@ -0,0 +1,3 @@
HTTP/1.1 200 OK

wildcards-extended
@@ -2,10 +2,11 @@ var MockReq = require('mock-req');
var assert = require("assert");
var colors = require('colors');
var mockserver = require("./../mockserver");
var path = require('path')

var res;
var req;
var mocksDirectory = './test/mocks';
var mocksDirectory = path.join('.', 'test', 'mocks');

var verbose = process.env.DEBUG === 'true' || false;

@@ -331,6 +332,20 @@ describe('mockserver', function() {
assert.equal(res.body, 'this always comes up\n');
});

it('wildcard matches directories named foo/__/bar with numeric slug', function() {
processRequest('/wildcard-extended/123/foobar', 'GET');

assert.equal(res.status, 200);
assert.equal(res.body, 'wildcards-extended');
});

it('wildcard matches directories named foo/__/bar with string slug', function() {
processRequest('/wildcard-extended/abc/foobar', 'GET');

assert.equal(res.status, 200);
assert.equal(res.body, 'wildcards-extended');
});

it('__ not used if more specific match exist', function() {
processRequest('/wildcard/exact', 'GET');

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.