forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 15
/
url.js
80 lines (67 loc) · 2.99 KB
/
url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
require('mocha');
var assert = require('assert');
var hbs = require('handlebars').create();
var helpers = require('..');
helpers.object({handlebars: hbs});
helpers.url({handlebars: hbs});
describe('url', function() {
describe('urlResolve', function() {
it('should resolve a URL', function() {
var fn1 = hbs.compile('{{urlResolve "/one/two/three" "four"}}');
assert.equal(fn1(), '/one/two/four');
var fn2 = hbs.compile('{{urlResolve "http://example.com/" "/one"}}');
assert.equal(fn2(), 'http://example.com/one');
var fn3 = hbs.compile('{{urlResolve "http://example.com/one" "/two"}}');
assert.equal(fn3(), 'http://example.com/two');
});
});
describe('stripQuerystring', function() {
it('should return a url without its query string.', function() {
var fn = hbs.compile('{{stripQuerystring "http://example.com?tests=true"}}');
assert.equal(fn(), 'http://example.com');
});
});
describe('encodeURI', function() {
it('should return an encoded uri string.', function() {
var fn = hbs.compile('{{encodeURI "http://example.com?comment=Thyme &time=again"}}');
assert.equal(fn(), 'http%3A%2F%2Fexample.com%3Fcomment%3DThyme%20%26time%3Dagain');
});
});
describe('decodeURI', function() {
it('should return an decoded uri string.', function() {
var fn = hbs.compile('{{{decodeURI "http%3A%2F%2Fexample.com%3Fcomment%3DThyme%20%26time%3Dagain"}}}');
assert.equal(fn(), 'http://example.com?comment=Thyme &time=again');
});
});
describe('urlParse', function() {
it('should take a string, and return an object stringified to JSON.', function() {
var fn = hbs.compile('{{{JSONstringify (urlParse "http://foo.com/bar/baz?key=value" "json")}}}');
assert.deepEqual(fn(), '{"protocol":"http:","slashes":true,"auth":null,"host":"foo.com","port":null,"hostname":"foo.com","hash":null,"search":"?key=value","query":"key=value","pathname":"/bar/baz","path":"/bar/baz?key=value","href":"http://foo.com/bar/baz?key=value"}');
});
});
describe('strip protocol', function() {
it('should take an http url and return without the protocol', function() {
var data = { testUrl: 'http://foo.bar' };
var fn = hbs.compile('{{stripProtocol testUrl}}');
assert.equal(fn(data), '//foo.bar/');
});
it('strip https protocol', function() {
var data = { testUrl: 'https://foo.bar' };
var fn = hbs.compile('{{stripProtocol testUrl}}');
assert.equal(fn(data), '//foo.bar/');
});
it('should leave a relative url unchanged', function() {
var expected = 'path/to/file';
var data = { testUrl: expected };
var fn = hbs.compile('{{stripProtocol testUrl}}');
assert.equal(fn(data), expected);
});
it('should leave an absolute url unchanged', function() {
var expected = '/path/to/file';
var data = { testUrl: expected };
var fn = hbs.compile('{{stripProtocol testUrl}}');
assert.equal(fn(data), expected);
});
});
});