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