From ccf22f29e57dfe66a0df59c992120b4fcbbe2c0d Mon Sep 17 00:00:00 2001 From: John-Philip Johansson Date: Sun, 13 Sep 2015 21:54:07 +0200 Subject: [PATCH] fix(apimock): correctly detect URL commands Fixes #37 --- app/index.html | 2 +- app/scripts/angular-apimock.js | 33 ++++++++++----------------- test/spec/services/angular-apimock.js | 30 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/app/index.html b/app/index.html index 2eab94a..a814138 100644 --- a/app/index.html +++ b/app/index.html @@ -33,7 +33,7 @@

Debug info

app.config(function (apiMockProvider) { apiMockProvider.config({ - mockDataPath: 'mock_data', + mockDataPath: '/mock_data', apiPath: '/api' }); }); diff --git a/app/scripts/angular-apimock.js b/app/scripts/angular-apimock.js index 6f344e9..859322a 100644 --- a/app/scripts/angular-apimock.js +++ b/app/scripts/angular-apimock.js @@ -171,27 +171,18 @@ angular.module('apiMock', []) } function getCommand(mockValue) { - switch (typeof mockValue) { - case 'number': - if (mockValue !== 0 && !isNaN(mockValue)) { - return { type: 'respond', value: mockValue }; - } - break; - - case 'string': - switch(mockValue.toLowerCase()) { - case 'auto': - return { type: 'recover' }; - case 'true': - return { type: 'reroute' }; - } - break; - - case 'boolean': - if (mockValue === true) { - return { type: 'reroute' }; - } - break; + // Depending how we got mockValue it might've been parsed into a type or not. + switch ((mockValue || '').toString().toLowerCase()) { + case '200': + case '404': + case '500': + return { type: 'respond', value: parseInt(mockValue, 10) }; + + case 'auto': + return { type: 'recover' }; + + case 'true': + return { type: 'reroute' }; } return { type: 'ignore' }; diff --git a/test/spec/services/angular-apimock.js b/test/spec/services/angular-apimock.js index 8a3112e..ab73498 100644 --- a/test/spec/services/angular-apimock.js +++ b/test/spec/services/angular-apimock.js @@ -151,6 +151,36 @@ describe('Service: apiMock', function () { }); }); + it('should detect HTTP verb command as string', function () { + $location.url('/page?apiMock=200'); + + // Cannot use $httpBackend.expect() because HTTP status doesn't do a request + $http(defaultRequest) + .success(fail) + .error(function(data, status) { + expect(apiMock._countFallbacks()).toEqual(0); + expect(status).toEqual(200); + }); + + $rootScope.$digest(); + $timeout.flush(); + }); + + it('should detect HTTP verb command as number', function () { + $location.search('apimock', 200); + + // Cannot use $httpBackend.expect() because HTTP status doesn't do a request + $http(defaultRequest) + .success(fail) + .error(function(data, status) { + expect(apiMock._countFallbacks()).toEqual(0); + expect(status).toEqual(200); + }); + + $rootScope.$digest(); + $timeout.flush(); + }); + it('should detect in search queries', function () { $location.url('/page?apiMock=true');