summaryrefslogtreecommitdiffstats
path: root/js/vendor/js-url/tests.js
blob: 6c44d6d52fbcf0398246143f4159eea6e9d6dbc8 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
(function() {

var url = 'http://rob:abcd1234@www.domain.com/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese';
var urlHttps = 'https://rob:abcd1234@www.domain.com/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese';
var urlIp = 'https://rob:abcd1234@1.2.3.4/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese';

module('url');

test('url', function() {
  deepEqual( window.url( ), window.location.href );
});

test('domain', function() {
  deepEqual( window.url( 'domain', url ), 'domain.com' );
  deepEqual( window.url( 'domain', urlIp ), '1.2.3.4' );
});

test('hostname', function() {
  deepEqual( window.url( 'hostname', url ), 'www.domain.com' );
});

test('sub', function() {
  deepEqual( window.url( 'sub', url ), 'www' );
});

test('domain parts', function() {
  deepEqual( window.url( '.0', url ), '' );
  deepEqual( window.url( '.1', url ), 'www' );
  deepEqual( window.url( '.2', url ), 'domain' );
  deepEqual( window.url( '.-1', url ), 'com' );
});

test('auth', function() {
  deepEqual( window.url( 'auth', url ), 'rob:abcd1234' );
});

test('user', function() {
  deepEqual( window.url( 'user', url ), 'rob' );
});

test('pass', function() {
  deepEqual( window.url( 'pass', url ), 'abcd1234' );
});

test('port', function() {
  deepEqual( window.url( 'port', url ), '80' );
  deepEqual( window.url( 'port', url.toUpperCase() ), '80' );
  deepEqual( window.url( 'port', "http://example.com:80" ), '80' );
  deepEqual( window.url( 'port', urlHttps ), '443' );
  deepEqual( window.url( 'port', urlHttps.toUpperCase() ), '443' );
  deepEqual( window.url( 'port', "https://example.com:443" ), '443' );
});

test('protocol', function() {
  deepEqual( window.url( 'protocol', url ), 'http' );
});

test('path', function() {
  deepEqual( window.url( 'path', url ), '/path/index.html' );
  deepEqual( window.url( 'path', 'http://www.domain.com/first/second' ), '/first/second' );
  deepEqual( window.url( 'path', 'http://www.domain.com/first/second/' ), '/first/second/' );
  deepEqual( window.url( 'path', 'http://www.domain.com:8080/first/second' ), '/first/second' );
  deepEqual( window.url( 'path', 'http://www.domain.com:8080/first/second/' ), '/first/second/' );
  deepEqual( window.url( 'path', 'http://www.domain.com/first/second?test=foo' ), '/first/second' );
  deepEqual( window.url( 'path', 'http://www.domain.com/first/second/?test=foo' ), '/first/second/' );
  deepEqual( window.url( 'path', 'http://www.domain.com/path#anchor' ), '/path' );
  deepEqual( window.url( 'path', 'http://www.domain.com/path/#anchor' ), '/path/' );
  deepEqual( window.url( 'path', 'http://www.domain.com' ), '' );
  deepEqual( window.url( 'path', 'http://www.domain.com/' ), '/' );
  deepEqual( window.url( 'path', 'http://www.domain.com#anchor' ), '' );
  deepEqual( window.url( 'path', 'http://www.domain.com/#anchor' ), '/' );
  deepEqual( window.url( 'path', 'http://www.domain.com?test=foo' ), '' );
  deepEqual( window.url( 'path', 'http://www.domain.com/?test=foo' ), '/' );
  deepEqual( window.url( 'path', 'http://www.domain.com:80' ), '' );
  deepEqual( window.url( 'path', 'http://www.domain.com:80/' ), '/' );
  deepEqual( window.url( 'path', 'http://www.domain.com:80#anchor' ), '' );
  deepEqual( window.url( 'path', 'http://www.domain.com:80/#anchor' ), '/' );
  deepEqual( window.url( 'path', 'http://www.domain.com:80?test=foo' ), '' );
  deepEqual( window.url( 'path', 'http://www.domain.com:80/?test=foo' ), '/' );
});

test('file', function() {
  deepEqual( window.url( 'file', url ), 'index.html' );
  deepEqual( window.url( 'filename', url ), 'index' );
  deepEqual( window.url( 'fileext', url ), 'html' );
});

test('url parts', function() {
  deepEqual( window.url( '1', url ), 'path' );
  deepEqual( window.url( 1, url ), 'path' );

  deepEqual( window.url( '2', url ), 'index.html' );
  deepEqual( window.url( '3', url ), '' );
  deepEqual( window.url( '-1', url ), 'index.html' );

  deepEqual( window.url( '1', 'http://www.domain.com/first/second' ), 'first' );
  deepEqual( window.url( '1', 'http://www.domain.com/first/second/' ), 'first' );
  deepEqual( window.url( '-1', 'http://www.domain.com/first/second?test=foo' ), 'second' );
  deepEqual( window.url( '-1', 'http://www.domain.com/first/second/?test=foo' ), 'second' );
});

test('query string', function() {
  deepEqual( window.url( '?', url ), 'query1=test&silly=willy' );
  deepEqual( window.url( '?silly', url ), 'willy' );
  deepEqual( window.url( '?poo', url ), null );

  deepEqual( window.url( '?poo', 'http://domain.com?poo=' ), '' );
  deepEqual( window.url( '?poo', 'http://domain.com/?poo' ), '' );
  deepEqual( window.url( '?poo', 'http://domain.com?poo' ), '' );
  deepEqual( window.url( '?poo', 'http://domain.com?' ), null );
  deepEqual( window.url( '?poo', 'http://domain.com' ), null );
});

test('url fragment', function() {
  deepEqual( window.url( '#', url ), 'test=hash&chucky=cheese' );
  deepEqual( window.url( '#chucky', url ), 'cheese' );
  deepEqual( window.url( '#poo', url ), null );

  deepEqual( window.url( '#poo', 'http://domain.com#poo=' ), '' );
  deepEqual( window.url( '#poo', 'http://domain.com/#poo' ), '' );
  deepEqual( window.url( '#poo', 'http://domain.com#poo' ), '' );
  deepEqual( window.url( '#poo', 'http://domain.com#' ), null );
  deepEqual( window.url( '#poo', 'http://domain.com' ), null );
});

if (typeof jQuery !== 'undefined') {
  test('jQuery', function() {
    deepEqual( $.url( 'domain', url ), 'domain.com' );
    deepEqual( $.url( 'path', url ), '/path/index.html' );
    deepEqual( $.url( '?silly', url ), 'willy' );
    deepEqual( $.url( '#poo', url ), null );
  });
}

}());