Skip to content

Commit

Permalink
feat(queryParams): ignore nested objects and arrays on $http config.p…
Browse files Browse the repository at this point in the history
…aram

$http’s config.param can include nested objects and arrays, but parsing
them to a valid file path will have to wait. So they’re not being
included in the path for now.

Ref #23
  • Loading branch information
seriema committed Aug 6, 2015
1 parent 67d1d2d commit 0e3138a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/scripts/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,19 @@ 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.

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);
});

Expand Down
23 changes: 23 additions & 0 deletions test/spec/services/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand Down

0 comments on commit 0e3138a

Please sign in to comment.