11'use strict' ;
2+ const originalFetch = global . fetch ;
23
34const Parse = require ( '../../node' ) ;
45const sleep = require ( './sleep' ) ;
5-
66const Item = Parse . Object . extend ( 'IdempotencyItem' ) ;
7- const RESTController = Parse . CoreManager . getRESTController ( ) ;
87
9- const XHR = RESTController . _getXHR ( ) ;
10- function DuplicateXHR ( requestId ) {
11- function XHRWrapper ( ) {
12- const xhr = new XHR ( ) ;
13- const send = xhr . send ;
14- xhr . send = function ( ) {
15- this . setRequestHeader ( 'X-Parse-Request-Id' , requestId ) ;
16- send . apply ( this , arguments ) ;
17- } ;
18- return xhr ;
19- }
20- return XHRWrapper ;
8+ function DuplicateRequestId ( requestId ) {
9+ global . fetch = async ( ...args ) => {
10+ const options = args [ 1 ] ;
11+ options . headers [ 'X-Parse-Request-Id' ] = requestId ;
12+ return originalFetch ( ...args ) ;
13+ } ;
2114}
2215
2316describe ( 'Idempotency' , ( ) => {
24- beforeEach ( ( ) => {
25- RESTController . _setXHR ( XHR ) ;
17+ afterEach ( ( ) => {
18+ global . fetch = originalFetch ;
2619 } ) ;
2720
2821 it ( 'handle duplicate cloud code function request' , async ( ) => {
29- RESTController . _setXHR ( DuplicateXHR ( '1234' ) ) ;
22+ DuplicateRequestId ( '1234' ) ;
3023 await Parse . Cloud . run ( 'CloudFunctionIdempotency' ) ;
3124 await expectAsync ( Parse . Cloud . run ( 'CloudFunctionIdempotency' ) ) . toBeRejectedWithError (
3225 'Duplicate request'
3326 ) ;
3427 await expectAsync ( Parse . Cloud . run ( 'CloudFunctionIdempotency' ) ) . toBeRejectedWithError (
3528 'Duplicate request'
3629 ) ;
37-
3830 const query = new Parse . Query ( Item ) ;
3931 const results = await query . find ( ) ;
4032 expect ( results . length ) . toBe ( 1 ) ;
4133 } ) ;
4234
4335 it ( 'handle duplicate job request' , async ( ) => {
44- RESTController . _setXHR ( DuplicateXHR ( '1234' ) ) ;
36+ DuplicateRequestId ( '1234' ) ;
4537 const params = { startedBy : 'Monty Python' } ;
4638 const jobStatusId = await Parse . Cloud . startJob ( 'CloudJob1' , params ) ;
4739 await expectAsync ( Parse . Cloud . startJob ( 'CloudJob1' , params ) ) . toBeRejectedWithError (
@@ -61,12 +53,12 @@ describe('Idempotency', () => {
6153 } ) ;
6254
6355 it ( 'handle duplicate POST / PUT request' , async ( ) => {
64- RESTController . _setXHR ( DuplicateXHR ( '1234' ) ) ;
56+ DuplicateRequestId ( '1234' ) ;
6557 const testObject = new Parse . Object ( 'IdempotentTest' ) ;
6658 await testObject . save ( ) ;
6759 await expectAsync ( testObject . save ( ) ) . toBeRejectedWithError ( 'Duplicate request' ) ;
6860
69- RESTController . _setXHR ( DuplicateXHR ( '5678' ) ) ;
61+ DuplicateRequestId ( '5678' ) ;
7062 testObject . set ( 'foo' , 'bar' ) ;
7163 await testObject . save ( ) ;
7264 await expectAsync ( testObject . save ( ) ) . toBeRejectedWithError ( 'Duplicate request' ) ;
0 commit comments