Skip to content

Commit cb43c23

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 angular#11868 Closes angular#6860
1 parent 487744f commit cb43c23

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

src/ng/templateRequest.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,27 @@ var $compileMinErr = minErr('$compile');
1010
*/
1111
function $TemplateRequestProvider() {
1212

13-
var acceptHeaders;
13+
var httpOptions;
1414

1515
/**
1616
* @ngdoc method
17-
* @name $templateRequestProvider#acceptHeaders
17+
* @name $templateRequestProvider#httpOptions
1818
* @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.
19+
* The options to be passed to the $http service when making the request.
20+
* You can use this to override options such as the Accept header for template requests.
2221
*
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.
22+
* The {$templateRequest} will set the `cache` and the `transformResponse` properties of the
23+
* options if not overridden here.
24+
*
25+
* @param {string=} value new value for the {@link $http} options.
26+
* @returns {string|self} Returns the {@link $http} options when used as getter and self if used as setter.
2527
*/
26-
this.acceptHeaders = function(val) {
28+
this.httpOptions = function(val) {
2729
if (val) {
28-
acceptHeaders = val;
30+
httpOptions = val;
2931
return this;
3032
}
31-
return acceptHeaders;
33+
return httpOptions;
3234
};
3335

3436
/**
@@ -43,6 +45,9 @@ function $TemplateRequestProvider() {
4345
* contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted
4446
* when `tpl` is of type string and `$templateCache` has the matching entry.
4547
*
48+
* If you want to pass custom options to the `$http` service, such as setting the Accept header you
49+
* can configure this via {@link $templateRequestProvider#httpOptions}.
50+
*
4651
* @param {string|TrustedResourceUrl} tpl The HTTP request template URL
4752
* @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty
4853
*
@@ -74,16 +79,10 @@ function $TemplateRequestProvider() {
7479
transformResponse = null;
7580
}
7681

77-
var httpOptions = {
78-
cache: $templateCache,
79-
transformResponse: transformResponse
80-
};
81-
82-
if (acceptHeaders) {
83-
httpOptions.headers = { 'Accept': acceptHeaders };
84-
}
85-
86-
return $http.get(tpl, httpOptions)
82+
return $http.get(tpl, extend({
83+
cache: $templateCache,
84+
transformResponse: transformResponse
85+
}, httpOptions))
8786
['finally'](function() {
8887
handleRequestFn.totalPendingRequests--;
8988
})

test/ng/templateRequestSpec.js

+28-18
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,54 @@ describe('$templateRequest', function() {
44

55
describe('provider', function() {
66

7-
describe('acceptHeaders', function() {
7+
describe('httpOptions', function() {
88

9-
it('should default to undefined and fallback to $http accept headers', function() {
9+
it('should default to undefined and fallback to default $http options', function() {
1010

1111
var defaultHeader;
1212

13-
module(function($templateRequestProvider, $httpProvider) {
14-
defaultHeader = $httpProvider.defaults.headers.get || $httpProvider.defaults.headers.common;
15-
expect($templateRequestProvider.acceptHeaders()).toBeUndefined();
13+
module(function($templateRequestProvider) {
14+
expect($templateRequestProvider.httpOptions()).toBeUndefined();
1615
});
1716

18-
inject(function($templateRequest, $httpBackend) {
19-
$httpBackend.expectGET('tpl.html', defaultHeader).respond();
17+
inject(function($templateRequest, $http, $templateCache) {
18+
spyOn($http, 'get').andCallThrough();
19+
2020
$templateRequest('tpl.html');
21-
$httpBackend.verifyNoOutstandingExpectation();
21+
22+
expect($http.get).toHaveBeenCalledOnceWith('tpl.html', {
23+
cache: $templateCache,
24+
transformResponse: [ ]
25+
});
2226
});
2327

2428
});
2529

2630
it('should be configurable', function() {
2731

32+
function someTransform() {}
33+
2834
var expectedHeader;
2935

3036
module(function($templateRequestProvider, $httpProvider) {
3137

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'});
38+
// Configure the template request service to provide specific headers and transforms
39+
$templateRequestProvider.httpOptions({
40+
headers: { Accept: 'moo' },
41+
transformResponse: [someTransform]
42+
});
3943
});
4044

41-
inject(function($templateRequest, $httpBackend) {
42-
$httpBackend.expectGET('tpl.html', expectedHeader).respond();
45+
inject(function($templateRequest, $http, $templateCache) {
46+
spyOn($http, 'get').andCallThrough();
47+
4348
$templateRequest('tpl.html');
44-
$httpBackend.verifyNoOutstandingExpectation();
49+
50+
expect($http.get).toHaveBeenCalledOnceWith('tpl.html', {
51+
cache: $templateCache,
52+
transformResponse: [someTransform],
53+
headers: { Accept: 'moo' }
54+
});
4555
});
4656
});
4757
});

0 commit comments

Comments
 (0)