source: trip-planner-front/node_modules/dns-txt/test.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.9 KB
Line 
1'use strict'
2
3var test = require('tape')
4var txtStr = require('./')()
5var txtBin = require('./')({ binary: true })
6
7var obj = {
8 String: 'foo',
9 number: 42,
10 empty: '',
11 null: null,
12 bool: true,
13 buffer: new Buffer('bar')
14}
15
16test('encodingLength', function (t) {
17 var len = txtBin.encodingLength(obj)
18 t.equal(len, 54)
19 t.end()
20})
21
22test('encode', function (t) {
23 var buf = txtBin.encode(obj)
24 var expected = new Buffer('0a' + '537472696e67' + '3d' + '666f6f' +
25 '09' + '6e756d626572' + '3d' + '3432' +
26 '06' + '656d707479' + '3d' +
27 '09' + '6e756c6c' + '3d' + '6e756c6c' +
28 '04' + '626f6f6c' +
29 '0a' + '627566666572' + '3d' + '626172', 'hex')
30 t.deepEqual(buf, expected)
31 t.equal(txtBin.encode.bytes, expected.length)
32 t.end()
33})
34
35test('encode - empty', function (t) {
36 var buf = txtBin.encode({})
37 var expected = new Buffer('00', 'hex')
38 t.deepEqual(buf, expected)
39 t.equal(txtBin.encode.bytes, expected.length)
40 t.end()
41})
42
43test('encode - undefined', function (t) {
44 var buf = txtBin.encode()
45 var expected = new Buffer('00', 'hex')
46 t.deepEqual(buf, expected)
47 t.equal(txtBin.encode.bytes, expected.length)
48 t.end()
49})
50
51test('encode - with buffer', function (t) {
52 var buf = new Buffer(3)
53 buf.fill(255)
54 txtBin.encode({}, buf)
55 var expected = new Buffer('00ffff', 'hex')
56 t.deepEqual(buf, expected)
57 t.equal(txtBin.encode.bytes, 1)
58 t.end()
59})
60
61test('encode - with buffer and offset', function (t) {
62 var buf = new Buffer(3)
63 buf.fill(255)
64 txtBin.encode({}, buf, 1)
65 var expected = new Buffer('ff00ff', 'hex')
66 t.deepEqual(buf, expected)
67 t.equal(txtBin.encode.bytes, 1)
68 t.end()
69})
70
71test('decode', function (t) {
72 var encoded = txtBin.encode(obj)
73 var result = txtBin.decode(encoded)
74 var expected = {
75 string: new Buffer('foo'),
76 number: new Buffer('42'),
77 empty: new Buffer(0),
78 null: new Buffer('null'),
79 bool: true,
80 buffer: new Buffer('bar')
81 }
82 t.deepEqual(result, expected)
83 t.equal(txtBin.decode.bytes, encoded.length)
84 t.end()
85})
86
87test('decode - strings', function (t) {
88 var encoded = txtStr.encode(obj)
89 var result = txtStr.decode(encoded)
90 var expected = {
91 string: 'foo',
92 number: '42',
93 empty: '',
94 null: 'null',
95 bool: true,
96 buffer: 'bar'
97 }
98 t.deepEqual(result, expected)
99 t.equal(txtStr.decode.bytes, encoded.length)
100 t.end()
101})
102
103test('decode - duplicate', function (t) {
104 var orig = {
105 Foo: 'bar',
106 foo: 'ignore this'
107 }
108 var expected = {
109 foo: new Buffer('bar')
110 }
111 var encoded = txtBin.encode(orig)
112 var result = txtBin.decode(encoded)
113 t.deepEqual(result, expected)
114 t.equal(txtBin.decode.bytes, encoded.length)
115 t.end()
116})
117
118test('decode - single zero bype', function (t) {
119 var encoded = new Buffer('00', 'hex')
120 var result = txtBin.decode(encoded)
121 t.deepEqual(result, {})
122 t.equal(txtBin.decode.bytes, encoded.length)
123 t.end()
124})
125
126test('decode - with offset', function (t) {
127 var encoded = new Buffer('012300', 'hex')
128 var result = txtBin.decode(encoded, 2)
129 t.deepEqual(result, {})
130 t.equal(txtBin.decode.bytes, 1)
131 t.end()
132})
133
134test('decode - exactly 256 bytes', function (t) {
135 var expected = { foo: '' }
136 var maxLength = Object.keys(expected).reduce(function (total, key) {
137 return total - key.length - 1 // - 1 for the equal sign used to separate the key and the value
138 }, 255)
139
140 for (var n = 0; n < maxLength; n++) {
141 expected.foo += 'x'
142 }
143
144 // the max case:
145 var encoded = txtStr.encode(expected)
146 t.equal(txtStr.encode.bytes, 256)
147 var result = txtStr.decode(encoded)
148 t.deepEqual(result, expected)
149 t.equal(txtStr.decode.bytes, encoded.length)
150
151 // go beound the max:
152 expected.foo += 'x'
153 encoded = txtStr.encode(expected)
154 t.equal(txtStr.encode.bytes, 257)
155 result = txtStr.decode(encoded)
156 t.notDeepEqual(result, expected)
157 t.ok(txtStr.decode.bytes > encoded.length)
158
159 t.end()
160})
Note: See TracBrowser for help on using the repository browser.