Skip to content
Permalink
Browse files

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 #13188
Closes #11868
Closes #6860
  • Loading branch information...
petebacondarwin committed Oct 28, 2015
1 parent 7c0731e commit b2fc39d2ddac64249b4f2961ee18b878a1e98251
Showing with 131 additions and 22 deletions.
  1. +54 −22 src/ng/templateRequest.js
  2. +77 −0 test/ng/templateRequestSpec.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--;
})
@@ -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) {

0 comments on commit b2fc39d

Please sign in to comment.
You can’t perform that action at this time.