Skip to content

Commit 23a80ae

Browse files
ghermetommarchini
authored andcommitted
chore(examples): update todoapp
1 parent 7228b94 commit 23a80ae

File tree

4 files changed

+131
-127
lines changed

4 files changed

+131
-127
lines changed

examples/todoapp/lib/client.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var util = require('util');
44

55
var assert = require('assert-plus');
6-
var restify = require('restify');
6+
var clients = require('restify-clients');
77

88
///--- Globals
99

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

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

23-
this.client = restify.createClient({
23+
this.client = clients.createJSONClient({
2424
log: options.log,
2525
name: 'TodoClient',
2626
socketPath: options.socketPath,
27-
type: 'json',
2827
url: options.url,
2928
version: ver
3029
});

examples/todoapp/lib/server.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ var errors = require('restify-errors');
1111

1212
///--- Errors
1313

14-
errors.makeConstructor('MissingTaskError', {
14+
var MissingTaskError = errors.makeConstructor('MissingTaskError', {
1515
statusCode: 409,
1616
restCode: 'MissingTask',
1717
message: '"task" is a required parameter'
1818
});
1919

20-
errors.makeConstructor('TodoExistsError', {
20+
var TodoExistsError = errors.makeConstructor('TodoExistsError', {
2121
statusCode: 409,
2222
restCode: 'TodoExists',
2323
message: 'Todo already exists'
2424
});
2525

26-
errors.makeConstructor('TodoNotFoundError', {
26+
var TodoNotFoundError = errors.makeConstructor('TodoNotFoundError', {
2727
statusCode: 404,
2828
restCode: 'TodoNotFound',
2929
message: 'Todo was not found'
@@ -102,20 +102,20 @@ function authenticate(req, res, next) {
102102
* Which would have `name` and `task` available in req.params
103103
*/
104104
function createTodo(req, res, next) {
105-
if (!req.params.task) {
106-
req.log.warn({ params: p }, 'createTodo: missing task');
107-
next(new errors.MissingTaskError());
105+
if (!req.body.task) {
106+
req.log.warn({ body: req.body }, 'createTodo: missing task');
107+
next(new MissingTaskError());
108108
return;
109109
}
110110

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

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

@@ -184,7 +184,7 @@ function ensureTodo(req, res, next) {
184184

185185
if (req.params.name && req.todos.indexOf(req.params.name) === -1) {
186186
req.log.warn('%s not found', req.params.name);
187-
next(new errors.TodoNotFoundError(req.params.name));
187+
next(new TodoNotFoundError(req.params.name));
188188
} else {
189189
next();
190190
}
@@ -277,9 +277,9 @@ function listTodos(req, res, next) {
277277
* Replaces a TODO completely
278278
*/
279279
function putTodo(req, res, next) {
280-
if (!req.params.task) {
281-
req.log.warn({ params: req.params }, 'putTodo: missing task');
282-
next(new errors.MissingTaskError());
280+
if (!req.body.task) {
281+
req.log.warn({ params: req.params, body: req.body }, 'putTodo: missing task');
282+
next(new MissingTaskError());
283283
return;
284284
}
285285

@@ -318,20 +318,20 @@ function createServer(options) {
318318
});
319319

320320
// Ensure we don't drop data on uploads
321-
server.pre(restify.pre.pause());
321+
server.pre(restify.plugins.pre.pause());
322322

323323
// Clean up sloppy paths like //todo//////1//
324-
server.pre(restify.pre.sanitizePath());
324+
server.pre(restify.plugins.pre.sanitizePath());
325325

326326
// Handles annoying user agents (curl)
327-
server.pre(restify.pre.userAgentConnection());
327+
server.pre(restify.plugins.pre.userAgentConnection());
328328

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

332332
// Allow 5 requests/second by IP, and burst to 10
333333
server.use(
334-
restify.throttle({
334+
restify.plugins.throttle({
335335
burst: 10,
336336
rate: 5,
337337
ip: true

examples/todoapp/package.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
"description": "Kitchen Sink App of restify",
55
"main": "main.js",
66
"dependencies": {
7-
"assert-plus": "0.2.0",
8-
"pino": "^6.3.2",
9-
"posix-getopt": "1.2.0",
10-
"restify-errors": "^4.0.0",
11-
"restify": "^4.0.3"
7+
"assert-plus": "1.0.0",
8+
"pino": "^7.11.0",
9+
"posix-getopt": "^1.2.1",
10+
"restify-errors": "^8.0.2",
11+
"restify-clients": "^3.1.0",
12+
"restify": "^8.6.1"
1213
},
1314
"devDependencies": {
14-
"nodeunit": "0.9.1",
15-
"pino-pretty": "^4.0.0"
15+
"mocha": "^10.0.0",
16+
"chai": "^4.3.6",
17+
"pino-pretty": "^7.6.1"
1618
},
1719
"scripts": {
1820
"start": "node main.js 2>&1 | pino-pretty",
19-
"test": "nodeunit test"
21+
"test": "mocha test"
2022
},
2123
"author": "Mark Cavage",
2224
"license": "MIT"

examples/todoapp/test/todo.test.js

Lines changed: 101 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,127 +4,130 @@ var fs = require('fs');
44

55
var pino = require('pino');
66
var restify = require('restify');
7+
var assert = require('chai').assert;
78

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

1011
///--- Globals
1112

12-
var CLIENT;
1313
var DIR = '/tmp/.todo_unit_test';
14-
var SERVER;
1514
var SOCK = '/tmp/.todo_sock';
1615

1716
///--- Tests
1817

19-
exports.setup = function(t) {
20-
var log = pino({
21-
name: 'todo_unit_test',
22-
level: process.env.LOG_LEVEL || 'info'
23-
});
24-
25-
fs.mkdir(DIR, function(err) {
26-
if (err && err.code !== 'EEXIST') {
27-
console.error('unable to mkdir: ' + err.stack);
28-
process.exit(1);
29-
}
18+
describe('todoapp', function () {
19+
var CLIENT;
20+
var SERVER;
3021

31-
SERVER = todo.createServer({
32-
directory: DIR,
33-
log: log.child({ component: 'server' }, true),
34-
noAudit: true
22+
before(function (done) {
23+
var log = pino({
24+
name: 'todo_unit_test',
25+
level: process.env.LOG_LEVEL || 'info'
3526
});
36-
37-
t.ok(SERVER);
38-
SERVER.listen(SOCK, function() {
39-
CLIENT = todo.createClient({
40-
log: log.child({ component: 'client' }, true),
41-
socketPath: SOCK
27+
28+
fs.mkdir(DIR, function(err) {
29+
if (err && err.code !== 'EEXIST') {
30+
console.error('unable to mkdir: ' + err.stack);
31+
process.exit(1);
32+
}
33+
34+
SERVER = todo.createServer({
35+
directory: DIR,
36+
log: log.child({ component: 'server' }, true),
37+
noAudit: true
4238
});
43-
t.ok(CLIENT);
44-
t.done();
45-
});
39+
40+
assert.ok(SERVER);
41+
SERVER.listen(SOCK, function() {
42+
CLIENT = todo.createClient({
43+
log: log.child({ component: 'client' }, true),
44+
socketPath: SOCK
45+
});
46+
assert.ok(CLIENT);
47+
done();
48+
});
49+
});
4650
});
47-
};
4851

49-
exports.listEmpty = function(t) {
50-
CLIENT.list(function(err, todos) {
51-
t.ifError(err);
52-
t.ok(todos);
53-
t.ok(Array.isArray(todos));
54-
55-
if (todos) {
56-
t.equal(todos.length, 0);
57-
}
58-
t.done();
52+
it('should return an empty list', function (done) {
53+
CLIENT.list(function(err, todos) {
54+
assert.ifError(err);
55+
assert.ok(todos);
56+
assert.ok(Array.isArray(todos));
57+
58+
if (todos) {
59+
assert.equal(todos.length, 0);
60+
}
61+
done();
62+
});
5963
});
60-
};
6164

62-
exports.create = function(t) {
63-
var task = 'check that unit test works';
64-
CLIENT.create(task, function(err, todo) {
65-
t.ifError(err);
66-
t.ok(todo);
67-
68-
if (todo) {
69-
t.ok(todo.name);
70-
t.equal(todo.task, task);
71-
}
72-
t.done();
65+
it('should create a new task', function (done) {
66+
var task = 'check that unit test works';
67+
CLIENT.create(task, function(err, todo) {
68+
assert.ifError(err);
69+
assert.ok(todo);
70+
71+
if (todo) {
72+
assert.ok(todo.name);
73+
assert.equal(todo.task, task);
74+
}
75+
done();
76+
});
7377
});
74-
};
7578

76-
exports.listAndGet = function(t) {
77-
CLIENT.list(function(err, todos) {
78-
t.ifError(err);
79-
t.ok(todos);
80-
t.ok(Array.isArray(todos));
81-
82-
if (todos) {
83-
t.equal(todos.length, 1);
84-
CLIENT.get(todos[0], function(err2, todo) {
85-
t.ifError(err2);
86-
t.ok(todo);
87-
t.done();
88-
});
89-
} else {
90-
t.done();
91-
}
79+
it('should list and get', function (done) {
80+
CLIENT.list(function(err, todos) {
81+
assert.ifError(err);
82+
assert.ok(todos);
83+
assert.ok(Array.isArray(todos));
84+
85+
if (todos) {
86+
assert.equal(todos.length, 1);
87+
CLIENT.get(todos[0], function(err2, todo) {
88+
assert.ifError(err2);
89+
assert.ok(todo);
90+
done();
91+
});
92+
} else {
93+
done();
94+
}
95+
});
9296
});
93-
};
94-
95-
exports.update = function(t) {
96-
CLIENT.list(function(err, todos) {
97-
t.ifError(err);
98-
t.ok(todos);
99-
t.ok(Array.isArray(todos));
10097

101-
if (todos) {
102-
t.equal(todos.length, 1);
103-
104-
var todo = {
105-
name: todos[0],
106-
task: 'something else'
107-
};
108-
CLIENT.update(todo, function(err2) {
109-
t.ifError(err2);
110-
t.done();
111-
});
112-
} else {
113-
t.done();
114-
}
98+
it('should update', function (done) {
99+
CLIENT.list(function(err, todos) {
100+
assert.ifError(err);
101+
assert.ok(todos);
102+
assert.ok(Array.isArray(todos));
103+
104+
if (todos) {
105+
assert.equal(todos.length, 1);
106+
107+
var todo = {
108+
name: todos[0],
109+
task: 'something else'
110+
};
111+
CLIENT.update(todo, function(err2) {
112+
assert.ifError(err2);
113+
done();
114+
});
115+
} else {
116+
done();
117+
}
118+
});
115119
});
116-
};
117-
118-
exports.teardown = function teardown(t) {
119-
CLIENT.del(function(err) {
120-
t.ifError(err);
121120

122-
SERVER.once('close', function() {
123-
fs.rmdir(DIR, function(err) {
124-
t.ifError(err);
125-
t.done();
121+
after(function(done) {
122+
CLIENT.del(function(err) {
123+
assert.ifError(err);
124+
CLIENT.client.close();
125+
SERVER.close(function () {
126+
fs.rmdir(DIR, function(err) {
127+
assert.ifError(err);
128+
done();
129+
});
126130
});
127131
});
128-
SERVER.close();
129132
});
130-
};
133+
});

0 commit comments

Comments
 (0)