Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(http): allow caching for JSONP requests #8356

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,8 @@ function $HttpProvider() {
promise.then(removePendingReq, removePendingReq);


if ((config.cache || defaults.cache) && config.cache !== false && config.method == 'GET') {
if ((config.cache || defaults.cache) && config.cache !== false &&
(config.method === 'GET' || config.method === 'JSONP')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSONP requests are always essentially GET requests, so it would be cool if we could just rename the method to GET --- but this would take a bit more refactoring, so this is fine for now.

I think this implementation is generally fine, so LGTM

cache = isObject(config.cache) ? config.cache
: isObject(defaults.cache) ? defaults.cache
: defaultCache;
Expand Down
12 changes: 12 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,18 @@ describe('$http', function() {
expect(callback.mostRecentCall.args[0]).toBe('content');
}));

it('should cache JSONP request when cache is provided', inject(function($rootScope) {
$httpBackend.expect('JSONP', '/url?cb=JSON_CALLBACK').respond('content');
$http({method: 'JSONP', url: '/url?cb=JSON_CALLBACK', cache: cache});
$httpBackend.flush();

$http({method: 'JSONP', url: '/url?cb=JSON_CALLBACK', cache: cache}).success(callback);
$rootScope.$digest();

expect(callback).toHaveBeenCalledOnce();
expect(callback.mostRecentCall.args[0]).toBe('content');
}));

it('should cache request when cache is provided and no method specified', function () {
doFirstCacheRequest();

Expand Down