-
-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathIdempotencyTest.js
79 lines (68 loc) · 2.56 KB
/
IdempotencyTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
const Parse = require('../../node');
const sleep = require('./sleep');
const Item = Parse.Object.extend('IdempotencyItem');
const RESTController = Parse.CoreManager.getRESTController();
const XHR = RESTController._getXHR();
function DuplicateXHR(requestId) {
function XHRWrapper() {
const xhr = new XHR();
const send = xhr.send;
xhr.send = function () {
this.setRequestHeader('X-Parse-Request-Id', requestId);
send.apply(this, arguments);
};
return xhr;
}
return XHRWrapper;
}
describe('Idempotency', () => {
beforeEach(() => {
RESTController._setXHR(XHR);
});
it('handle duplicate cloud code function request', async () => {
RESTController._setXHR(DuplicateXHR('1234'));
await Parse.Cloud.run('CloudFunctionIdempotency');
await expectAsync(Parse.Cloud.run('CloudFunctionIdempotency')).toBeRejectedWithError(
'Duplicate request'
);
await expectAsync(Parse.Cloud.run('CloudFunctionIdempotency')).toBeRejectedWithError(
'Duplicate request'
);
const query = new Parse.Query(Item);
const results = await query.find();
expect(results.length).toBe(1);
});
it('handle duplicate job request', async () => {
RESTController._setXHR(DuplicateXHR('1234'));
const params = { startedBy: 'Monty Python' };
const jobStatusId = await Parse.Cloud.startJob('CloudJob1', params);
await expectAsync(Parse.Cloud.startJob('CloudJob1', params)).toBeRejectedWithError(
'Duplicate request'
);
const checkJobStatus = async () => {
const result = await Parse.Cloud.getJobStatus(jobStatusId);
return result && result.get('status') === 'succeeded';
};
while (!(await checkJobStatus())) {
await sleep(100);
}
const jobStatus = await Parse.Cloud.getJobStatus(jobStatusId);
expect(jobStatus.get('status')).toBe('succeeded');
expect(jobStatus.get('params').startedBy).toBe('Monty Python');
});
it('handle duplicate POST / PUT request', async () => {
RESTController._setXHR(DuplicateXHR('1234'));
const testObject = new Parse.Object('IdempotentTest');
await testObject.save();
await expectAsync(testObject.save()).toBeRejectedWithError('Duplicate request');
RESTController._setXHR(DuplicateXHR('5678'));
testObject.set('foo', 'bar');
await testObject.save();
await expectAsync(testObject.save()).toBeRejectedWithError('Duplicate request');
const query = new Parse.Query('IdempotentTest');
const results = await query.find();
expect(results.length).toBe(1);
expect(results[0].get('foo')).toBe('bar');
});
});