Skip to content

Commit ea0cb90

Browse files
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 angular#11868 Closes angular#6860
1 parent a995ee1 commit ea0cb90

File tree

2 files changed

+94
-16
lines changed

2 files changed

+94
-16
lines changed

src/ng/templateRequest.js

+49-16
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,55 @@
33
var $compileMinErr = minErr('$compile');
44

55
/**
6-
* @ngdoc service
7-
* @name $templateRequest
8-
*
6+
* @ngdoc provider
7+
* @name $templateRequestProvider
98
* @description
10-
* The `$templateRequest` service runs security checks then downloads the provided template using
11-
* `$http` and, upon success, stores the contents inside of `$templateCache`. If the HTTP request
12-
* fails or the response data of the HTTP request is empty, a `$compile` error will be thrown (the
13-
* exception can be thwarted by setting the 2nd parameter of the function to true). Note that the
14-
* contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted
15-
* when `tpl` is of type string and `$templateCache` has the matching entry.
16-
*
17-
* @param {string|TrustedResourceUrl} tpl The HTTP request template URL
18-
* @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty
19-
*
20-
* @return {Promise} a promise for the HTTP response data of the given URL.
21-
*
22-
* @property {number} totalPendingRequests total amount of pending template requests being downloaded.
9+
* Used to configure the Accept header that is sent to the server when requesting a template.
2310
*/
2411
function $TemplateRequestProvider() {
12+
13+
var acceptHeaders;
14+
15+
/**
16+
* @ngdoc method
17+
* @name $templateRequestProvider#acceptHeaders
18+
* @description
19+
* The accept header to be sent to the server as part of the request.
20+
* If this value is undefined then the default {@link $http} Accept header
21+
* will be used.
22+
*
23+
* @param {string=} value new value for the Accept header.
24+
* @returns {string|self} Returns the Accept header value when used as getter and self if used as setter.
25+
*/
26+
this.acceptHeaders = function(val) {
27+
if (val) {
28+
acceptHeaders = val;
29+
return this;
30+
}
31+
return acceptHeaders;
32+
};
33+
34+
/**
35+
* @ngdoc service
36+
* @name $templateRequest
37+
*
38+
* @description
39+
* The `$templateRequest` service runs security checks then downloads the provided template using
40+
* `$http` and, upon success, stores the contents inside of `$templateCache`. If the HTTP request
41+
* fails or the response data of the HTTP request is empty, a `$compile` error will be thrown (the
42+
* exception can be thwarted by setting the 2nd parameter of the function to true). Note that the
43+
* contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted
44+
* when `tpl` is of type string and `$templateCache` has the matching entry.
45+
*
46+
* @param {string|TrustedResourceUrl} tpl The HTTP request template URL
47+
* @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty
48+
*
49+
* @return {Promise} a promise for the HTTP response data of the given URL.
50+
*
51+
* @property {number} totalPendingRequests total amount of pending template requests being downloaded.
52+
*/
2553
this.$get = ['$templateCache', '$http', '$q', '$sce', function($templateCache, $http, $q, $sce) {
54+
2655
function handleRequestFn(tpl, ignoreRequestError) {
2756
handleRequestFn.totalPendingRequests++;
2857

@@ -50,6 +79,10 @@ function $TemplateRequestProvider() {
5079
transformResponse: transformResponse
5180
};
5281

82+
if (acceptHeaders) {
83+
httpOptions.headers = { 'Accept': acceptHeaders };
84+
}
85+
5386
return $http.get(tpl, httpOptions)
5487
['finally'](function() {
5588
handleRequestFn.totalPendingRequests--;

test/ng/templateRequestSpec.js

+45
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,51 @@
22

33
describe('$templateRequest', function() {
44

5+
describe('provider', function() {
6+
7+
describe('acceptHeaders', function() {
8+
9+
it('should default to undefined and fallback to $http accept headers', function() {
10+
11+
var defaultHeader;
12+
13+
module(function($templateRequestProvider, $httpProvider) {
14+
defaultHeader = $httpProvider.defaults.headers.get || $httpProvider.defaults.headers.common;
15+
expect($templateRequestProvider.acceptHeaders()).toBeUndefined();
16+
});
17+
18+
inject(function($templateRequest, $httpBackend) {
19+
$httpBackend.expectGET('tpl.html', defaultHeader).respond();
20+
$templateRequest('tpl.html');
21+
$httpBackend.verifyNoOutstandingExpectation();
22+
});
23+
24+
});
25+
26+
it('should be configurable', function() {
27+
28+
var expectedHeader;
29+
30+
module(function($templateRequestProvider, $httpProvider) {
31+
32+
// Configure the standard $http headers to something unusual
33+
$httpProvider.defaults.headers.get = { 'Other': 'header value' };
34+
// Configure the template request service to provide a specific accept header
35+
$templateRequestProvider.acceptHeaders('moo');
36+
37+
// Compute what we expect the actual header object to be
38+
expectedHeader = extend($httpProvider.defaults.headers.get, {Accept: 'moo'});
39+
});
40+
41+
inject(function($templateRequest, $httpBackend) {
42+
$httpBackend.expectGET('tpl.html', expectedHeader).respond();
43+
$templateRequest('tpl.html');
44+
$httpBackend.verifyNoOutstandingExpectation();
45+
});
46+
});
47+
});
48+
});
49+
550
it('should download the provided template file',
651
inject(function($rootScope, $templateRequest, $httpBackend) {
752

0 commit comments

Comments
 (0)