@@ -1267,6 +1267,82 @@ describe('globals', function() {
1267
1267
} ) ;
1268
1268
1269
1269
describe ( 'makeRequest' , function ( ) {
1270
+ beforeEach ( function ( ) {
1271
+ // use fake xml http request so we can muck w/ its prototype
1272
+ this . xhr = sinon . useFakeXMLHttpRequest ( ) ;
1273
+ this . sinon . stub ( window , 'makeImageRequest' ) ;
1274
+ this . sinon . stub ( window , 'makeXhrRequest' ) ;
1275
+ } ) ;
1276
+
1277
+ afterEach ( function ( ) {
1278
+ this . xhr . restore ( ) ;
1279
+ } ) ;
1280
+
1281
+ it ( 'should call makeXhrRequest if CORS is supported' , function ( ) {
1282
+ XMLHttpRequest . prototype . withCredentials = true ;
1283
+
1284
+ makeRequest ( {
1285
+ url : 'http://localhost/' ,
1286
+ auth : { a : '1' , b : '2' } ,
1287
+ data : { foo : 'bar' } ,
1288
+ options : globalOptions
1289
+ } ) ;
1290
+
1291
+ assert . isTrue ( makeImageRequest . notCalled ) ;
1292
+ assert . isTrue ( makeXhrRequest . calledOnce ) ;
1293
+ } ) ;
1294
+
1295
+ it ( 'should call makeImageRequest if CORS is NOT supported' , function ( ) {
1296
+ delete XMLHttpRequest . prototype . withCredentials ;
1297
+
1298
+ var oldXDR = window . XDomainRequest ;
1299
+ window . XDomainRequest = undefined ;
1300
+
1301
+ makeRequest ( {
1302
+ url : 'http://localhost/' ,
1303
+ auth : { a : '1' , b : '2' } ,
1304
+ data : { foo : 'bar' } ,
1305
+ options : globalOptions
1306
+ } ) ;
1307
+
1308
+ assert . isTrue ( makeImageRequest . calledOnce ) ;
1309
+ assert . isTrue ( makeXhrRequest . notCalled ) ;
1310
+
1311
+ window . XDomainRequest = oldXDR ;
1312
+ } ) ;
1313
+ } ) ;
1314
+
1315
+ describe ( 'makeXhrRequest' , function ( ) {
1316
+ beforeEach ( function ( ) {
1317
+ // NOTE: can't seem to call useFakeXMLHttpRequest via sandbox; must
1318
+ // restore manually
1319
+ this . xhr = sinon . useFakeXMLHttpRequest ( ) ;
1320
+ var requests = this . requests = [ ] ;
1321
+
1322
+ this . xhr . onCreate = function ( xhr ) {
1323
+ requests . push ( xhr ) ;
1324
+ } ;
1325
+ } ) ;
1326
+
1327
+ afterEach ( function ( ) {
1328
+ this . xhr . restore ( ) ;
1329
+ } ) ;
1330
+
1331
+ it ( 'should create an XMLHttpRequest object with body as JSON payload' , function ( ) {
1332
+ makeXhrRequest ( {
1333
+ url : 'http://localhost/' ,
1334
+ auth : { a : '1' , b : '2' } ,
1335
+ data : { foo : 'bar' } ,
1336
+ options : globalOptions
1337
+ } ) ;
1338
+
1339
+ var lastXhr = this . requests [ this . requests . length - 1 ] ;
1340
+ assert . equal ( lastXhr . requestBody , '{"foo":"bar"}' ) ;
1341
+ assert . equal ( lastXhr . url , 'http://localhost/?a=1&b=2' ) ;
1342
+ } ) ;
1343
+ } ) ;
1344
+
1345
+ describe ( 'makeImageRequest' , function ( ) {
1270
1346
var imageCache ;
1271
1347
1272
1348
beforeEach ( function ( ) {
@@ -1275,7 +1351,7 @@ describe('globals', function() {
1275
1351
} )
1276
1352
1277
1353
it ( 'should load an Image' , function ( ) {
1278
- makeRequest ( {
1354
+ makeImageRequest ( {
1279
1355
url : 'http://localhost/' ,
1280
1356
auth : { a : '1' , b : '2' } ,
1281
1357
data : { foo : 'bar' } ,
@@ -1289,7 +1365,7 @@ describe('globals', function() {
1289
1365
globalOptions = {
1290
1366
crossOrigin : 'something'
1291
1367
} ;
1292
- makeRequest ( {
1368
+ makeImageRequest ( {
1293
1369
url : globalServer ,
1294
1370
auth : { lol : '1' } ,
1295
1371
data : { foo : 'bar' } ,
@@ -1303,7 +1379,7 @@ describe('globals', function() {
1303
1379
globalOptions = {
1304
1380
crossOrigin : ''
1305
1381
} ;
1306
- makeRequest ( {
1382
+ makeImageRequest ( {
1307
1383
url : globalServer ,
1308
1384
auth : { lol : '1' } ,
1309
1385
data : { foo : 'bar' } ,
@@ -1317,7 +1393,7 @@ describe('globals', function() {
1317
1393
globalOptions = {
1318
1394
crossOrigin : false
1319
1395
} ;
1320
- makeRequest ( {
1396
+ makeImageRequest ( {
1321
1397
url : globalServer ,
1322
1398
auth : { lol : '1' } ,
1323
1399
data : { foo : 'bar' } ,
@@ -1972,11 +2048,11 @@ describe('Raven (public API)', function() {
1972
2048
1973
2049
it ( 'should work as advertised #integration' , function ( ) {
1974
2050
var imageCache = [ ] ;
1975
- this . sinon . stub ( window , 'newImage' , function ( ) { var img = { } ; imageCache . push ( img ) ; return img ; } ) ;
2051
+ this . sinon . stub ( window , 'makeRequest' ) ;
1976
2052
1977
2053
setupRaven ( ) ;
1978
2054
Raven . captureMessage ( 'lol' , { foo : 'bar' } ) ;
1979
- assert . equal ( imageCache . length , 1 ) ;
2055
+ assert . equal ( window . makeRequest . callCount , 1 ) ;
1980
2056
// It'd be hard to assert the actual payload being sent
1981
2057
// since it includes the generated url, which is going to
1982
2058
// vary between users running the tests
0 commit comments