Projekt

Obecné

Profil

Stáhnout (4.87 KB) Statistiky
| Větev: | Revize:
1
'use strict'
2

    
3
var os = require('os')
4
var dgram = require('dgram')
5
var tape = require('tape')
6
var afterAll = require('after-all')
7
var Service = require('../lib/service')
8
var Bonjour = require('../')
9

    
10
var getAddresses = function () {
11
  var addresses = []
12
  var itrs = os.networkInterfaces()
13
  for (var i in itrs) {
14
    var addrs = itrs[i]
15
    for (var j in addrs) {
16
      if (addrs[j].internal === false) {
17
        addresses.push(addrs[j].address)
18
      }
19
    }
20
  }
21
  return addresses
22
}
23

    
24
var port = function (cb) {
25
  var s = dgram.createSocket('udp4')
26
  s.bind(0, function () {
27
    var port = s.address().port
28
    s.on('close', function () {
29
      cb(port)
30
    })
31
    s.close()
32
  })
33
}
34

    
35
var test = function (name, fn) {
36
  tape(name, function (t) {
37
    port(function (p) {
38
      fn(Bonjour({ ip: '127.0.0.1', port: p, multicast: false }), t)
39
    })
40
  })
41
}
42

    
43
test('bonjour.publish', function (bonjour, t) {
44
  var service = bonjour.publish({ name: 'foo', type: 'bar', port: 3000 })
45
  t.ok(service instanceof Service)
46
  t.equal(service.published, false)
47
  service.on('up', function () {
48
    t.equal(service.published, true)
49
    bonjour.destroy()
50
    t.end()
51
  })
52
})
53

    
54
test('bonjour.unpublishAll', function (bonjour, t) {
55
  t.test('published services', function (t) {
56
    var service = bonjour.publish({ name: 'foo', type: 'bar', port: 3000 })
57
    service.on('up', function () {
58
      bonjour.unpublishAll(function (err) {
59
        t.error(err)
60
        t.equal(service.published, false)
61
        bonjour.destroy()
62
        t.end()
63
      })
64
    })
65
  })
66

    
67
  t.test('no published services', function (t) {
68
    bonjour.unpublishAll(function (err) {
69
      t.error(err)
70
      t.end()
71
    })
72
  })
73
})
74

    
75
test('bonjour.find', function (bonjour, t) {
76
  var next = afterAll(function () {
77
    var browser = bonjour.find({ type: 'test' })
78
    var ups = 0
79

    
80
    browser.on('up', function (s) {
81
      if (s.name === 'Foo Bar') {
82
        t.equal(s.name, 'Foo Bar')
83
        t.equal(s.fqdn, 'Foo Bar._test._tcp.local')
84
        t.deepEqual(s.txt, {})
85
        t.deepEqual(s.rawTxt, new Buffer('00', 'hex'))
86
      } else {
87
        t.equal(s.name, 'Baz')
88
        t.equal(s.fqdn, 'Baz._test._tcp.local')
89
        t.deepEqual(s.txt, { foo: 'bar' })
90
        t.deepEqual(s.rawTxt, new Buffer('07666f6f3d626172', 'hex'))
91
      }
92
      t.equal(s.host, os.hostname())
93
      t.equal(s.port, 3000)
94
      t.equal(s.type, 'test')
95
      t.equal(s.protocol, 'tcp')
96
      t.equal(s.referer.address, '127.0.0.1')
97
      t.equal(s.referer.family, 'IPv4')
98
      t.ok(Number.isFinite(s.referer.port))
99
      t.ok(Number.isFinite(s.referer.size))
100
      t.deepEqual(s.subtypes, [])
101
      t.deepEqual(s.addresses.sort(), getAddresses().sort())
102

    
103
      if (++ups === 2) {
104
        // use timeout in an attempt to make sure the invalid record doesn't
105
        // bubble up
106
        setTimeout(function () {
107
          bonjour.destroy()
108
          t.end()
109
        }, 50)
110
      }
111
    })
112
  })
113

    
114
  bonjour.publish({ name: 'Foo Bar', type: 'test', port: 3000 }).on('up', next())
115
  bonjour.publish({ name: 'Invalid', type: 'test2', port: 3000 }).on('up', next())
116
  bonjour.publish({ name: 'Baz', type: 'test', port: 3000, txt: { foo: 'bar' } }).on('up', next())
117
})
118

    
119
test('bonjour.find - binary txt', function (bonjour, t) {
120
  var next = afterAll(function () {
121
    var browser = bonjour.find({ type: 'test', txt: { binary: true } })
122

    
123
    browser.on('up', function (s) {
124
      t.equal(s.name, 'Foo')
125
      t.deepEqual(s.txt, { bar: new Buffer('buz') })
126
      t.deepEqual(s.rawTxt, new Buffer('076261723d62757a', 'hex'))
127
      bonjour.destroy()
128
      t.end()
129
    })
130
  })
131

    
132
  bonjour.publish({ name: 'Foo', type: 'test', port: 3000, txt: { bar: new Buffer('buz') } }).on('up', next())
133
})
134

    
135
test('bonjour.find - down event', function (bonjour, t) {
136
  var service = bonjour.publish({ name: 'Foo Bar', type: 'test', port: 3000 })
137

    
138
  service.on('up', function () {
139
    var browser = bonjour.find({ type: 'test' })
140

    
141
    browser.on('up', function (s) {
142
      t.equal(s.name, 'Foo Bar')
143
      service.stop()
144
    })
145

    
146
    browser.on('down', function (s) {
147
      t.equal(s.name, 'Foo Bar')
148
      bonjour.destroy()
149
      t.end()
150
    })
151
  })
152
})
153

    
154
test('bonjour.findOne - callback', function (bonjour, t) {
155
  var next = afterAll(function () {
156
    bonjour.findOne({ type: 'test' }, function (s) {
157
      t.equal(s.name, 'Callback')
158
      bonjour.destroy()
159
      t.end()
160
    })
161
  })
162

    
163
  bonjour.publish({ name: 'Invalid', type: 'test2', port: 3000 }).on('up', next())
164
  bonjour.publish({ name: 'Callback', type: 'test', port: 3000 }).on('up', next())
165
})
166

    
167
test('bonjour.findOne - emitter', function (bonjour, t) {
168
  var next = afterAll(function () {
169
    var browser = bonjour.findOne({ type: 'test' })
170
    browser.on('up', function (s) {
171
      t.equal(s.name, 'Emitter')
172
      bonjour.destroy()
173
      t.end()
174
    })
175
  })
176

    
177
  bonjour.publish({ name: 'Emitter', type: 'test', port: 3000 }).on('up', next())
178
  bonjour.publish({ name: 'Invalid', type: 'test2', port: 3000 }).on('up', next())
179
})
(1-1/2)