forked from rv-kip/express-redis-cache
-
Notifications
You must be signed in to change notification settings - Fork 2
/
route.js
216 lines (156 loc) · 5.13 KB
/
route.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(function () {
'use strict';
var path = require('path');
var assert = require('assert');
var mocha = require('mocha');
var should = require('should');
var prefix = process.env.EX_RE_CA_PREFIX || 'erct:';
var host = process.env.EX_RE_CA_HOST || 'localhost';
var port = process.env.EX_RE_CA_PORT || 6379;
var cache = require('../')({
prefix: prefix,
host: host,
port: port
});
var _name = 'test-route' + process.pid;
var _expire = 60;
/** Emulate req **/
var req = {
originalUrl: '/index'
};
/** Emulate res **/
var res = {
statusCode: 200,
send: function (body) {
},
_headers: {
'content-type': 'text/plain'
}
};
/** Emulate next **/
var next = function () {
// res.send(entry.body);
};
describe ( 'route', function () {
var middleware, error, results;
it ( 'should be a function', function () {
cache.route.should.be.a.Function;
});
it ( 'should return a function', function () {
middleware = cache.route(_name, _expire);
middleware.should.be.a.Function;
});
describe('On Calling the route', function () {
it ( 'should call next', function (done) {
middleware(
req,
res,
function (error) {
if ( error ) {
throw error;
}
res.send('hello folks!');
done();
});
});
it ( 'should have created the cache entry', function (done) {
cache.get(_name, function (error, $results) {
if ( error ) {
throw error;
}
$results.length.should.be.above(0);
results = $results;
done();
});
});
describe ( 'cache entry', function () {
it ( 'should be have a property "body" which is a string and equals the sent text', function () {
results.forEach(function (entry) {
entry.should.have.property('body')
.which.is.a.String
.and.equal('hello folks!');
});
});
it ( 'should be have a property "type" which is a string and equals the sent type', function () {
results.forEach(function (entry) {
entry.should.have.property('type')
.which.is.a.String
.and.equal(res._headers['content-type']);
});
});
it ( ' - entry which has a property touched which is a number which, when resolved to date, is less than 2 seconds from now', function () {
results.forEach(function (entry) {
Number(entry.touched).should.be.a.Number;
var date = new Date(Number(entry.touched));
( (Date.now() - date) ).should.be.below(2000);
});
});
it ( ' - entry which has a property expire which equals sent expire', function () {
results.forEach(function (entry) {
should(+entry.expire).equal(_expire);
});
});
});
});
});
})();
/**
Basic smoke test for method cache.route()
#########################################
Called by `test/test.js`
# Abstract
Expecting `test/test.js` to call this file and bind it with a `(require('express-redis-cache')())` instance.
This instance should have an array property called `caches` which represent the entries that have been created when `test/test.js` is called. We use these entries to verify we can perform a basic smoke-test on method `cache.route()`.
**/
module.exports = function (cb) {
if ( typeof cb !== 'function' ) {
throw new Error('Missing callback');
}
var cache = this;
var domain = require('domain').create();
var cacheEntry = '/path';
domain.on('error', function (error) {
cb(error);
});
domain.run(function () {
var assert = require('./assert');
require('async').parallel(
/* for each new cache **/
cache.newCaches
/** if it is a route cache */
.filter(function (entry) {
return !!( /^\/route_/.test(entry.name) );
})
.map(function (entry) {
return function (then) {
var name = this.name;
var entry = this.entry;
console.log();
console.log(' Testing cache.route'.bold.blue, name, entry);
console.log();
/** Emulate req **/
var req = {
path: name
};
/** Emulate res **/
var res = {
statusCode: 200,
send: function (body) {
assert('it should be the same message', body === entry.body);
then();
}
};
/** Emulate next **/
var next = function () {
assert('There should be a response variable', !!res.expressRedisCache);
res.send(entry.body);
};
var router = cache.route();
assert('it should be a function', typeof router === 'function');
/** Emulate a HTTP request **/
router.apply(cache, [req, res, next]);
}.bind(entry);
}),
cb);
});
};