Skip to content
Browse files

server accepts json only. rest methods return json only. tests update…

…d to utilize supertest and work with changes to server.js and rest.js
  • Loading branch information...
1 parent f6abd40 commit 8121b2731af78e90271dc76f8b6c377f5bda05d9 Ricard Aspeljung committed
Showing with 57 additions and 114 deletions.
  1. +7 −7 lib/rest.js
  2. +1 −0 server.js
  3. +49 −107 test/test.js
View
14 lib/rest.js
@@ -76,15 +76,15 @@ function handleGet(req, res, next) {
if (req.params.id) {
if (docs.length > 0) {
result = util.flavorize(docs[0], "out");
- res.send(result, {'content-type': 'application/json; charset=utf-8'});
+ res.json(result, {'content-type': 'application/json; charset=utf-8'});
} else {
- res.send(404);
+ res.json(404);
}
} else {
docs.forEach(function (doc) {
result.push(util.flavorize(doc, "out"));
});
- res.send(result, {'content-type': 'application/json; charset=utf-8'});
+ res.json(result, {'content-type': 'application/json; charset=utf-8'});
}
db.close();
});
@@ -109,13 +109,13 @@ server.post('/:db/:collection', function (req, res) {
collection.insert(Array.isArray(req.params) ? util.cleanParams(req.params[0]) : util.cleanParams(req.params), function (err, docs) {
res.header('Location', '/' + req.params.db + '/' + req.params.collection + '/' + docs[0]._id.toHexString());
res.set('content-type', 'application/json; charset=utf-8');
- res.send(201, '{"ok":1}');
+ res.json(201, {"ok": 1});
db.close();
});
});
} else {
res.set('content-type', 'application/json; charset=utf-8');
- res.send(200, '{"ok":0}');
+ res.json(200, {"ok": 0});
}
});
@@ -131,7 +131,7 @@ server.put('/:db/:collection/:id', function (req, res) {
db.collection(req.params.collection, function (err, collection) {
collection.update(spec, util.cleanParams(req.params), true, function (err, docs) {
res.set('content-type', 'application/json; charset=utf-8');
- res.send('{"ok":1}');
+ res.json({"ok": 1});
});
});
});
@@ -149,7 +149,7 @@ server.del('/:db/:collection/:id', function (req, res) {
db.collection(req.params.collection, function (err, collection) {
collection.remove(spec, function (err, docs) {
res.set('content-type', 'application/json; charset=utf-8');
- res.send('{"ok":1}');
+ res.json({"ok": 1});
db.close();
});
});
View
1 server.js
@@ -41,6 +41,7 @@ module.exports.config = config;
var server = restify.createServer({
name: "crest"
});
+server.acceptable = ['application/json'];
server.use(restify.acceptParser(server.acceptable));
server.use(restify.bodyParser());
server.use(restify.fullResponse());
View
156 test/test.js
@@ -1,23 +1,16 @@
var main = require('../server'),
- request = require('supertest'),
+ supertest = require('supertest'),
assert = require('assert'),
http = require('http');
var objectId;
+var request = supertest(main.server);
+
main.config.flavor = "normal";
delete main.config.db.username;
delete main.config.db.password;
-function parseBody(body) {
- if (body instanceof Error) {
- return false;
- }
- if (Buffer.isBuffer(body)) {
- return true;
- }
-}
-
describe("Testing crest", function () {
after(function (done) {
@@ -26,141 +19,90 @@ describe("Testing crest", function () {
});
it("Should create a simple document", function (done) {
- var post_data = '{"test":"create"}';
- var post_options = {
- host: main.config.server.host,
- port: main.config.server.port,
- path: '/tests/tests',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Content-Length': post_data.length
- }
- };
- var post_req = http.request(post_options, function (res) {
- res.on('data', function (body) {
- if (parseBody(body)) {
- body = body.toString();
- } else {
- return body.stack();
+ request
+ .post('/tests/tests')
+ .type('application/json')
+ .send({"test" : "create"})
+ .expect(201)
+ .end(function (err, res) {
+ if (err) {
+ return done(err);
}
- assert.deepEqual(JSON.parse(body), {"ok": 1});
- assert.equal(res.statusCode, 201);
- var location = res.header('Location').split('/').slice(1);
-
+ console.log(res);
+ assert.deepEqual(res.body, {"ok": 1});
+ var location = res.header.location.split('/').slice(1);
assert.equal(location[0], 'tests');
assert.equal(location[1], 'tests');
- assert.notEqual(location[2], null);
assert.equal(location[2].length, 24);
objectId = location[2];
done();
});
- });
- post_req.write(post_data);
- post_req.end();
});
it("Should check that document exists", function (done) {
- var get_options = {
- host: main.config.server.host,
- port: main.config.server.port,
- path: '/tests/tests/' + objectId,
- method: 'GET'
- };
- http.get(get_options, function (res) {
- res.on('data', function (chunk) {
- assert.deepEqual(JSON.parse(chunk), {
+ request
+ .get('/tests/tests/' + objectId)
+ .expect(200)
+ .end(function (err, res) {
+ if (err) {
+ return done(err);
+ }
+ assert.deepEqual(res.body, {
"test": "create",
"id": objectId
});
- assert.equal(res.statusCode, 200);
done();
});
- });
});
it("Should update a document", function (done) {
- var update_data = '{"test": "updated"}';
- var update_options = {
- host: main.config.server.host,
- port: main.config.server.port,
- path: '/tests/tests/' + objectId,
- method: 'PUT',
- headers: {
- 'Content-Type': 'application/json'
- }
- };
- var update_req = http.request(update_options, function (res) {
- res.on('data', function (body) {
- if (parseBody(body)) {
- body = body.toString();
- } else {
- return body.stack();
+ request
+ .put('/tests/tests/' + objectId)
+ .type('application/json')
+ .send({"test" : "updated"})
+ .expect(200)
+ .end(function (err, res) {
+ if (err) {
+ return done(err);
}
- assert.deepEqual(JSON.parse(body), {"ok": 1});
- assert.equal(res.statusCode, 200);
+ assert.deepEqual(res.body, {"ok": 1});
done();
});
- });
- update_req.write(update_data);
- update_req.end();
});
it("Should check that document is updated", function (done) {
- var get_options = {
- host: main.config.server.host,
- port: main.config.server.port,
- path: '/tests/tests/' + objectId,
- method: 'GET'
- };
- http.get(get_options, function (res) {
- res.on('data', function (chunk) {
- assert.deepEqual(JSON.parse(chunk), {
+ request
+ .get('/tests/tests/' + objectId)
+ .expect(200)
+ .end(function (err, res) {
+ if (err) {
+ return done(err);
+ }
+ assert.deepEqual(res.body, {
"test": "updated",
"id": objectId
});
- assert.equal(res.statusCode, 200);
done();
});
- });
-
});
it("Should delete a document", function (done) {
- var delete_options = {
- host: main.config.server.host,
- port: main.config.server.port,
- path: '/tests/tests/' + objectId,
- method: 'DELETE'
- };
- var delete_req = http.request(delete_options, function (res) {
- res.on('data', function (body) {
- if (parseBody(body)) {
- body = body.toString();
- } else {
- return body.stack();
+ request
+ .del('/tests/tests/' + objectId)
+ .expect(200)
+ .end(function (err, res) {
+ if (err) {
+ return done(err);
}
- assert.deepEqual(JSON.parse(body), {"ok": 1});
- assert.equal(res.statusCode, 200);
+ assert.deepEqual(res.body, {"ok": 1});
done();
});
- });
- delete_req.end();
});
it("Should check that document is deleted", function (done) {
- var get_options = {
- host: main.config.server.host,
- port: main.config.server.port,
- path: '/tests/tests/' + objectId,
- method: 'GET'
- };
- http.get(get_options, function (res) {
- res.on('end', function (body) {
- assert.equal(res.statusCode, 404);
- done();
- });
- });
+ request
+ .get('/tests/tests/' + objectId)
+ .expect(404, done);
});
});

0 comments on commit 8121b27

Please sign in to comment.
Something went wrong with that request. Please try again.