1 | const test = require('tape');
|
2 | const {
|
3 | buildParamString,
|
4 | buildUrl,
|
5 | getSegment,
|
6 | getSegments,
|
7 | objectifyParams,
|
8 | } = require('./url');
|
9 |
|
10 | test('getSegments()', assert => {
|
11 | {
|
12 | const actual = getSegments('/foo/bar/baz');
|
13 | const expected = ['foo', 'bar', 'baz'];
|
14 | const message = 'returns the path segments of a given path component';
|
15 |
|
16 | assert.deepEqual(actual, expected, message);
|
17 | }
|
18 | {
|
19 | const actual = getSegments('/foo/bar?baz');
|
20 | const expected = ['foo', 'bar'];
|
21 | const message =
|
22 | 'returns the path segments of a given path and query component';
|
23 |
|
24 | assert.deepEqual(actual, expected, message);
|
25 | }
|
26 |
|
27 | assert.end();
|
28 | });
|
29 |
|
30 | test('getSegment()', assert => {
|
31 | {
|
32 | const actual = getSegment('/foo/bar/baz');
|
33 | const expected = 'bar';
|
34 | const message = 'returns the second segment of a given path component';
|
35 |
|
36 | assert.equal(actual, expected, message);
|
37 | }
|
38 | {
|
39 | const actual = getSegment('/foo/bar?baz');
|
40 | const expected = 'bar';
|
41 | const message =
|
42 | 'returns the second segment of a given path and query component';
|
43 |
|
44 | assert.equal(actual, expected, message);
|
45 | }
|
46 |
|
47 | assert.end();
|
48 | });
|
49 |
|
50 | test('buildParamString()', assert => {
|
51 | {
|
52 | const params = {
|
53 | one: 'first',
|
54 | two: 'second',
|
55 | three: 'third',
|
56 | };
|
57 | const actual = buildParamString(params);
|
58 | const expected = '?one=first&two=second&three=third';
|
59 | const message = 'returns the params of an object as a string with ?-prefix';
|
60 |
|
61 | assert.equal(actual, expected, message);
|
62 | }
|
63 | {
|
64 | const params = {};
|
65 | const actual = buildParamString(params);
|
66 | const expected = '';
|
67 | const message = 'returns an empty string for an empty object';
|
68 |
|
69 | assert.equal(actual, expected, message);
|
70 | }
|
71 |
|
72 | assert.end();
|
73 | });
|
74 |
|
75 | const url = '/api/v1/test';
|
76 |
|
77 | test('buildUrl()', assert => {
|
78 | {
|
79 | const params = {
|
80 | one: 'first',
|
81 | two: 'second',
|
82 | three: 'third',
|
83 | };
|
84 | const actual = buildUrl(url, params);
|
85 | const expected = `${url}?one=first&two=second&three=third`;
|
86 | const message = 'returns the given path with params';
|
87 |
|
88 | assert.equal(actual, expected, message);
|
89 | }
|
90 | {
|
91 | const params = {
|
92 | one: 'f&irst',
|
93 | two: 's:econd',
|
94 | three: 't?hird',
|
95 | };
|
96 | const actual = buildUrl(url, params);
|
97 | const expected = `${url}?one=f%26irst&two=s%3Aecond&three=t%3Fhird`;
|
98 | const message = 'returns the given path with encoded params';
|
99 |
|
100 | assert.equal(actual, expected, message);
|
101 | }
|
102 | {
|
103 | const params = {
|
104 | one: 'first',
|
105 | two: '',
|
106 | three: null,
|
107 | four: 'fourth',
|
108 | };
|
109 | const actual = buildUrl(url, params);
|
110 | const expected = `${url}?one=first&four=fourth`;
|
111 | const message = 'returns the given path with params that have a value';
|
112 |
|
113 | assert.equal(actual, expected, message);
|
114 | }
|
115 |
|
116 | assert.end();
|
117 | });
|
118 |
|
119 | test('objectifyParams()', assert => {
|
120 | const paramString = 'foo=FOO&bar=BAR&quux=QUUX&test=%20';
|
121 | const actual = objectifyParams(paramString);
|
122 | const expected = {
|
123 | foo: 'FOO',
|
124 | bar: 'BAR',
|
125 | quux: 'QUUX',
|
126 | test: ' ',
|
127 | };
|
128 | const message =
|
129 | 'transforms and decode a URL string of params to an object with key value pairs';
|
130 |
|
131 | assert.deepEqual(actual, expected, message);
|
132 | assert.end();
|
133 | });
|