Skip to content

Commit

Permalink
feat(apiPath): add support for arrays and/or regexp when matching
Browse files Browse the repository at this point in the history
It includes documentation updated and tests for added feature

Fix #16
  • Loading branch information
Nestor Lobo committed Sep 23, 2015
1 parent f986e25 commit 0a08a7d
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ Set the path to be rerouted to.

#### apiPath

Type: `string`
Type: `string` | `RegExp` | `[<string|RegExp>]`

Default: `'/api'`

Set the path to be rerouted from.
Set the path to be rerouted from, for strings, will match request path from the left part, if it is a regular expresion it will evaluate expression.
It can handle arrays and both mixed.

#### disable

Expand Down
35 changes: 32 additions & 3 deletions app/scripts/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,33 @@ angular.module('apiMock', [])
}

function isApiPath(url) {
return url.indexOf(config.apiPath) === 0;
return (apiPathMatched(url, config.apiPath) !== undefined);
}

function apiPathMatched(url, apiPath) {
var match; // Lets initially assume undefined as no match

if (angular.isArray(apiPath)) {
angular.forEach(apiPath, function (path) {
if (match) { return; } // Hack to skip more recursive calls if already matched
var found = apiPathMatched(url, path);
if (found) {
match = found;
}
});
}
if (match) {
return match;
}
if (apiPath instanceof RegExp) {
if (apiPath.test(url)) {
return apiPath;
}
}
if ((url.toString().indexOf(apiPath) === 0)) {
return apiPath;
}
return match;
}

function prepareFallback(req) {
Expand Down Expand Up @@ -234,14 +260,16 @@ angular.module('apiMock', [])

// replace apiPath with mockDataPath.
var oldPath = req.url;
var redirectedPath = req.url.replace(config.apiPath, config.mockDataPath);

var redirectedPath = req.url.replace(apiPathMatched(req.url, config.apiPath), config.mockDataPath);

var split = redirectedPath.split('?');
var newPath = split[0];
var queries = split[1] || '';

// query strings are stripped by default (like ?search=banana).
if (!config.stripQueries) {

//test if we have query params
//if we do merge them on to the params object
var queryParamsFromUrl = queryStringToObject(queries);
Expand Down Expand Up @@ -371,7 +399,8 @@ angular.module('apiMock', [])

$timeout(
function() {
deferred.resolve( apiMock.onResponse(res) ); // TODO: Apparently, no tests break regardless what this resolves to. Fix the tests!
// TODO: Apparently, no tests break regardless what this resolves to. Fix the tests!
deferred.resolve( apiMock.onResponse(res) );
},
apiMock.getDelay(),
true // Trigger a $digest.
Expand Down
117 changes: 117 additions & 0 deletions test/spec/services/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ describe('Service: apiMock', function () {
});

describe('module config', function () {

describe('disable option', function () {

beforeEach(function() {
Expand Down Expand Up @@ -439,6 +440,122 @@ describe('Service: apiMock', function () {

});

describe('allow regexp for apiPath option instead of string', function () {

beforeEach(function() {
// apiMockProvider.config({apiPath: [/\/(aPI)/i]});
apiMockProvider.config({apiPath: /\/(aPi|UPI|APU)/i});
setGlobalCommand(true);
});

afterEach(function() {
apiMockProvider.config({apiPath: '/api'});
unsetGlobalCommand();
});

it('should redirect when match', function () {
defaultRequest.url = '/api/pokemon';
defaultExpectPath = '/mock_data/pokemon.get.json';
expectHttpSuccess();
});

it('should redirect any match', function () {
defaultRequest.url = '/UPI/pokemon';
defaultExpectPath = '/mock_data/pokemon.get.json';
expectHttpSuccess();
});

it('should NOT redirect not matched', function () {
defaultRequest.url = '/EPI/picachu';
defaultExpectPath = '/EPI/picachu';
expectHttpFailure();
});

});

describe('allow strings array for apiPath option', function () {

beforeEach(function() {
apiMockProvider.config({apiPath: ['/other/api', '/api', '/v2api']});
setGlobalCommand(true);
});

afterEach(function() {
apiMockProvider.config({apiPath: '/api'});
unsetGlobalCommand();
});

it('should redirect when match first in list', function () {
defaultRequest.url = '/other/api/otherpikamon';
defaultExpectPath = '/mock_data/otherpikamon.get.json';
expectHttpSuccess();
});

it('should redirect when match second in list', function () {
defaultRequest.url = '/api/pikamon';
defaultExpectPath = '/mock_data/pikamon.get.json';
expectHttpSuccess();
});

it('should redirect when match third in list as regexp', function () {
defaultRequest.url = '/v2api/v2pikamon';
defaultExpectPath = '/mock_data/v2pikamon.get.json';
expectHttpSuccess();
});

it('should NOT redirect not matched', function () {
defaultRequest.url = '/v9api/v9picachu';
defaultExpectPath = '/v9api/v9picachu';
expectHttpFailure();
});

});

describe('allow regexp array for apiPath option', function () {

beforeEach(function() {
apiMockProvider.config({apiPath: [/\/other\/api/i, /\/api/i, /\/v(2|3|4)api/i]});
setGlobalCommand(true);
});

afterEach(function() {
apiMockProvider.config({apiPath: '/api'});
unsetGlobalCommand();
});

it('should redirect when match first in list', function () {
defaultRequest.url = '/other/api/otherpikamon';
defaultExpectPath = '/mock_data/otherpikamon.get.json';
expectHttpSuccess();
});

it('should redirect when match second in list', function () {
defaultRequest.url = '/api/pikamon';
defaultExpectPath = '/mock_data/pikamon.get.json';
expectHttpSuccess();
});


it('should redirect when match third in list as regexp', function () {
defaultRequest.url = '/v2api/v2pikamon';
defaultExpectPath = '/mock_data/v2pikamon.get.json';
expectHttpSuccess();
});

it('should redirect when match third again in list as regexp', function () {
defaultRequest.url = '/v3api/v3pikamon';
defaultExpectPath = '/mock_data/v3pikamon.get.json';
expectHttpSuccess();
});

it('should NOT redirect not matched', function () {
defaultRequest.url = '/v9api/v9picachu';
defaultExpectPath = '/v9api/v9picachu';
expectHttpFailure();
});

});

describe('enable query params', function () {

beforeEach(function() {
Expand Down

0 comments on commit 0a08a7d

Please sign in to comment.