|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const clear = require('./clear'); |
| 4 | +const Parse = require('../../node'); |
| 5 | + |
| 6 | +const Item = Parse.Object.extend('IdempotencyItem'); |
| 7 | +const RESTController = Parse.CoreManager.getRESTController(); |
| 8 | + |
| 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; |
| 21 | +} |
| 22 | + |
| 23 | +describe('Idempotency', () => { |
| 24 | + beforeEach((done) => { |
| 25 | + Parse.initialize('integration', null, 'notsosecret'); |
| 26 | + Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse'); |
| 27 | + Parse.Storage._clear(); |
| 28 | + RESTController._setXHR(XHR); |
| 29 | + clear().then(() => { |
| 30 | + done(); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('handle duplicate cloud code function request', async () => { |
| 35 | + RESTController._setXHR(DuplicateXHR('1234')); |
| 36 | + await Parse.Cloud.run('CloudFunctionIdempotency'); |
| 37 | + await expectAsync(Parse.Cloud.run('CloudFunctionIdempotency')).toBeRejectedWithError('Duplicate request'); |
| 38 | + await expectAsync(Parse.Cloud.run('CloudFunctionIdempotency')).toBeRejectedWithError('Duplicate request'); |
| 39 | + |
| 40 | + const query = new Parse.Query(Item); |
| 41 | + const results = await query.find(); |
| 42 | + expect(results.length).toBe(1); |
| 43 | + }); |
| 44 | + |
| 45 | + it('handle duplicate job request', async () => { |
| 46 | + RESTController._setXHR(DuplicateXHR('1234')); |
| 47 | + const params = { startedBy: 'Monty Python' }; |
| 48 | + const jobStatusId = await Parse.Cloud.startJob('CloudJob1', params); |
| 49 | + await expectAsync(Parse.Cloud.startJob('CloudJob1', params)).toBeRejectedWithError('Duplicate request'); |
| 50 | + |
| 51 | + const jobStatus = await Parse.Cloud.getJobStatus(jobStatusId); |
| 52 | + expect(jobStatus.get('status')).toBe('succeeded'); |
| 53 | + expect(jobStatus.get('params').startedBy).toBe('Monty Python'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('handle duplicate POST / PUT request', async () => { |
| 57 | + RESTController._setXHR(DuplicateXHR('1234')); |
| 58 | + const testObject = new Parse.Object('IdempotentTest'); |
| 59 | + await testObject.save(); |
| 60 | + await expectAsync(testObject.save()).toBeRejectedWithError('Duplicate request'); |
| 61 | + |
| 62 | + RESTController._setXHR(DuplicateXHR('5678')); |
| 63 | + testObject.set('foo', 'bar'); |
| 64 | + await testObject.save(); |
| 65 | + await expectAsync(testObject.save()).toBeRejectedWithError('Duplicate request'); |
| 66 | + |
| 67 | + const query = new Parse.Query('IdempotentTest'); |
| 68 | + const results = await query.find(); |
| 69 | + expect(results.length).toBe(1); |
| 70 | + expect(results[0].get('foo')).toBe('bar'); |
| 71 | + }); |
| 72 | +}); |
0 commit comments