-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(http): add multiple parameters serializers #7423
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,6 +274,12 @@ describe('$http', function() { | |
describe('the instance', function() { | ||
var $httpBackend, $http, $rootScope; | ||
|
||
beforeEach(module(function($httpProvider) { | ||
$httpProvider.paramsSerializers.custom = function(params, callback) { | ||
callback('foo', 'bar'); | ||
}; | ||
})); | ||
|
||
beforeEach(inject(['$rootScope', function($rs) { | ||
$rootScope = $rs; | ||
|
||
|
@@ -341,6 +347,90 @@ describe('$http', function() { | |
$httpBackend.expect('GET', '/url').respond(''); | ||
$http({url: '/url', params: {}, method: 'GET'}); | ||
}); | ||
|
||
describe('params serialization', function() { | ||
it('should be possible to use key-value serialization', inject(function($httpBackend, $http) { | ||
var testCases = [ | ||
{ | ||
params: {a: 1, b: 2}, | ||
serialized: 'a=1&b=2' | ||
}, | ||
{ | ||
params: {a: 1, b: [2, 3]}, | ||
serialized: 'a=1&b=2&b=3' | ||
}, | ||
{ | ||
params: {a: 'abc', b: {foo: 'bar'}}, | ||
serialized: 'a=abc&b=%7B%22foo%22:%22bar%22%7D' | ||
} | ||
]; | ||
angular.forEach(testCases, function(testCase) { | ||
$httpBackend.expect('GET', '/url?' + testCase.serialized).respond(''); | ||
$http({url: '/url', params: testCase.params, method: 'GET', paramsSerializer: 'keyValue'}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like the option name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also want a way to specify a "default" param serializer, since most of your requests would be talking to your origin server, or at least a specific main web service There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not a big fan of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can change the default parameter serializer by changing |
||
}); | ||
})); | ||
|
||
it('should be possible to use JSON serialization', inject(function($httpBackend, $http) { | ||
var testCases = [ | ||
{ | ||
params: {a: 1, b: 2}, | ||
serialized: 'a=1&b=2' | ||
}, | ||
{ | ||
params: {a: 1, b: [2, 3]}, | ||
serialized: 'a=1&b=%5B2,3%5D' | ||
}, | ||
{ | ||
params: {a: 'abc', b: {foo: 'bar'}}, | ||
serialized: 'a=abc&b=%7B%22foo%22:%22bar%22%7D' | ||
} | ||
]; | ||
angular.forEach(testCases, function(testCase) { | ||
$httpBackend.expect('GET', '/url?' + testCase.serialized).respond(''); | ||
$http({url: '/url', params: testCase.params, method: 'GET', paramsSerializer: 'JSON'}); | ||
}); | ||
})); | ||
|
||
it('should be possible to use nonShallowJSON serialization', inject(function($httpBackend, $http) { | ||
var testCases = [ | ||
{ | ||
params: {a: 1, b: 2}, | ||
serialized: 'a=1&b=2' | ||
}, | ||
{ | ||
params: {a: 1, b: [2, 3]}, | ||
serialized: 'a=1&b%5B%5D=2&b%5B%5D=3' // a=1&b[]=2&b[]=3 | ||
}, | ||
{ | ||
params: {a: 1, b: [2, {foo: 'bar'}]}, | ||
serialized: 'a=1&b%5B%5D=2&b%5B%5D%5Bfoo%5D=bar' // a=1&b[]=2&b[][foo]=bar | ||
}, | ||
{ | ||
params: {a: 'abc', b: {foo: 'bar', man: 'shell'}}, | ||
serialized: 'a=abc&b%5Bfoo%5D=bar&b%5Bman%5D=shell' // a=abc&b[foo]=bar&b[man]=shell | ||
} | ||
]; | ||
angular.forEach(testCases, function(testCase) { | ||
$httpBackend.expect('GET', '/url?' + testCase.serialized).respond(''); | ||
$http({url: '/url', params: testCase.params, method: 'GET', paramsSerializer: 'nonShallowJSON'}); | ||
}); | ||
|
||
})); | ||
|
||
it('should be possible to use a custom serialization', inject(function($httpBackend, $http) { | ||
function serializer(params, callback) { | ||
callback('foo', 'bar'); | ||
} | ||
|
||
$httpBackend.expect('GET', '/url?foo=bar').respond(''); | ||
$http({url: '/url', params: {}, method: 'GET', paramsSerializer: serializer}); | ||
})); | ||
|
||
it('should be possible to use a predefined custom serializer', inject(function($httpBackend, $http) { | ||
$httpBackend.expect('GET', '/url?foo=bar').respond(''); | ||
$http({url: '/url', params: {}, method: 'GET', paramsSerializer: 'custom'}); | ||
})); | ||
}); | ||
}); | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(as far as I'm aware,) no backend expects a JSON query string, that would be crazy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it makes sense to ship more than one of these in core, we should probably just keep doing what we're already doing in core, and give people the option to extend it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, that would be less code (and I always like less code)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm... not even the one that is asked at #7363 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want jQuery behaviour, you could easily just say
to use jQuery's style automatically --- or if you didn't want to include jQuery, you could probably come up with a variation on it