check the checkpoint related options only if we specified checkpoints, duh
[lhc/web/wiklou.git] / tests / jasmine / spec / mediawiki.uri.spec.js
1 ( function() {
2
3 describe( "mw.Uri", function() {
4
5 describe( "should work well in loose and strict mode", function() {
6
7 function basicTests( strict ) {
8
9 describe( "should parse a simple HTTP URI correctly", function() {
10
11 var uriString = 'http://www.ietf.org/rfc/rfc2396.txt';
12 var uri;
13 if ( strict ) {
14 uri = new mw.Uri( uriString, strict );
15 } else {
16 uri = new mw.Uri( uriString );
17 }
18
19 it( "should have basic object properties", function() {
20 expect( uri.protocol ).toEqual( 'http' );
21 expect( uri.host ).toEqual( 'www.ietf.org' );
22 expect( uri.port ).not.toBeDefined();
23 expect( uri.path ).toEqual( '/rfc/rfc2396.txt' );
24 expect( uri.query ).toEqual( {} );
25 expect( uri.fragment ).not.toBeDefined();
26 } );
27
28 describe( "should construct composite components of URI on request", function() {
29 it( "should have empty userinfo", function() {
30 expect( uri.getUserInfo() ).toEqual( '' );
31 } );
32
33 it( "should have authority equal to host", function() {
34 expect( uri.getAuthority() ).toEqual( 'www.ietf.org' );
35 } );
36
37 it( "should have hostport equal to host", function() {
38 expect( uri.getHostPort() ).toEqual( 'www.ietf.org' );
39 } );
40
41 it( "should have empty string as query string", function() {
42 expect( uri.getQueryString() ).toEqual( '' );
43 } );
44
45 it( "should have path as relative path", function() {
46 expect( uri.getRelativePath() ).toEqual( '/rfc/rfc2396.txt' );
47 } );
48
49 it( "should return a uri string equivalent to original", function() {
50 expect( uri.toString() ).toEqual( uriString );
51 } );
52 } );
53 } );
54 }
55
56 describe( "should work in loose mode", function() {
57 basicTests( false );
58 } );
59
60 describe( "should work in strict mode", function() {
61 basicTests( true );
62 } );
63
64 } );
65
66 it( "should parse a simple ftp URI correctly with user and password", function() {
67 var uri = new mw.Uri( 'ftp://usr:pwd@192.0.2.16/' );
68 expect( uri.protocol ).toEqual( 'ftp' );
69 expect( uri.user ).toEqual( 'usr' );
70 expect( uri.password ).toEqual( 'pwd' );
71 expect( uri.host ).toEqual( '192.0.2.16' );
72 expect( uri.port ).not.toBeDefined();
73 expect( uri.path ).toEqual( '/' );
74 expect( uri.query ).toEqual( {} );
75 expect( uri.fragment ).not.toBeDefined();
76 } );
77
78 it( "should parse a simple querystring", function() {
79 var uri = new mw.Uri( 'http://www.google.com/?q=uri' );
80 expect( uri.protocol ).toEqual( 'http' );
81 expect( uri.host ).toEqual( 'www.google.com' );
82 expect( uri.port ).not.toBeDefined();
83 expect( uri.path ).toEqual( '/' );
84 expect( uri.query ).toBeDefined();
85 expect( uri.query ).toEqual( { q: 'uri' } );
86 expect( uri.fragment ).not.toBeDefined();
87 expect( uri.getQueryString() ).toEqual( 'q=uri' );
88 } );
89
90 describe( "should handle multiple value query args", function() {
91 var uri = new mw.Uri( 'http://www.sample.com/dir/?m=foo&m=bar&n=1' );
92 it ( "should parse with multiple values", function() {
93 expect( uri.query.m.length ).toEqual( 2 );
94 expect( uri.query.m[0] ).toEqual( 'foo' );
95 expect( uri.query.m[1] ).toEqual( 'bar' );
96 expect( uri.query.n ).toEqual( '1' );
97 } );
98 it ( "should accept multiple values", function() {
99 uri.query.n = [ "x", "y", "z" ];
100 expect( uri.toString() ).toContain( 'm=foo&m=bar' );
101 expect( uri.toString() ).toContain( 'n=x&n=y&n=z' );
102 expect( uri.toString().length ).toEqual( 'http://www.sample.com/dir/?m=foo&m=bar&n=x&n=y&n=z'.length );
103 } );
104 it ( "should be okay with removing values", function() {
105 uri.query.m.splice( 0, 1 );
106 delete uri.query.n;
107 expect( uri.toString() ).toEqual( 'http://www.sample.com/dir/?m=bar' );
108 uri.query.m.splice( 0, 1 );
109 expect( uri.toString() ).toEqual( 'http://www.sample.com/dir/' );
110 } );
111 } );
112
113 describe( "should deal with an all-dressed URI with everything", function() {
114 var uri = new mw.Uri( 'http://auth@www.sample.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value+%28escaped%29#top' );
115
116 it( "should have basic object properties", function() {
117 expect( uri.protocol ).toEqual( 'http' );
118 expect( uri.user ).toEqual( 'auth' );
119 expect( uri.password ).not.toBeDefined();
120 expect( uri.host ).toEqual( 'www.sample.com' );
121 expect( uri.port ).toEqual( '81' );
122 expect( uri.path ).toEqual( '/dir/dir.2/index.htm' );
123 expect( uri.query ).toEqual( { q1: '0', test1: null, test2: 'value (escaped)' } );
124 expect( uri.fragment ).toEqual( 'top' );
125 } );
126
127 describe( "should construct composite components of URI on request", function() {
128 it( "should have userinfo", function() {
129 expect( uri.getUserInfo() ).toEqual( 'auth' );
130 } );
131
132 it( "should have authority equal to auth@hostport", function() {
133 expect( uri.getAuthority() ).toEqual( 'auth@www.sample.com:81' );
134 } );
135
136 it( "should have hostport equal to host:port", function() {
137 expect( uri.getHostPort() ).toEqual( 'www.sample.com:81' );
138 } );
139
140 it( "should have query string which contains all components", function() {
141 var queryString = uri.getQueryString();
142 expect( queryString ).toContain( 'q1=0' );
143 expect( queryString ).toContain( 'test1' );
144 expect( queryString ).not.toContain( 'test1=' );
145 expect( queryString ).toContain( 'test2=value+%28escaped%29' );
146 } );
147
148 it( "should have path as relative path", function() {
149 expect( uri.getRelativePath() ).toContain( uri.path );
150 expect( uri.getRelativePath() ).toContain( uri.getQueryString() );
151 expect( uri.getRelativePath() ).toContain( uri.fragment );
152 } );
153
154 } );
155 } );
156
157 describe( "should be able to clone itself", function() {
158 var original = new mw.Uri( 'http://en.wiki.local/w/api.php?action=query&foo=bar' );
159 var clone = original.clone();
160
161 it( "should make clones equivalent", function() {
162 expect( original ).toEqual( clone );
163 expect( original.toString() ).toEqual( clone.toString() );
164 } );
165
166 it( "should be able to manipulate clones independently", function() {
167 // but they are still different objects
168 expect( original ).not.toBe( clone );
169 // and can diverge
170 clone.host = 'fr.wiki.local';
171 expect( original.host ).not.toEqual( clone.host );
172 expect( original.toString() ).not.toEqual( clone.toString() );
173 } );
174 } );
175
176 describe( "should be able to construct URL from object", function() {
177 it ( "should construct given basic arguments", function() {
178 var uri = new mw.Uri( { protocol: 'http', host: 'www.foo.local', path: '/this' } );
179 expect( uri.toString() ).toEqual( 'http://www.foo.local/this' );
180 } );
181
182 it ( "should construct given more complex arguments", function() {
183 var uri = new mw.Uri( {
184 protocol: 'http',
185 host: 'www.foo.local',
186 path: '/this',
187 query: { hi: 'there' },
188 fragment: 'blah'
189 } );
190 expect( uri.toString() ).toEqual( 'http://www.foo.local/this?hi=there#blah' );
191 } );
192
193 it ( "should fail to construct without required properties", function() {
194 expect( function() {
195 var uri = new mw.Uri( { protocol: 'http', host: 'www.foo.local' } );
196 } ).toThrow( "Bad constructor arguments" );
197 } );
198 } );
199
200 describe( "should be able to manipulate properties", function() {
201 var uri;
202
203 beforeEach( function() {
204 uri = new mw.Uri( 'http://en.wiki.local/w/api.php' );
205 } );
206
207 it( "can add a fragment", function() {
208 uri.fragment = 'frag';
209 expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php#frag' );
210 } );
211
212 it( "can change host and port", function() {
213 uri.host = 'fr.wiki.local';
214 uri.port = '8080';
215 expect( uri.toString() ).toEqual( 'http://fr.wiki.local:8080/w/api.php' );
216 } );
217
218 it ( "can add query arguments", function() {
219 uri.query.foo = 'bar';
220 expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php?foo=bar' );
221 } );
222
223 it ( "can extend query arguments", function() {
224 uri.query.foo = 'bar';
225 expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php?foo=bar' );
226 uri.extend( { foo: 'quux', pif: 'paf' } );
227 expect( uri.toString() ).toContain( 'foo=quux' );
228 expect( uri.toString() ).not.toContain( 'foo=bar' );
229 expect( uri.toString() ).toContain( 'pif=paf' );
230 } );
231
232 it ( "can remove query arguments", function() {
233 uri.query.foo = 'bar';
234 expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php?foo=bar' );
235 delete( uri.query.foo );
236 expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php' );
237 } );
238
239 } );
240
241 it( "should throw error on no arguments to constructor", function() {
242 expect( function() {
243 uri = new mw.Uri();
244 } ).toThrow( "Bad constructor arguments" );
245 } );
246
247 it( "should throw error on empty string as argument to constructor", function() {
248 expect( function() {
249 uri = new mw.Uri( '' );
250 } ).toThrow( "Bad constructor arguments" );
251 } );
252
253 it( "should throw error on non-URI as argument to constructor", function() {
254 expect( function() {
255 uri = new mw.Uri( 'glaswegian penguins' );
256 } ).toThrow( "Bad constructor arguments" );
257 } );
258
259 it( "should throw error on improper URI as argument to constructor", function() {
260 expect( function() {
261 uri = new mw.Uri( 'http:/foo.com' );
262 } ).toThrow( "Bad constructor arguments" );
263 } );
264
265 it( "should throw error on URI without protocol as argument to constructor", function() {
266 expect( function() {
267 uri = new mw.Uri( 'foo.com/bar/baz' );
268 } ).toThrow( "Bad constructor arguments" );
269 } );
270
271
272 } );
273
274 } )();