@@ -326,10 +326,8 @@ describe('$http', function() {
326
326
} ) ;
327
327
328
328
it ( 'should accept a $sce trusted object for the request configuration url' , function ( ) {
329
- expect ( function ( ) {
330
- $httpBackend . expect ( 'GET' , '/url' ) . respond ( '' ) ;
331
- $http ( { url : $sce . trustAsResourceUrl ( '/url' ) } ) ;
332
- } ) . not . toThrowMinErr ( '$http' , 'badreq' , 'Http request configuration url must be a string. Received: false' ) ;
329
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( '' ) ;
330
+ $http ( { url : $sce . trustAsResourceUrl ( '/url' ) } ) ;
333
331
} ) ;
334
332
335
333
it ( 'should send GET requests if no method specified' , function ( ) {
@@ -622,7 +620,7 @@ describe('$http', function() {
622
620
expect ( r . headers ( ) ) . toEqual ( Object . create ( null ) ) ;
623
621
} ) ;
624
622
625
- $httpBackend . expect ( 'JSONP' , '/some' ) . respond ( 200 ) ;
623
+ $httpBackend . expect ( 'JSONP' , '/some?callback=JSON_CALLBACK ' ) . respond ( 200 ) ;
626
624
$http ( { url : $sce . trustAsResourceUrl ( '/some' ) , method : 'JSONP' } ) . then ( callback ) ;
627
625
$httpBackend . flush ( ) ;
628
626
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
@@ -1030,39 +1028,80 @@ describe('$http', function() {
1030
1028
} ) ;
1031
1029
1032
1030
it ( 'should have jsonp()' , function ( ) {
1033
- $httpBackend . expect ( 'JSONP' , '/url' ) . respond ( '' ) ;
1031
+ $httpBackend . expect ( 'JSONP' , '/url?callback=JSON_CALLBACK ' ) . respond ( '' ) ;
1034
1032
$http . jsonp ( $sce . trustAsResourceUrl ( '/url' ) ) ;
1035
1033
} ) ;
1036
1034
1037
1035
1038
1036
it ( 'jsonp() should allow config param' , function ( ) {
1039
- $httpBackend . expect ( 'JSONP' , '/url' , undefined , checkHeader ( 'Custom' , 'Header' ) ) . respond ( '' ) ;
1037
+ $httpBackend . expect ( 'JSONP' , '/url?callback=JSON_CALLBACK ' , undefined , checkHeader ( 'Custom' , 'Header' ) ) . respond ( '' ) ;
1040
1038
$http . jsonp ( $sce . trustAsResourceUrl ( '/url' ) , { headers : { 'Custom' : 'Header' } } ) ;
1041
1039
} ) ;
1042
1040
} ) ;
1043
1041
1044
1042
describe ( 'jsonp trust' , function ( ) {
1045
1043
it ( 'should throw error if the url is not a trusted resource' , function ( ) {
1046
1044
var success , error ;
1047
- $http ( { method : 'JSONP' , url : 'http://example.org/path?cb=JSON_CALLBACK' } ) . catch (
1048
- function ( e ) { error = e ; }
1049
- ) ;
1045
+ $http ( { method : 'JSONP' , url : 'http://example.org/path' } )
1046
+ . catch ( function ( e ) { error = e ; } ) ;
1050
1047
$rootScope . $digest ( ) ;
1051
1048
expect ( error . message ) . toContain ( '[$sce:insecurl]' ) ;
1052
1049
} ) ;
1053
1050
1054
1051
it ( 'should accept an explicitly trusted resource url' , function ( ) {
1055
- $httpBackend . expect ( 'JSONP' , 'http://example.org/path?cb =JSON_CALLBACK' ) . respond ( '' ) ;
1056
- $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( 'http://example.org/path?cb=JSON_CALLBACK ' ) } ) ;
1052
+ $httpBackend . expect ( 'JSONP' , 'http://example.org/path?callback =JSON_CALLBACK' ) . respond ( '' ) ;
1053
+ $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( 'http://example.org/path' ) } ) ;
1057
1054
} ) ;
1058
1055
1059
1056
it ( 'jsonp() should accept explictly trusted urls' , function ( ) {
1060
- $httpBackend . expect ( 'JSONP' , '/url' ) . respond ( '' ) ;
1057
+ $httpBackend . expect ( 'JSONP' , '/url?callback=JSON_CALLBACK ' ) . respond ( '' ) ;
1061
1058
$http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( '/url' ) } ) ;
1062
1059
1063
- $httpBackend . expect ( 'JSONP' , '/url?a=b' ) . respond ( '' ) ;
1060
+ $httpBackend . expect ( 'JSONP' , '/url?a=b&callback=JSON_CALLBACK ' ) . respond ( '' ) ;
1064
1061
$http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( '/url' ) , params : { a : 'b' } } ) ;
1065
1062
} ) ;
1063
+
1064
+ it ( 'should error if the URL contains a JSON_CALLBACK parameter' , function ( ) {
1065
+ var error ;
1066
+ $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( 'http://example.org/path?callback=JSON_CALLBACK' ) } )
1067
+ . catch ( function ( e ) { error = e ; } ) ;
1068
+ $rootScope . $digest ( ) ;
1069
+ expect ( error . message ) . toContain ( '[$http:badjsonp]' ) ;
1070
+
1071
+ error = undefined ;
1072
+ $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( 'http://example.org/path?other=JSON_CALLBACK' ) } )
1073
+ . catch ( function ( e ) { error = e ; } ) ;
1074
+ $rootScope . $digest ( ) ;
1075
+ expect ( error . message ) . toContain ( '[$http:badjsonp]' ) ;
1076
+ } ) ;
1077
+
1078
+ it ( 'should error if a param contains a JSON_CALLBACK value' , function ( ) {
1079
+ var error ;
1080
+ $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( 'http://example.org/path' ) , params : { callback : 'JSON_CALLBACK' } } )
1081
+ . catch ( function ( e ) { error = e ; } ) ;
1082
+ $rootScope . $digest ( ) ;
1083
+ expect ( error . message ) . toContain ( '[$http:badjsonp]' ) ;
1084
+
1085
+ error = undefined ;
1086
+ $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( 'http://example.org/path' ) , params : { other : 'JSON_CALLBACK' } } )
1087
+ . catch ( function ( e ) { error = e ; } ) ;
1088
+ $rootScope . $digest ( ) ;
1089
+ expect ( error . message ) . toContain ( '[$http:badjsonp]' ) ;
1090
+ } ) ;
1091
+
1092
+ it ( 'should error if there is already a param matching the jsonpCallbackParam key' , function ( ) {
1093
+ var error ;
1094
+ $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( 'http://example.org/path' ) , params : { callback : 'evilThing' } } )
1095
+ . catch ( function ( e ) { error = e ; } ) ;
1096
+ $rootScope . $digest ( ) ;
1097
+ expect ( error . message ) . toContain ( '[$http:badjsonp]' ) ;
1098
+
1099
+ error = undefined ;
1100
+ $http ( { method : 'JSONP' , jsonpCallbackParam : 'cb' , url : $sce . trustAsResourceUrl ( 'http://example.org/path' ) , params : { cb : 'evilThing' } } )
1101
+ . catch ( function ( e ) { error = e ; } ) ;
1102
+ $rootScope . $digest ( ) ;
1103
+ expect ( error . message ) . toContain ( '[$http:badjsonp]' ) ;
1104
+ } ) ;
1066
1105
} ) ;
1067
1106
1068
1107
describe ( 'callbacks' , function ( ) {
@@ -1524,11 +1563,11 @@ describe('$http', function() {
1524
1563
} ) ) ;
1525
1564
1526
1565
it ( 'should cache JSONP request when cache is provided' , inject ( function ( $rootScope ) {
1527
- $httpBackend . expect ( 'JSONP' , '/url?cb =JSON_CALLBACK' ) . respond ( 'content' ) ;
1528
- $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( '/url?cb=JSON_CALLBACK ' ) , cache : cache } ) ;
1566
+ $httpBackend . expect ( 'JSONP' , '/url?callback =JSON_CALLBACK' ) . respond ( 'content' ) ;
1567
+ $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( '/url' ) , cache : cache } ) ;
1529
1568
$httpBackend . flush ( ) ;
1530
1569
1531
- $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( '/url?cb=JSON_CALLBACK ' ) , cache : cache } ) . success ( callback ) ;
1570
+ $http ( { method : 'JSONP' , url : $sce . trustAsResourceUrl ( '/url' ) , cache : cache } ) . success ( callback ) ;
1532
1571
$rootScope . $digest ( ) ;
1533
1572
1534
1573
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
0 commit comments