Skip to content

Commit

Permalink
Merge pull request #3 from Pindar/2-created
Browse files Browse the repository at this point in the history
feat: api returns 201 created and location header for post requests
  • Loading branch information
sebelga committed Aug 2, 2017
2 parents 84053de + dce0567 commit 2f6ccfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ class ApiBuilder {
readAll: typeof options.readAll === 'undefined' ? _this.settings.readAll : options.readAll,
showKey: typeof options.showKey === 'undefined' ? _this.settings.showKey : options.showKey,
};
return res.json(entity.plain(plainOptions));

res.location(`${req.path}/${entity.plain(plainOptions).id}`);
return res.status(201).json(entity.plain(plainOptions));
})
.catch(err => errorsHandler.rpcError(err, res));
}
Expand Down
9 changes: 7 additions & 2 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ describe('Datastore API', () => {
status: () => ({ json: () => {}, send: () => {} }),
set: () => {},
json: () => {},
location: () => {},
};

sinon.spy(res, 'json');
sinon.spy(res, 'location');
sinon.spy(res, 'status');
sinon.spy(errorsHandler, 'rpcError');
sinon.spy(entity, 'plain');
});

afterEach(() => {
router.route.restore();
res.json.restore();
res.status.restore();
res.location.restore();
errorsHandler.rpcError.restore();
entity.plain.restore();
});
Expand Down Expand Up @@ -408,7 +413,8 @@ describe('Datastore API', () => {
return routerRef.__gstoreApi.create(req, res)
.then(() => {
expect(myEntity.save.called).equal(true);
expect(res.json.called).equal(true);
expect(res.location.called).equal(true);
expect(res.status.called).equal(true);
expect(myEntity.plain.called).equal(true);
});
});
Expand Down Expand Up @@ -475,7 +481,6 @@ describe('Datastore API', () => {
it('should return 500 if uploading file without handler', () => {
const req2 = extend(true, {}, req);
req2.file = new Buffer('string');
sinon.spy(res, 'status');

const routerRef = apiBuilder.create(namespace.Model);
routerRef.__gstoreApi.create(req2, res);
Expand Down

0 comments on commit 2f6ccfd

Please sign in to comment.