3
3
4
4
describe ( '$httpBackend' , function ( ) {
5
5
6
- var $backend , $browser , callbacks ,
6
+ var $sce , $ backend, $browser , callbacks ,
7
7
xhr , fakeDocument , callback ;
8
8
9
9
10
10
beforeEach ( inject ( function ( $injector ) {
11
11
callbacks = { counter : 0 } ;
12
+ $sce = $injector . get ( '$sce' ) ;
12
13
$browser = $injector . get ( '$browser' ) ;
13
14
fakeDocument = {
14
15
$$scripts : [ ] ,
@@ -28,7 +29,7 @@ describe('$httpBackend', function() {
28
29
} )
29
30
}
30
31
} ;
31
- $backend = createHttpBackend ( $browser , createMockXhr , $browser . defer , callbacks , fakeDocument ) ;
32
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr , $browser . defer , callbacks , fakeDocument ) ;
32
33
callback = jasmine . createSpy ( 'done' ) ;
33
34
} ) ) ;
34
35
@@ -255,7 +256,22 @@ describe('$httpBackend', function() {
255
256
} ) ;
256
257
257
258
258
- describe ( 'JSONP' , function ( ) {
259
+ [ true , false ] . forEach ( function ( trustAsResourceUrl ) {
260
+ describe ( 'JSONP: trustAsResourceUrl=' + trustAsResourceUrl , function ( ) {
261
+
262
+
263
+ function $backendCall ( ) {
264
+ var args = Array . prototype . slice . call ( arguments ) ;
265
+ var url = args [ 1 ] ;
266
+ if ( trustAsResourceUrl || url == null ) {
267
+ args [ 1 ] = $sce . trustAsResourceUrl ( url ) ;
268
+ return $backend . apply ( null , args ) ;
269
+ } else {
270
+ var badUrlPrefix = url ? url . replace ( 'JSON_CALLBACK' , '' ) : '' ;
271
+ expect ( function ( ) { $backend . apply ( null , args ) ; } ) . toThrowMinErr (
272
+ '$sce' , 'insecurl' , 'Blocked loading resource from url not allowed by $sceDelegate policy. URL: ' + badUrlPrefix ) ;
273
+ }
274
+ }
259
275
260
276
var SCRIPT_URL = / ( [ ^ \? ] * ) \? c b = a n g u l a r \. c a l l b a c k s \. ( .* ) / ;
261
277
@@ -266,7 +282,9 @@ describe('$httpBackend', function() {
266
282
expect ( response ) . toBe ( 'some-data' ) ;
267
283
} ) ;
268
284
269
- $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
285
+ $backendCall ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
286
+ if ( ! trustAsResourceUrl ) return ;
287
+
270
288
expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
271
289
272
290
var script = fakeDocument . $$scripts . shift ( ) ,
@@ -281,7 +299,9 @@ describe('$httpBackend', function() {
281
299
282
300
283
301
it ( 'should clean up the callback and remove the script' , function ( ) {
284
- $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
302
+ $backendCall ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
303
+ if ( ! trustAsResourceUrl ) return ;
304
+
285
305
expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
286
306
287
307
@@ -297,11 +317,13 @@ describe('$httpBackend', function() {
297
317
298
318
299
319
it ( 'should set url to current location if not specified or empty string' , function ( ) {
300
- $backend ( 'JSONP' , undefined , null , callback ) ;
320
+ $backendCall ( 'JSONP' , undefined , null , callback ) ;
321
+ if ( ! trustAsResourceUrl ) return ;
322
+
301
323
expect ( fakeDocument . $$scripts [ 0 ] . src ) . toBe ( $browser . url ( ) ) ;
302
324
fakeDocument . $$scripts . shift ( ) ;
303
325
304
- $backend ( 'JSONP' , '' , null , callback ) ;
326
+ $backendCall ( 'JSONP' , '' , null , callback ) ;
305
327
expect ( fakeDocument . $$scripts [ 0 ] . src ) . toBe ( $browser . url ( ) ) ;
306
328
} ) ;
307
329
@@ -311,7 +333,9 @@ describe('$httpBackend', function() {
311
333
expect ( status ) . toBe ( - 1 ) ;
312
334
} ) ;
313
335
314
- $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback , null , 2000 ) ;
336
+ $backendCall ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback , null , 2000 ) ;
337
+ if ( ! trustAsResourceUrl ) return ;
338
+
315
339
expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
316
340
expect ( $browser . deferredFns [ 0 ] . time ) . toBe ( 2000 ) ;
317
341
@@ -328,7 +352,7 @@ describe('$httpBackend', function() {
328
352
329
353
// TODO(vojta): test whether it fires "async-start"
330
354
// TODO(vojta): test whether it fires "async-end" on both success and error
331
- } ) ;
355
+ } ) } ) ;
332
356
333
357
describe ( 'protocols that return 0 status code' , function ( ) {
334
358
@@ -341,7 +365,7 @@ describe('$httpBackend', function() {
341
365
342
366
343
367
it ( 'should convert 0 to 200 if content and file protocol' , function ( ) {
344
- $backend = createHttpBackend ( $browser , createMockXhr ) ;
368
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr ) ;
345
369
346
370
$backend ( 'GET' , 'file:///whatever/index.html' , null , callback ) ;
347
371
respond ( 0 , 'SOME CONTENT' ) ;
@@ -351,7 +375,7 @@ describe('$httpBackend', function() {
351
375
} ) ;
352
376
353
377
it ( 'should convert 0 to 200 if content for protocols other than file' , function ( ) {
354
- $backend = createHttpBackend ( $browser , createMockXhr ) ;
378
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr ) ;
355
379
356
380
$backend ( 'GET' , 'someProtocol:///whatever/index.html' , null , callback ) ;
357
381
respond ( 0 , 'SOME CONTENT' ) ;
@@ -361,7 +385,7 @@ describe('$httpBackend', function() {
361
385
} ) ;
362
386
363
387
it ( 'should convert 0 to 404 if no content and file protocol' , function ( ) {
364
- $backend = createHttpBackend ( $browser , createMockXhr ) ;
388
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr ) ;
365
389
366
390
$backend ( 'GET' , 'file:///whatever/index.html' , null , callback ) ;
367
391
respond ( 0 , '' ) ;
@@ -371,7 +395,7 @@ describe('$httpBackend', function() {
371
395
} ) ;
372
396
373
397
it ( 'should not convert 0 to 404 if no content for protocols other than file' , function ( ) {
374
- $backend = createHttpBackend ( $browser , createMockXhr ) ;
398
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr ) ;
375
399
376
400
$backend ( 'GET' , 'someProtocol:///whatever/index.html' , null , callback ) ;
377
401
respond ( 0 , '' ) ;
@@ -399,7 +423,7 @@ describe('$httpBackend', function() {
399
423
400
424
try {
401
425
402
- $backend = createHttpBackend ( $browser , createMockXhr ) ;
426
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr ) ;
403
427
404
428
$backend ( 'GET' , '/whatever/index.html' , null , callback ) ;
405
429
respond ( 0 , '' ) ;
@@ -414,7 +438,7 @@ describe('$httpBackend', function() {
414
438
415
439
416
440
it ( 'should return original backend status code if different from 0' , function ( ) {
417
- $backend = createHttpBackend ( $browser , createMockXhr ) ;
441
+ $backend = createHttpBackend ( $sce , $ browser, createMockXhr ) ;
418
442
419
443
// request to http://
420
444
$backend ( 'POST' , 'http://rest_api/create_whatever' , null , callback ) ;
0 commit comments