diff --git a/src/ng/templateRequest.js b/src/ng/templateRequest.js index 6219308ed671..5cea2696c802 100644 --- a/src/ng/templateRequest.js +++ b/src/ng/templateRequest.js @@ -3,26 +3,60 @@ 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 httpOptions; + + /** + * @ngdoc method + * @name $templateRequestProvider#httpOptions + * @description + * 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. + * + * 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.httpOptions = function(val) { + if (val) { + httpOptions = val; + return this; + } + return httpOptions; + }; + + /** + * @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. + * + * 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 + * + * @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++; @@ -45,12 +79,10 @@ function $TemplateRequestProvider() { transformResponse = null; } - var httpOptions = { - cache: $templateCache, - transformResponse: transformResponse - }; - - 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 a297bbfafe2d..e572d40fbc5b 100644 --- a/test/ng/templateRequestSpec.js +++ b/test/ng/templateRequestSpec.js @@ -2,6 +2,83 @@ describe('$templateRequest', function() { + describe('provider', function() { + + describe('httpOptions', function() { + + it('should default to undefined and fallback to default $http options', function() { + + var defaultHeader; + + module(function($templateRequestProvider) { + expect($templateRequestProvider.httpOptions()).toBeUndefined(); + }); + + inject(function($templateRequest, $http, $templateCache) { + spyOn($http, 'get').andCallThrough(); + + $templateRequest('tpl.html'); + + expect($http.get).toHaveBeenCalledOnceWith('tpl.html', { + cache: $templateCache, + transformResponse: [ ] + }); + }); + + }); + + it('should be configurable', function() { + + function someTransform() {} + + module(function($templateRequestProvider, $httpProvider) { + + // Configure the template request service to provide specific headers and transforms + $templateRequestProvider.httpOptions({ + headers: { Accept: 'moo' }, + transformResponse: [someTransform] + }); + }); + + inject(function($templateRequest, $http, $templateCache) { + spyOn($http, 'get').andCallThrough(); + + $templateRequest('tpl.html'); + + expect($http.get).toHaveBeenCalledOnceWith('tpl.html', { + cache: $templateCache, + transformResponse: [someTransform], + headers: { Accept: 'moo' } + }); + }); + }); + + + 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: [] + }); + }); + }); + }); + }); + it('should download the provided template file', inject(function($rootScope, $templateRequest, $httpBackend) {