diff --git a/app/scripts/angular-apimock.js b/app/scripts/angular-apimock.js index ffd4871..76f8906 100644 --- a/app/scripts/angular-apimock.js +++ b/app/scripts/angular-apimock.js @@ -69,6 +69,7 @@ angular.module('apiMock', []) return newArray; } + // TODO: Does not support complex objects. $httpParamSerializerJQLike from Angular 1.4 supports it, and sorts them. But we need to support 1.2+. ("Borrow" their source code?) function serializeQueryObject(paramObj) { var keys = Object.keys(paramObj); keys.sort(); // We want the query params alphabetically. @@ -76,6 +77,11 @@ angular.module('apiMock', []) var paramArray = mapArray(keys, function(key) { var value = paramObj[key]; + // Strip complex parts. + if (angular.isObject(value) || angular.isArray(value)) { // Actually angular.isObject() returns true for arrays as well, but this way the intent is clearer. + return safeURI(key); + } + return safeURI(key) + '=' + safeURI(value); }); diff --git a/test/spec/services/angular-apimock.js b/test/spec/services/angular-apimock.js index c17c952..b5a59ae 100644 --- a/test/spec/services/angular-apimock.js +++ b/test/spec/services/angular-apimock.js @@ -471,6 +471,29 @@ describe('Service: apiMock', function () { defaultExpectPath = '/mock_data/pokemon/name=pikachu.get.json'; expectHttpSuccess(); }); + + it('should strip nested objects but keep the key (for now)', function () { + defaultRequest.url = '/api/pokemon'; + defaultRequest.params = { + 'movesAppearences': { + 'Thunder Shock': 'Pokémon - I Choose You!', + 'Volt Tackle': 'May\'s Egg-Cellent Adventure!' + } + }; + defaultExpectPath = '/mock_data/pokemon/movesappearences.get.json'; + + expectHttpSuccess(); + }); + + it('should strip nested array but keep the key (for now)', function () { + defaultRequest.url = '/api/pokemon'; + defaultRequest.params = { + 'moves': ['Thunder Shock', 'Volt Tackle'] + }; + defaultExpectPath = '/mock_data/pokemon/moves.get.json'; + + expectHttpSuccess(); + }); }); });