fix(directAccess): Properly handle response status (#6966)

* fix(directAccess): Properly handle response status

* clean up

* handle status in batch
This commit is contained in:
Diamond Lewis
2020-10-25 12:34:50 -05:00
committed by GitHub
parent 12abae8f41
commit e89cf25bc2
3 changed files with 81 additions and 3 deletions

View File

@@ -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 (
(semver.satisfies(process.env.MONGODB_VERSION, '>=4.0.4') &&
process.env.MONGODB_TOPOLOGY === 'replicaset' &&

View File

@@ -64,6 +64,11 @@ function ParseServerRESTController(applicationId, router) {
config
).then(
response => {
if (options.returnStatus) {
const status = response._status;
delete response._status;
return { success: response, _status: status };
}
return { success: response };
},
error => {
@@ -117,8 +122,13 @@ function ParseServerRESTController(applicationId, router) {
return router.tryRouteRequest(method, path, request);
})
.then(
response => {
resolve(response.response, response.status, response);
resp => {
const { response, status } = resp;
if (options.returnStatus) {
resolve({ ...response, _status: status });
} else {
resolve(response);
}
},
err => {
if (

View File

@@ -1704,7 +1704,8 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
RestWrite.prototype.location = function () {
var middle =
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.