Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(directAccess): Properly handle response status #6966

Merged
merged 3 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions spec/ParseServerRESTController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand Down
14 changes: 12 additions & 2 deletions src/ParseServerRESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down