Apply lowerCamelCase to files for constructors as well.
[mediawiki.git] / tests / jasmine / spec / mediawiki.uri.spec.js
blob9171cdc2dfc65c0a62f4855aa47076ed14908e7f
1 ( function() {
3         describe( "mw.Uri", function() {
5                 describe( "should work well in loose and strict mode", function() {
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                                         }
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                                         } );
28                                         describe( "should construct composite components of URI on request", function() { 
29                                                 it( "should have empty userinfo", function() { 
30                                                         expect( uri.getUserInfo() ).toEqual( '' );
31                                                 } );
33                                                 it( "should have authority equal to host", function() { 
34                                                         expect( uri.getAuthority() ).toEqual( 'www.ietf.org' );
35                                                 } );
37                                                 it( "should have hostport equal to host", function() { 
38                                                         expect( uri.getHostPort() ).toEqual( 'www.ietf.org' );
39                                                 } );
41                                                 it( "should have empty string as query string", function() { 
42                                                         expect( uri.getQueryString() ).toEqual( '' );
43                                                 } );
45                                                 it( "should have path as relative path", function() { 
46                                                         expect( uri.getRelativePath() ).toEqual( '/rfc/rfc2396.txt' );
47                                                 } );
49                                                 it( "should return a uri string equivalent to original", function() { 
50                                                         expect( uri.toString() ).toEqual( uriString );
51                                                 } );
52                                         } );
53                                 } );
54                         }
56                         describe( "should work in loose mode", function() { 
57                                 basicTests( false );
58                         } );
60                         describe( "should work in strict mode", function() {
61                                 basicTests( true );
62                         } );
64                 } );
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                 } );
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                 } );
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                 } );
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' );
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                         } );
127                         describe( "should construct composite components of URI on request", function() { 
128                                 it( "should have userinfo", function() { 
129                                         expect( uri.getUserInfo() ).toEqual( 'auth' );
130                                 } );
132                                 it( "should have authority equal to auth@hostport", function() { 
133                                         expect( uri.getAuthority() ).toEqual( 'auth@www.sample.com:81' );
134                                 } );
136                                 it( "should have hostport equal to host:port", function() { 
137                                         expect( uri.getHostPort() ).toEqual( 'www.sample.com:81' );
138                                 } );
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                                 } );
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                                 } );
154                         } );
155                 } );
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();
161                         it( "should make clones equivalent", function() { 
162                                 expect( original ).toEqual( clone );
163                                 expect( original.toString() ).toEqual( clone.toString() );
164                         } );
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                 } );
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                         } );    
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                 } );
200                 describe( "should be able to manipulate properties", function() { 
201                         var uri;
203                         beforeEach( function() { 
204                                 uri = new mw.Uri( 'http://en.wiki.local/w/api.php' );                   
205                         } );
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                         } );
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                         } );
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                         } );
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                         } );
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                         } );
239                 } );
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                 } );
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                 } );
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                 } );
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                 } );
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                 } );
272         } );
274 } )();