@@ -27,95 +27,137 @@ QUnit.test( "jQuery.ajax() deprecations on jqXHR", function( assert ) {
27
27
} ) ;
28
28
29
29
[ " - Same Domain" , " - Cross Domain" ] . forEach ( function ( label , crossDomain ) {
30
+ function runTests ( options ) {
31
+ var forceEnablePatch = ( options || { } ) . forceEnablePatch || false ;
30
32
31
- // The JSON-to-JSONP auto-promotion behavior is gone in jQuery 4.0 and as
32
- // it has security implications, we don't want to restore the legacy behavior.
33
- QUnit [ jQueryVersionSince ( "4.0.0" ) ? "skip" : "test" ] (
34
- "jQuery.ajax() JSON-to-JSONP auto-promotion" + label , function ( assert ) {
33
+ QUnit . test ( "jQuery.ajax() JSON-to-JSONP auto-promotion" + label + (
34
+ forceEnablePatch ? ", patch force-enabled" : ""
35
+ ) , function ( assert ) {
35
36
36
- assert . expect ( 5 ) ;
37
+ assert . expect ( 10 ) ;
37
38
38
- var done = assert . async ( ) ,
39
- tests = [
40
- function ( ) {
41
- return expectNoWarning ( assert , "dataType: \"json\"" ,
42
- function ( ) {
39
+ if ( forceEnablePatch ) {
40
+ jQuery . migrateEnablePatches ( "jsonp-promotion" ) ;
41
+ }
42
+
43
+ var done = assert . async ( ) ,
44
+ patchEnabled = forceEnablePatch || ! jQueryVersionSince ( "4.0.0" ) ,
45
+ tests = [
46
+ function ( ) {
47
+ var testName = "dataType: \"json\"" ;
48
+ return expectNoWarning ( assert , testName , function ( ) {
43
49
return jQuery . ajax ( {
44
50
url : url ( "null.json" ) ,
51
+ context : { testName : testName } ,
45
52
crossDomain : crossDomain ,
46
- dataType : "json"
47
- } ) . catch ( jQuery . noop ) ;
48
- }
49
- ) ;
50
- } ,
51
-
52
- function ( ) {
53
- return expectWarning ( assert , "dataType: \"json\", URL callback" , 1 ,
54
- function ( ) {
53
+ dataType : "json" ,
54
+ jsonpCallback : "customJsonpCallback"
55
+ } ) . then ( function ( ) {
56
+ assert . ok ( true , this . testName + " (success)" ) ;
57
+ } ) . catch ( function ( ) {
58
+ assert . ok ( false , this . testName + " (failure)" ) ;
59
+ } ) ;
60
+ } ) ;
61
+ } ,
62
+
63
+ function ( ) {
64
+ var testName = "dataType: \"json\", URL callback" ;
65
+ return expectWarning ( assert , testName , patchEnabled ? 1 : 0 , function ( ) {
55
66
return jQuery . ajax ( {
56
- url : url ( "null.json?callback=?" ) ,
67
+ url : url ( "jsonpScript.js?callback=?" ) ,
68
+ context : { testName : testName } ,
57
69
crossDomain : crossDomain ,
58
- dataType : "json"
59
- } ) . catch ( jQuery . noop ) ;
60
- }
61
- ) ;
62
- } ,
63
-
64
- function ( ) {
65
- return expectWarning ( assert , "dataType: \"json\", data callback" , 1 ,
66
- function ( ) {
70
+ dataType : "json" ,
71
+ jsonpCallback : "customJsonpCallback"
72
+ } ) . then ( function ( ) {
73
+ assert . ok ( patchEnabled , this . testName + " (success)" ) ;
74
+ } ) . catch ( function ( ) {
75
+ assert . ok ( ! patchEnabled , this . testName + " (failure)" ) ;
76
+ } ) ;
77
+ } ) ;
78
+ } ,
79
+
80
+ function ( ) {
81
+ var testName = "dataType: \"json\", data callback" ;
82
+ return expectWarning ( assert , testName , patchEnabled ? 1 : 0 , function ( ) {
67
83
return jQuery . ajax ( {
68
- url : url ( "null.json" ) ,
84
+ url : url ( "jsonpScript.js" ) ,
85
+ context : { testName : testName } ,
69
86
crossDomain : crossDomain ,
70
87
data : "callback=?" ,
71
- dataType : "json"
72
- } ) . catch ( jQuery . noop ) ;
73
- }
74
- ) ;
75
- } ,
76
-
77
- function ( ) {
78
- return expectNoWarning ( assert , "dataType: \"jsonp\", URL callback" ,
79
- function ( ) {
88
+ dataType : "json" ,
89
+ jsonpCallback : "customJsonpCallback"
90
+ } ) . then ( function ( ) {
91
+ assert . ok ( patchEnabled , this . testName + " (success)" ) ;
92
+ } ) . catch ( function ( ) {
93
+ assert . ok ( ! patchEnabled , this . testName + " (failure)" ) ;
94
+ } ) ;
95
+ } ) ;
96
+ } ,
97
+
98
+ function ( ) {
99
+ var testName = "dataType: \"jsonp\", URL callback" ;
100
+ return expectNoWarning ( assert , testName , function ( ) {
80
101
return jQuery . ajax ( {
81
- url : url ( "null.json?callback=?" ) ,
102
+ url : url ( "jsonpScript.js?callback=?" ) ,
103
+ context : { testName : testName } ,
82
104
crossDomain : crossDomain ,
83
- dataType : "jsonp"
84
- } ) . catch ( jQuery . noop ) ;
85
- }
86
- ) ;
87
- } ,
88
-
89
- function ( ) {
90
- return expectNoWarning ( assert , "dataType: \"jsonp\", data callback" ,
91
- function ( ) {
92
- return jQuery . ajax ( {
93
- url : url ( "null.json" ) ,
105
+ dataType : "jsonp" ,
106
+ jsonpCallback : "customJsonpCallback"
107
+ } ) . then ( function ( ) {
108
+ assert . ok ( true , this . testName + " (success)" ) ;
109
+ } ) . catch ( function ( ) {
110
+ assert . ok ( false , this . testName + " (failure)" ) ;
111
+ } ) ;
112
+ } ) ;
113
+ } ,
114
+
115
+ function ( ) {
116
+ var testName = "dataType: \"jsonp\", data callback" ;
117
+ return expectNoWarning ( assert , testName , function ( ) {
118
+ return jQuery . ajax ( {
119
+ url : url ( "jsonpScript.js" ) ,
120
+ context : { testName : testName } ,
94
121
crossDomain : crossDomain ,
95
122
data : "callback=?" ,
96
- dataType : "jsonp"
97
- } ) . catch ( jQuery . noop ) ;
98
- }
99
- ) ;
100
- }
101
- ] ;
102
-
103
- // Invoke tests sequentially as they're async and early tests could get warnings
104
- // from later ones.
105
- function run ( tests ) {
106
- var test = tests [ 0 ] ;
107
- return test ( ) . then ( function ( ) {
108
- if ( tests . length > 1 ) {
109
- return run ( tests . slice ( 1 ) ) ;
110
- }
111
- } ) ;
112
- }
113
-
114
- run ( tests )
115
- . then ( function ( ) {
116
- done ( ) ;
117
- } ) ;
118
- } ) ;
123
+ dataType : "jsonp" ,
124
+ jsonpCallback : "customJsonpCallback"
125
+ } ) . then ( function ( ) {
126
+ assert . ok ( true , this . testName + " (success)" ) ;
127
+ } ) . catch ( function ( ) {
128
+ assert . ok ( false , this . testName + " (failure)" ) ;
129
+ } ) ;
130
+ } ) ;
131
+ }
132
+ ] ;
133
+
134
+ // Invoke tests sequentially as they're async and early tests could get warnings
135
+ // from later ones.
136
+ function run ( tests ) {
137
+ var test = tests [ 0 ] ;
138
+ return test ( ) . then ( function ( ) {
139
+ if ( tests . length > 1 ) {
140
+ return run ( tests . slice ( 1 ) ) ;
141
+ }
142
+ } ) ;
143
+ }
144
+
145
+ run ( tests )
146
+ . then ( function ( ) {
147
+ done ( ) ;
148
+ } ) ;
149
+ } ) ;
150
+ }
151
+
152
+ if ( jQueryVersionSince ( "4.0.0" ) ) {
153
+
154
+ // In jQuery 4+, this behavior is disabled by default for security
155
+ // reasons, re-enable for this test, but test default behavior as well.
156
+ runTests ( { forceEnablePatch : true } ) ;
157
+ runTests ( { forceEnablePatch : false } ) ;
158
+ } else {
159
+ runTests ( ) ;
160
+ }
119
161
} ) ;
120
162
121
163
}
0 commit comments