@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.
@@ -0,0 +1,77 @@
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 cache;
12
+
13
+ var prefix = process.env.EX_RE_CA_PREFIX || 'erct:';
14
+ var host = process.env.EX_RE_CA_HOST || 'localhost';
15
+ var port = process.env.EX_RE_CA_PORT || 6379;
16
+
17
+ describe ( 'Module', function () {
18
+
19
+ it ( 'should be a function', function () {
20
+ cache = require('../');
21
+ cache.should.be.a.function;
22
+ });
23
+
24
+ it ( 'should return a new ExpressRedisCache', function (done) {
25
+ cache = cache({ prefix: prefix, host: host, port: port });
26
+ cache.constructor.name.should.equal('ExpressRedisCache');
27
+ cache.on('error', function (error) {
28
+ throw error;
29
+ });
30
+ cache.on('connected', function () {
31
+ done();
32
+ });
33
+ });
34
+
35
+ it ( 'should have a property options which is an object', function () {
36
+ cache.should.have.property('options')
37
+ .which.is.an.Object();
38
+ });
39
+
40
+ it ( 'should have a property prefix which is a string and equals request prefix', function () {
41
+ cache.should.have.property('prefix').which.is.a.String();
42
+ cache.prefix.should.equal(prefix);
43
+ });
44
+
45
+ it ( 'should have a property host which is a string and equals request host', function () {
46
+ cache.should.have.property('host').which.is.a.String();
47
+ cache.host.should.equal(host);
48
+ });
49
+
50
+ it ( 'should have a property port which is a number and equals request port', function () {
51
+ cache.should.have.property('port').which.is.a.Number();
52
+ cache.port.should.equal(port);
53
+ });
54
+
55
+ it ( 'should have a property FOREVER which is a number and equals -1', function () {
56
+ cache.should.have.property('FOREVER').which.is.a.Number();
57
+ cache.FOREVER.should.equal(-1);
58
+ });
59
+
60
+ it ( 'should have a property connected which is a boolean and is true', function () {
61
+ cache.should.have.property('connected').which.is.a.Boolean();
62
+ cache.connected.should.be.true;
63
+ });
64
+
65
+ it ( 'should have a property expire which is a number and equals FOREVER', function () {
66
+ cache.should.have.property('expire').which.is.a.Number();
67
+ cache.expire.should.equal(cache.FOREVER);
68
+ });
69
+
70
+ it ( 'should have a property client which is a RedisClient', function () {
71
+ cache.should.have.property('client').which.is.an.Object();
72
+ cache.client.constructor.name.should.equal('RedisClient');
73
+ });
74
+
75
+ });
76
+
77
+ })();
package/test/add.js ADDED
@@ -0,0 +1,119 @@
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 config = require('../package.json').config;
12
+
13
+ var prefix = process.env.EX_RE_CA_PREFIX || 'erct:';
14
+ var host = process.env.EX_RE_CA_HOST || 'localhost';
15
+ var port = process.env.EX_RE_CA_PORT || 6379;
16
+
17
+ var _name = 'test1';
18
+ var _body = 'test1 test1 test1';
19
+ var _type = 'text/plain';
20
+
21
+ var cache = require('../')({
22
+ prefix: prefix,
23
+ host: host,
24
+ port: port,
25
+ expire: 2
26
+ });
27
+
28
+ describe ( 'add', function () {
29
+
30
+ var error, name, entry;
31
+
32
+ it ( 'should be a function', function () {
33
+ cache.add.should.be.a.Function();
34
+ });
35
+
36
+ it ( 'should callback', function (done) {
37
+ cache.add(_name, _body,
38
+ function ($error, $name, $entry) {
39
+ error = $error;
40
+ name = $name;
41
+ entry = $entry;
42
+ done();
43
+ });
44
+ });
45
+
46
+ it ( 'should allow for zero expiration', function(done) {
47
+ cache.add(_name, _body, { expire: 0 },
48
+ function($error, $name, $entry) {
49
+ var resp;
50
+ if($entry.expire !== 0) {
51
+ var resp = new Error('entry.expire should be 0. It is '+$entry.expire);
52
+ }
53
+ done(resp);
54
+ }
55
+ );
56
+ });
57
+
58
+ it ( 'should not have error', function () {
59
+ should(error).be.null;
60
+ });
61
+
62
+ it ( 'should have a name which is a string and match the request', function () {
63
+ name.should.be.a.String();
64
+ name.should.equal(_name);
65
+ });
66
+
67
+ it ( 'should have a entry which is an object', function () {
68
+ entry.should.be.an.Object();
69
+ });
70
+
71
+ it ( ' - entry which has a property body which a string matching the request', function () {
72
+ entry.body.should.be.a.String();
73
+ entry.body.should.equal(_body);
74
+ });
75
+
76
+ it ( ' - entry which has a property type which a string matching default type', function () {
77
+ entry.type.should.be.a.String();
78
+ entry.type.should.equal(config.type);
79
+ });
80
+
81
+ it ( ' - entry which has a property touched which is a number which, when resolved to date, is less than 2 seconds from now', function () {
82
+ entry.touched.should.be.a.Number();
83
+
84
+ var date = new Date(entry.touched);
85
+
86
+ ( (Date.now() - date) ).should.be.below(2000);
87
+ });
88
+
89
+ it ( ' - entry which has a property expire which equals cache.expire', function () {
90
+ should(entry.expire).equal(cache.expire);
91
+ });
92
+
93
+ it ( 'should have cached the content', function (done) {
94
+ this.timeout(2500); // allow more time for this test
95
+
96
+ setTimeout(function(){
97
+ cache.get(_name, function (err, res) {
98
+ should(err).not.be.ok;
99
+ res.should.be.an.Array();
100
+ res.should.have.a.lengthOf(1);
101
+ done();
102
+ });
103
+ }, (cache.expire - 1) * 1000);
104
+ });
105
+
106
+ it ( 'should expire in ' + cache.expire + ' seconds', function (done) {
107
+ this.timeout(2500); // allow more time for this test
108
+
109
+ setTimeout(function(){
110
+ cache.get(_name, function (err, res) {
111
+ should(err).not.be.ok;
112
+ res.should.be.an.Array();
113
+ res.should.have.a.lengthOf(0);
114
+ done();
115
+ });
116
+ }, cache.expire * 1000);
117
+ });
118
+ });
119
+ })();
package/test/del.js ADDED
@@ -0,0 +1,99 @@
1
+ (function () {
2
+ 'use strict';
3
+
4
+ var path = require('path');
5
+ var assert = require('assert');
6
+
7
+ var mocha = require('mocha');
8
+ var should = require('should');
9
+
10
+ var prefix = process.env.EX_RE_CA_PREFIX || 'erct:';
11
+ var host = process.env.EX_RE_CA_HOST || 'localhost';
12
+ var port = process.env.EX_RE_CA_PORT || 6379;
13
+
14
+ var cache = require('../')({
15
+ prefix: prefix,
16
+ host: host,
17
+ port: port
18
+ });
19
+
20
+ describe ( 'del', function () {
21
+
22
+ var error, deletions;
23
+
24
+ before(function (done) {
25
+ cache.add('test-to-del', '-', done);
26
+ });
27
+
28
+ it ( 'should be a function', function () {
29
+ cache.del.should.be.a.Function;
30
+ });
31
+
32
+ it ( 'should callback', function (done) {
33
+ cache.del('test-to-del', function ($error, $deletions) {
34
+ error = $error;
35
+ deletions = $deletions;
36
+ done();
37
+ });
38
+ });
39
+
40
+ it ( 'should not have error', function () {
41
+ should(error).be.null;
42
+ });
43
+
44
+ it ( 'should be a number', function () {
45
+ deletions.should.be.a.Number;
46
+ });
47
+
48
+ it ( 'should be a 1 or above', function () {
49
+ deletions.should.be.above(0);
50
+ });
51
+
52
+ });
53
+
54
+ describe ( 'del with wilcard', function () {
55
+
56
+ var error, deletions;
57
+
58
+ before(function (done) {
59
+
60
+ var parallel = [0, 1, 2, 3 ,4, 5].map(function (num) {
61
+
62
+ return function (done) {
63
+ cache.add('test-to-del.' + this.num, '-', done);
64
+ }.bind({ num: num });
65
+
66
+ });
67
+
68
+ require('async').series(parallel, function (error) {
69
+ done(error);
70
+ });
71
+ });
72
+
73
+ it ( 'should be a function', function () {
74
+ cache.del.should.be.a.Function;
75
+ });
76
+
77
+ it ( 'should callback', function (done) {
78
+ cache.del('test-to-del.*', function ($error, $deletions) {
79
+ error = $error;
80
+ deletions = $deletions;
81
+ done();
82
+ });
83
+ });
84
+
85
+ it ( 'should not have error', function () {
86
+ should(error).be.null;
87
+ });
88
+
89
+ it ( 'should be a number', function () {
90
+ deletions.should.be.a.Number;
91
+ });
92
+
93
+ it ( 'should be a 1 or above', function () {
94
+ deletions.should.be.eql(6);
95
+ });
96
+
97
+ });
98
+
99
+ })();
package/test/expire.js ADDED
@@ -0,0 +1,127 @@
1
+ (function () {
2
+ 'use strict';
3
+
4
+ var mocha = require('mocha');
5
+ var should = require('should');
6
+
7
+ var createExpirationPolicy = require('../lib/ExpressRedisCache/expire');
8
+
9
+ describe ( 'expire', function () {
10
+
11
+ it ( 'should be a function', function () {
12
+ createExpirationPolicy.should.be.a.Function;
13
+ });
14
+
15
+ it ( 'should accept a number', function () {
16
+ createExpirationPolicy(1).should.be.a.Function;
17
+ });
18
+
19
+ it ( 'should accept an object', function () {
20
+ createExpirationPolicy({
21
+ 'xxx': -1,
22
+ }).should.be.a.Function;
23
+ });
24
+
25
+ it ( 'should require an argument', function () {
26
+ createExpirationPolicy.should.throw();
27
+ });
28
+
29
+ it ( 'should not accept a boolean', function () {
30
+ (function () {
31
+ createExpirationPolicy(true);
32
+ }).should.throw();
33
+ });
34
+
35
+ it ( 'should require a default (xxx) expiration', function () {
36
+ (function () {
37
+ createExpirationPolicy({
38
+ '1xx': 1,
39
+ '2xx': 2
40
+ });
41
+ }).should.throw();
42
+ });
43
+
44
+ it ( 'should only accept two orders of wildcards', function () {
45
+ (function () {
46
+ createExpirationPolicy({
47
+ 'xxx': -1,
48
+ '33x': 1
49
+ });
50
+ }).should.throw();
51
+ });
52
+
53
+ it ( 'should only accept number values for status code specific values', function () {
54
+ (function () {
55
+ createExpirationPolicy({
56
+ 'xxx': -1,
57
+ '33x': true
58
+ });
59
+ }).should.throw();
60
+ });
61
+
62
+ it ( 'should only accept valid status code keys', function () {
63
+ (function () {
64
+ createExpirationPolicy({
65
+ 'xxx': -1,
66
+ '6xx': 1
67
+ });
68
+ }).should.throw();
69
+ (function () {
70
+ createExpirationPolicy({
71
+ 'xxx': -1,
72
+ '600': 1
73
+ });
74
+ }).should.throw();
75
+ (function () {
76
+ createExpirationPolicy({
77
+ 'xxx': -1,
78
+ '6': 1
79
+ });
80
+ }).should.throw();
81
+ })
82
+
83
+ it ( 'should return the specified value for all status codes', function () {
84
+ createExpirationPolicy(-1)(null, { statusCode: 200 }).should.equal(-1);
85
+ createExpirationPolicy(1)(null, { statusCode: 300 }).should.equal(1);
86
+ createExpirationPolicy(22)(null, { statusCode: 403 }).should.equal(22);
87
+ createExpirationPolicy(333)(null, { statusCode: 404 }).should.equal(333);
88
+ createExpirationPolicy(4444)(null, { statusCode: 500 }).should.equal(4444);
89
+ });
90
+
91
+ it ( 'should return the appropriate value for status codes', function () {
92
+ var policy = createExpirationPolicy({
93
+ 'xxx': -1,
94
+ '1xx': 1,
95
+ '100': 100,
96
+ '2xx': 2,
97
+ '200': 200,
98
+ '4xx': 4,
99
+ '400': 400,
100
+ '403': 403,
101
+ '404': 404,
102
+ '5xx': 5,
103
+ '500': 500
104
+ });
105
+ policy(null, { statusCode: 500 }).should.equal(500);
106
+ policy(null, { statusCode: 501 }).should.equal(5);
107
+ policy(null, { statusCode: 404 }).should.equal(404);
108
+ policy(null, { statusCode: 403 }).should.equal(403);
109
+ policy(null, { statusCode: 400 }).should.equal(400);
110
+ policy(null, { statusCode: 480 }).should.equal(4);
111
+ policy(null, { statusCode: 200 }).should.equal(200);
112
+ policy(null, { statusCode: 201 }).should.equal(2);
113
+ policy(null, { statusCode: 100 }).should.equal(100);
114
+ policy(null, { statusCode: 101 }).should.equal(1);
115
+ policy(null, { statusCode: 300 }).should.equal(-1);
116
+ });
117
+
118
+ it ( 'should treat status code keys as case insensitive', function () {
119
+ var policy = createExpirationPolicy({
120
+ '1XX': 1,
121
+ 'xXx': 55
122
+ });
123
+ policy(null, { statusCode: 101 }).should.equal(1);
124
+ policy(null, { statusCode: 200 }).should.equal(55);
125
+ });
126
+ });
127
+ })();
@@ -0,0 +1,229 @@
1
+ // var path = require('path');
2
+ var assert = require('assert');
3
+
4
+ var mocha = require('mocha');
5
+ var should = require('should');
6
+ var request = require('request');
7
+ var util = require('util');
8
+
9
+ var prefix = process.env.EX_RE_CA_PREFIX || 'erct:';
10
+ var host = process.env.EX_RE_CA_HOST || 'localhost';
11
+ var port = process.env.EX_RE_CA_PORT || 6379;
12
+
13
+ var spawn,
14
+ express_port,
15
+ home;
16
+
17
+ describe ( 'test with small express server', function () {
18
+
19
+ before(function (done) {
20
+ this.timeout(5000);
21
+ spawn = require('child_process')
22
+ .spawn('express/server.js', [], {});
23
+
24
+ spawn.on('error', function (error) {
25
+ throw error;
26
+ });
27
+
28
+ spawn.on('exit', function (status) {
29
+ console.log('Express Server exit with status ' + status);
30
+ });
31
+
32
+ spawn.stdout.on('data', function (data) {
33
+ console.log(data.toString());
34
+ if ( /express-redis-cache test server started on port/.test(data.toString()) ) {
35
+
36
+ express_port = data.toString().split(' ').pop().trim();
37
+
38
+ done();
39
+ }
40
+ });
41
+ });
42
+
43
+ it ( 'should have a / route', function (done) {
44
+ request('http://localhost:' + express_port,
45
+ function (error, response, body) {
46
+ if ( error ) {
47
+ throw error;
48
+ }
49
+ response.statusCode.should.equal(200);
50
+ done();
51
+ });
52
+ });
53
+
54
+ it ( 'should not have /foobar route', function (done) {
55
+ request('http://localhost:' + express_port + "/foobar",
56
+ function (error, response, body) {
57
+ if ( error ) {
58
+ throw error;
59
+ }
60
+ response.statusCode.should.equal(404);
61
+ done();
62
+ });
63
+ });
64
+
65
+ it ( '/1sec route should return json with a timestamp property', function (done) {
66
+ var url = 'http://localhost:' + express_port + "/1sec";
67
+ request(url,
68
+ function (error, response, body) {
69
+ if ( error ) {
70
+ throw error;
71
+ }
72
+ var p_body = JSON.parse(body);
73
+ // Some Mocha weirdness requires a try/catch
74
+ // or an AssertionError will crash the mocha process on error
75
+ try {
76
+ response.statusCode.should.equal(200);
77
+ p_body.should.have.property('timestamp');
78
+ done();
79
+ } catch (e) {
80
+ done(e);
81
+ }
82
+ });
83
+ });
84
+
85
+ it ( '/default_expire route should return json with a timestamp property', function (done) {
86
+ var url = 'http://localhost:' + express_port + "/default_expire";
87
+ request(url,
88
+ function (error, response, body) {
89
+ if ( error ) {
90
+ throw error;
91
+ }
92
+ var p_body = JSON.parse(body);
93
+ // Some Mocha weirdness requires a try/catch
94
+ // or an AssertionError will crash the mocha process on error
95
+ try {
96
+ response.statusCode.should.equal(200);
97
+ p_body.should.have.property('timestamp');
98
+ done();
99
+ } catch (e) {
100
+ done(e);
101
+ }
102
+ });
103
+ });
104
+
105
+ it ( '/never_expire route should return json with a timestamp property', function (done) {
106
+ var url = 'http://localhost:' + express_port + "/never_expire";
107
+ request(url,
108
+ function (error, response, body) {
109
+ if ( error ) {
110
+ throw error;
111
+ }
112
+ var p_body = JSON.parse(body);
113
+ // Some Mocha weirdness requires a try/catch
114
+ // or an AssertionError will crash the mocha process on error
115
+ try {
116
+ response.statusCode.should.equal(200);
117
+ p_body.should.have.property('timestamp');
118
+ done();
119
+ } catch (e) {
120
+ done(e);
121
+ }
122
+ });
123
+ });
124
+
125
+ it ( '/1sec route data should expire after 1 seconds', function (done) {
126
+ setTimeout(function () {
127
+ var url = 'http://localhost:' + express_port + "/1sec";
128
+ request(url,
129
+ function (error, response, body) {
130
+ if ( error ) {
131
+ throw error;
132
+ }
133
+ var p_body = JSON.parse(body),
134
+ timestamp = p_body.timestamp,
135
+ now_timestamp = Math.floor(Date.now() / 1000);
136
+
137
+ // Some Mocha weirdness requires a try/catch
138
+ // or an AssertionError will crash the mocha process on error
139
+ try {
140
+ response.statusCode.should.equal(200);
141
+ timestamp.should.be.above(now_timestamp - 1);
142
+ done();
143
+ } catch (e) {
144
+ done(e);
145
+ }
146
+ });
147
+ }, 1100);
148
+ });
149
+
150
+
151
+
152
+ it ( '/default_expire route data should expire after 3 seconds', function (done) {
153
+ this.timeout(4000); // allow 5 secs to execute
154
+ setTimeout(function () {
155
+ var url = 'http://localhost:' + express_port + "/default_expire";
156
+ request(url,
157
+ function (error, response, body) {
158
+ if ( error ) {
159
+ throw error;
160
+ }
161
+ var p_body = JSON.parse(body),
162
+ timestamp = p_body.timestamp,
163
+ now_timestamp = Math.floor(Date.now() / 1000);
164
+
165
+ // Some Mocha weirdness requires a try/catch
166
+ // or an AssertionError will crash the mocha process on error
167
+ try {
168
+ response.statusCode.should.equal(200);
169
+ timestamp.should.be.above(now_timestamp - 3);
170
+ done();
171
+ } catch (e) {
172
+ done(e);
173
+ }
174
+ });
175
+ }, 3100);
176
+ });
177
+
178
+ it ( '/never_expire route data should not expire after 3 seconds', function (done) {
179
+ this.timeout(4000); // allow 5 secs to execute
180
+ setTimeout(function () {
181
+ var url = 'http://localhost:' + express_port + "/never_expire";
182
+ request(url,
183
+ function (error, response, body) {
184
+ if ( error ) {
185
+ throw error;
186
+ }
187
+ var p_body = JSON.parse(body),
188
+ timestamp = p_body.timestamp,
189
+ now_timestamp = Math.floor(Date.now() / 1000);
190
+
191
+ // Some Mocha weirdness requires a try/catch
192
+ // or an AssertionError will crash the mocha process on error
193
+ try {
194
+ response.statusCode.should.equal(200);
195
+ timestamp.should.be.below(now_timestamp - 3);
196
+ done();
197
+ } catch (e) {
198
+ done(e);
199
+ }
200
+ });
201
+ }, 3100);
202
+ });
203
+
204
+ it ( '/never_expire/delete route data should be deleted', function (done) {
205
+ var url = 'http://localhost:' + express_port + "/delete_never_expire";
206
+ request(url,
207
+ function (error, response, body) {
208
+ if ( error ) {
209
+ throw error;
210
+ }
211
+ // Some Mocha weirdness requires a try/catch
212
+ // or an AssertionError will crash the mocha process on error
213
+ try {
214
+ response.statusCode.should.equal(200);
215
+ body.should.equal("count:1");
216
+ done();
217
+ } catch (e) {
218
+ done(e);
219
+ }
220
+ });
221
+ });
222
+
223
+
224
+ after(function (done) {
225
+ process.kill(spawn.pid);
226
+ done();
227
+ });
228
+
229
+ });