1
1
/* global createHttpBackend: false, createMockXhr: false, MockXhr: false */
2
2
'use strict' ;
3
3
4
- describe ( '$httpBackend' , function ( ) {
4
+ fdescribe ( '$httpBackend' , function ( ) {
5
5
6
- var $backend , $browser , $jsonpCallbacks ,
6
+ var $sce , $ backend, $browser , $jsonpCallbacks ,
7
7
xhr , fakeDocument , callback ;
8
8
9
- beforeEach ( inject ( function ( $injector ) {
9
+ beforeEach ( module ( function ( $sceDelegateProvider ) {
10
+ // Setup a special whitelisted url that we can use in testing JSONP requests
11
+ $sceDelegateProvider . resourceUrlWhitelist ( [ 'http://special.whitelisted.resource.com/**' ] ) ;
12
+ } ) ) ;
10
13
14
+ beforeEach ( inject ( function ( $injector ) {
15
+ $sce = $injector . get ( '$sce' ) ;
11
16
$browser = $injector . get ( '$browser' ) ;
12
17
13
18
fakeDocument = {
@@ -48,7 +53,7 @@ describe('$httpBackend', function() {
48
53
}
49
54
} ;
50
55
51
- $backend = createHttpBackend ( $browser , createMockXhr , $browser . defer , $jsonpCallbacks , fakeDocument ) ;
56
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr , $browser . defer , $jsonpCallbacks , fakeDocument ) ;
52
57
callback = jasmine . createSpy ( 'done' ) ;
53
58
} ) ) ;
54
59
@@ -273,7 +278,7 @@ describe('$httpBackend', function() {
273
278
274
279
it ( 'should call $xhrFactory with method and url' , function ( ) {
275
280
var mockXhrFactory = jasmine . createSpy ( 'mockXhrFactory' ) . and . callFake ( createMockXhr ) ;
276
- $backend = createHttpBackend ( $browser , mockXhrFactory , $browser . defer , $jsonpCallbacks , fakeDocument ) ;
281
+ $backend = createHttpBackend ( $sce , $ browser, mockXhrFactory , $browser . defer , $jsonpCallbacks , fakeDocument ) ;
277
282
$backend ( 'GET' , '/some-url' , 'some-data' , noop ) ;
278
283
expect ( mockXhrFactory ) . toHaveBeenCalledWith ( 'GET' , '/some-url' ) ;
279
284
} ) ;
@@ -334,20 +339,20 @@ describe('$httpBackend', function() {
334
339
335
340
var SCRIPT_URL = / ( [ ^ \? ] * ) \? c b = ( .* ) / ;
336
341
337
-
338
342
it ( 'should add script tag for JSONP request' , function ( ) {
339
343
callback . and . callFake ( function ( status , response ) {
340
344
expect ( status ) . toBe ( 200 ) ;
341
345
expect ( response ) . toBe ( 'some-data' ) ;
342
346
} ) ;
343
347
344
- $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
348
+ $backend ( 'JSONP' , 'http://special.whitelisted.resource.com/path?cb=JSON_CALLBACK' , null , callback ) ;
349
+
345
350
expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
346
351
347
352
var script = fakeDocument . $$scripts . shift ( ) ,
348
353
url = script . src . match ( SCRIPT_URL ) ;
349
354
350
- expect ( url [ 1 ] ) . toBe ( 'http://example.org /path' ) ;
355
+ expect ( url [ 1 ] ) . toBe ( 'http://special.whitelisted.resource.com /path' ) ;
351
356
$jsonpCallbacks [ url [ 2 ] ] ( 'some-data' ) ;
352
357
browserTrigger ( script , 'load' ) ;
353
358
@@ -358,7 +363,8 @@ describe('$httpBackend', function() {
358
363
it ( 'should clean up the callback and remove the script' , function ( ) {
359
364
spyOn ( $jsonpCallbacks , 'removeCallback' ) . and . callThrough ( ) ;
360
365
361
- $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
366
+ $backend ( 'JSONP' , 'http://special.whitelisted.resource.com/path?cb=JSON_CALLBACK' , null , callback ) ;
367
+
362
368
expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
363
369
364
370
@@ -375,6 +381,7 @@ describe('$httpBackend', function() {
375
381
376
382
it ( 'should set url to current location if not specified or empty string' , function ( ) {
377
383
$backend ( 'JSONP' , undefined , null , callback ) ;
384
+
378
385
expect ( fakeDocument . $$scripts [ 0 ] . src ) . toBe ( $browser . url ( ) ) ;
379
386
fakeDocument . $$scripts . shift ( ) ;
380
387
@@ -390,7 +397,8 @@ describe('$httpBackend', function() {
390
397
expect ( status ) . toBe ( - 1 ) ;
391
398
} ) ;
392
399
393
- $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback , null , 2000 ) ;
400
+ $backend ( 'JSONP' , 'http://special.whitelisted.resource.com/path?cb=JSON_CALLBACK' , null , callback , null , 2000 ) ;
401
+
394
402
expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
395
403
expect ( $browser . deferredFns [ 0 ] . time ) . toBe ( 2000 ) ;
396
404
@@ -405,6 +413,18 @@ describe('$httpBackend', function() {
405
413
} ) ;
406
414
407
415
416
+ it ( 'should throw error if the url is not a trusted resource' , function ( ) {
417
+ expect ( function ( ) {
418
+ $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
419
+ } ) . toThrowMinErr ( '$sce' , 'insecurl' ) ;
420
+ } ) ;
421
+
422
+ it ( 'should not throw error if the url is an explicitly trusted resource' , function ( ) {
423
+ expect ( function ( ) {
424
+ $backend ( 'JSONP' , $sce . trustAsResourceUrl ( 'http://example.org/path?cb=JSON_CALLBACK' ) , null , callback ) ;
425
+ } ) . not . toThrowMinErr ( '$sce' , 'insecurl' ) ;
426
+ } ) ;
427
+
408
428
// TODO(vojta): test whether it fires "async-start"
409
429
// TODO(vojta): test whether it fires "async-end" on both success and error
410
430
} ) ;
@@ -420,7 +440,7 @@ describe('$httpBackend', function() {
420
440
}
421
441
422
442
beforeEach ( function ( ) {
423
- $backend = createHttpBackend ( $browser , createMockXhr ) ;
443
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr ) ;
424
444
} ) ;
425
445
426
446
0 commit comments