chain-class.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var test = require('tap').test
  2. var CC = require('../index.js').ConfigChain
  3. var env = { foo_blaz : 'blzaa', foo_env : 'myenv' }
  4. var jsonObj = { blaz: 'json', json: true }
  5. var iniObj = { 'x.y.z': 'xyz', blaz: 'ini' }
  6. var fs = require('fs')
  7. var ini = require('ini')
  8. fs.writeFileSync('/tmp/config-chain-class.json', JSON.stringify(jsonObj))
  9. fs.writeFileSync('/tmp/config-chain-class.ini', ini.stringify(iniObj))
  10. var http = require('http')
  11. var reqs = 0
  12. http.createServer(function (q, s) {
  13. if (++reqs === 2) this.close()
  14. if (q.url === '/json') {
  15. // make sure that the requests come back from the server
  16. // out of order. they should still be ordered properly
  17. // in the resulting config object set.
  18. setTimeout(function () {
  19. s.setHeader('content-type', 'application/json')
  20. s.end(JSON.stringify({
  21. blaz: 'http',
  22. http: true,
  23. json: true
  24. }))
  25. }, 200)
  26. } else {
  27. s.setHeader('content-type', 'application/ini')
  28. s.end(ini.stringify({
  29. blaz: 'http',
  30. http: true,
  31. ini: true,
  32. json: false
  33. }))
  34. }
  35. }).listen(1337)
  36. test('basic class test', function (t) {
  37. var cc = new CC()
  38. var expectlist =
  39. [ { blaz: 'json', json: true },
  40. { 'x.y.z': 'xyz', blaz: 'ini' },
  41. { blaz: 'blzaa', env: 'myenv' },
  42. { blaz: 'http', http: true, json: true },
  43. { blaz: 'http', http: true, ini: true, json: false } ]
  44. cc.addFile('/tmp/config-chain-class.json')
  45. .addFile('/tmp/config-chain-class.ini')
  46. .addEnv('foo_', env)
  47. .addUrl('http://localhost:1337/json')
  48. .addUrl('http://localhost:1337/ini')
  49. .on('load', function () {
  50. t.same(cc.list, expectlist)
  51. t.same(cc.snapshot, { blaz: 'json',
  52. json: true,
  53. 'x.y.z': 'xyz',
  54. env: 'myenv',
  55. http: true,
  56. ini: true })
  57. cc.del('blaz', '/tmp/config-chain-class.json')
  58. t.same(cc.snapshot, { blaz: 'ini',
  59. json: true,
  60. 'x.y.z': 'xyz',
  61. env: 'myenv',
  62. http: true,
  63. ini: true })
  64. cc.del('blaz')
  65. t.same(cc.snapshot, { json: true,
  66. 'x.y.z': 'xyz',
  67. env: 'myenv',
  68. http: true,
  69. ini: true })
  70. cc.shift()
  71. t.same(cc.snapshot, { 'x.y.z': 'xyz',
  72. env: 'myenv',
  73. http: true,
  74. json: true,
  75. ini: true })
  76. cc.shift()
  77. t.same(cc.snapshot, { env: 'myenv',
  78. http: true,
  79. json: true,
  80. ini: true })
  81. cc.shift()
  82. t.same(cc.snapshot, { http: true,
  83. json: true,
  84. ini: true })
  85. cc.shift()
  86. t.same(cc.snapshot, { http: true,
  87. ini: true,
  88. json: false })
  89. cc.shift()
  90. t.same(cc.snapshot, {})
  91. t.end()
  92. })
  93. })