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

Make sure GET /:id/exists returns 200 {exists: true|false} #681

Merged
merged 1 commit into from
Oct 23, 2014
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
4 changes: 4 additions & 0 deletions lib/persisted-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ PersistedModel.setupRemoting = function() {
rest: {
// After hook to map exists to 200/404 for HEAD
after: function(ctx, cb) {
if (ctx.req.method === 'GET') {
// For GET, return {exists: true|false} as is
return cb();
}
if(!ctx.result.exists) {
var modelName = ctx.method.sharedClass.name;
var id = ctx.getArgByName('id');
Expand Down
26 changes: 26 additions & 0 deletions test/rest.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ describe('loopback.rest', function() {
.end(done);
});

it('should report 200 for GET /:id/exists not found', function(done) {
app.model(MyModel);
app.use(loopback.rest());
request(app).get('/mymodels/1/exists')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to.eql({exists: false});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I believe we use { exists: false } style, i.e. with spaces around curly braces.

done();
});
});

it('should report 200 for GET /:id found', function(done) {
app.model(MyModel);
app.use(loopback.rest());
Expand All @@ -49,6 +61,20 @@ describe('loopback.rest', function() {
});
});

it('should report 200 for GET /:id/exists found', function(done) {
app.model(MyModel);
app.use(loopback.rest());
MyModel.create({name: 'm2'}, function(err, inst) {
request(app).get('/mymodels/' + inst.id + '/exists')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to.eql({exists: true});
done();
});
});
});

it('includes loopback.token when necessary', function(done) {
givenUserModelWithAuth();
app.enableAuth();
Expand Down