@ohos-ports/express-redis-cache 0.0.0-stage → 1.1.3-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/get.js ADDED
@@ -0,0 +1,123 @@
1
+ (function () {
2
+
3
+ 'use strict';
4
+
5
+ var path = require('path');
6
+ var assert = require('assert');
7
+
8
+ var mocha = require('mocha');
9
+ var should = require('should');
10
+
11
+ var prefix = process.env.EX_RE_CA_PREFIX || 'erct:';
12
+ var host = process.env.EX_RE_CA_HOST || 'localhost';
13
+ var port = process.env.EX_RE_CA_PORT || 6379;
14
+
15
+ var _name = 'test1';
16
+ var _body = 'test1 test1 test1';
17
+ var _type = 'text/plain';
18
+
19
+ var cache = require('../')({
20
+ prefix: prefix,
21
+ host: host,
22
+ port: port
23
+ });
24
+
25
+ describe ( 'get', function () {
26
+
27
+ var error, results;
28
+
29
+ it ( 'should be a function', function () {
30
+ cache.get.should.be.a.Function;
31
+ });
32
+
33
+ it ( 'should callback', function (done) {
34
+ cache.get(null, function ($error, $results) {
35
+ error = $error;
36
+ results = $results;
37
+ done();
38
+ });
39
+ });
40
+
41
+ it ( 'should not have error', function () {
42
+ should(error).be.null;
43
+ });
44
+
45
+ it ( 'should be an array', function () {
46
+ results.should.be.an.Array;
47
+ });
48
+
49
+ it ( ' - entry which has a property name which a string', function () {
50
+ results.forEach(function (result) {
51
+ result.name.should.be.a.String;
52
+ });
53
+ });
54
+
55
+ it ( ' - entry which has a property body which a string', function () {
56
+ results.forEach(function (result) {
57
+ result.body.should.be.a.String;
58
+ });
59
+ });
60
+
61
+ it ( ' - entry which has a property type which a string', function () {
62
+ results.forEach(function (result) {
63
+ result.type.should.be.a.String;
64
+ });
65
+ });
66
+
67
+ it ( ' - entry which has a property touched which is a number which resolves to date', function () {
68
+ results.forEach(function (result) {
69
+ Number(result.touched).should.be.a.Number;
70
+
71
+ new Date(Number(result.touched))
72
+ .should.be.a.Date;
73
+ });
74
+ });
75
+
76
+ it ( 'should support wildcard gets', function (done) {
77
+ cache.add('wildkey1', 'abc',
78
+ function ($error, $name, $entry) {
79
+ cache.add('wildkey2', 'def',
80
+ function ($error, $name, $entry) {
81
+ cache.get('wildkey*', function ($error, $results) {
82
+ $results.should.be.an.Array;
83
+ $results.should.have.a.lengthOf(2);
84
+ done();
85
+ });
86
+ });
87
+ });
88
+ });
89
+
90
+ it ( 'should support specific gets without calling keys', function (done) {
91
+
92
+ // wrap the call to keys, so we can see if it's called
93
+ var callCount = 0;
94
+ var wrap = function(fn){
95
+ return function(){
96
+ console.log('What!?');
97
+ callCount++;
98
+ return fn.apply(this, arguments);
99
+ };
100
+ };
101
+
102
+ cache.client.keys = wrap(cache.client.keys);
103
+
104
+ cache.add('wildkey1', 'abc',
105
+ function ($error, $name, $entry) {
106
+ cache.add('wildkey2', 'def',
107
+ function ($error, $name, $entry) {
108
+ cache.get('wildkey1', function ($error, $results) {
109
+ try {
110
+ $results.should.be.an.Array;
111
+ $results.should.have.a.lengthOf(1);
112
+ callCount.should.equal(0);
113
+ done();
114
+ } catch (e) {
115
+ done(e);
116
+ }
117
+ });
118
+ });
119
+ });
120
+ });
121
+ });
122
+
123
+ })();
package/test/route.js ADDED
@@ -0,0 +1,286 @@
1
+ (function () {
2
+
3
+ 'use strict';
4
+
5
+ var path = require('path');
6
+ var assert = require('assert');
7
+
8
+ var mocha = require('mocha');
9
+ var should = require('should');
10
+
11
+ var prefix = process.env.EX_RE_CA_PREFIX || 'erct:';
12
+ var host = process.env.EX_RE_CA_HOST || 'localhost';
13
+ var port = process.env.EX_RE_CA_PORT || 6379;
14
+
15
+ var cache = require('../')({
16
+ prefix: prefix,
17
+ host: host,
18
+ port: port
19
+ });
20
+
21
+ var _name = 'test-route' + process.pid;
22
+ var _expire = 60;
23
+
24
+ /** Emulate req **/
25
+ var req = {
26
+ originalUrl: '/index'
27
+ };
28
+
29
+ /** Emulate res **/
30
+ var res = {
31
+ statusCode: 200,
32
+ send: function (body) {
33
+
34
+ },
35
+ _headers: {
36
+ 'content-type': 'text/plain'
37
+ }
38
+ };
39
+
40
+ /** Emulate next **/
41
+ var next = function () {
42
+ // res.send(entry.body);
43
+ };
44
+
45
+ describe ( 'route', function () {
46
+
47
+ var middleware, error, results;
48
+
49
+ it ( 'should be a function', function () {
50
+ cache.route.should.be.a.Function();
51
+ });
52
+
53
+ it ( 'should return a function', function () {
54
+ middleware = cache.route(_name, _expire);
55
+ middleware.should.be.a.Function();
56
+ });
57
+
58
+ describe('On Calling the route', function () {
59
+
60
+ it ( 'should call next', function (done) {
61
+ middleware(
62
+ req,
63
+ res,
64
+ function (error) {
65
+ if ( error ) {
66
+ throw error;
67
+ }
68
+ res.send('hello folks!');
69
+ done();
70
+ });
71
+ });
72
+
73
+ it ( 'should have created the cache entry', function (done) {
74
+ cache.get(_name, function (error, $results) {
75
+ if ( error ) {
76
+ throw error;
77
+ }
78
+ $results.length.should.be.above(0);
79
+ results = $results;
80
+ done();
81
+ });
82
+ });
83
+
84
+ describe ( 'cache entry', function () {
85
+
86
+ it ( 'should be have a property "body" which is a string and equals the sent text', function () {
87
+ results.forEach(function (entry) {
88
+ entry.should.have.property('body').which.is.a.String();
89
+ entry.body.should.equal('hello folks!');
90
+ });
91
+ });
92
+
93
+ it ( 'should be have a property "type" which is a string and equals the sent type', function () {
94
+ results.forEach(function (entry) {
95
+ entry.should.have.property('type').which.is.a.String();
96
+ entry.type.should.equal(res._headers['content-type']);
97
+ });
98
+ });
99
+
100
+ it ( ' - entry which has a property touched which is a number which, when resolved to date, is less than 2 seconds from now', function () {
101
+ results.forEach(function (entry) {
102
+ Number(entry.touched).should.be.a.Number();
103
+
104
+ var date = new Date(Number(entry.touched));
105
+
106
+ ( (Date.now() - date) ).should.be.below(2000);
107
+ });
108
+ });
109
+
110
+ it ( ' - entry which has a property expire which equals sent expire', function () {
111
+ results.forEach(function (entry) {
112
+ should(+entry.expire).equal(_expire);
113
+ });
114
+ });
115
+ });
116
+ });
117
+ });
118
+ describe ( 'binaryroute', function () {
119
+
120
+ var middleware, error, results;
121
+
122
+ it ( 'should be a function', function () {
123
+ cache.route.should.be.a.Function();
124
+ });
125
+
126
+ it ( 'should return a function', function () {
127
+ middleware = cache.route({name: 'binary', expire: _expire, binary: true});
128
+ middleware.should.be.a.Function();
129
+ });
130
+
131
+ describe('On Calling the route', function () {
132
+
133
+ it ( 'should call next', function (done) {
134
+ middleware(
135
+ req,
136
+ res,
137
+ function (error) {
138
+ if ( error ) {
139
+ throw error;
140
+ }
141
+
142
+ res.send(new Buffer('hello folks!', 'binary'));
143
+ done();
144
+ });
145
+ });
146
+
147
+ it ( 'should have created the cache entry', function (done) {
148
+ cache.get('binary', function (error, $results) {
149
+ if ( error ) {
150
+ throw error;
151
+ }
152
+ $results.length.should.be.above(0);
153
+ results = $results;
154
+ done();
155
+ });
156
+ });
157
+
158
+ describe ( 'cache entry', function () {
159
+
160
+ it ( 'should be have a property "body" which is a base64 string and decodes to sent text', function () {
161
+ results.forEach(function (entry) {
162
+ entry.should.have.property('body').which.is.a.String();
163
+ entry.body.should.equal('aGVsbG8gZm9sa3Mh'); //aGVsbG8gZm9sa3Mh = 'hello folks!' in base64
164
+ var decodedString = new Buffer(entry.body, 'base64').toString('utf8');
165
+ decodedString.should.equal('hello folks!');
166
+ });
167
+ });
168
+
169
+ it ( 'should be have a property "type" which is a string and equals the sent type', function () {
170
+ results.forEach(function (entry) {
171
+ entry.should.have.property('type').which.is.a.String();
172
+ entry.type.should.equal(res._headers['content-type']);
173
+ });
174
+ });
175
+
176
+ it ( ' - entry which has a property touched which is a number which, when resolved to date, is less than 2 seconds from now', function () {
177
+ results.forEach(function (entry) {
178
+ Number(entry.touched).should.be.a.Number();
179
+
180
+ var date = new Date(Number(entry.touched));
181
+
182
+ ( (Date.now() - date) ).should.be.below(2000);
183
+ });
184
+ });
185
+
186
+ it ( ' - entry which has a property expire which equals sent expire', function () {
187
+ results.forEach(function (entry) {
188
+ should(+entry.expire).equal(_expire);
189
+ });
190
+ });
191
+ });
192
+ });
193
+ });
194
+ })();
195
+
196
+ /**
197
+
198
+ Basic smoke test for method cache.route()
199
+ #########################################
200
+
201
+ Called by `test/test.js`
202
+
203
+ # Abstract
204
+
205
+ Expecting `test/test.js` to call this file and bind it with a `(require('express-redis-cache')())` instance.
206
+
207
+ 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()`.
208
+
209
+
210
+ **/
211
+
212
+ module.exports = function (cb) {
213
+
214
+ if ( typeof cb !== 'function' ) {
215
+ throw new Error('Missing callback');
216
+ }
217
+
218
+ var cache = this;
219
+
220
+ var domain = require('domain').create();
221
+
222
+ var cacheEntry = '/path';
223
+
224
+ domain.on('error', function (error) {
225
+ cb(error);
226
+ });
227
+
228
+ domain.run(function () {
229
+
230
+ var assert = require('./assert');
231
+
232
+ require('async').parallel(
233
+ /* for each new cache **/
234
+ cache.newCaches
235
+
236
+ /** if it is a route cache */
237
+ .filter(function (entry) {
238
+ return !!( /^\/route_/.test(entry.name) );
239
+ })
240
+
241
+ .map(function (entry) {
242
+ return function (then) {
243
+ var name = this.name;
244
+ var entry = this.entry;
245
+
246
+ console.log();
247
+ console.log(' Testing cache.route'.bold.blue, name, entry);
248
+ console.log();
249
+
250
+ /** Emulate req **/
251
+ var req = {
252
+ path: name
253
+ };
254
+
255
+ /** Emulate res **/
256
+ var res = {
257
+ statusCode: 200,
258
+ send: function (body) {
259
+ assert('it should be the same message', body === entry.body);
260
+
261
+ then();
262
+ }
263
+ };
264
+
265
+ /** Emulate next **/
266
+ var next = function () {
267
+ assert('There should be a response variable', !!res.expressRedisCache);
268
+
269
+ res.send(entry.body);
270
+ };
271
+
272
+ var router = cache.route();
273
+
274
+ assert('it should be a function', typeof router === 'function');
275
+
276
+ /** Emulate a HTTP request **/
277
+
278
+ router.apply(cache, [req, res, next]);
279
+
280
+ }.bind(entry);
281
+ }),
282
+
283
+ cb);
284
+
285
+ });
286
+ };