You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.8 KiB

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