Skip to content

Commit

Permalink
Merge 5eae790 into 27fb1df
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfium committed Sep 13, 2015
2 parents 27fb1df + 5eae790 commit 05bc39f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 14 deletions.
34 changes: 31 additions & 3 deletions app/scripts/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,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 @@ -243,7 +269,8 @@ 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];
Expand Down Expand Up @@ -380,7 +407,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
34 changes: 31 additions & 3 deletions dist/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,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 @@ -246,7 +272,8 @@ 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];
Expand Down Expand Up @@ -383,7 +410,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
2 changes: 1 addition & 1 deletion dist/angular-apimock.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports = function(config) {
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
'app/scripts/**/*.js': sourcePreprocessors
'app/scripts/**/*.js': sourcePreprocessors,
'test/spec/**/*.js': sourcePreprocessors
},

// list of files / patterns to load in the browser
Expand Down
92 changes: 86 additions & 6 deletions test/spec/services/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ describe('Service: apiMock', function () {
$httpBackend.expect(defaultExpectMethod, defaultExpectPath).respond(404);

$http(defaultRequest)
.success(function () {
fail(); // Todo: How to fail the test if this happens?
failCb && failCb();
.success(/* istanbul ignore next - it is expecting a failure, so should fail tests if happen */
function () {
fail(); // Todo: How to fail the test if this happens?
failCb && failCb();
})
.error(function (data, status) {
doneCb && doneCb(data, status);
Expand All @@ -111,9 +112,10 @@ describe('Service: apiMock', function () {
.success(function () {
doneCb && doneCb();
})
.error(function () {
fail();
failCb && failCb();
.error( /* istanbul ignore next - it is expecting a success, so should fail tests if happen */
function () {
fail();
failCb && failCb();
});

$rootScope.$digest();
Expand Down Expand Up @@ -377,6 +379,7 @@ describe('Service: apiMock', function () {
});

describe('module config', function () {

describe('disable option', function () {

beforeEach(function() {
Expand Down Expand Up @@ -409,6 +412,83 @@ describe('Service: apiMock', function () {

});

describe('allow regexp for apiPath option', 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', /\/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 = '/api/pikamon';
defaultExpectPath = '/mock_data/pikamon.get.json';
expectHttpSuccess();
});

it('should redirect when match second in list', function () {
defaultRequest.url = '/other/api/otherpikamon';
defaultExpectPath = '/mock_data/otherpikamon.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 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 05bc39f

Please sign in to comment.