1
+ import * as angular from "angular" ;
2
+
3
+ declare let inject ;
4
+
5
+ let module = angular [ 'mock' ] . module ;
6
+
7
+ describe ( 'templateFactory' , function ( ) {
8
+
9
+ beforeEach ( module ( 'ui.router' ) ) ;
10
+
11
+ it ( 'exists' , inject ( function ( $templateFactory ) {
12
+ expect ( $templateFactory ) . toBeDefined ( ) ;
13
+ } ) ) ;
14
+
15
+ if ( angular . version . major >= 1 && angular . version . minor >= 3 ) {
16
+ // Post 1.2, there is a $templateRequest and a $sce service
17
+ describe ( 'should follow $sce policy and' , function ( ) {
18
+ it ( 'accepts relative URLs' , inject ( function ( $templateFactory , $httpBackend , $sce ) {
19
+ $httpBackend . expectGET ( 'views/view.html' ) . respond ( 200 , 'template!' ) ;
20
+ $templateFactory . fromUrl ( 'views/view.html' ) ;
21
+ $httpBackend . flush ( ) ;
22
+ } ) ) ;
23
+
24
+ it ( 'rejects untrusted URLs' ,
25
+ inject ( function ( $templateFactory , $httpBackend , $sce ) {
26
+ let error = 'No error thrown' ;
27
+ try {
28
+ $templateFactory . fromUrl ( 'http://evil.com/views/view.html' ) ;
29
+ } catch ( e ) {
30
+ error = e . message ;
31
+ }
32
+ expect ( error ) . toMatch ( / s c e : i n s e c u r l / ) ;
33
+ } ) ) ;
34
+
35
+ it ( 'accepts explicitly trusted URLs' ,
36
+ inject ( function ( $templateFactory , $httpBackend , $sce ) {
37
+ $httpBackend . expectGET ( 'http://evil.com/views/view.html' ) . respond ( 200 , 'template!' ) ;
38
+ $templateFactory . fromUrl (
39
+ $sce . trustAsResourceUrl ( 'http://evil.com/views/view.html' ) ) ;
40
+ $httpBackend . flush ( ) ;
41
+ } ) ) ;
42
+ } ) ;
43
+ } else { // 1.2 and before will use directly $http
44
+ it ( 'does not restrict URL loading' , inject ( function ( $templateFactory , $httpBackend ) {
45
+ $httpBackend . expectGET ( 'http://evil.com/views/view.html' ) . respond ( 200 , 'template!' ) ;
46
+ $templateFactory . fromUrl ( 'http://evil.com/views/view.html' ) ;
47
+ $httpBackend . flush ( ) ;
48
+
49
+ $httpBackend . expectGET ( 'data:text/html,foo' ) . respond ( 200 , 'template!' ) ;
50
+ $templateFactory . fromUrl ( 'data:text/html,foo' ) ;
51
+ $httpBackend . flush ( ) ;
52
+ } ) ) ;
53
+
54
+ // Behavior not kept in >1.2 with $templateRequest
55
+ it ( 'should request templates as text/html' , inject ( function ( $templateFactory , $httpBackend ) {
56
+ $httpBackend . expectGET ( 'views/view.html' , function ( headers ) {
57
+ return headers . Accept === 'text/html' ;
58
+ } ) . respond ( 200 ) ;
59
+ $templateFactory . fromUrl ( 'views/view.html' ) ;
60
+ $httpBackend . flush ( ) ;
61
+ } ) ) ;
62
+ }
63
+ } ) ;
0 commit comments