-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
71 lines (66 loc) · 2.14 KB
/
test.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
var Vhosts = require('./')
var request = require('request')
var test = require('tape')
var http = require('http')
var pizzaServer = http.createServer(function(req, res) { res.end('pizza') }).listen(8080)
var shouldStop
var opts = {
confDir: '/usr/local/etc/nginx/conf.d/',
pidLocation: '/var/run/nginx.pid'
}
test('.write() writes a new vhost', function(t) {
var vhosts = Vhosts(opts, function running(isRunning) {
if (isRunning) return writeConfig()
if (!isRunning) return vhosts.nginx.start(function(err) {
shouldStop = true
if (err) throw err
})
})
function writeConfig() {
request('http://test.local', function(err, resp, body) {
t.false(err, 'no error')
t.true(body.toString().indexOf('pizza') === -1, 'no vhost')
vhosts.write({
name: 'test',
port: '8080',
domain: 'test.local'
}, function(err, stdout, stderr) {
t.false(err, 'wrote vhost test')
// give nginx 250ms to reload configuration
setTimeout(function() {
request('http://test.local', function(err, resp, body) {
t.false(err, 'no error')
t.true(body.toString().indexOf('pizza') > -1, 'vhost')
t.end()
vhosts.nginx.end()
})
}, 250)
})
})
}
})
test('.remove() should remove a vhost', function(t) {
var vhosts = Vhosts(opts, function running(isRunning) {
if (isRunning) return removeConfig()
})
function removeConfig() {
request('http://test.local', function(err, resp, body) {
t.false(err, 'no error')
t.true(body.toString().indexOf('pizza') > -1, 'is vhosting')
vhosts.remove('test', function(err) {
t.false(err, 'removed vhost test')
// give nginx 250ms to reload configuration
setTimeout(function() {
request('http://test.local', function(err, resp, body) {
t.false(err, 'no error')
t.true(body.toString().indexOf('pizza') === -1, 'not vhosting')
vhosts.nginx.end()
if (shouldStop) vhosts.nginx.stop()
pizzaServer.close()
t.end()
})
}, 250)
})
})
}
})