fix(directAccess): Properly handle response status (#6966)
* fix(directAccess): Properly handle response status * clean up * handle status in batch
This commit is contained in:
@@ -101,6 +101,73 @@ describe('ParseServerRESTController', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle response status', async () => {
|
||||||
|
const router = ParseServer.promiseRouter({ appId: Parse.applicationId });
|
||||||
|
spyOn(router, 'tryRouteRequest').and.callThrough();
|
||||||
|
RESTController = ParseServerRESTController(Parse.applicationId, router);
|
||||||
|
const resp = await RESTController.request('POST', '/classes/MyObject');
|
||||||
|
const {
|
||||||
|
status,
|
||||||
|
response,
|
||||||
|
location,
|
||||||
|
} = await router.tryRouteRequest.calls.all()[0].returnValue;
|
||||||
|
|
||||||
|
expect(status).toBe(201);
|
||||||
|
expect(response).toEqual(resp);
|
||||||
|
expect(location).toBe(
|
||||||
|
`http://localhost:8378/1/classes/MyObject/${resp.objectId}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle response status in batch', async () => {
|
||||||
|
const router = ParseServer.promiseRouter({ appId: Parse.applicationId });
|
||||||
|
spyOn(router, 'tryRouteRequest').and.callThrough();
|
||||||
|
RESTController = ParseServerRESTController(Parse.applicationId, router);
|
||||||
|
const resp = await RESTController.request(
|
||||||
|
'POST',
|
||||||
|
'batch',
|
||||||
|
{
|
||||||
|
requests: [
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
path: '/classes/MyObject',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
path: '/classes/MyObject',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
returnStatus: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
expect(resp.length).toBe(2);
|
||||||
|
expect(resp[0]._status).toBe(201);
|
||||||
|
expect(resp[1]._status).toBe(201);
|
||||||
|
expect(resp[0].success).toBeDefined();
|
||||||
|
expect(resp[1].success).toBeDefined();
|
||||||
|
expect(router.tryRouteRequest.calls.all().length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('properly handle existed', async done => {
|
||||||
|
const restController = Parse.CoreManager.getRESTController();
|
||||||
|
Parse.CoreManager.setRESTController(RESTController);
|
||||||
|
Parse.Cloud.define('handleStatus', async () => {
|
||||||
|
const obj = new Parse.Object('TestObject');
|
||||||
|
expect(obj.existed()).toBe(false);
|
||||||
|
await obj.save();
|
||||||
|
expect(obj.existed()).toBe(false);
|
||||||
|
|
||||||
|
const query = new Parse.Query('TestObject');
|
||||||
|
const result = await query.get(obj.id);
|
||||||
|
expect(result.existed()).toBe(true);
|
||||||
|
Parse.CoreManager.setRESTController(restController);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
await Parse.Cloud.run('handleStatus');
|
||||||
|
});
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(semver.satisfies(process.env.MONGODB_VERSION, '>=4.0.4') &&
|
(semver.satisfies(process.env.MONGODB_VERSION, '>=4.0.4') &&
|
||||||
process.env.MONGODB_TOPOLOGY === 'replicaset' &&
|
process.env.MONGODB_TOPOLOGY === 'replicaset' &&
|
||||||
|
|||||||
@@ -64,6 +64,11 @@ function ParseServerRESTController(applicationId, router) {
|
|||||||
config
|
config
|
||||||
).then(
|
).then(
|
||||||
response => {
|
response => {
|
||||||
|
if (options.returnStatus) {
|
||||||
|
const status = response._status;
|
||||||
|
delete response._status;
|
||||||
|
return { success: response, _status: status };
|
||||||
|
}
|
||||||
return { success: response };
|
return { success: response };
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
@@ -117,8 +122,13 @@ function ParseServerRESTController(applicationId, router) {
|
|||||||
return router.tryRouteRequest(method, path, request);
|
return router.tryRouteRequest(method, path, request);
|
||||||
})
|
})
|
||||||
.then(
|
.then(
|
||||||
response => {
|
resp => {
|
||||||
resolve(response.response, response.status, response);
|
const { response, status } = resp;
|
||||||
|
if (options.returnStatus) {
|
||||||
|
resolve({ ...response, _status: status });
|
||||||
|
} else {
|
||||||
|
resolve(response);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
err => {
|
err => {
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -1704,7 +1704,8 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
|
|||||||
RestWrite.prototype.location = function () {
|
RestWrite.prototype.location = function () {
|
||||||
var middle =
|
var middle =
|
||||||
this.className === '_User' ? '/users/' : '/classes/' + this.className + '/';
|
this.className === '_User' ? '/users/' : '/classes/' + this.className + '/';
|
||||||
return this.config.mount + middle + this.data.objectId;
|
const mount = this.config.mount || this.config.serverURL;
|
||||||
|
return mount + middle + this.data.objectId;
|
||||||
};
|
};
|
||||||
|
|
||||||
// A helper to get the object id for this operation.
|
// A helper to get the object id for this operation.
|
||||||
|
|||||||
Reference in New Issue
Block a user