From 487744fd24cebcb0c321fff7d84d67ccc604c9d6 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 28 Oct 2015 07:42:54 +0000 Subject: [PATCH 1/3] feat($templateRequest): support configuration of accept headers It is now possible to configure the Accept header for template requests. If a value is configured then this will override only the Accept property of the headers that are passed. If no value is configured then the request will use the default $http Accept header. Thanks to @luckycadow for help on this feature Closes #11868 Closes #6860 --- src/ng/templateRequest.js | 65 +++++++++++++++++++++++++--------- test/ng/templateRequestSpec.js | 45 +++++++++++++++++++++++ 2 files changed, 94 insertions(+), 16 deletions(-) diff --git a/src/ng/templateRequest.js b/src/ng/templateRequest.js index 6219308ed671..a20c58abaf83 100644 --- a/src/ng/templateRequest.js +++ b/src/ng/templateRequest.js @@ -3,26 +3,55 @@ var $compileMinErr = minErr('$compile'); /** - * @ngdoc service - * @name $templateRequest - * + * @ngdoc provider + * @name $templateRequestProvider * @description - * The `$templateRequest` service runs security checks then downloads the provided template using - * `$http` and, upon success, stores the contents inside of `$templateCache`. If the HTTP request - * fails or the response data of the HTTP request is empty, a `$compile` error will be thrown (the - * exception can be thwarted by setting the 2nd parameter of the function to true). Note that the - * contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted - * when `tpl` is of type string and `$templateCache` has the matching entry. - * - * @param {string|TrustedResourceUrl} tpl The HTTP request template URL - * @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty - * - * @return {Promise} a promise for the HTTP response data of the given URL. - * - * @property {number} totalPendingRequests total amount of pending template requests being downloaded. + * Used to configure the Accept header that is sent to the server when requesting a template. */ function $TemplateRequestProvider() { + + var acceptHeaders; + + /** + * @ngdoc method + * @name $templateRequestProvider#acceptHeaders + * @description + * The accept header to be sent to the server as part of the request. + * If this value is undefined then the default {@link $http} Accept header + * will be used. + * + * @param {string=} value new value for the Accept header. + * @returns {string|self} Returns the Accept header value when used as getter and self if used as setter. + */ + this.acceptHeaders = function(val) { + if (val) { + acceptHeaders = val; + return this; + } + return acceptHeaders; + }; + + /** + * @ngdoc service + * @name $templateRequest + * + * @description + * The `$templateRequest` service runs security checks then downloads the provided template using + * `$http` and, upon success, stores the contents inside of `$templateCache`. If the HTTP request + * fails or the response data of the HTTP request is empty, a `$compile` error will be thrown (the + * exception can be thwarted by setting the 2nd parameter of the function to true). Note that the + * contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted + * when `tpl` is of type string and `$templateCache` has the matching entry. + * + * @param {string|TrustedResourceUrl} tpl The HTTP request template URL + * @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty + * + * @return {Promise} a promise for the HTTP response data of the given URL. + * + * @property {number} totalPendingRequests total amount of pending template requests being downloaded. + */ this.$get = ['$templateCache', '$http', '$q', '$sce', function($templateCache, $http, $q, $sce) { + function handleRequestFn(tpl, ignoreRequestError) { handleRequestFn.totalPendingRequests++; @@ -50,6 +79,10 @@ function $TemplateRequestProvider() { transformResponse: transformResponse }; + if (acceptHeaders) { + httpOptions.headers = { 'Accept': acceptHeaders }; + } + return $http.get(tpl, httpOptions) ['finally'](function() { handleRequestFn.totalPendingRequests--; diff --git a/test/ng/templateRequestSpec.js b/test/ng/templateRequestSpec.js index a297bbfafe2d..833e300277a8 100644 --- a/test/ng/templateRequestSpec.js +++ b/test/ng/templateRequestSpec.js @@ -2,6 +2,51 @@ describe('$templateRequest', function() { + describe('provider', function() { + + describe('acceptHeaders', function() { + + it('should default to undefined and fallback to $http accept headers', function() { + + var defaultHeader; + + module(function($templateRequestProvider, $httpProvider) { + defaultHeader = $httpProvider.defaults.headers.get || $httpProvider.defaults.headers.common; + expect($templateRequestProvider.acceptHeaders()).toBeUndefined(); + }); + + inject(function($templateRequest, $httpBackend) { + $httpBackend.expectGET('tpl.html', defaultHeader).respond(); + $templateRequest('tpl.html'); + $httpBackend.verifyNoOutstandingExpectation(); + }); + + }); + + it('should be configurable', function() { + + var expectedHeader; + + module(function($templateRequestProvider, $httpProvider) { + + // Configure the standard $http headers to something unusual + $httpProvider.defaults.headers.get = { 'Other': 'header value' }; + // Configure the template request service to provide a specific accept header + $templateRequestProvider.acceptHeaders('moo'); + + // Compute what we expect the actual header object to be + expectedHeader = extend($httpProvider.defaults.headers.get, {Accept: 'moo'}); + }); + + inject(function($templateRequest, $httpBackend) { + $httpBackend.expectGET('tpl.html', expectedHeader).respond(); + $templateRequest('tpl.html'); + $httpBackend.verifyNoOutstandingExpectation(); + }); + }); + }); + }); + it('should download the provided template file', inject(function($rootScope, $templateRequest, $httpBackend) { From cb43c2364ad7ed95274c02a8989e9df487b1562b Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 28 Oct 2015 10:24:24 +0000 Subject: [PATCH 2/3] feat($templateRequest): support configuration of $http options It is now possible to configure the options sent to $http for template requests. If no value is configured then the request will use the default $http options. Thanks to @luckycadow for help on this feature Closes #11868 Closes #6860 --- src/ng/templateRequest.js | 39 ++++++++++++++-------------- test/ng/templateRequestSpec.js | 46 +++++++++++++++++++++------------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/ng/templateRequest.js b/src/ng/templateRequest.js index a20c58abaf83..5cea2696c802 100644 --- a/src/ng/templateRequest.js +++ b/src/ng/templateRequest.js @@ -10,25 +10,27 @@ var $compileMinErr = minErr('$compile'); */ function $TemplateRequestProvider() { - var acceptHeaders; + var httpOptions; /** * @ngdoc method - * @name $templateRequestProvider#acceptHeaders + * @name $templateRequestProvider#httpOptions * @description - * The accept header to be sent to the server as part of the request. - * If this value is undefined then the default {@link $http} Accept header - * will be used. + * The options to be passed to the $http service when making the request. + * You can use this to override options such as the Accept header for template requests. * - * @param {string=} value new value for the Accept header. - * @returns {string|self} Returns the Accept header value when used as getter and self if used as setter. + * The {$templateRequest} will set the `cache` and the `transformResponse` properties of the + * options if not overridden here. + * + * @param {string=} value new value for the {@link $http} options. + * @returns {string|self} Returns the {@link $http} options when used as getter and self if used as setter. */ - this.acceptHeaders = function(val) { + this.httpOptions = function(val) { if (val) { - acceptHeaders = val; + httpOptions = val; return this; } - return acceptHeaders; + return httpOptions; }; /** @@ -43,6 +45,9 @@ function $TemplateRequestProvider() { * contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted * when `tpl` is of type string and `$templateCache` has the matching entry. * + * If you want to pass custom options to the `$http` service, such as setting the Accept header you + * can configure this via {@link $templateRequestProvider#httpOptions}. + * * @param {string|TrustedResourceUrl} tpl The HTTP request template URL * @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty * @@ -74,16 +79,10 @@ function $TemplateRequestProvider() { transformResponse = null; } - var httpOptions = { - cache: $templateCache, - transformResponse: transformResponse - }; - - if (acceptHeaders) { - httpOptions.headers = { 'Accept': acceptHeaders }; - } - - return $http.get(tpl, httpOptions) + return $http.get(tpl, extend({ + cache: $templateCache, + transformResponse: transformResponse + }, httpOptions)) ['finally'](function() { handleRequestFn.totalPendingRequests--; }) diff --git a/test/ng/templateRequestSpec.js b/test/ng/templateRequestSpec.js index 833e300277a8..0f77fc7f693f 100644 --- a/test/ng/templateRequestSpec.js +++ b/test/ng/templateRequestSpec.js @@ -4,44 +4,54 @@ describe('$templateRequest', function() { describe('provider', function() { - describe('acceptHeaders', function() { + describe('httpOptions', function() { - it('should default to undefined and fallback to $http accept headers', function() { + it('should default to undefined and fallback to default $http options', function() { var defaultHeader; - module(function($templateRequestProvider, $httpProvider) { - defaultHeader = $httpProvider.defaults.headers.get || $httpProvider.defaults.headers.common; - expect($templateRequestProvider.acceptHeaders()).toBeUndefined(); + module(function($templateRequestProvider) { + expect($templateRequestProvider.httpOptions()).toBeUndefined(); }); - inject(function($templateRequest, $httpBackend) { - $httpBackend.expectGET('tpl.html', defaultHeader).respond(); + inject(function($templateRequest, $http, $templateCache) { + spyOn($http, 'get').andCallThrough(); + $templateRequest('tpl.html'); - $httpBackend.verifyNoOutstandingExpectation(); + + expect($http.get).toHaveBeenCalledOnceWith('tpl.html', { + cache: $templateCache, + transformResponse: [ ] + }); }); }); it('should be configurable', function() { + function someTransform() {} + var expectedHeader; module(function($templateRequestProvider, $httpProvider) { - // Configure the standard $http headers to something unusual - $httpProvider.defaults.headers.get = { 'Other': 'header value' }; - // Configure the template request service to provide a specific accept header - $templateRequestProvider.acceptHeaders('moo'); - - // Compute what we expect the actual header object to be - expectedHeader = extend($httpProvider.defaults.headers.get, {Accept: 'moo'}); + // Configure the template request service to provide specific headers and transforms + $templateRequestProvider.httpOptions({ + headers: { Accept: 'moo' }, + transformResponse: [someTransform] + }); }); - inject(function($templateRequest, $httpBackend) { - $httpBackend.expectGET('tpl.html', expectedHeader).respond(); + inject(function($templateRequest, $http, $templateCache) { + spyOn($http, 'get').andCallThrough(); + $templateRequest('tpl.html'); - $httpBackend.verifyNoOutstandingExpectation(); + + expect($http.get).toHaveBeenCalledOnceWith('tpl.html', { + cache: $templateCache, + transformResponse: [someTransform], + headers: { Accept: 'moo' } + }); }); }); }); From 39eef70c9c8bd0c0747f5c2b6e043aab54f19280 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 28 Oct 2015 16:39:02 +0000 Subject: [PATCH 3/3] feat($templateRequest): support configuration of $http options It is now possible to configure the options sent to $http for template requests. If no value is configured then the request will use the default $http options. Thanks to @luckycadow for help on this feature Closes #11868 Closes #6860 --- test/ng/templateRequestSpec.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/test/ng/templateRequestSpec.js b/test/ng/templateRequestSpec.js index 0f77fc7f693f..e572d40fbc5b 100644 --- a/test/ng/templateRequestSpec.js +++ b/test/ng/templateRequestSpec.js @@ -31,8 +31,6 @@ describe('$templateRequest', function() { function someTransform() {} - var expectedHeader; - module(function($templateRequestProvider, $httpProvider) { // Configure the template request service to provide specific headers and transforms @@ -54,6 +52,30 @@ describe('$templateRequest', function() { }); }); }); + + + it('should be allow you to override the cache', function() { + + var httpOptions = {}; + + module(function($templateRequestProvider, $httpProvider) { + $templateRequestProvider.httpOptions(httpOptions); + }); + + inject(function($templateRequest, $http, $cacheFactory) { + spyOn($http, 'get').andCallThrough(); + + var customCache = $cacheFactory('customCache'); + httpOptions.cache = customCache; + + $templateRequest('tpl.html'); + + expect($http.get).toHaveBeenCalledOnceWith('tpl.html', { + cache: customCache, + transformResponse: [] + }); + }); + }); }); });