[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 /** 2 * @requires javelin-uri javelin-php-serializer 3 */ 4 describe('Javelin URI', function() { 5 6 it('should understand parts of a uri', function() { 7 var uri = JX.$U('http://www.facebook.com:123/home.php?key=value#fragment'); 8 expect(uri.getProtocol()).toEqual('http'); 9 expect(uri.getDomain()).toEqual('www.facebook.com'); 10 expect(uri.getPort()).toEqual('123'); 11 expect(uri.getPath()).toEqual('/home.php'); 12 expect(uri.getQueryParams()).toEqual({'key' : 'value'}); 13 expect(uri.getFragment()).toEqual('fragment'); 14 }); 15 16 it('can accept null as uri string', function() { 17 var uri = JX.$U(null); 18 expect(uri.getProtocol()).toEqual(undefined); 19 expect(uri.getDomain()).toEqual(undefined); 20 expect(uri.getPath()).toEqual(undefined); 21 expect(uri.getQueryParams()).toEqual({}); 22 expect(uri.getFragment()).toEqual(undefined); 23 expect(uri.toString()).toEqual(''); 24 }); 25 26 it('can accept empty string as uri string', function() { 27 var uri = JX.$U(''); 28 expect(uri.getProtocol()).toEqual(undefined); 29 expect(uri.getDomain()).toEqual(undefined); 30 expect(uri.getPath()).toEqual(undefined); 31 expect(uri.getQueryParams()).toEqual({}); 32 expect(uri.getFragment()).toEqual(undefined); 33 expect(uri.toString()).toEqual(''); 34 }); 35 36 it('should understand relative uri', function() { 37 var uri = JX.$U('/home.php?key=value#fragment'); 38 expect(uri.getProtocol()).toEqual(undefined); 39 expect(uri.getDomain()).toEqual(undefined); 40 expect(uri.getPath()).toEqual('/home.php'); 41 expect(uri.getQueryParams()).toEqual({'key' : 'value'}); 42 expect(uri.getFragment()).toEqual('fragment'); 43 }); 44 45 function charRange(from, to) { 46 res = ''; 47 for (var i = from.charCodeAt(0); i <= to.charCodeAt(0); i++) { 48 res += String.fromCharCode(i); 49 } 50 return res; 51 } 52 53 it('should reject unsafe domains', function() { 54 var unsafe_chars = 55 '\x00;\\%\u2047\u2048\ufe56\ufe5f\uff03\uff0f\uff1f' + 56 charRange('\ufdd0', '\ufdef') + charRange('\ufff0', '\uffff'); 57 for (var i = 0; i < unsafe_chars.length; i++) { 58 expect(function() { 59 JX.$U('http://foo' + unsafe_chars.charAt(i) + 'bar'); 60 }).toThrow(); 61 } 62 }); 63 64 it('should allow safe domains', function() { 65 var safe_chars = 66 '-._' + charRange('a', 'z') + charRange('A', 'Z') + charRange('0', '9') + 67 '\u2046\u2049\ufdcf\ufdf0\uffef'; 68 for (var i = 0; i < safe_chars.length; i++) { 69 var domain = 'foo' + safe_chars.charAt(i) + 'bar'; 70 var uri = JX.$U('http://' + domain); 71 expect(uri.getDomain()).toEqual(domain); 72 } 73 }); 74 75 it('should set slash as the default path', function() { 76 var uri = JX.$U('http://www.facebook.com'); 77 expect(uri.getPath()).toEqual('/'); 78 }); 79 80 it('should set empty map as the default query data', function() { 81 var uri = JX.$U('http://www.facebook.com/'); 82 expect(uri.getQueryParams()).toEqual({}); 83 }); 84 85 it('should set undefined as the default fragment', function() { 86 var uri = JX.$U('http://www.facebook.com/'); 87 expect(uri.getFragment()).toEqual(undefined); 88 }); 89 90 it('should understand uri with no path', function() { 91 var uri = JX.$U('http://www.facebook.com?key=value'); 92 expect(uri.getPath()).toEqual('/'); 93 expect(uri.getQueryParams()).toEqual({'key' : 'value'}); 94 }); 95 96 it('should understand multiple query keys', function() { 97 var uri = JX.$U('/?clown=town&herp=derp'); 98 expect(uri.getQueryParams()).toEqual({ 99 'clown' : 'town', 100 'herp' : 'derp' 101 }); 102 }); 103 104 it('does not set keys for nonexistant data', function() { 105 var uri = JX.$U('/?clown=town'); 106 expect(uri.getQueryParams().herp).toEqual(undefined); 107 }); 108 109 it('does not parse different types of query data', function() { 110 var uri = JX.$U('/?str=string&int=123&bool=true&badbool=false&raw'); 111 expect(uri.getQueryParams()).toEqual({ 112 'str' : 'string', 113 'int' : '123', 114 'bool' : 'true', 115 'badbool' : 'false', 116 'raw' : '' 117 }); 118 }); 119 120 it('should act as string', function() { 121 var string = 'http://www.facebook.com/home.php?key=value'; 122 var uri = JX.$U(string); 123 expect(uri.toString()).toEqual(string); 124 expect('' + uri).toEqual(string); 125 }); 126 127 it('can remove path', function() { 128 var uri = JX.$U('http://www.facebook.com/home.php?key=value'); 129 uri.setPath(undefined); 130 expect(uri.getPath()).toEqual(undefined); 131 expect(uri.toString()).toEqual('http://www.facebook.com/?key=value'); 132 }); 133 134 it('can remove queryData by undefining it', function() { 135 var uri = JX.$U('http://www.facebook.com/home.php?key=value'); 136 uri.setQueryParams(undefined); 137 expect(uri.getQueryParams()).toEqual(undefined); 138 expect(uri.toString()).toEqual('http://www.facebook.com/home.php'); 139 }); 140 141 it('can remove queryData by replacing it', function() { 142 var uri = JX.$U('http://www.facebook.com/home.php?key=value'); 143 uri.setQueryParams({}); 144 expect(uri.getQueryParams()).toEqual({}); 145 expect(uri.toString()).toEqual('http://www.facebook.com/home.php'); 146 }); 147 148 it('can amend to removed queryData', function() { 149 var uri = JX.$U('http://www.facebook.com/home.php?key=value'); 150 uri.setQueryParams({}); 151 expect(uri.getQueryParams()).toEqual({}); 152 uri.addQueryParams({'herp' : 'derp'}); 153 expect(uri.getQueryParams()).toEqual({'herp' : 'derp'}); 154 expect(uri.toString()).toEqual( 155 'http://www.facebook.com/home.php?herp=derp'); 156 }); 157 158 it('should properly decode entities', function() { 159 var uri = JX.$U('/?from=clown+town&to=cloud%20city&pass=cloud%2Bcountry'); 160 expect(uri.getQueryParams()).toEqual({ 161 'from' : 'clown town', 162 'to' : 'cloud city', 163 'pass' : 'cloud+country' 164 }); 165 expect(uri.toString()).toEqual( 166 '/?from=clown%20town&to=cloud%20city&pass=cloud%2Bcountry'); 167 }); 168 169 it('can add query data', function() { 170 var uri = JX.$U('http://www.facebook.com/'); 171 uri.addQueryParams({'key' : 'value'}); 172 expect(uri.getQueryParams()).toEqual({'key' : 'value'}); 173 expect(uri.toString()).toEqual('http://www.facebook.com/?key=value'); 174 uri.setQueryParam('key', 'lock'); 175 expect(uri.getQueryParams()).toEqual({'key' : 'lock'}); 176 expect(uri.toString()).toEqual('http://www.facebook.com/?key=lock'); 177 }); 178 179 it('can add different types of query data', function() { 180 var uri = new JX.URI(); 181 uri.setQueryParams({ 182 'str' : 'string', 183 'int' : 123, 184 'bool' : true, 185 'badbool' : false, 186 'raw' : '' 187 }); 188 expect(uri.toString()).toEqual( 189 '?str=string&int=123&bool=true&badbool=false&raw'); 190 }); 191 192 it('should properly encode entities in added query data', function() { 193 var uri = new JX.URI(); 194 uri.addQueryParams({'key' : 'two words'}); 195 expect(uri.getQueryParams()).toEqual({'key' : 'two words'}); 196 expect(uri.toString()).toEqual('?key=two%20words'); 197 }); 198 199 it('can add multiple query data', function() { 200 var uri = JX.$U('http://www.facebook.com/'); 201 uri.addQueryParams({ 202 'clown' : 'town', 203 'herp' : 'derp' 204 }); 205 expect(uri.getQueryParams()).toEqual({ 206 'clown' : 'town', 207 'herp' : 'derp' 208 }); 209 expect(uri.toString()).toEqual( 210 'http://www.facebook.com/?clown=town&herp=derp'); 211 }); 212 213 it('can append to existing query data', function() { 214 var uri = JX.$U('/?key=value'); 215 uri.addQueryParams({'clown' : 'town'}); 216 expect(uri.getQueryParams()).toEqual({ 217 'key' : 'value', 218 'clown' : 'town' 219 }); 220 expect(uri.toString()).toEqual('/?key=value&clown=town'); 221 }); 222 223 it('can merge with existing query data', function() { 224 var uri = JX.$U('/?key=value&clown=town'); 225 uri.addQueryParams({ 226 'clown' : 'ville', 227 'herp' : 'derp' 228 }); 229 expect(uri.getQueryParams()).toEqual({ 230 'key' : 'value', 231 'clown' : 'ville', 232 'herp' : 'derp' 233 }); 234 expect(uri.toString()).toEqual('/?key=value&clown=ville&herp=derp'); 235 }); 236 237 it('can replace query data', function() { 238 var uri = JX.$U('/?key=value&clown=town'); 239 uri.setQueryParams({'herp' : 'derp'}); 240 expect(uri.getQueryParams()).toEqual({'herp' : 'derp'}); 241 expect(uri.toString()).toEqual('/?herp=derp'); 242 }); 243 244 it('can remove query data', function() { 245 var uri = JX.$U('/?key=value&clown=town'); 246 uri.addQueryParams({'key' : null}); 247 expect(uri.getQueryParams()).toEqual({ 248 'clown' : 'town', 249 'key' : null 250 }); 251 expect(uri.toString()).toEqual('/?clown=town'); 252 }); 253 254 it('can remove multiple query data', function() { 255 var uri = JX.$U('/?key=value&clown=town&herp=derp'); 256 uri.addQueryParams({'key' : null, 'herp' : undefined}); 257 expect(uri.getQueryParams()).toEqual({ 258 'clown' : 'town', 259 'key' : null, 260 'herp' : undefined 261 }); 262 expect(uri.toString()).toEqual('/?clown=town'); 263 }); 264 265 it('can remove non existent query data', function() { 266 var uri = JX.$U('/?key=value'); 267 uri.addQueryParams({'magic' : null}); 268 expect(uri.getQueryParams()).toEqual({ 269 'key' : 'value', 270 'magic' : null 271 }); 272 expect(uri.toString()).toEqual('/?key=value'); 273 }); 274 275 it('can build uri from scratch', function() { 276 var uri = new JX.URI(); 277 uri.setProtocol('http'); 278 uri.setDomain('www.facebook.com'); 279 uri.setPath('/home.php'); 280 uri.setQueryParams({'key' : 'value'}); 281 uri.setFragment('fragment'); 282 expect(uri.toString()).toEqual( 283 'http://www.facebook.com/home.php?key=value#fragment'); 284 }); 285 286 it('no global state interference', function() { 287 var uri1 = JX.$U('/?key=value'); 288 var uri2 = JX.$U(); 289 expect(uri2.getQueryParams()).not.toEqual({'key' : 'value'}); 290 }); 291 292 it('should not loop indefinitely when parsing empty params', function() { 293 expect(JX.$U('/?&key=value').getQueryParams()).toEqual({'key' : 'value'}); 294 expect(JX.$U('/?&&&key=value').getQueryParams()).toEqual({'key' : 'value'}); 295 expect(JX.$U('/?&&').getQueryParams()).toEqual({}); 296 }); 297 298 it('should parse values with =', function() { 299 expect(JX.$U('/?x=1=1').getQueryParams()).toEqual({'x' : '1=1'}); 300 }); 301 302 });
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |