Skip to content

Commit

Permalink
chore(examples): update todoapp
Browse files Browse the repository at this point in the history
  • Loading branch information
ghermeto authored and mmarchini committed Nov 15, 2022
1 parent 7228b94 commit 23a80ae
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 127 deletions.
5 changes: 2 additions & 3 deletions examples/todoapp/lib/client.js
Expand Up @@ -3,7 +3,7 @@
var util = require('util');

var assert = require('assert-plus');
var restify = require('restify');
var clients = require('restify-clients');

///--- Globals

Expand All @@ -20,11 +20,10 @@ function TodoClient(options) {

var ver = options.version || '~1.0';

this.client = restify.createClient({
this.client = clients.createJSONClient({
log: options.log,
name: 'TodoClient',
socketPath: options.socketPath,
type: 'json',
url: options.url,
version: ver
});
Expand Down
36 changes: 18 additions & 18 deletions examples/todoapp/lib/server.js
Expand Up @@ -11,19 +11,19 @@ var errors = require('restify-errors');

///--- Errors

errors.makeConstructor('MissingTaskError', {
var MissingTaskError = errors.makeConstructor('MissingTaskError', {
statusCode: 409,
restCode: 'MissingTask',
message: '"task" is a required parameter'
});

errors.makeConstructor('TodoExistsError', {
var TodoExistsError = errors.makeConstructor('TodoExistsError', {
statusCode: 409,
restCode: 'TodoExists',
message: 'Todo already exists'
});

errors.makeConstructor('TodoNotFoundError', {
var TodoNotFoundError = errors.makeConstructor('TodoNotFoundError', {
statusCode: 404,
restCode: 'TodoNotFound',
message: 'Todo was not found'
Expand Down Expand Up @@ -102,20 +102,20 @@ function authenticate(req, res, next) {
* Which would have `name` and `task` available in req.params
*/
function createTodo(req, res, next) {
if (!req.params.task) {
req.log.warn({ params: p }, 'createTodo: missing task');
next(new errors.MissingTaskError());
if (!req.body.task) {
req.log.warn({ body: req.body }, 'createTodo: missing task');
next(new MissingTaskError());
return;
}

var todo = {
name: req.params.name || req.params.task.replace(/\W+/g, '_'),
task: req.params.task
name: req.body.name || req.body.task.replace(/\W+/g, '_'),
task: req.body.task
};

if (req.todos.indexOf(todo.name) !== -1) {
req.log.warn('%s already exists', todo.name);
next(new errors.TodoExistsError(todo.name));
next(new TodoExistsError(todo.name));
return;
}

Expand Down Expand Up @@ -184,7 +184,7 @@ function ensureTodo(req, res, next) {

if (req.params.name && req.todos.indexOf(req.params.name) === -1) {
req.log.warn('%s not found', req.params.name);
next(new errors.TodoNotFoundError(req.params.name));
next(new TodoNotFoundError(req.params.name));
} else {
next();
}
Expand Down Expand Up @@ -277,9 +277,9 @@ function listTodos(req, res, next) {
* Replaces a TODO completely
*/
function putTodo(req, res, next) {
if (!req.params.task) {
req.log.warn({ params: req.params }, 'putTodo: missing task');
next(new errors.MissingTaskError());
if (!req.body.task) {
req.log.warn({ params: req.params, body: req.body }, 'putTodo: missing task');
next(new MissingTaskError());
return;
}

Expand Down Expand Up @@ -318,20 +318,20 @@ function createServer(options) {
});

// Ensure we don't drop data on uploads
server.pre(restify.pre.pause());
server.pre(restify.plugins.pre.pause());

// Clean up sloppy paths like //todo//////1//
server.pre(restify.pre.sanitizePath());
server.pre(restify.plugins.pre.sanitizePath());

// Handles annoying user agents (curl)
server.pre(restify.pre.userAgentConnection());
server.pre(restify.plugins.pre.userAgentConnection());

// Set a per request pino logger (with requestid filled in)
server.use(restify.requestLogger());
server.use(restify.plugins.requestLogger());

// Allow 5 requests/second by IP, and burst to 10
server.use(
restify.throttle({
restify.plugins.throttle({
burst: 10,
rate: 5,
ip: true
Expand Down
18 changes: 10 additions & 8 deletions examples/todoapp/package.json
Expand Up @@ -4,19 +4,21 @@
"description": "Kitchen Sink App of restify",
"main": "main.js",
"dependencies": {
"assert-plus": "0.2.0",
"pino": "^6.3.2",
"posix-getopt": "1.2.0",
"restify-errors": "^4.0.0",
"restify": "^4.0.3"
"assert-plus": "1.0.0",
"pino": "^7.11.0",
"posix-getopt": "^1.2.1",
"restify-errors": "^8.0.2",
"restify-clients": "^3.1.0",
"restify": "^8.6.1"
},
"devDependencies": {
"nodeunit": "0.9.1",
"pino-pretty": "^4.0.0"
"mocha": "^10.0.0",
"chai": "^4.3.6",
"pino-pretty": "^7.6.1"
},
"scripts": {
"start": "node main.js 2>&1 | pino-pretty",
"test": "nodeunit test"
"test": "mocha test"
},
"author": "Mark Cavage",
"license": "MIT"
Expand Down
199 changes: 101 additions & 98 deletions examples/todoapp/test/todo.test.js
Expand Up @@ -4,127 +4,130 @@ var fs = require('fs');

var pino = require('pino');
var restify = require('restify');
var assert = require('chai').assert;

var todo = require('../lib');

///--- Globals

var CLIENT;
var DIR = '/tmp/.todo_unit_test';
var SERVER;
var SOCK = '/tmp/.todo_sock';

///--- Tests

exports.setup = function(t) {
var log = pino({
name: 'todo_unit_test',
level: process.env.LOG_LEVEL || 'info'
});

fs.mkdir(DIR, function(err) {
if (err && err.code !== 'EEXIST') {
console.error('unable to mkdir: ' + err.stack);
process.exit(1);
}
describe('todoapp', function () {
var CLIENT;
var SERVER;

SERVER = todo.createServer({
directory: DIR,
log: log.child({ component: 'server' }, true),
noAudit: true
before(function (done) {
var log = pino({
name: 'todo_unit_test',
level: process.env.LOG_LEVEL || 'info'
});

t.ok(SERVER);
SERVER.listen(SOCK, function() {
CLIENT = todo.createClient({
log: log.child({ component: 'client' }, true),
socketPath: SOCK

fs.mkdir(DIR, function(err) {
if (err && err.code !== 'EEXIST') {
console.error('unable to mkdir: ' + err.stack);
process.exit(1);
}

SERVER = todo.createServer({
directory: DIR,
log: log.child({ component: 'server' }, true),
noAudit: true
});
t.ok(CLIENT);
t.done();
});

assert.ok(SERVER);
SERVER.listen(SOCK, function() {
CLIENT = todo.createClient({
log: log.child({ component: 'client' }, true),
socketPath: SOCK
});
assert.ok(CLIENT);
done();
});
});
});
};

exports.listEmpty = function(t) {
CLIENT.list(function(err, todos) {
t.ifError(err);
t.ok(todos);
t.ok(Array.isArray(todos));

if (todos) {
t.equal(todos.length, 0);
}
t.done();
it('should return an empty list', function (done) {
CLIENT.list(function(err, todos) {
assert.ifError(err);
assert.ok(todos);
assert.ok(Array.isArray(todos));

if (todos) {
assert.equal(todos.length, 0);
}
done();
});
});
};

exports.create = function(t) {
var task = 'check that unit test works';
CLIENT.create(task, function(err, todo) {
t.ifError(err);
t.ok(todo);

if (todo) {
t.ok(todo.name);
t.equal(todo.task, task);
}
t.done();
it('should create a new task', function (done) {
var task = 'check that unit test works';
CLIENT.create(task, function(err, todo) {
assert.ifError(err);
assert.ok(todo);

if (todo) {
assert.ok(todo.name);
assert.equal(todo.task, task);
}
done();
});
});
};

exports.listAndGet = function(t) {
CLIENT.list(function(err, todos) {
t.ifError(err);
t.ok(todos);
t.ok(Array.isArray(todos));

if (todos) {
t.equal(todos.length, 1);
CLIENT.get(todos[0], function(err2, todo) {
t.ifError(err2);
t.ok(todo);
t.done();
});
} else {
t.done();
}
it('should list and get', function (done) {
CLIENT.list(function(err, todos) {
assert.ifError(err);
assert.ok(todos);
assert.ok(Array.isArray(todos));

if (todos) {
assert.equal(todos.length, 1);
CLIENT.get(todos[0], function(err2, todo) {
assert.ifError(err2);
assert.ok(todo);
done();
});
} else {
done();
}
});
});
};

exports.update = function(t) {
CLIENT.list(function(err, todos) {
t.ifError(err);
t.ok(todos);
t.ok(Array.isArray(todos));

if (todos) {
t.equal(todos.length, 1);

var todo = {
name: todos[0],
task: 'something else'
};
CLIENT.update(todo, function(err2) {
t.ifError(err2);
t.done();
});
} else {
t.done();
}
it('should update', function (done) {
CLIENT.list(function(err, todos) {
assert.ifError(err);
assert.ok(todos);
assert.ok(Array.isArray(todos));

if (todos) {
assert.equal(todos.length, 1);

var todo = {
name: todos[0],
task: 'something else'
};
CLIENT.update(todo, function(err2) {
assert.ifError(err2);
done();
});
} else {
done();
}
});
});
};

exports.teardown = function teardown(t) {
CLIENT.del(function(err) {
t.ifError(err);

SERVER.once('close', function() {
fs.rmdir(DIR, function(err) {
t.ifError(err);
t.done();
after(function(done) {
CLIENT.del(function(err) {
assert.ifError(err);
CLIENT.client.close();
SERVER.close(function () {
fs.rmdir(DIR, function(err) {
assert.ifError(err);
done();
});
});
});
SERVER.close();
});
};
});

0 comments on commit 23a80ae

Please sign in to comment.