@@ -23,6 +23,7 @@ _Raven.prototype._getUuid = function() {
23
23
var utils = require ( '../src/utils' ) ;
24
24
var joinRegExp = utils . joinRegExp ;
25
25
var supportsErrorEvent = utils . supportsErrorEvent ;
26
+ var supportsFetch = utils . supportsFetch ;
26
27
27
28
// window.console must be stubbed in for browsers that don't have it
28
29
if ( typeof window . console === 'undefined' ) {
@@ -1495,7 +1496,10 @@ describe('globals', function() {
1495
1496
assert . equal ( Raven . _backoffDuration , 2000 ) ;
1496
1497
} ) ;
1497
1498
1498
- it ( 'should set backoffDuration to value of Retry-If header if present' , function ( ) {
1499
+ it ( 'should set backoffDuration to value of Retry-If header if present - XHR API' , function ( ) {
1500
+ var origFetch = window . fetch ;
1501
+ delete window . fetch ;
1502
+
1499
1503
this . sinon . stub ( Raven , 'isSetup' ) . returns ( true ) ;
1500
1504
this . sinon . stub ( Raven , '_makeRequest' ) ;
1501
1505
@@ -1509,12 +1513,40 @@ describe('globals', function() {
1509
1513
. withArgs ( 'Retry-After' )
1510
1514
. returns ( '2' )
1511
1515
} ;
1516
+
1512
1517
opts . onError ( mockError ) ;
1513
1518
1514
1519
assert . equal ( Raven . _backoffStart , 100 ) ; // clock is at 100ms
1515
1520
assert . equal ( Raven . _backoffDuration , 2000 ) ; // converted to ms, int
1521
+
1522
+ window . fetch = origFetch ;
1516
1523
} ) ;
1517
1524
1525
+ if ( supportsFetch ( ) ) {
1526
+ it ( 'should set backoffDuration to value of Retry-If header if present - FETCH API' , function ( ) {
1527
+ this . sinon . stub ( Raven , 'isSetup' ) . returns ( true ) ;
1528
+ this . sinon . stub ( Raven , '_makeRequest' ) ;
1529
+
1530
+ Raven . _send ( { message : 'bar' } ) ;
1531
+ var opts = Raven . _makeRequest . lastCall . args [ 0 ] ;
1532
+ var mockError = new Error ( '401: Unauthorized' ) ;
1533
+ mockError . request = {
1534
+ status : 401 ,
1535
+ headers : {
1536
+ get : sinon
1537
+ . stub ( )
1538
+ . withArgs ( 'Retry-After' )
1539
+ . returns ( '2' )
1540
+ }
1541
+ } ;
1542
+
1543
+ opts . onError ( mockError ) ;
1544
+
1545
+ assert . equal ( Raven . _backoffStart , 100 ) ; // clock is at 100ms
1546
+ assert . equal ( Raven . _backoffDuration , 2000 ) ; // converted to ms, int
1547
+ } ) ;
1548
+ }
1549
+
1518
1550
it ( 'should reset backoffDuration and backoffStart if onSuccess is fired (200)' , function ( ) {
1519
1551
this . sinon . stub ( Raven , 'isSetup' ) . returns ( true ) ;
1520
1552
this . sinon . stub ( Raven , '_makeRequest' ) ;
@@ -1653,74 +1685,138 @@ describe('globals', function() {
1653
1685
} ) ;
1654
1686
1655
1687
describe ( 'makeRequest' , function ( ) {
1656
- beforeEach ( function ( ) {
1657
- // NOTE: can't seem to call useFakeXMLHttpRequest via sandbox; must
1658
- // restore manually
1659
- this . xhr = sinon . useFakeXMLHttpRequest ( ) ;
1660
- var requests = ( this . requests = [ ] ) ;
1688
+ if ( supportsFetch ( ) ) {
1689
+ describe ( 'using Fetch API' , function ( ) {
1690
+ afterEach ( function ( ) {
1691
+ window . fetch . restore ( ) ;
1692
+ } ) ;
1661
1693
1662
- this . xhr . onCreate = function ( xhr ) {
1663
- requests . push ( xhr ) ;
1664
- } ;
1665
- } ) ;
1694
+ it ( 'should create an XMLHttpRequest object with body as JSON payload' , function ( ) {
1695
+ this . sinon . spy ( window , 'fetch' ) ;
1666
1696
1667
- afterEach ( function ( ) {
1668
- this . xhr . restore ( ) ;
1669
- } ) ;
1697
+ Raven . _makeRequest ( {
1698
+ url : 'http://localhost/' ,
1699
+ auth : { a : '1' , b : '2' } ,
1700
+ data : { foo : 'bar' } ,
1701
+ options : Raven . _globalOptions
1702
+ } ) ;
1703
+
1704
+ assert . deepEqual ( window . fetch . lastCall . args , [
1705
+ 'http://localhost/?a=1&b=2' ,
1706
+ {
1707
+ method : 'POST' ,
1708
+ body : '{"foo":"bar"}'
1709
+ }
1710
+ ] ) ;
1711
+ } ) ;
1670
1712
1671
- it ( 'should create an XMLHttpRequest object with body as JSON payload' , function ( ) {
1672
- XMLHttpRequest . prototype . withCredentials = true ;
1713
+ it ( 'should pass a request object to onError' , function ( done ) {
1714
+ sinon . stub ( window , 'fetch' ) ;
1715
+ window . fetch . returns (
1716
+ Promise . resolve (
1717
+ new window . Response ( '{"foo":"bar"}' , {
1718
+ ok : false ,
1719
+ status : 429 ,
1720
+ headers : {
1721
+ 'Content-type' : 'text/html'
1722
+ }
1723
+ } )
1724
+ )
1725
+ ) ;
1726
+
1727
+ Raven . _makeRequest ( {
1728
+ url : 'http://localhost/' ,
1729
+ auth : { a : '1' , b : '2' } ,
1730
+ data : { foo : 'bar' } ,
1731
+ options : Raven . _globalOptions ,
1732
+ onError : function ( error ) {
1733
+ assert . equal ( error . request . status , 429 ) ;
1734
+ done ( ) ;
1735
+ }
1736
+ } ) ;
1737
+ } ) ;
1738
+ } ) ;
1739
+ }
1740
+
1741
+ describe ( 'using XHR API' , function ( ) {
1742
+ var origFetch = window . fetch ;
1743
+ var xhr ;
1744
+ var requests ;
1673
1745
1674
- Raven . _makeRequest ( {
1675
- url : 'http://localhost/' ,
1676
- auth : { a : '1' , b : '2' } ,
1677
- data : { foo : 'bar' } ,
1678
- options : Raven . _globalOptions
1746
+ before ( function ( ) {
1747
+ delete window . fetch ;
1679
1748
} ) ;
1680
1749
1681
- var lastXhr = this . requests [ this . requests . length - 1 ] ;
1682
- assert . equal ( lastXhr . requestBody , '{"foo":"bar"}' ) ;
1683
- assert . equal ( lastXhr . url , 'http://localhost/?a=1&b=2' ) ;
1684
- } ) ;
1750
+ after ( function ( ) {
1751
+ window . fetch = origFetch ;
1752
+ } ) ;
1685
1753
1686
- it ( 'should no-op if CORS is not supported' , function ( ) {
1687
- delete XMLHttpRequest . prototype . withCredentials ;
1688
- var oldSupportsCORS = sinon . xhr . supportsCORS ;
1689
- sinon . xhr . supportsCORS = false ;
1754
+ beforeEach ( function ( ) {
1755
+ // NOTE: can't seem to call useFakeXMLHttpRequest via sandbox; must restore manually
1756
+ xhr = sinon . useFakeXMLHttpRequest ( ) ;
1757
+ requests = [ ] ;
1690
1758
1691
- var oldXDR = window . XDomainRequest ;
1692
- window . XDomainRequest = undefined ;
1759
+ XMLHttpRequest . prototype . withCredentials = true ;
1693
1760
1694
- Raven . _makeRequest ( {
1695
- url : 'http://localhost/' ,
1696
- auth : { a : '1' , b : '2' } ,
1697
- data : { foo : 'bar' } ,
1698
- options : Raven . _globalOptions
1761
+ xhr . onCreate = function ( xhr ) {
1762
+ requests . push ( xhr ) ;
1763
+ } ;
1699
1764
} ) ;
1700
1765
1701
- assert . equal ( this . requests . length , 1 ) ; // the "test" xhr
1702
- assert . equal ( this . requests [ 0 ] . readyState , 0 ) ;
1766
+ afterEach ( function ( ) {
1767
+ xhr . restore ( ) ;
1768
+ } ) ;
1703
1769
1704
- sinon . xhr . supportsCORS = oldSupportsCORS ;
1705
- window . XDomainRequest = oldXDR ;
1706
- } ) ;
1770
+ it ( 'should create an XMLHttpRequest object with body as JSON payload' , function ( ) {
1771
+ Raven . _makeRequest ( {
1772
+ url : 'http://localhost/' ,
1773
+ auth : { a : '1' , b : '2' } ,
1774
+ data : { foo : 'bar' } ,
1775
+ options : Raven . _globalOptions
1776
+ } ) ;
1707
1777
1708
- it ( 'should pass a request object to onError' , function ( done ) {
1709
- XMLHttpRequest . prototype . withCredentials = true ;
1778
+ var lastXhr = requests [ requests . length - 1 ] ;
1779
+ assert . equal ( lastXhr . requestBody , '{"foo":"bar"}' ) ;
1780
+ assert . equal ( lastXhr . url , 'http://localhost/?a=1&b=2' ) ;
1781
+ } ) ;
1710
1782
1711
- Raven . _makeRequest ( {
1712
- url : 'http://localhost/' ,
1713
- auth : { a : '1' , b : '2' } ,
1714
- data : { foo : 'bar' } ,
1715
- options : Raven . _globalOptions ,
1716
- onError : function ( error ) {
1717
- assert . equal ( error . request . status , 429 ) ;
1718
- done ( ) ;
1719
- }
1783
+ it ( 'should pass a request object to onError' , function ( done ) {
1784
+ Raven . _makeRequest ( {
1785
+ url : 'http://localhost/' ,
1786
+ auth : { a : '1' , b : '2' } ,
1787
+ data : { foo : 'bar' } ,
1788
+ options : Raven . _globalOptions ,
1789
+ onError : function ( error ) {
1790
+ assert . equal ( error . request . status , 429 ) ;
1791
+ done ( ) ;
1792
+ }
1793
+ } ) ;
1794
+
1795
+ var lastXhr = requests [ requests . length - 1 ] ;
1796
+ lastXhr . respond ( 429 , { 'Content-Type' : 'text/html' } , 'Too many requests' ) ;
1720
1797
} ) ;
1721
1798
1722
- var lastXhr = this . requests [ this . requests . length - 1 ] ;
1723
- lastXhr . respond ( 429 , { 'Content-Type' : 'text/html' } , 'Too many requests' ) ;
1799
+ it ( 'should no-op if CORS is not supported' , function ( ) {
1800
+ delete XMLHttpRequest . prototype . withCredentials ;
1801
+ var oldSupportsCORS = sinon . xhr . supportsCORS ;
1802
+ sinon . xhr . supportsCORS = false ;
1803
+
1804
+ var oldXDR = window . XDomainRequest ;
1805
+ window . XDomainRequest = undefined ;
1806
+
1807
+ Raven . _makeRequest ( {
1808
+ url : 'http://localhost/' ,
1809
+ auth : { a : '1' , b : '2' } ,
1810
+ data : { foo : 'bar' } ,
1811
+ options : Raven . _globalOptions
1812
+ } ) ;
1813
+
1814
+ assert . equal ( requests . length , 1 ) ; // the "test" xhr
1815
+ assert . equal ( requests [ 0 ] . readyState , 0 ) ;
1816
+
1817
+ sinon . xhr . supportsCORS = oldSupportsCORS ;
1818
+ window . XDomainRequest = oldXDR ;
1819
+ } ) ;
1724
1820
} ) ;
1725
1821
} ) ;
1726
1822
0 commit comments