Skip to content

Commit

Permalink
Merge pull request #1360 from itsfadnis/flags_for_201_and_422
Browse files Browse the repository at this point in the history
Add flags for 201 & 422 response statuses
  • Loading branch information
Morita0711 committed Apr 2, 2018
2 parents e399e4b + e8b4623 commit 02265d7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/response-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ ResponseBase.prototype._setStatusProperties = function(status){
: false;

// sugar
this.created = 201 == status;
this.accepted = 202 == status;
this.noContent = 204 == status;
this.badRequest = 400 == status;
this.unauthorized = 401 == status;
this.notAcceptable = 406 == status;
this.forbidden = 403 == status;
this.notFound = 404 == status;
this.unprocessableEntity = 422 == status;
};
20 changes: 20 additions & 0 deletions test/node/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,24 @@ describe("flags", () => {
});
});
});

describe("with 201 Created", () => {
it("should set res.created", done => {
request.post(`${base}/created`).end((err, res) => {
assert(!err);
assert(res.created, "response should be .created");
done();
});
});
});

describe("with 422 Unprocessable Entity", () => {
it("should set res.unprocessableEntity", done => {
request.post(`${base}/unprocessable-entity`).end((err, res) => {
assert(err);
assert(res.unprocessableEntity, "response should be .unprocessableEntity");
done();
});
});
});
});
8 changes: 8 additions & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ app.delete('/no-content', function(req, res){
res.sendStatus(204);
});

app.post('/created', function(req, res) {
res.status(201).send('created');
});

app.post('/unprocessable-entity', function(req, res) {
res.status(422).send('unprocessable entity');
});

app.get('/arraybuffer', function(req, res) {
var content = new ArrayBuffer(1000);
res.set('Content-Type', 'application/vnd.superagent');
Expand Down

0 comments on commit 02265d7

Please sign in to comment.