-
Notifications
You must be signed in to change notification settings - Fork 9
/
varnish.js
169 lines (152 loc) · 5.42 KB
/
varnish.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
var assert = require('assert');
var net = require('net');
var varnish = require(__dirname + '/../../lib/node-varnish');
var VarnishEmu = require('./varnish_emu');
var tests = module.exports = {};
suite('varnish', function() {
test('should connect', function(done) {
var ok = false;
var server = new VarnishEmu();
server.on('listening', function() {
var client = new varnish.VarnishClient('127.0.0.1', server.address().port);
client.on('connect', function() {
ok = true;
});
});
setTimeout(function() {
assert.ok(ok);
server.close_connections();
server.close(done);
}, 200);
});
test('should send a command', function(done) {
var ok = false;
var server = new VarnishEmu(function() {
ok = true;
});
server.on('listening', function() {
var client = new varnish.VarnishClient('127.0.0.1', server.address().port);
client.on('ready', function myReady() {
client.run_cmd('purge obj.http.X == test', function(){});
});
});
setTimeout(function() {
assert.ok(ok);
server.close_connections();
server.close(done);
}, 100);
});
test('should emit close on server disconect', function(done) {
var ok = false;
var server = new VarnishEmu();
server.on('listening', function() {
var client = new varnish.VarnishClient('127.0.0.1', server.address().port);
client.on('ready', function() {
client.on('close', function() { ok = true; });
server.close_connections();
server.close();
});
});
setTimeout(function() {
assert.ok(ok);
done();
}, 500);
});
test('should emit response on command', function(done) {
var ok = false;
var server = new VarnishEmu();
server.on('listening', function() {
var client = new varnish.VarnishClient('127.0.0.1', server.address().port);
client.on('ready', function() {
client.run_cmd('purge obj.http.X == test', function(){});
client.on('response', function(code, body) {
ok = true;
assert.equal(200, code);
});
});
});
setTimeout(function() {
assert.ok(ok);
server.close_connections();
server.close();
done();
}, 100);
});
test('should emit error when the user tries to send when thereis a pending command', function(done) {
var ok = false;
var server = new VarnishEmu();
server.on('listening', function() {
var client = new varnish.VarnishClient('127.0.0.1', server.address().port);
client.on('ready', function() {
client.run_cmd('purge obj.http.X == test', function(){});
client.on('error', function(e) {
ok = true;
assert.equal('RESPONSE_PENDING', e.code);
});
client.run_cmd('purge obj.http.X == test', function(){});
});
});
setTimeout(function() { assert.ok(ok); done(); }, 100);
});
//
// queue
//
test('should send command', function(done) {
var server = new VarnishEmu();
server.on('listening', function() {
var queue = new varnish.VarnishQueue('127.0.0.1', server.address().port);
for(var i = 0; i < 5; ++i) {
queue.run_cmd('purge simon_is == the_best');
}
queue.on('empty', function() {
assert.equal(5, server.commands);
done();
});
});
});
test('should not send duplicate commands if asked', function(done) {
var server = new VarnishEmu();
server.on('listening', function() {
var queue = new varnish.VarnishQueue('127.0.0.1', server.address().port);
for(var i = 0; i < 5; ++i) {
queue.run_cmd('purge ' + (i%2), true);
}
queue.on('empty', function() {
assert.equal(2, server.commands);
done();
});
});
});
test('should send commands on connect', function(done) {
// first create queue
var queue = new varnish.VarnishQueue('127.0.0.1', 1234);
for(var i = 0; i < 10; ++i) {
queue.run_cmd('purge simon_is == the_best');
}
// then server
var server = new VarnishEmu(null, 1234);
queue.on('empty', function() {
assert.equal(10, server.commands); done();
});
});
//
// auth
//
test('should authenticate', function(done) {
var ok = false;
var server = new VarnishEmu(null, null, true);//requires auth
server.on('listening', function() {
var secret = 'foo',
client = new varnish.VarnishClient('127.0.0.1', server.address().port, secret);
//client.debug = true;
client.on('connect', function() {
ok = true;
});
});
setTimeout(function() {
assert.ok(ok);
server.close_connections();
server.close(done);
}, 200);
});
});