From d19d274821a9117d54ee4e9e104cd69426ebb8e6 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Sat, 26 Jul 2014 23:29:59 +0300 Subject: [PATCH] feat(http): allow caching for JSONP requests Closes #1947 --- src/ng/http.js | 3 ++- test/ng/httpSpec.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ng/http.js b/src/ng/http.js index caaf8d4ce943..219954930e4e 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -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')) { cache = isObject(config.cache) ? config.cache : isObject(defaults.cache) ? defaults.cache : defaultCache; diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 0bb23dba49a2..c1c33ffb8788 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -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();