-
Notifications
You must be signed in to change notification settings - Fork 14
/
hooks.js
110 lines (97 loc) · 2.33 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
var async = require('async');
var request = require('request');
// imports the hooks module _injected_ by dredd.
var hooks = require('hooks');
var url = require('url');
var BASE_URL;
// DELETE /todos
// after: check that there is no more completed todos.
hooks.after('Todos > Todos Collection > Archive completed Todos', function (t, done) {
request.get(uri(t, '/todos'), function (err, res, body) {
JSON.parse(body).forEach(function (todo) {
console.assert(!todo.complete);
});
return done();
});
});
// GET /todos/:id
// before: create a new todo.
hooks.before('Todos > Todo > Get a Todo', function (t, done) {
request.post({
uri: uri(t, '/todos'),
json: {
title: 'dredd'
}
}, function (err, res, todo) {
t.fullPath = '/todos/' + todo.id;
return done();
});
});
// PUT /todos/:id
// before: create a new todo.
// after: check that the todo is marked complete.
hooks.before('Todos > Todo > Update a Todo', function (t, done) {
request.post({
uri: uri(t, '/todos'),
json: {
title: 'dredd',
completed: false
}
}, function (err, res, todo) {
t.fullPath = '/todos/' + todo.id;
return done();
});
});
// DELETE /todos/:id
// before: create a new todo.
// after: check that the todo is gone.
hooks.before('Todos > Todo > Delete a Todo', function (t, done) {
request.post({
uri: uri(t, '/todos'),
json: {
title: 'dredd'
}
}, function (err, res, todo) {
t.fullPath = '/todos/' + todo.id;
return done();
});
});
hooks.after('Todos > Todo > Delete a Todo', function (t, done) {
request.get(uri(t, t.fullPath), function (err, res) {
console.assert(res.statusCode === 404);
return done();
});
});
// Remove all todos that were created by the tests.
hooks.afterAll(function (done) {
request.get(uri('/todos'), function (err, res, todos) {
if (!Array.isArray(todos)) {
try {
todos = JSON.parse(todos);
} catch (e) {
done(e);
}
}
var todosToDelete = todos.filter(function (todo) {
return todo.title === 'dredd';
});
async.parallel(todosToDelete.map(function (todo) {
return function (next) {
request.del(uri('/todos/' + todo.id), next);
};
}), done);
});
});
function uri(t, path) {
path = path || t;
if (!BASE_URL) {
BASE_URL = t;
}
return url.format({
protocol: BASE_URL.protocol,
hostname: BASE_URL.host,
port: BASE_URL.port,
pathname: path
});
}