use log-helper fileScope
[squeep-indieauth-helper] / test / lib / common.js
1 /* eslint-env mocha */
2 'use strict';
3
4 const assert = require('assert');
5 const common = require('../../lib/common');
6
7 describe('common', function () {
8
9 describe('pick', function () {
10 it('picks', function () {
11 const srcObj = {
12 a: 1,
13 b: 2,
14 c: 3,
15 };
16 const result = common.pick(srcObj, ['a', 'c', 'd']);
17 assert.deepStrictEqual(result, {
18 'a': 1,
19 'c': 3,
20 });
21 });
22 }); // pick
23
24 describe('logTruncate', function () {
25 it('returns short string', function () {
26 const str = 'this is a short string';
27 const result = common.logTruncate(str, 100);
28 assert.strictEqual(result, str);
29 });
30 it('truncates long string', function () {
31 const str = 'this is not really a very long string but it is long enough for this test';
32 const result = common.logTruncate(str, 10);
33 assert(result.length < str.length);
34 });
35 }); // logTruncate
36
37 describe('gotResponseLogData', function () {
38 it('covers', function () {
39 const response = {
40 statusCode: 200,
41 statusMessage: 'OK',
42 headers: {
43 'Content-Type': 'text/plain',
44 },
45 otherData: 'blah',
46 timings: {
47 phases: {
48 total: 89,
49 },
50 },
51 retryCount: 0,
52 redirectUrls: [],
53 body: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green Meadows," said Old Mother West Wind, “and there I saw the Best Thing in the World.”',
54 };
55 const expected = {
56 statusCode: 200,
57 statusMessage: 'OK',
58 elapsedTimeMs: 89,
59 headers: {
60 'Content-Type': 'text/plain',
61 },
62 body: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
63 };
64 const result = common.gotResponseLogData(response);
65 assert.deepStrictEqual(result, expected);
66 });
67 it('covers no body', function () {
68 const response = {
69 statusCode: 200,
70 statusMessage: 'OK',
71 headers: {
72 'Content-Type': 'text/plain',
73 },
74 timings: {
75 phases: {
76 total: 89,
77 },
78 },
79 retryCount: 1,
80 };
81 const expected = {
82 statusCode: 200,
83 statusMessage: 'OK',
84 headers: {
85 'Content-Type': 'text/plain',
86 },
87 elapsedTimeMs: 89,
88 retryCount: 1,
89 };
90 const result = common.gotResponseLogData(response);
91 assert.deepStrictEqual(result, expected);
92 });
93 it('covers json', function () {
94 const response = {
95 statusCode: 200,
96 statusMessage: 'OK',
97 headers: {
98 'Content-Type': 'application/json',
99 },
100 timings: {
101 phases: {
102 total: 89,
103 },
104 },
105 body: {
106 foo: 'bar',
107 },
108 redirectUrls: ['https://redirect.example.com/'],
109 };
110 const expected = {
111 statusCode: 200,
112 statusMessage: 'OK',
113 headers: {
114 'Content-Type': 'application/json',
115 },
116 elapsedTimeMs: 89,
117 body: {
118 foo: 'bar',
119 },
120 redirectUrls: ['https://redirect.example.com/'],
121 };
122 const result = common.gotResponseLogData(response);
123 assert.deepStrictEqual(result, expected);
124 });
125 it('covers buffer', function () {
126 const response = {
127 statusCode: 200,
128 statusMessage: 'OK',
129 headers: {
130 'Content-Type': 'text/plain',
131 },
132 timings: {
133 phases: {
134 total: 89,
135 },
136 },
137 body: Buffer.from('ᘛ⁐̤ᕐᐷ'),
138 };
139 const expected = {
140 statusCode: 200,
141 statusMessage: 'OK',
142 headers: {
143 'Content-Type': 'text/plain',
144 },
145 elapsedTimeMs: 89,
146 body: '<Buffer 14 bytes>',
147 };
148 const result = common.gotResponseLogData(response);
149 assert.deepStrictEqual(result, expected);
150 });
151 }); // gotResponseLogData
152
153 describe('setSymmetricDifference', function () {
154 it('covers difference', function () {
155 const setA = new Set([1, 2, 3]);
156 const setB = new Set([2, 3, 4]);
157 const expected = new Set([1, 4]);
158 const result = common.setSymmetricDifference(setA, setB);
159 assert(result.size);
160 assert.deepStrictEqual(result, expected);
161 });
162 it('covers no difference', function () {
163 const setA = new Set([1, 2, 3, 4]);
164 const setB = new Set([1, 2, 3, 4]);
165 const expected = new Set();
166 const result = common.setSymmetricDifference(setA, setB);
167 assert(!result.size);
168 assert.deepStrictEqual(result, expected);
169 });
170 }); // setSymmetricDifference
171
172 describe('properURLComponentName', function () {
173 it('maps proper names', function () {
174 [
175 ['hash', 'fragment'],
176 ['protocol', 'scheme'],
177 ['host', 'host'],
178 ].forEach(([name, expected]) => {
179 const result = common.properURLComponentName(name);
180 assert.strictEqual(result, expected);
181 });
182 });
183 }); // properURLComponentName
184
185 }); // common