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