UNPKG

2.91 kBJavaScriptView Raw
1'use strict'
2
3var os = require('os')
4var test = require('tape')
5var Service = require('../lib/service')
6
7var getAddressesRecords = function (host) {
8 var addresses_records = []
9 var itrs = os.networkInterfaces()
10 for (var i in itrs) {
11 var addrs = itrs[i]
12 for (var j in addrs) {
13 if (addrs[j].internal === false) {
14 addresses_records.push({ data: addrs[j].address, name: host, ttl: 120, type: addrs[j].family === 'IPv4' ? 'A' : 'AAAA' })
15 }
16 }
17 }
18 return addresses_records
19}
20
21test('no name', function (t) {
22 t.throws(function () {
23 new Service({ type: 'http', port: 3000 }) // eslint-disable-line no-new
24 }, 'Required name not given')
25 t.end()
26})
27
28test('no type', function (t) {
29 t.throws(function () {
30 new Service({ name: 'Foo Bar', port: 3000 }) // eslint-disable-line no-new
31 }, 'Required type not given')
32 t.end()
33})
34
35test('no port', function (t) {
36 t.throws(function () {
37 new Service({ name: 'Foo Bar', type: 'http' }) // eslint-disable-line no-new
38 }, 'Required port not given')
39 t.end()
40})
41
42test('minimal', function (t) {
43 var s = new Service({ name: 'Foo Bar', type: 'http', port: 3000 })
44 t.equal(s.name, 'Foo Bar')
45 t.equal(s.protocol, 'tcp')
46 t.equal(s.type, '_http._tcp')
47 t.equal(s.host, os.hostname())
48 t.equal(s.port, 3000)
49 t.equal(s.fqdn, 'Foo Bar._http._tcp.local')
50 t.equal(s.txt, null)
51 t.equal(s.subtypes, null)
52 t.equal(s.published, false)
53 t.end()
54})
55
56test('protocol', function (t) {
57 var s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, protocol: 'udp' })
58 t.deepEqual(s.protocol, 'udp')
59 t.end()
60})
61
62test('host', function (t) {
63 var s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, host: 'example.com' })
64 t.deepEqual(s.host, 'example.com')
65 t.end()
66})
67
68test('txt', function (t) {
69 var s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, txt: { foo: 'bar' } })
70 t.deepEqual(s.txt, { foo: 'bar' })
71 t.end()
72})
73
74test('_records() - minimal', function (t) {
75 var s = new Service({ name: 'Foo Bar', type: 'http', protocol: 'tcp', port: 3000 })
76 t.deepEqual(s._records(), [
77 { data: s.fqdn, name: '_http._tcp.local', ttl: 28800, type: 'PTR' },
78 { data: { port: 3000, target: os.hostname() }, name: s.fqdn, ttl: 120, type: 'SRV' },
79 { data: new Buffer('00', 'hex'), name: s.fqdn, ttl: 4500, type: 'TXT' }
80 ].concat(getAddressesRecords(s.host)))
81 t.end()
82})
83
84test('_records() - everything', function (t) {
85 var s = new Service({ name: 'Foo Bar', type: 'http', protocol: 'tcp', port: 3000, host: 'example.com', txt: { foo: 'bar' } })
86 t.deepEqual(s._records(), [
87 { data: s.fqdn, name: '_http._tcp.local', ttl: 28800, type: 'PTR' },
88 { data: { port: 3000, target: 'example.com' }, name: s.fqdn, ttl: 120, type: 'SRV' },
89 { data: new Buffer('07666f6f3d626172', 'hex'), name: s.fqdn, ttl: 4500, type: 'TXT' }
90 ].concat(getAddressesRecords(s.host)))
91 t.end()
92})