mislav-addressable 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3632 @@
1
+ # encoding: utf-8
2
+ #--
3
+ # Addressable, Copyright (c) 2006-2007 Bob Aman
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #++
24
+
25
+ $:.unshift(File.expand_path(File.dirname(__FILE__) + '/../../lib'))
26
+ $:.uniq!
27
+
28
+ require "addressable/uri"
29
+
30
+ if !"".respond_to?("force_encoding")
31
+ class String
32
+ def force_encoding(encoding)
33
+ @encoding = encoding
34
+ end
35
+
36
+ def encoding
37
+ @encoding ||= Encoding::ASCII_8BIT
38
+ end
39
+ end
40
+
41
+ class Encoding
42
+ def initialize(name)
43
+ @name = name
44
+ end
45
+
46
+ def to_s
47
+ return @name
48
+ end
49
+
50
+ UTF_8 = Encoding.new("UTF-8")
51
+ ASCII_8BIT = Encoding.new("US-ASCII")
52
+ end
53
+ end
54
+
55
+ module URI
56
+ class HTTP
57
+ def initialize(uri)
58
+ @uri = uri
59
+ end
60
+
61
+ def to_s
62
+ return @uri.to_s
63
+ end
64
+ end
65
+ end
66
+
67
+ describe Addressable::URI, "when created with a non-numeric port number" do
68
+ it "should raise an error" do
69
+ (lambda do
70
+ Addressable::URI.new(:port => "bogus")
71
+ end).should raise_error(Addressable::URI::InvalidURIError)
72
+ end
73
+ end
74
+
75
+ describe Addressable::URI, "when created with a non-string scheme" do
76
+ it "should raise an error" do
77
+ (lambda do
78
+ Addressable::URI.new(:scheme => :bogus)
79
+ end).should raise_error(TypeError)
80
+ end
81
+ end
82
+
83
+ describe Addressable::URI, "when created with a non-string user" do
84
+ it "should raise an error" do
85
+ (lambda do
86
+ Addressable::URI.new(:user => :bogus)
87
+ end).should raise_error(TypeError)
88
+ end
89
+ end
90
+
91
+ describe Addressable::URI, "when created with a non-string password" do
92
+ it "should raise an error" do
93
+ (lambda do
94
+ Addressable::URI.new(:password => :bogus)
95
+ end).should raise_error(TypeError)
96
+ end
97
+ end
98
+
99
+ describe Addressable::URI, "when created with a non-string userinfo" do
100
+ it "should raise an error" do
101
+ (lambda do
102
+ Addressable::URI.new(:userinfo => :bogus)
103
+ end).should raise_error(TypeError)
104
+ end
105
+ end
106
+
107
+ describe Addressable::URI, "when created with a non-string host" do
108
+ it "should raise an error" do
109
+ (lambda do
110
+ Addressable::URI.new(:host => :bogus)
111
+ end).should raise_error(TypeError)
112
+ end
113
+ end
114
+
115
+ describe Addressable::URI, "when created with a non-string authority" do
116
+ it "should raise an error" do
117
+ (lambda do
118
+ Addressable::URI.new(:authority => :bogus)
119
+ end).should raise_error(TypeError)
120
+ end
121
+ end
122
+
123
+ describe Addressable::URI, "when created with a non-string authority" do
124
+ it "should raise an error" do
125
+ (lambda do
126
+ Addressable::URI.new(:authority => :bogus)
127
+ end).should raise_error(TypeError)
128
+ end
129
+ end
130
+
131
+ describe Addressable::URI, "when created with a non-string path" do
132
+ it "should raise an error" do
133
+ (lambda do
134
+ Addressable::URI.new(:path => :bogus)
135
+ end).should raise_error(TypeError)
136
+ end
137
+ end
138
+
139
+ describe Addressable::URI, "when created with a non-string query" do
140
+ it "should raise an error" do
141
+ (lambda do
142
+ Addressable::URI.new(:query => :bogus)
143
+ end).should raise_error(TypeError)
144
+ end
145
+ end
146
+
147
+ describe Addressable::URI, "when created with a non-string fragment" do
148
+ it "should raise an error" do
149
+ (lambda do
150
+ Addressable::URI.new(:fragment => :bogus)
151
+ end).should raise_error(TypeError)
152
+ end
153
+ end
154
+
155
+ describe Addressable::URI, "when created with a scheme but no hierarchical " +
156
+ "segment" do
157
+ it "should raise an error" do
158
+ (lambda do
159
+ Addressable::URI.parse("http:")
160
+ end).should raise_error(Addressable::URI::InvalidURIError)
161
+ end
162
+ end
163
+
164
+ describe Addressable::URI, "when created from nil components" do
165
+ before do
166
+ @uri = Addressable::URI.new
167
+ end
168
+
169
+ it "should have an empty path" do
170
+ @uri.path.should == ""
171
+ end
172
+
173
+ it "should be an empty uri" do
174
+ @uri.to_s.should == ""
175
+ end
176
+
177
+ it "should still be an empty uri if the scheme is set to whitespace" do
178
+ @uri.scheme = "\t \n"
179
+ @uri.to_s.should == ""
180
+ end
181
+
182
+ it "should raise an error if set into an invalid state" do
183
+ (lambda do
184
+ @uri.user = "user"
185
+ end).should raise_error(Addressable::URI::InvalidURIError)
186
+ end
187
+
188
+ it "should raise an error if set into an invalid state" do
189
+ (lambda do
190
+ @uri.password = "pass"
191
+ end).should raise_error(Addressable::URI::InvalidURIError)
192
+ end
193
+
194
+ it "should raise an error if set into an invalid state" do
195
+ (lambda do
196
+ @uri.scheme = "http"
197
+ @uri.fragment = "fragment"
198
+ end).should raise_error(Addressable::URI::InvalidURIError)
199
+ end
200
+
201
+ it "should raise an error if set into an invalid state" do
202
+ (lambda do
203
+ @uri.fragment = "fragment"
204
+ @uri.scheme = "http"
205
+ end).should raise_error(Addressable::URI::InvalidURIError)
206
+ end
207
+ end
208
+
209
+ describe Addressable::URI, "when created from string components" do
210
+ before do
211
+ @uri = Addressable::URI.new(
212
+ :scheme => "http", :host => "example.com"
213
+ )
214
+ end
215
+
216
+ it "should be equal to the equivalent parsed URI" do
217
+ @uri.should == Addressable::URI.parse("http://example.com")
218
+ end
219
+
220
+ it "should raise an error if invalid components omitted" do
221
+ (lambda do
222
+ @uri.omit(:bogus)
223
+ end).should raise_error(ArgumentError)
224
+ (lambda do
225
+ @uri.omit(:scheme, :bogus, :path)
226
+ end).should raise_error(ArgumentError)
227
+ end
228
+ end
229
+
230
+ describe Addressable::URI, "when created with a nil host but " +
231
+ "non-nil authority components" do
232
+ it "should raise an error" do
233
+ (lambda do
234
+ Addressable::URI.new(:user => "user", :password => "pass", :port => 80)
235
+ end).should raise_error(Addressable::URI::InvalidURIError)
236
+ end
237
+ end
238
+
239
+ describe Addressable::URI, "when created with both an authority and a user" do
240
+ it "should raise an error" do
241
+ (lambda do
242
+ Addressable::URI.new(
243
+ :user => "user", :authority => "user@example.com:80"
244
+ )
245
+ end).should raise_error(ArgumentError)
246
+ end
247
+ end
248
+
249
+ describe Addressable::URI, "when created with an authority and no port" do
250
+ before do
251
+ @uri = Addressable::URI.new(:authority => "user@example.com")
252
+ end
253
+
254
+ it "should not infer a port" do
255
+ @uri.port.should == nil
256
+ @uri.inferred_port.should == nil
257
+ end
258
+ end
259
+
260
+ describe Addressable::URI, "when created with both a userinfo and a user" do
261
+ it "should raise an error" do
262
+ (lambda do
263
+ Addressable::URI.new(:user => "user", :userinfo => "user:pass")
264
+ end).should raise_error(ArgumentError)
265
+ end
266
+ end
267
+
268
+ describe Addressable::URI, "when created with a path that hasn't been " +
269
+ "prefixed with a '/' but a host specified" do
270
+ it "should prefix a '/' to the path" do
271
+ Addressable::URI.new(
272
+ :scheme => "http", :host => "example.com", :path => "path"
273
+ ).should == Addressable::URI.parse("http://example.com/path")
274
+ end
275
+ end
276
+
277
+ describe Addressable::URI, "when created with a path that hasn't been " +
278
+ "prefixed with a '/' but no host specified" do
279
+ it "should prefix a '/' to the path" do
280
+ Addressable::URI.new(
281
+ :scheme => "http", :path => "path"
282
+ ).should == Addressable::URI.parse("http:path")
283
+ end
284
+ end
285
+
286
+ describe Addressable::URI, "when parsed from an Addressable::URI object" do
287
+ it "should return the object" do
288
+ uri = Addressable::URI.parse("http://example.com/")
289
+ (lambda do
290
+ Addressable::URI.parse(uri).object_id.should == uri.object_id
291
+ end).should_not raise_error
292
+ end
293
+
294
+ it "should return the object" do
295
+ uri = Addressable::URI.parse("http://example.com/")
296
+ (lambda do
297
+ Addressable::URI.heuristic_parse(uri).object_id.should == uri.object_id
298
+ end).should_not raise_error
299
+ end
300
+ end
301
+
302
+ describe Addressable::URI, "when parsed from something that looks " +
303
+ "like a URI object" do
304
+ it "should parse without error" do
305
+ uri = Addressable::URI.parse(URI::HTTP.new("http://example.com/"))
306
+ (lambda do
307
+ Addressable::URI.parse(uri)
308
+ end).should_not raise_error
309
+ end
310
+ end
311
+
312
+ describe Addressable::URI, "when parsed from ''" do
313
+ before do
314
+ @uri = Addressable::URI.parse("")
315
+ end
316
+
317
+ it "should have no scheme" do
318
+ @uri.scheme.should == nil
319
+ end
320
+
321
+ it "should not be considered to be ip-based" do
322
+ @uri.should_not be_ip_based
323
+ end
324
+
325
+ it "should have a path of ''" do
326
+ @uri.path.should == ""
327
+ end
328
+
329
+ it "should be considered relative" do
330
+ @uri.should be_relative
331
+ end
332
+
333
+ it "should be considered to be in normal form" do
334
+ @uri.normalize.should be_eql(@uri)
335
+ end
336
+ end
337
+
338
+ # Section 1.1.2 of RFC 3986
339
+ describe Addressable::URI, "when parsed from " +
340
+ "'ftp://ftp.is.co.za/rfc/rfc1808.txt'" do
341
+ before do
342
+ @uri = Addressable::URI.parse("ftp://ftp.is.co.za/rfc/rfc1808.txt")
343
+ end
344
+
345
+ it "should use the 'ftp' scheme" do
346
+ @uri.scheme.should == "ftp"
347
+ end
348
+
349
+ it "should be considered to be ip-based" do
350
+ @uri.should be_ip_based
351
+ end
352
+
353
+ it "should have a host of 'ftp.is.co.za'" do
354
+ @uri.host.should == "ftp.is.co.za"
355
+ end
356
+
357
+ it "should have a path of '/rfc/rfc1808.txt'" do
358
+ @uri.path.should == "/rfc/rfc1808.txt"
359
+ end
360
+
361
+ it "should be considered to be in normal form" do
362
+ @uri.normalize.should be_eql(@uri)
363
+ end
364
+ end
365
+
366
+ # Section 1.1.2 of RFC 3986
367
+ describe Addressable::URI, "when parsed from " +
368
+ "'http://www.ietf.org/rfc/rfc2396.txt'" do
369
+ before do
370
+ @uri = Addressable::URI.parse("http://www.ietf.org/rfc/rfc2396.txt")
371
+ end
372
+
373
+ it "should use the 'http' scheme" do
374
+ @uri.scheme.should == "http"
375
+ end
376
+
377
+ it "should be considered to be ip-based" do
378
+ @uri.should be_ip_based
379
+ end
380
+
381
+ it "should have a host of 'www.ietf.org'" do
382
+ @uri.host.should == "www.ietf.org"
383
+ end
384
+
385
+ it "should have a path of '/rfc/rfc2396.txt'" do
386
+ @uri.path.should == "/rfc/rfc2396.txt"
387
+ end
388
+
389
+ it "should be considered to be in normal form" do
390
+ @uri.normalize.should be_eql(@uri)
391
+ end
392
+
393
+ it "should correctly omit components" do
394
+ @uri.omit(:scheme).to_s.should == "//www.ietf.org/rfc/rfc2396.txt"
395
+ @uri.omit(:path).to_s.should == "http://www.ietf.org"
396
+ end
397
+
398
+ it "should correctly omit components destructively" do
399
+ @uri.omit!(:scheme)
400
+ @uri.to_s.should == "//www.ietf.org/rfc/rfc2396.txt"
401
+ end
402
+ end
403
+
404
+ # Section 1.1.2 of RFC 3986
405
+ describe Addressable::URI, "when parsed from " +
406
+ "'ldap://[2001:db8::7]/c=GB?objectClass?one'" do
407
+ before do
408
+ @uri = Addressable::URI.parse("ldap://[2001:db8::7]/c=GB?objectClass?one")
409
+ end
410
+
411
+ it "should use the 'ldap' scheme" do
412
+ @uri.scheme.should == "ldap"
413
+ end
414
+
415
+ it "should be considered to be ip-based" do
416
+ @uri.should be_ip_based
417
+ end
418
+
419
+ it "should have a host of '[2001:db8::7]'" do
420
+ @uri.host.should == "[2001:db8::7]"
421
+ end
422
+
423
+ it "should have a path of '/c=GB'" do
424
+ @uri.path.should == "/c=GB"
425
+ end
426
+
427
+ it "should have a query of 'objectClass?one'" do
428
+ @uri.query.should == "objectClass?one"
429
+ end
430
+
431
+ it "should be considered to be in normal form" do
432
+ @uri.normalize.should be_eql(@uri)
433
+ end
434
+
435
+ it "should correctly omit components" do
436
+ @uri.omit(:scheme, :authority).to_s.should == "/c=GB?objectClass?one"
437
+ @uri.omit(:path).to_s.should == "ldap://[2001:db8::7]?objectClass?one"
438
+ end
439
+
440
+ it "should correctly omit components destructively" do
441
+ @uri.omit!(:scheme, :authority)
442
+ @uri.to_s.should == "/c=GB?objectClass?one"
443
+ end
444
+
445
+ it "should raise an error if omission would create an invalid URI" do
446
+ (lambda do
447
+ @uri.omit(:authority, :path)
448
+ end).should raise_error(Addressable::URI::InvalidURIError)
449
+ end
450
+ end
451
+
452
+ # Section 1.1.2 of RFC 3986
453
+ describe Addressable::URI, "when parsed from " +
454
+ "'mailto:John.Doe@example.com'" do
455
+ before do
456
+ @uri = Addressable::URI.parse("mailto:John.Doe@example.com")
457
+ end
458
+
459
+ it "should use the 'mailto' scheme" do
460
+ @uri.scheme.should == "mailto"
461
+ end
462
+
463
+ it "should not be considered to be ip-based" do
464
+ @uri.should_not be_ip_based
465
+ end
466
+
467
+ it "should have a path of 'John.Doe@example.com'" do
468
+ @uri.path.should == "John.Doe@example.com"
469
+ end
470
+
471
+ it "should be considered to be in normal form" do
472
+ @uri.normalize.should be_eql(@uri)
473
+ end
474
+ end
475
+
476
+ # Section 1.1.2 of RFC 3986
477
+ describe Addressable::URI, "when parsed from " +
478
+ "'news:comp.infosystems.www.servers.unix'" do
479
+ before do
480
+ @uri = Addressable::URI.parse("news:comp.infosystems.www.servers.unix")
481
+ end
482
+
483
+ it "should use the 'news' scheme" do
484
+ @uri.scheme.should == "news"
485
+ end
486
+
487
+ it "should not be considered to be ip-based" do
488
+ @uri.should_not be_ip_based
489
+ end
490
+
491
+ it "should have a path of 'comp.infosystems.www.servers.unix'" do
492
+ @uri.path.should == "comp.infosystems.www.servers.unix"
493
+ end
494
+
495
+ it "should be considered to be in normal form" do
496
+ @uri.normalize.should be_eql(@uri)
497
+ end
498
+ end
499
+
500
+ # Section 1.1.2 of RFC 3986
501
+ describe Addressable::URI, "when parsed from " +
502
+ "'tel:+1-816-555-1212'" do
503
+ before do
504
+ @uri = Addressable::URI.parse("tel:+1-816-555-1212")
505
+ end
506
+
507
+ it "should use the 'tel' scheme" do
508
+ @uri.scheme.should == "tel"
509
+ end
510
+
511
+ it "should not be considered to be ip-based" do
512
+ @uri.should_not be_ip_based
513
+ end
514
+
515
+ it "should have a path of '+1-816-555-1212'" do
516
+ @uri.path.should == "+1-816-555-1212"
517
+ end
518
+
519
+ it "should be considered to be in normal form" do
520
+ @uri.normalize.should be_eql(@uri)
521
+ end
522
+ end
523
+
524
+ # Section 1.1.2 of RFC 3986
525
+ describe Addressable::URI, "when parsed from " +
526
+ "'telnet://192.0.2.16:80/'" do
527
+ before do
528
+ @uri = Addressable::URI.parse("telnet://192.0.2.16:80/")
529
+ end
530
+
531
+ it "should use the 'telnet' scheme" do
532
+ @uri.scheme.should == "telnet"
533
+ end
534
+
535
+ it "should have a host of '192.0.2.16'" do
536
+ @uri.host.should == "192.0.2.16"
537
+ end
538
+
539
+ it "should have a port of '80'" do
540
+ @uri.port.should == 80
541
+ end
542
+
543
+ it "should be considered to be ip-based" do
544
+ @uri.should be_ip_based
545
+ end
546
+
547
+ it "should have a path of '/'" do
548
+ @uri.path.should == "/"
549
+ end
550
+
551
+ it "should be considered to be in normal form" do
552
+ @uri.normalize.should be_eql(@uri)
553
+ end
554
+ end
555
+
556
+ # Section 1.1.2 of RFC 3986
557
+ describe Addressable::URI, "when parsed from " +
558
+ "'urn:oasis:names:specification:docbook:dtd:xml:4.1.2'" do
559
+ before do
560
+ @uri = Addressable::URI.parse(
561
+ "urn:oasis:names:specification:docbook:dtd:xml:4.1.2")
562
+ end
563
+
564
+ it "should use the 'urn' scheme" do
565
+ @uri.scheme.should == "urn"
566
+ end
567
+
568
+ it "should not be considered to be ip-based" do
569
+ @uri.should_not be_ip_based
570
+ end
571
+
572
+ it "should have a path of " +
573
+ "'oasis:names:specification:docbook:dtd:xml:4.1.2'" do
574
+ @uri.path.should == "oasis:names:specification:docbook:dtd:xml:4.1.2"
575
+ end
576
+
577
+ it "should be considered to be in normal form" do
578
+ @uri.normalize.should be_eql(@uri)
579
+ end
580
+ end
581
+
582
+ describe Addressable::URI, "when parsed from " +
583
+ "'http://example.com'" do
584
+ before do
585
+ @uri = Addressable::URI.parse("http://example.com")
586
+ end
587
+
588
+ it "when inspected, should have the correct URI" do
589
+ @uri.inspect.should include("http://example.com")
590
+ end
591
+
592
+ it "when inspected, should have the correct class name" do
593
+ @uri.inspect.should include("Addressable::URI")
594
+ end
595
+
596
+ it "when inspected, should have the correct object id" do
597
+ @uri.inspect.should include("%#0x" % @uri.object_id)
598
+ end
599
+
600
+ it "should use the 'http' scheme" do
601
+ @uri.scheme.should == "http"
602
+ end
603
+
604
+ it "should be considered to be ip-based" do
605
+ @uri.should be_ip_based
606
+ end
607
+
608
+ it "should have an authority segment of 'example.com'" do
609
+ @uri.authority.should == "example.com"
610
+ end
611
+
612
+ it "should have a host of 'example.com'" do
613
+ @uri.host.should == "example.com"
614
+ end
615
+
616
+ it "should be considered ip-based" do
617
+ @uri.should be_ip_based
618
+ end
619
+
620
+ it "should have no username" do
621
+ @uri.user.should == nil
622
+ end
623
+
624
+ it "should have no password" do
625
+ @uri.password.should == nil
626
+ end
627
+
628
+ it "should use port 80" do
629
+ @uri.inferred_port.should == 80
630
+ end
631
+
632
+ it "should not have a specified port" do
633
+ @uri.port.should == nil
634
+ end
635
+
636
+ it "should have an empty path" do
637
+ @uri.path.should == ""
638
+ end
639
+
640
+ it "should have no query string" do
641
+ @uri.query.should == nil
642
+ @uri.query_values.should == nil
643
+ end
644
+
645
+ it "should have no fragment" do
646
+ @uri.fragment.should == nil
647
+ end
648
+
649
+ it "should be considered absolute" do
650
+ @uri.should be_absolute
651
+ end
652
+
653
+ it "should not be considered relative" do
654
+ @uri.should_not be_relative
655
+ end
656
+
657
+ it "should not be exactly equal to 42" do
658
+ @uri.eql?(42).should == false
659
+ end
660
+
661
+ it "should not be equal to 42" do
662
+ (@uri == 42).should == false
663
+ end
664
+
665
+ it "should not be roughly equal to 42" do
666
+ (@uri === 42).should == false
667
+ end
668
+
669
+ it "should be exactly equal to http://example.com" do
670
+ @uri.eql?(Addressable::URI.parse("http://example.com")).should == true
671
+ end
672
+
673
+ it "should be roughly equal to http://example.com/" do
674
+ (@uri === Addressable::URI.parse("http://example.com/")).should == true
675
+ end
676
+
677
+ it "should be roughly equal to the string 'http://example.com/'" do
678
+ (@uri === "http://example.com/").should == true
679
+ end
680
+
681
+ it "should not be roughly equal to the string " +
682
+ "'http://example.com:bogus/'" do
683
+ (lambda do
684
+ (@uri === "http://example.com:bogus/").should == false
685
+ end).should_not raise_error
686
+ end
687
+
688
+ it "should result in itself when joined with itself" do
689
+ @uri.join(@uri).to_s.should == "http://example.com"
690
+ @uri.join!(@uri).to_s.should == "http://example.com"
691
+ end
692
+
693
+ # Section 6.2.3 of RFC 3986
694
+ it "should be equivalent to http://example.com/" do
695
+ @uri.should == Addressable::URI.parse("http://example.com/")
696
+ end
697
+
698
+ # Section 6.2.3 of RFC 3986
699
+ it "should be equivalent to http://example.com:/" do
700
+ @uri.should == Addressable::URI.parse("http://example.com:/")
701
+ end
702
+
703
+ # Section 6.2.3 of RFC 3986
704
+ it "should be equivalent to http://example.com:80/" do
705
+ @uri.should == Addressable::URI.parse("http://example.com:80/")
706
+ end
707
+
708
+ # Section 6.2.2.1 of RFC 3986
709
+ it "should be equivalent to http://EXAMPLE.COM/" do
710
+ @uri.should == Addressable::URI.parse("http://EXAMPLE.COM/")
711
+ end
712
+
713
+ it "should have a route of '/path/' to 'http://example.com/path/'" do
714
+ @uri.route_to("http://example.com/path/").should ==
715
+ Addressable::URI.parse("/path/")
716
+ end
717
+
718
+ it "should have a route of '/' from 'http://example.com/path/'" do
719
+ @uri.route_from("http://example.com/path/").should ==
720
+ Addressable::URI.parse("/")
721
+ end
722
+
723
+ it "should have a route of '#' to 'http://example.com/'" do
724
+ @uri.route_to("http://example.com/").should ==
725
+ Addressable::URI.parse("#")
726
+ end
727
+
728
+ it "should have a route of 'http://elsewhere.com/' to " +
729
+ "'http://elsewhere.com/'" do
730
+ @uri.route_to("http://elsewhere.com/").should ==
731
+ Addressable::URI.parse("http://elsewhere.com/")
732
+ end
733
+
734
+ it "when joined with 'relative/path' should be " +
735
+ "'http://example.com/relative/path'" do
736
+ @uri.join('relative/path').should ==
737
+ Addressable::URI.parse("http://example.com/relative/path")
738
+ end
739
+
740
+ it "when joined with a bogus object a TypeError should be raised" do
741
+ (lambda do
742
+ @uri.join(42)
743
+ end).should raise_error(TypeError)
744
+ end
745
+
746
+ it "should have the correct username after assignment" do
747
+ @uri.user = "newuser"
748
+ @uri.user.should == "newuser"
749
+ @uri.password.should == nil
750
+ @uri.to_s.should == "http://newuser@example.com"
751
+ end
752
+
753
+ it "should have the correct username after assignment" do
754
+ @uri.user = "user@123!"
755
+ @uri.user.should == "user@123!"
756
+ @uri.normalized_user.should == "user%40123%21"
757
+ @uri.password.should == nil
758
+ @uri.normalize.to_s.should == "http://user%40123%21@example.com/"
759
+ end
760
+
761
+ it "should have the correct password after assignment" do
762
+ @uri.password = "newpass"
763
+ @uri.password.should == "newpass"
764
+ @uri.user.should == ""
765
+ @uri.to_s.should == "http://:newpass@example.com"
766
+ end
767
+
768
+ it "should have the correct password after assignment" do
769
+ @uri.password = "secret@123!"
770
+ @uri.password.should == "secret@123!"
771
+ @uri.normalized_password.should == "secret%40123%21"
772
+ @uri.user.should == ""
773
+ @uri.normalize.to_s.should == "http://:secret%40123%21@example.com/"
774
+ @uri.omit(:password).to_s.should == "http://example.com"
775
+ end
776
+
777
+ it "should have the correct user/pass after repeated assignment" do
778
+ @uri.user = nil
779
+ @uri.user.should == nil
780
+ @uri.password = "newpass"
781
+ @uri.password.should == "newpass"
782
+ # Username cannot be nil if the password is set
783
+ @uri.user.should == ""
784
+ @uri.to_s.should == "http://:newpass@example.com"
785
+ @uri.user = "newuser"
786
+ @uri.user.should == "newuser"
787
+ @uri.password = nil
788
+ @uri.password.should == nil
789
+ @uri.to_s.should == "http://newuser@example.com"
790
+ @uri.user = "newuser"
791
+ @uri.user.should == "newuser"
792
+ @uri.password = ""
793
+ @uri.password.should == ""
794
+ @uri.to_s.should == "http://newuser:@example.com"
795
+ @uri.password = "newpass"
796
+ @uri.password.should == "newpass"
797
+ @uri.user = nil
798
+ # Username cannot be nil if the password is set
799
+ @uri.user.should == ""
800
+ @uri.to_s.should == "http://:newpass@example.com"
801
+ end
802
+
803
+ it "should have the correct user/pass after userinfo assignment" do
804
+ @uri.user = "newuser"
805
+ @uri.user.should == "newuser"
806
+ @uri.password = "newpass"
807
+ @uri.password.should == "newpass"
808
+ @uri.userinfo = nil
809
+ @uri.userinfo.should == nil
810
+ @uri.user.should == nil
811
+ @uri.password.should == nil
812
+ end
813
+
814
+ it "should correctly convert to a hash" do
815
+ @uri.to_hash.should == {
816
+ :scheme => "http",
817
+ :user => nil,
818
+ :password => nil,
819
+ :host => "example.com",
820
+ :port => nil,
821
+ :path => "",
822
+ :query => nil,
823
+ :fragment => nil
824
+ }
825
+ end
826
+
827
+ it "should be identical to its duplicate" do
828
+ @uri.should == @uri.dup
829
+ end
830
+ end
831
+
832
+ describe Addressable::URI, "when parsed from " +
833
+ "'http://example.com/'" do
834
+ before do
835
+ @uri = Addressable::URI.parse("http://example.com/")
836
+ end
837
+
838
+ # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
839
+ it "should be equivalent to http://example.com" do
840
+ @uri.should == Addressable::URI.parse("http://example.com")
841
+ end
842
+
843
+ # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
844
+ it "should be equivalent to HTTP://example.com/" do
845
+ @uri.should == Addressable::URI.parse("HTTP://example.com/")
846
+ end
847
+
848
+ # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
849
+ it "should be equivalent to http://example.com:/" do
850
+ @uri.should == Addressable::URI.parse("http://example.com:/")
851
+ end
852
+
853
+ # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
854
+ it "should be equivalent to http://example.com:80/" do
855
+ @uri.should == Addressable::URI.parse("http://example.com:80/")
856
+ end
857
+
858
+ # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
859
+ it "should be equivalent to http://Example.com/" do
860
+ @uri.should == Addressable::URI.parse("http://Example.com/")
861
+ end
862
+
863
+ it "should have the correct username after assignment" do
864
+ @uri.user = nil
865
+ @uri.user.should == nil
866
+ @uri.password.should == nil
867
+ @uri.to_s.should == "http://example.com/"
868
+ end
869
+
870
+ it "should have the correct password after assignment" do
871
+ @uri.password = nil
872
+ @uri.password.should == nil
873
+ @uri.user.should == nil
874
+ @uri.to_s.should == "http://example.com/"
875
+ end
876
+
877
+ it "should correctly convert to a hash" do
878
+ @uri.to_hash.should == {
879
+ :scheme => "http",
880
+ :user => nil,
881
+ :password => nil,
882
+ :host => "example.com",
883
+ :port => nil,
884
+ :path => "/",
885
+ :query => nil,
886
+ :fragment => nil
887
+ }
888
+ end
889
+
890
+ it "should be identical to its duplicate" do
891
+ @uri.should == @uri.dup
892
+ end
893
+
894
+ it "should have the same hash as its duplicate" do
895
+ @uri.hash.should == @uri.dup.hash
896
+ end
897
+
898
+ it "should have a different hash from its equivalent String value" do
899
+ @uri.hash.should_not == @uri.to_s.hash
900
+ end
901
+
902
+ it "should have the same hash as an equal URI" do
903
+ @uri.hash.should == Addressable::URI.parse("http://example.com/").hash
904
+ end
905
+ end
906
+
907
+ describe Addressable::URI, "when parsed from " +
908
+ "'http://@example.com/'" do
909
+ before do
910
+ @uri = Addressable::URI.parse("http://@example.com/")
911
+ end
912
+
913
+ it "should be equivalent to http://example.com" do
914
+ @uri.should == Addressable::URI.parse("http://example.com")
915
+ end
916
+
917
+ it "should correctly convert to a hash" do
918
+ @uri.to_hash.should == {
919
+ :scheme => "http",
920
+ :user => "",
921
+ :password => nil,
922
+ :host => "example.com",
923
+ :port => nil,
924
+ :path => "/",
925
+ :query => nil,
926
+ :fragment => nil
927
+ }
928
+ end
929
+
930
+ it "should be identical to its duplicate" do
931
+ @uri.should == @uri.dup
932
+ end
933
+ end
934
+
935
+ describe Addressable::URI, "when parsed from " +
936
+ "'http://example.com./'" do
937
+ before do
938
+ @uri = Addressable::URI.parse("http://example.com./")
939
+ end
940
+
941
+ it "should be equivalent to http://example.com" do
942
+ @uri.should == Addressable::URI.parse("http://example.com")
943
+ end
944
+
945
+ it "should not be considered to be in normal form" do
946
+ @uri.normalize.should_not be_eql(@uri)
947
+ end
948
+
949
+ it "should be identical to its duplicate" do
950
+ @uri.should == @uri.dup
951
+ end
952
+ end
953
+
954
+ describe Addressable::URI, "when parsed from " +
955
+ "'http://:@example.com/'" do
956
+ before do
957
+ @uri = Addressable::URI.parse("http://:@example.com/")
958
+ end
959
+
960
+ it "should be equivalent to http://example.com" do
961
+ @uri.should == Addressable::URI.parse("http://example.com")
962
+ end
963
+
964
+ it "should correctly convert to a hash" do
965
+ @uri.to_hash.should == {
966
+ :scheme => "http",
967
+ :user => "",
968
+ :password => "",
969
+ :host => "example.com",
970
+ :port => nil,
971
+ :path => "/",
972
+ :query => nil,
973
+ :fragment => nil
974
+ }
975
+ end
976
+
977
+ it "should be identical to its duplicate" do
978
+ @uri.should == @uri.dup
979
+ end
980
+ end
981
+
982
+ describe Addressable::URI, "when parsed from " +
983
+ "'http://example.com/~smith/'" do
984
+ before do
985
+ @uri = Addressable::URI.parse("http://example.com/~smith/")
986
+ end
987
+
988
+ # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
989
+ it "should be equivalent to http://example.com/%7Esmith/" do
990
+ @uri.should == Addressable::URI.parse("http://example.com/%7Esmith/")
991
+ end
992
+
993
+ # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
994
+ it "should be equivalent to http://example.com/%7esmith/" do
995
+ @uri.should == Addressable::URI.parse("http://example.com/%7esmith/")
996
+ end
997
+
998
+ it "should be identical to its duplicate" do
999
+ @uri.should == @uri.dup
1000
+ end
1001
+ end
1002
+
1003
+ describe Addressable::URI, "when parsed from " +
1004
+ "'http://example.com/%E8'" do
1005
+ before do
1006
+ @uri = Addressable::URI.parse("http://example.com/%E8")
1007
+ end
1008
+
1009
+ it "should not raise an exception when normalized" do
1010
+ (lambda do
1011
+ @uri.normalize
1012
+ end).should_not raise_error(ArgumentError)
1013
+ end
1014
+
1015
+ it "should be considered to be in normal form" do
1016
+ @uri.normalize.should be_eql(@uri)
1017
+ end
1018
+
1019
+ it "should not change if encoded with the normalizing algorithm" do
1020
+ Addressable::URI.normalized_encode(@uri).to_s.should ==
1021
+ "http://example.com/%E8"
1022
+ Addressable::URI.normalized_encode(@uri, Addressable::URI).to_s.should ===
1023
+ "http://example.com/%E8"
1024
+ end
1025
+ end
1026
+
1027
+ describe Addressable::URI, "when parsed from " +
1028
+ "'http://example.com/%C3%87'" do
1029
+ before do
1030
+ @uri = Addressable::URI.parse("http://example.com/%C3%87")
1031
+ end
1032
+
1033
+ # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
1034
+ it "should be equivalent to 'http://example.com/C%CC%A7'" do
1035
+ @uri.should == Addressable::URI.parse("http://example.com/C%CC%A7")
1036
+ end
1037
+
1038
+ it "should not change if encoded with the normalizing algorithm" do
1039
+ Addressable::URI.normalized_encode(@uri).to_s.should ==
1040
+ "http://example.com/%C3%87"
1041
+ Addressable::URI.normalized_encode(@uri, Addressable::URI).to_s.should ===
1042
+ "http://example.com/%C3%87"
1043
+ end
1044
+
1045
+ it "should raise an error if encoding with an unexpected return type" do
1046
+ (lambda do
1047
+ Addressable::URI.normalized_encode(@uri, Integer)
1048
+ end).should raise_error(TypeError)
1049
+ end
1050
+
1051
+ it "if percent encoded should be 'http://example.com/C%25CC%25A7'" do
1052
+ Addressable::URI.encode(@uri).to_s.should ==
1053
+ "http://example.com/%25C3%2587"
1054
+ end
1055
+
1056
+ it "if percent encoded should be 'http://example.com/C%25CC%25A7'" do
1057
+ Addressable::URI.encode(@uri, Addressable::URI).should ==
1058
+ Addressable::URI.parse("http://example.com/%25C3%2587")
1059
+ end
1060
+
1061
+ it "should raise an error if encoding with an unexpected return type" do
1062
+ (lambda do
1063
+ Addressable::URI.encode(@uri, Integer)
1064
+ end).should raise_error(TypeError)
1065
+ end
1066
+
1067
+ it "should be identical to its duplicate" do
1068
+ @uri.should == @uri.dup
1069
+ end
1070
+ end
1071
+
1072
+ describe Addressable::URI, "when parsed from " +
1073
+ "'http://example.com/?q=string'" do
1074
+ before do
1075
+ @uri = Addressable::URI.parse("http://example.com/?q=string")
1076
+ end
1077
+
1078
+ it "should use the 'http' scheme" do
1079
+ @uri.scheme.should == "http"
1080
+ end
1081
+
1082
+ it "should have an authority segment of 'example.com'" do
1083
+ @uri.authority.should == "example.com"
1084
+ end
1085
+
1086
+ it "should have a host of 'example.com'" do
1087
+ @uri.host.should == "example.com"
1088
+ end
1089
+
1090
+ it "should have no username" do
1091
+ @uri.user.should == nil
1092
+ end
1093
+
1094
+ it "should have no password" do
1095
+ @uri.password.should == nil
1096
+ end
1097
+
1098
+ it "should use port 80" do
1099
+ @uri.inferred_port.should == 80
1100
+ end
1101
+
1102
+ it "should have a path of '/'" do
1103
+ @uri.path.should == "/"
1104
+ end
1105
+
1106
+ it "should have a query string of 'q=string'" do
1107
+ @uri.query.should == "q=string"
1108
+ end
1109
+
1110
+ it "should have no fragment" do
1111
+ @uri.fragment.should == nil
1112
+ end
1113
+
1114
+ it "should be considered absolute" do
1115
+ @uri.should be_absolute
1116
+ end
1117
+
1118
+ it "should not be considered relative" do
1119
+ @uri.should_not be_relative
1120
+ end
1121
+
1122
+ it "should be considered to be in normal form" do
1123
+ @uri.normalize.should be_eql(@uri)
1124
+ end
1125
+
1126
+ it "should be identical to its duplicate" do
1127
+ @uri.should == @uri.dup
1128
+ end
1129
+ end
1130
+
1131
+ describe Addressable::URI, "when parsed from " +
1132
+ "'http://example.com:80/'" do
1133
+ before do
1134
+ @uri = Addressable::URI.parse("http://example.com:80/")
1135
+ end
1136
+
1137
+ it "should use the 'http' scheme" do
1138
+ @uri.scheme.should == "http"
1139
+ end
1140
+
1141
+ it "should have an authority segment of 'example.com:80'" do
1142
+ @uri.authority.should == "example.com:80"
1143
+ end
1144
+
1145
+ it "should have a host of 'example.com'" do
1146
+ @uri.host.should == "example.com"
1147
+ end
1148
+
1149
+ it "should have no username" do
1150
+ @uri.user.should == nil
1151
+ end
1152
+
1153
+ it "should have no password" do
1154
+ @uri.password.should == nil
1155
+ end
1156
+
1157
+ it "should use port 80" do
1158
+ @uri.port.should == 80
1159
+ end
1160
+
1161
+ it "should have a path of '/'" do
1162
+ @uri.path.should == "/"
1163
+ end
1164
+
1165
+ it "should have no query string" do
1166
+ @uri.query.should == nil
1167
+ end
1168
+
1169
+ it "should have no fragment" do
1170
+ @uri.fragment.should == nil
1171
+ end
1172
+
1173
+ it "should be considered absolute" do
1174
+ @uri.should be_absolute
1175
+ end
1176
+
1177
+ it "should not be considered relative" do
1178
+ @uri.should_not be_relative
1179
+ end
1180
+
1181
+ it "should be exactly equal to http://example.com:80/" do
1182
+ @uri.eql?(Addressable::URI.parse("http://example.com:80/")).should == true
1183
+ end
1184
+
1185
+ it "should be roughly equal to http://example.com/" do
1186
+ (@uri === Addressable::URI.parse("http://example.com/")).should == true
1187
+ end
1188
+
1189
+ it "should be roughly equal to the string 'http://example.com/'" do
1190
+ (@uri === "http://example.com/").should == true
1191
+ end
1192
+
1193
+ it "should not be roughly equal to the string " +
1194
+ "'http://example.com:bogus/'" do
1195
+ (lambda do
1196
+ (@uri === "http://example.com:bogus/").should == false
1197
+ end).should_not raise_error
1198
+ end
1199
+
1200
+ it "should result in itself when joined with itself" do
1201
+ @uri.join(@uri).to_s.should == "http://example.com:80/"
1202
+ @uri.join!(@uri).to_s.should == "http://example.com:80/"
1203
+ end
1204
+
1205
+ # Section 6.2.3 of RFC 3986
1206
+ it "should be equal to http://example.com/" do
1207
+ @uri.should == Addressable::URI.parse("http://example.com/")
1208
+ end
1209
+
1210
+ # Section 6.2.3 of RFC 3986
1211
+ it "should be equal to http://example.com:/" do
1212
+ @uri.should == Addressable::URI.parse("http://example.com:/")
1213
+ end
1214
+
1215
+ # Section 6.2.3 of RFC 3986
1216
+ it "should be equal to http://example.com:80/" do
1217
+ @uri.should == Addressable::URI.parse("http://example.com:80/")
1218
+ end
1219
+
1220
+ # Section 6.2.2.1 of RFC 3986
1221
+ it "should be equal to http://EXAMPLE.COM/" do
1222
+ @uri.should == Addressable::URI.parse("http://EXAMPLE.COM/")
1223
+ end
1224
+
1225
+ it "should correctly convert to a hash" do
1226
+ @uri.to_hash.should == {
1227
+ :scheme => "http",
1228
+ :user => nil,
1229
+ :password => nil,
1230
+ :host => "example.com",
1231
+ :port => 80,
1232
+ :path => "/",
1233
+ :query => nil,
1234
+ :fragment => nil
1235
+ }
1236
+ end
1237
+
1238
+ it "should be identical to its duplicate" do
1239
+ @uri.should == @uri.dup
1240
+ end
1241
+ end
1242
+
1243
+ describe Addressable::URI, "when parsed from " +
1244
+ "'http://example.com:8080/'" do
1245
+ before do
1246
+ @uri = Addressable::URI.parse("http://example.com:8080/")
1247
+ end
1248
+
1249
+ it "should use the 'http' scheme" do
1250
+ @uri.scheme.should == "http"
1251
+ end
1252
+
1253
+ it "should have an authority segment of 'example.com:8080'" do
1254
+ @uri.authority.should == "example.com:8080"
1255
+ end
1256
+
1257
+ it "should have a host of 'example.com'" do
1258
+ @uri.host.should == "example.com"
1259
+ end
1260
+
1261
+ it "should have no username" do
1262
+ @uri.user.should == nil
1263
+ end
1264
+
1265
+ it "should have no password" do
1266
+ @uri.password.should == nil
1267
+ end
1268
+
1269
+ it "should use port 8080" do
1270
+ @uri.port.should == 8080
1271
+ end
1272
+
1273
+ it "should have a path of '/'" do
1274
+ @uri.path.should == "/"
1275
+ end
1276
+
1277
+ it "should have no query string" do
1278
+ @uri.query.should == nil
1279
+ end
1280
+
1281
+ it "should have no fragment" do
1282
+ @uri.fragment.should == nil
1283
+ end
1284
+
1285
+ it "should be considered absolute" do
1286
+ @uri.should be_absolute
1287
+ end
1288
+
1289
+ it "should not be considered relative" do
1290
+ @uri.should_not be_relative
1291
+ end
1292
+
1293
+ it "should be exactly equal to http://example.com:8080/" do
1294
+ @uri.eql?(Addressable::URI.parse(
1295
+ "http://example.com:8080/")).should == true
1296
+ end
1297
+
1298
+ it "should have a route of 'http://example.com:8080/' from " +
1299
+ "'http://example.com/path/to/'" do
1300
+ @uri.route_from("http://example.com/path/to/").should ==
1301
+ Addressable::URI.parse("http://example.com:8080/")
1302
+ end
1303
+
1304
+ it "should have a route of 'http://example.com:8080/' from " +
1305
+ "'http://example.com:80/path/to/'" do
1306
+ @uri.route_from("http://example.com:80/path/to/").should ==
1307
+ Addressable::URI.parse("http://example.com:8080/")
1308
+ end
1309
+
1310
+ it "should have a route of '/' from " +
1311
+ "'http://example.com:8080/path/to/'" do
1312
+ @uri.route_from("http://example.com:8080/path/to/").should ==
1313
+ Addressable::URI.parse("/")
1314
+ end
1315
+
1316
+ it "should have a route of 'http://example.com:8080/' from " +
1317
+ "'http://user:pass@example.com/path/to/'" do
1318
+ @uri.route_from("http://user:pass@example.com/path/to/").should ==
1319
+ Addressable::URI.parse("http://example.com:8080/")
1320
+ end
1321
+
1322
+ it "should correctly convert to a hash" do
1323
+ @uri.to_hash.should == {
1324
+ :scheme => "http",
1325
+ :user => nil,
1326
+ :password => nil,
1327
+ :host => "example.com",
1328
+ :port => 8080,
1329
+ :path => "/",
1330
+ :query => nil,
1331
+ :fragment => nil
1332
+ }
1333
+ end
1334
+
1335
+ it "should be identical to its duplicate" do
1336
+ @uri.should == @uri.dup
1337
+ end
1338
+ end
1339
+
1340
+ describe Addressable::URI, "when parsed from " +
1341
+ "'http://example.com:%38%30/'" do
1342
+ before do
1343
+ @uri = Addressable::URI.parse("http://example.com:%38%30/")
1344
+ end
1345
+
1346
+ it "should have the correct port" do
1347
+ @uri.port.should == 80
1348
+ end
1349
+
1350
+ it "should not be considered to be in normal form" do
1351
+ @uri.normalize.should_not be_eql(@uri)
1352
+ end
1353
+
1354
+ it "should normalize to 'http://example.com/'" do
1355
+ @uri.normalize.should === "http://example.com/"
1356
+ end
1357
+ end
1358
+
1359
+ describe Addressable::URI, "when parsed from " +
1360
+ "'http://example.com/path/to/resource/'" do
1361
+ before do
1362
+ @uri = Addressable::URI.parse("http://example.com/path/to/resource/")
1363
+ end
1364
+
1365
+ it "should use the 'http' scheme" do
1366
+ @uri.scheme.should == "http"
1367
+ end
1368
+
1369
+ it "should have an authority segment of 'example.com'" do
1370
+ @uri.authority.should == "example.com"
1371
+ end
1372
+
1373
+ it "should have a host of 'example.com'" do
1374
+ @uri.host.should == "example.com"
1375
+ end
1376
+
1377
+ it "should have no username" do
1378
+ @uri.user.should == nil
1379
+ end
1380
+
1381
+ it "should have no password" do
1382
+ @uri.password.should == nil
1383
+ end
1384
+
1385
+ it "should use port 80" do
1386
+ @uri.inferred_port.should == 80
1387
+ end
1388
+
1389
+ it "should have a path of '/path/to/resource/'" do
1390
+ @uri.path.should == "/path/to/resource/"
1391
+ end
1392
+
1393
+ it "should have no query string" do
1394
+ @uri.query.should == nil
1395
+ end
1396
+
1397
+ it "should have no fragment" do
1398
+ @uri.fragment.should == nil
1399
+ end
1400
+
1401
+ it "should be considered absolute" do
1402
+ @uri.should be_absolute
1403
+ end
1404
+
1405
+ it "should not be considered relative" do
1406
+ @uri.should_not be_relative
1407
+ end
1408
+
1409
+ it "should be exactly equal to http://example.com:8080/" do
1410
+ @uri.eql?(Addressable::URI.parse(
1411
+ "http://example.com/path/to/resource/")).should == true
1412
+ end
1413
+
1414
+ it "should have a route of 'resource/' from " +
1415
+ "'http://example.com/path/to/'" do
1416
+ @uri.route_from("http://example.com/path/to/").should ==
1417
+ Addressable::URI.parse("resource/")
1418
+ end
1419
+
1420
+ it "should have a route of 'resource/' from " +
1421
+ "'http://example.com:80/path/to/'" do
1422
+ @uri.route_from("http://example.com:80/path/to/").should ==
1423
+ Addressable::URI.parse("resource/")
1424
+ end
1425
+
1426
+ it "should have a route of 'http://example.com/path/to/' from " +
1427
+ "'http://example.com:8080/path/to/'" do
1428
+ @uri.route_from("http://example.com:8080/path/to/").should ==
1429
+ Addressable::URI.parse("http://example.com/path/to/resource/")
1430
+ end
1431
+
1432
+ it "should have a route of 'http://example.com/path/to/' from " +
1433
+ "'http://user:pass@example.com/path/to/'" do
1434
+ @uri.route_from("http://user:pass@example.com/path/to/").should ==
1435
+ Addressable::URI.parse("http://example.com/path/to/resource/")
1436
+ end
1437
+
1438
+ it "should have a route of '/path/to/resource/' from " +
1439
+ "'http://example.com/to/resource/'" do
1440
+ @uri.route_from("http://example.com/to/resource/").should ==
1441
+ Addressable::URI.parse("/path/to/resource/")
1442
+ end
1443
+
1444
+ it "should correctly convert to a hash" do
1445
+ @uri.to_hash.should == {
1446
+ :scheme => "http",
1447
+ :user => nil,
1448
+ :password => nil,
1449
+ :host => "example.com",
1450
+ :port => nil,
1451
+ :path => "/path/to/resource/",
1452
+ :query => nil,
1453
+ :fragment => nil
1454
+ }
1455
+ end
1456
+
1457
+ it "should be identical to its duplicate" do
1458
+ @uri.should == @uri.dup
1459
+ end
1460
+ end
1461
+
1462
+ describe Addressable::URI, "when parsed from " +
1463
+ "'relative/path/to/resource'" do
1464
+ before do
1465
+ @uri = Addressable::URI.parse("relative/path/to/resource")
1466
+ end
1467
+
1468
+ it "should not have a scheme" do
1469
+ @uri.scheme.should == nil
1470
+ end
1471
+
1472
+ it "should not be considered ip-based" do
1473
+ @uri.should_not be_ip_based
1474
+ end
1475
+
1476
+ it "should not have an authority segment" do
1477
+ @uri.authority.should == nil
1478
+ end
1479
+
1480
+ it "should not have a host" do
1481
+ @uri.host.should == nil
1482
+ end
1483
+
1484
+ it "should have no username" do
1485
+ @uri.user.should == nil
1486
+ end
1487
+
1488
+ it "should have no password" do
1489
+ @uri.password.should == nil
1490
+ end
1491
+
1492
+ it "should not have a port" do
1493
+ @uri.port.should == nil
1494
+ end
1495
+
1496
+ it "should have a path of 'relative/path/to/resource'" do
1497
+ @uri.path.should == "relative/path/to/resource"
1498
+ end
1499
+
1500
+ it "should have no query string" do
1501
+ @uri.query.should == nil
1502
+ end
1503
+
1504
+ it "should have no fragment" do
1505
+ @uri.fragment.should == nil
1506
+ end
1507
+
1508
+ it "should not be considered absolute" do
1509
+ @uri.should_not be_absolute
1510
+ end
1511
+
1512
+ it "should be considered relative" do
1513
+ @uri.should be_relative
1514
+ end
1515
+
1516
+ it "should raise an error if routing is attempted" do
1517
+ (lambda do
1518
+ @uri.route_to("http://example.com/")
1519
+ end).should raise_error(ArgumentError, /relative\/path\/to\/resource/)
1520
+ (lambda do
1521
+ @uri.route_from("http://example.com/")
1522
+ end).should raise_error(ArgumentError, /relative\/path\/to\/resource/)
1523
+ end
1524
+
1525
+ it "when joined with 'another/relative/path' should be " +
1526
+ "'relative/path/to/another/relative/path'" do
1527
+ @uri.join('another/relative/path').should ==
1528
+ Addressable::URI.parse("relative/path/to/another/relative/path")
1529
+ end
1530
+
1531
+ it "should be identical to its duplicate" do
1532
+ @uri.should == @uri.dup
1533
+ end
1534
+ end
1535
+
1536
+ describe Addressable::URI, "when parsed from " +
1537
+ "'relative_path_with_no_slashes'" do
1538
+ before do
1539
+ @uri = Addressable::URI.parse("relative_path_with_no_slashes")
1540
+ end
1541
+
1542
+ it "should not have a scheme" do
1543
+ @uri.scheme.should == nil
1544
+ end
1545
+
1546
+ it "should not be considered ip-based" do
1547
+ @uri.should_not be_ip_based
1548
+ end
1549
+
1550
+ it "should not have an authority segment" do
1551
+ @uri.authority.should == nil
1552
+ end
1553
+
1554
+ it "should not have a host" do
1555
+ @uri.host.should == nil
1556
+ end
1557
+
1558
+ it "should have no username" do
1559
+ @uri.user.should == nil
1560
+ end
1561
+
1562
+ it "should have no password" do
1563
+ @uri.password.should == nil
1564
+ end
1565
+
1566
+ it "should not have a port" do
1567
+ @uri.port.should == nil
1568
+ end
1569
+
1570
+ it "should have a path of 'relative_path_with_no_slashes'" do
1571
+ @uri.path.should == "relative_path_with_no_slashes"
1572
+ end
1573
+
1574
+ it "should have no query string" do
1575
+ @uri.query.should == nil
1576
+ end
1577
+
1578
+ it "should have no fragment" do
1579
+ @uri.fragment.should == nil
1580
+ end
1581
+
1582
+ it "should not be considered absolute" do
1583
+ @uri.should_not be_absolute
1584
+ end
1585
+
1586
+ it "should be considered relative" do
1587
+ @uri.should be_relative
1588
+ end
1589
+
1590
+ it "when joined with 'another_relative_path' should be " +
1591
+ "'another_relative_path'" do
1592
+ @uri.join('another_relative_path').should ==
1593
+ Addressable::URI.parse("another_relative_path")
1594
+ end
1595
+ end
1596
+
1597
+ describe Addressable::URI, "when parsed from " +
1598
+ "'http://example.com/file.txt'" do
1599
+ before do
1600
+ @uri = Addressable::URI.parse("http://example.com/file.txt")
1601
+ end
1602
+
1603
+ it "should have a scheme of 'http'" do
1604
+ @uri.scheme.should == "http"
1605
+ end
1606
+
1607
+ it "should have an authority segment of 'example.com'" do
1608
+ @uri.authority.should == "example.com"
1609
+ end
1610
+
1611
+ it "should have a host of 'example.com'" do
1612
+ @uri.host.should == "example.com"
1613
+ end
1614
+
1615
+ it "should have no username" do
1616
+ @uri.user.should == nil
1617
+ end
1618
+
1619
+ it "should have no password" do
1620
+ @uri.password.should == nil
1621
+ end
1622
+
1623
+ it "should use port 80" do
1624
+ @uri.inferred_port.should == 80
1625
+ end
1626
+
1627
+ it "should have a path of '/file.txt'" do
1628
+ @uri.path.should == "/file.txt"
1629
+ end
1630
+
1631
+ it "should have a basename of 'file.txt'" do
1632
+ @uri.basename.should == "file.txt"
1633
+ end
1634
+
1635
+ it "should have an extname of '.txt'" do
1636
+ @uri.extname.should == ".txt"
1637
+ end
1638
+
1639
+ it "should have no query string" do
1640
+ @uri.query.should == nil
1641
+ end
1642
+
1643
+ it "should have no fragment" do
1644
+ @uri.fragment.should == nil
1645
+ end
1646
+ end
1647
+
1648
+ describe Addressable::URI, "when parsed from " +
1649
+ "'http://example.com/file.txt;parameter'" do
1650
+ before do
1651
+ @uri = Addressable::URI.parse("http://example.com/file.txt;parameter")
1652
+ end
1653
+
1654
+ it "should have a scheme of 'http'" do
1655
+ @uri.scheme.should == "http"
1656
+ end
1657
+
1658
+ it "should have an authority segment of 'example.com'" do
1659
+ @uri.authority.should == "example.com"
1660
+ end
1661
+
1662
+ it "should have a host of 'example.com'" do
1663
+ @uri.host.should == "example.com"
1664
+ end
1665
+
1666
+ it "should have no username" do
1667
+ @uri.user.should == nil
1668
+ end
1669
+
1670
+ it "should have no password" do
1671
+ @uri.password.should == nil
1672
+ end
1673
+
1674
+ it "should use port 80" do
1675
+ @uri.inferred_port.should == 80
1676
+ end
1677
+
1678
+ it "should have a path of '/file.txt;parameter'" do
1679
+ @uri.path.should == "/file.txt;parameter"
1680
+ end
1681
+
1682
+ it "should have a basename of 'file.txt'" do
1683
+ @uri.basename.should == "file.txt"
1684
+ end
1685
+
1686
+ it "should have an extname of '.txt'" do
1687
+ @uri.extname.should == ".txt"
1688
+ end
1689
+
1690
+ it "should have no query string" do
1691
+ @uri.query.should == nil
1692
+ end
1693
+
1694
+ it "should have no fragment" do
1695
+ @uri.fragment.should == nil
1696
+ end
1697
+ end
1698
+
1699
+ describe Addressable::URI, "when parsed from " +
1700
+ "'http://example.com/file.txt;x=y'" do
1701
+ before do
1702
+ @uri = Addressable::URI.parse("http://example.com/file.txt;x=y")
1703
+ end
1704
+
1705
+ it "should have a scheme of 'http'" do
1706
+ @uri.scheme.should == "http"
1707
+ end
1708
+
1709
+ it "should have a scheme of 'http'" do
1710
+ @uri.scheme.should == "http"
1711
+ end
1712
+
1713
+ it "should have an authority segment of 'example.com'" do
1714
+ @uri.authority.should == "example.com"
1715
+ end
1716
+
1717
+ it "should have a host of 'example.com'" do
1718
+ @uri.host.should == "example.com"
1719
+ end
1720
+
1721
+ it "should have no username" do
1722
+ @uri.user.should == nil
1723
+ end
1724
+
1725
+ it "should have no password" do
1726
+ @uri.password.should == nil
1727
+ end
1728
+
1729
+ it "should use port 80" do
1730
+ @uri.inferred_port.should == 80
1731
+ end
1732
+
1733
+ it "should have a path of '/file.txt;x=y'" do
1734
+ @uri.path.should == "/file.txt;x=y"
1735
+ end
1736
+
1737
+ it "should have an extname of '.txt'" do
1738
+ @uri.extname.should == ".txt"
1739
+ end
1740
+
1741
+ it "should have no query string" do
1742
+ @uri.query.should == nil
1743
+ end
1744
+
1745
+ it "should have no fragment" do
1746
+ @uri.fragment.should == nil
1747
+ end
1748
+
1749
+ it "should be considered to be in normal form" do
1750
+ @uri.normalize.should be_eql(@uri)
1751
+ end
1752
+ end
1753
+
1754
+ describe Addressable::URI, "when parsed from " +
1755
+ "'svn+ssh://developername@rubyforge.org/var/svn/project'" do
1756
+ before do
1757
+ @uri = Addressable::URI.parse(
1758
+ "svn+ssh://developername@rubyforge.org/var/svn/project"
1759
+ )
1760
+ end
1761
+
1762
+ it "should have a scheme of 'svn+ssh'" do
1763
+ @uri.scheme.should == "svn+ssh"
1764
+ end
1765
+
1766
+ it "should be considered to be ip-based" do
1767
+ @uri.should be_ip_based
1768
+ end
1769
+
1770
+ it "should have a path of '/var/svn/project'" do
1771
+ @uri.path.should == "/var/svn/project"
1772
+ end
1773
+
1774
+ it "should have a username of 'developername'" do
1775
+ @uri.user.should == "developername"
1776
+ end
1777
+
1778
+ it "should have no password" do
1779
+ @uri.password.should == nil
1780
+ end
1781
+
1782
+ it "should be considered to be in normal form" do
1783
+ @uri.normalize.should be_eql(@uri)
1784
+ end
1785
+ end
1786
+
1787
+ describe Addressable::URI, "when parsed from " +
1788
+ "'ssh+svn://developername@rubyforge.org/var/svn/project'" do
1789
+ before do
1790
+ @uri = Addressable::URI.parse(
1791
+ "ssh+svn://developername@rubyforge.org/var/svn/project"
1792
+ )
1793
+ end
1794
+
1795
+ it "should have a scheme of 'ssh+svn'" do
1796
+ @uri.scheme.should == "ssh+svn"
1797
+ end
1798
+
1799
+ it "should have a normalized scheme of 'svn+ssh'" do
1800
+ @uri.normalized_scheme.should == "svn+ssh"
1801
+ end
1802
+
1803
+ it "should not be considered to be ip-based" do
1804
+ @uri.should_not be_ip_based
1805
+ end
1806
+
1807
+ it "should have a path of '/var/svn/project'" do
1808
+ @uri.path.should == "/var/svn/project"
1809
+ end
1810
+
1811
+ it "should have a username of 'developername'" do
1812
+ @uri.user.should == "developername"
1813
+ end
1814
+
1815
+ it "should have no password" do
1816
+ @uri.password.should == nil
1817
+ end
1818
+
1819
+ it "should not be considered to be in normal form" do
1820
+ @uri.normalize.should_not be_eql(@uri)
1821
+ end
1822
+ end
1823
+
1824
+ describe Addressable::URI, "when parsed from " +
1825
+ "'mailto:user@example.com'" do
1826
+ before do
1827
+ @uri = Addressable::URI.parse("mailto:user@example.com")
1828
+ end
1829
+
1830
+ it "should have a scheme of 'mailto'" do
1831
+ @uri.scheme.should == "mailto"
1832
+ end
1833
+
1834
+ it "should not be considered to be ip-based" do
1835
+ @uri.should_not be_ip_based
1836
+ end
1837
+
1838
+ it "should have a path of 'user@example.com'" do
1839
+ @uri.path.should == "user@example.com"
1840
+ end
1841
+
1842
+ it "should have no user" do
1843
+ @uri.user.should == nil
1844
+ end
1845
+
1846
+ it "should be considered to be in normal form" do
1847
+ @uri.normalize.should be_eql(@uri)
1848
+ end
1849
+ end
1850
+
1851
+ describe Addressable::URI, "when parsed from " +
1852
+ "'tag:example.com,2006-08-18:/path/to/something'" do
1853
+ before do
1854
+ @uri = Addressable::URI.parse(
1855
+ "tag:example.com,2006-08-18:/path/to/something")
1856
+ end
1857
+
1858
+ it "should have a scheme of 'tag'" do
1859
+ @uri.scheme.should == "tag"
1860
+ end
1861
+
1862
+ it "should be considered to be ip-based" do
1863
+ @uri.should_not be_ip_based
1864
+ end
1865
+
1866
+ it "should have a path of " +
1867
+ "'example.com,2006-08-18:/path/to/something'" do
1868
+ @uri.path.should == "example.com,2006-08-18:/path/to/something"
1869
+ end
1870
+
1871
+ it "should have no user" do
1872
+ @uri.user.should == nil
1873
+ end
1874
+
1875
+ it "should be considered to be in normal form" do
1876
+ @uri.normalize.should be_eql(@uri)
1877
+ end
1878
+ end
1879
+
1880
+ describe Addressable::URI, "when parsed from " +
1881
+ "'http://example.com/x;y/'" do
1882
+ before do
1883
+ @uri = Addressable::URI.parse("http://example.com/x;y/")
1884
+ end
1885
+
1886
+ it "should be considered to be in normal form" do
1887
+ @uri.normalize.should be_eql(@uri)
1888
+ end
1889
+ end
1890
+
1891
+ describe Addressable::URI, "when parsed from " +
1892
+ "'http://example.com/?x=1&y=2'" do
1893
+ before do
1894
+ @uri = Addressable::URI.parse("http://example.com/?x=1&y=2")
1895
+ end
1896
+
1897
+ it "should be considered to be in normal form" do
1898
+ @uri.normalize.should be_eql(@uri)
1899
+ end
1900
+ end
1901
+
1902
+ describe Addressable::URI, "when parsed from " +
1903
+ "'view-source:http://example.com/'" do
1904
+ before do
1905
+ @uri = Addressable::URI.parse("view-source:http://example.com/")
1906
+ end
1907
+
1908
+ it "should have a scheme of 'view-source'" do
1909
+ @uri.scheme.should == "view-source"
1910
+ end
1911
+
1912
+ it "should have a path of 'http://example.com/'" do
1913
+ @uri.path.should == "http://example.com/"
1914
+ end
1915
+
1916
+ it "should be considered to be in normal form" do
1917
+ @uri.normalize.should be_eql(@uri)
1918
+ end
1919
+ end
1920
+
1921
+ describe Addressable::URI, "when parsed from " +
1922
+ "'http://user:pass@example.com/path/to/resource?query=x#fragment'" do
1923
+ before do
1924
+ @uri = Addressable::URI.parse(
1925
+ "http://user:pass@example.com/path/to/resource?query=x#fragment")
1926
+ end
1927
+
1928
+ it "should use the 'http' scheme" do
1929
+ @uri.scheme.should == "http"
1930
+ end
1931
+
1932
+ it "should have an authority segment of 'user:pass@example.com'" do
1933
+ @uri.authority.should == "user:pass@example.com"
1934
+ end
1935
+
1936
+ it "should have a username of 'user'" do
1937
+ @uri.user.should == "user"
1938
+ end
1939
+
1940
+ it "should have a password of 'pass'" do
1941
+ @uri.password.should == "pass"
1942
+ end
1943
+
1944
+ it "should have a host of 'example.com'" do
1945
+ @uri.host.should == "example.com"
1946
+ end
1947
+
1948
+ it "should use port 80" do
1949
+ @uri.inferred_port.should == 80
1950
+ end
1951
+
1952
+ it "should have a path of '/path/to/resource'" do
1953
+ @uri.path.should == "/path/to/resource"
1954
+ end
1955
+
1956
+ it "should have a query string of 'query=x'" do
1957
+ @uri.query.should == "query=x"
1958
+ end
1959
+
1960
+ it "should have a fragment of 'fragment'" do
1961
+ @uri.fragment.should == "fragment"
1962
+ end
1963
+
1964
+ it "should be considered to be in normal form" do
1965
+ @uri.normalize.should be_eql(@uri)
1966
+ end
1967
+
1968
+ it "should have a route of '/path/' to " +
1969
+ "'http://user:pass@example.com/path/'" do
1970
+ @uri.route_to("http://user:pass@example.com/path/").should ==
1971
+ Addressable::URI.parse("/path/")
1972
+ end
1973
+
1974
+ it "should have a route of '/path/to/resource?query=x#fragment' " +
1975
+ "from 'http://user:pass@example.com/path/'" do
1976
+ @uri.route_from("http://user:pass@example.com/path/").should ==
1977
+ Addressable::URI.parse("to/resource?query=x#fragment")
1978
+ end
1979
+
1980
+ it "should have a route of '?query=x#fragment' " +
1981
+ "from 'http://user:pass@example.com/path/to/resource'" do
1982
+ @uri.route_from("http://user:pass@example.com/path/to/resource").should ==
1983
+ Addressable::URI.parse("?query=x#fragment")
1984
+ end
1985
+
1986
+ it "should have a route of '#fragment' " +
1987
+ "from 'http://user:pass@example.com/path/to/resource?query=x'" do
1988
+ @uri.route_from(
1989
+ "http://user:pass@example.com/path/to/resource?query=x").should ==
1990
+ Addressable::URI.parse("#fragment")
1991
+ end
1992
+
1993
+ it "should have a route of '#fragment' from " +
1994
+ "'http://user:pass@example.com/path/to/resource?query=x#fragment'" do
1995
+ @uri.route_from(
1996
+ "http://user:pass@example.com/path/to/resource?query=x#fragment"
1997
+ ).should == Addressable::URI.parse("#fragment")
1998
+ end
1999
+
2000
+ it "should have a route of 'http://elsewhere.com/' to " +
2001
+ "'http://elsewhere.com/'" do
2002
+ @uri.route_to("http://elsewhere.com/").should ==
2003
+ Addressable::URI.parse("http://elsewhere.com/")
2004
+ end
2005
+
2006
+ it "should have a route of " +
2007
+ "'http://user:pass@example.com/path/to/resource?query=x#fragment' " +
2008
+ "from 'http://example.com/path/to/'" do
2009
+ @uri.route_from("http://elsewhere.com/path/to/").should ==
2010
+ Addressable::URI.parse(
2011
+ "http://user:pass@example.com/path/to/resource?query=x#fragment")
2012
+ end
2013
+
2014
+ it "should have the correct scheme after assignment" do
2015
+ @uri.scheme = "ftp"
2016
+ @uri.scheme.should == "ftp"
2017
+ @uri.to_s.should ==
2018
+ "ftp://user:pass@example.com/path/to/resource?query=x#fragment"
2019
+ @uri.to_str.should ==
2020
+ "ftp://user:pass@example.com/path/to/resource?query=x#fragment"
2021
+ @uri.scheme = "bogus!"
2022
+ @uri.scheme.should == "bogus!"
2023
+ @uri.normalized_scheme.should == "bogus%21"
2024
+ @uri.normalize.to_s.should ==
2025
+ "bogus%21://user:pass@example.com/path/to/resource?query=x#fragment"
2026
+ @uri.normalize.to_str.should ==
2027
+ "bogus%21://user:pass@example.com/path/to/resource?query=x#fragment"
2028
+ end
2029
+
2030
+ it "should have the correct authority segment after assignment" do
2031
+ @uri.authority = "newuser:newpass@example.com:80"
2032
+ @uri.authority.should == "newuser:newpass@example.com:80"
2033
+ @uri.user.should == "newuser"
2034
+ @uri.password.should == "newpass"
2035
+ @uri.userinfo.should == "newuser:newpass"
2036
+ @uri.normalized_userinfo.should == "newuser:newpass"
2037
+ @uri.host.should == "example.com"
2038
+ @uri.port.should == 80
2039
+ @uri.inferred_port.should == 80
2040
+ @uri.to_s.should ==
2041
+ "http://newuser:newpass@example.com:80" +
2042
+ "/path/to/resource?query=x#fragment"
2043
+ end
2044
+
2045
+ it "should have the correct userinfo segment after assignment" do
2046
+ @uri.userinfo = "newuser:newpass"
2047
+ @uri.userinfo.should == "newuser:newpass"
2048
+ @uri.authority.should == "newuser:newpass@example.com"
2049
+ @uri.user.should == "newuser"
2050
+ @uri.password.should == "newpass"
2051
+ @uri.host.should == "example.com"
2052
+ @uri.port.should == nil
2053
+ @uri.inferred_port.should == 80
2054
+ @uri.to_s.should ==
2055
+ "http://newuser:newpass@example.com" +
2056
+ "/path/to/resource?query=x#fragment"
2057
+ end
2058
+
2059
+ it "should have the correct username after assignment" do
2060
+ @uri.user = "newuser"
2061
+ @uri.user.should == "newuser"
2062
+ @uri.authority.should == "newuser:pass@example.com"
2063
+ end
2064
+
2065
+ it "should have the correct password after assignment" do
2066
+ @uri.password = "newpass"
2067
+ @uri.password.should == "newpass"
2068
+ @uri.authority.should == "user:newpass@example.com"
2069
+ end
2070
+
2071
+ it "should have the correct host after assignment" do
2072
+ @uri.host = "newexample.com"
2073
+ @uri.host.should == "newexample.com"
2074
+ @uri.authority.should == "user:pass@newexample.com"
2075
+ end
2076
+
2077
+ it "should have the correct port after assignment" do
2078
+ @uri.port = 8080
2079
+ @uri.port.should == 8080
2080
+ @uri.authority.should == "user:pass@example.com:8080"
2081
+ end
2082
+
2083
+ it "should have the correct path after assignment" do
2084
+ @uri.path = "/newpath/to/resource"
2085
+ @uri.path.should == "/newpath/to/resource"
2086
+ @uri.to_s.should ==
2087
+ "http://user:pass@example.com/newpath/to/resource?query=x#fragment"
2088
+ end
2089
+
2090
+ it "should have the correct path after nil assignment" do
2091
+ @uri.path = nil
2092
+ @uri.path.should == ""
2093
+ @uri.to_s.should ==
2094
+ "http://user:pass@example.com?query=x#fragment"
2095
+ end
2096
+
2097
+ it "should have the correct query string after assignment" do
2098
+ @uri.query = "newquery=x"
2099
+ @uri.query.should == "newquery=x"
2100
+ @uri.to_s.should ==
2101
+ "http://user:pass@example.com/path/to/resource?newquery=x#fragment"
2102
+ @uri.query = nil
2103
+ @uri.query.should == nil
2104
+ @uri.to_s.should ==
2105
+ "http://user:pass@example.com/path/to/resource#fragment"
2106
+ end
2107
+
2108
+ it "should have the correct query string after hash assignment" do
2109
+ @uri.query_values = {"?uestion mark"=>"=sign", "hello"=>"g\xC3\xBCnther"}
2110
+ @uri.query.split("&").should include("%3Fuestion%20mark=%3Dsign")
2111
+ @uri.query.split("&").should include("hello=g%C3%BCnther")
2112
+ @uri.query_values.should == {
2113
+ "?uestion mark"=>"=sign", "hello"=>"g\xC3\xBCnther"
2114
+ }
2115
+ end
2116
+
2117
+ it "should have the correct query string after flag hash assignment" do
2118
+ @uri.query_values = {'flag?1' => true, 'fl=ag2' => true, 'flag3' => true}
2119
+ @uri.query.split("&").should include("flag%3F1")
2120
+ @uri.query.split("&").should include("fl%3Dag2")
2121
+ @uri.query.split("&").should include("flag3")
2122
+ @uri.query_values.should == {
2123
+ 'flag?1' => true, 'fl=ag2' => true, 'flag3' => true
2124
+ }
2125
+ end
2126
+
2127
+ it "should raise an error if query values are set to a bogus type" do
2128
+ (lambda do
2129
+ @uri.query_values = "bogus"
2130
+ end).should raise_error(TypeError)
2131
+ end
2132
+
2133
+ it "should have the correct fragment after assignment" do
2134
+ @uri.fragment = "newfragment"
2135
+ @uri.fragment.should == "newfragment"
2136
+ @uri.to_s.should ==
2137
+ "http://user:pass@example.com/path/to/resource?query=x#newfragment"
2138
+
2139
+ @uri.fragment = nil
2140
+ @uri.fragment.should == nil
2141
+ @uri.to_s.should ==
2142
+ "http://user:pass@example.com/path/to/resource?query=x"
2143
+ end
2144
+
2145
+ it "should have the correct values after a merge" do
2146
+ @uri.merge(:fragment => "newfragment").to_s.should ==
2147
+ "http://user:pass@example.com/path/to/resource?query=x#newfragment"
2148
+ end
2149
+
2150
+ it "should have the correct values after a merge" do
2151
+ @uri.merge(:fragment => nil).to_s.should ==
2152
+ "http://user:pass@example.com/path/to/resource?query=x"
2153
+ end
2154
+
2155
+ it "should have the correct values after a merge" do
2156
+ @uri.merge(:userinfo => "newuser:newpass").to_s.should ==
2157
+ "http://newuser:newpass@example.com/path/to/resource?query=x#fragment"
2158
+ end
2159
+
2160
+ it "should have the correct values after a merge" do
2161
+ @uri.merge(:userinfo => nil).to_s.should ==
2162
+ "http://example.com/path/to/resource?query=x#fragment"
2163
+ end
2164
+
2165
+ it "should have the correct values after a merge" do
2166
+ @uri.merge(:path => "newpath").to_s.should ==
2167
+ "http://user:pass@example.com/newpath?query=x#fragment"
2168
+ end
2169
+
2170
+ it "should have the correct values after a merge" do
2171
+ @uri.merge(:port => "42", :path => "newpath", :query => "").to_s.should ==
2172
+ "http://user:pass@example.com:42/newpath?#fragment"
2173
+ end
2174
+
2175
+ it "should have the correct values after a merge" do
2176
+ @uri.merge(:authority => "foo:bar@baz:42").to_s.should ==
2177
+ "http://foo:bar@baz:42/path/to/resource?query=x#fragment"
2178
+ @uri.to_s.should ==
2179
+ "http://user:pass@example.com/path/to/resource?query=x#fragment"
2180
+ end
2181
+
2182
+ it "should have the correct values after a destructive merge" do
2183
+ @uri.merge!(:authority => "foo:bar@baz:42")
2184
+ @uri.to_s.should ==
2185
+ "http://foo:bar@baz:42/path/to/resource?query=x#fragment"
2186
+ end
2187
+
2188
+ it "should fail to merge with bogus values" do
2189
+ (lambda do
2190
+ @uri.merge(:port => "bogus")
2191
+ end).should raise_error(Addressable::URI::InvalidURIError)
2192
+ end
2193
+
2194
+ it "should fail to merge with bogus parameters" do
2195
+ (lambda do
2196
+ @uri.merge(42)
2197
+ end).should raise_error(TypeError)
2198
+ end
2199
+
2200
+ it "should fail to merge with bogus parameters" do
2201
+ (lambda do
2202
+ @uri.merge("http://example.com/")
2203
+ end).should raise_error(TypeError)
2204
+ end
2205
+
2206
+ it "should fail to merge with both authority and subcomponents" do
2207
+ (lambda do
2208
+ @uri.merge(:authority => "foo:bar@baz:42", :port => "42")
2209
+ end).should raise_error(ArgumentError)
2210
+ end
2211
+
2212
+ it "should fail to merge with both userinfo and subcomponents" do
2213
+ (lambda do
2214
+ @uri.merge(:userinfo => "foo:bar", :user => "foo")
2215
+ end).should raise_error(ArgumentError)
2216
+ end
2217
+
2218
+ it "should be identical to its duplicate" do
2219
+ @uri.should == @uri.dup
2220
+ end
2221
+ end
2222
+
2223
+ describe Addressable::URI, "when parsed from " +
2224
+ "'http://user@example.com'" do
2225
+ before do
2226
+ @uri = Addressable::URI.parse("http://user@example.com")
2227
+ end
2228
+
2229
+ it "should use the 'http' scheme" do
2230
+ @uri.scheme.should == "http"
2231
+ end
2232
+
2233
+ it "should have a username of 'user'" do
2234
+ @uri.user.should == "user"
2235
+ end
2236
+
2237
+ it "should have no password" do
2238
+ @uri.password.should == nil
2239
+ end
2240
+
2241
+ it "should have a userinfo of 'user'" do
2242
+ @uri.userinfo.should == "user"
2243
+ end
2244
+
2245
+ it "should have a normalized userinfo of 'user'" do
2246
+ @uri.normalized_userinfo.should == "user"
2247
+ end
2248
+
2249
+ it "should have a host of 'example.com'" do
2250
+ @uri.host.should == "example.com"
2251
+ end
2252
+
2253
+ it "should use port 80" do
2254
+ @uri.inferred_port.should == 80
2255
+ end
2256
+
2257
+ it "should have the correct username after assignment" do
2258
+ @uri.user = "newuser"
2259
+ @uri.user.should == "newuser"
2260
+ @uri.password.should == nil
2261
+ @uri.to_s.should == "http://newuser@example.com"
2262
+ end
2263
+
2264
+ it "should have the correct password after assignment" do
2265
+ @uri.password = "newpass"
2266
+ @uri.password.should == "newpass"
2267
+ @uri.to_s.should == "http://user:newpass@example.com"
2268
+ end
2269
+
2270
+ it "should have the correct userinfo segment after assignment" do
2271
+ @uri.userinfo = "newuser:newpass"
2272
+ @uri.userinfo.should == "newuser:newpass"
2273
+ @uri.user.should == "newuser"
2274
+ @uri.password.should == "newpass"
2275
+ @uri.host.should == "example.com"
2276
+ @uri.port.should == nil
2277
+ @uri.inferred_port.should == 80
2278
+ @uri.to_s.should == "http://newuser:newpass@example.com"
2279
+ end
2280
+
2281
+ it "should have the correct userinfo segment after nil assignment" do
2282
+ @uri.userinfo = nil
2283
+ @uri.userinfo.should == nil
2284
+ @uri.user.should == nil
2285
+ @uri.password.should == nil
2286
+ @uri.host.should == "example.com"
2287
+ @uri.port.should == nil
2288
+ @uri.inferred_port.should == 80
2289
+ @uri.to_s.should == "http://example.com"
2290
+ end
2291
+
2292
+ it "should have the correct authority segment after assignment" do
2293
+ @uri.authority = "newuser@example.com"
2294
+ @uri.authority.should == "newuser@example.com"
2295
+ @uri.user.should == "newuser"
2296
+ @uri.password.should == nil
2297
+ @uri.host.should == "example.com"
2298
+ @uri.port.should == nil
2299
+ @uri.inferred_port.should == 80
2300
+ @uri.to_s.should == "http://newuser@example.com"
2301
+ end
2302
+
2303
+ it "should raise an error after nil assignment of authority segment" do
2304
+ (lambda do
2305
+ # This would create an invalid URI
2306
+ @uri.authority = nil
2307
+ end).should raise_error
2308
+ end
2309
+ end
2310
+
2311
+ describe Addressable::URI, "when parsed from " +
2312
+ "'http://user:@example.com'" do
2313
+ before do
2314
+ @uri = Addressable::URI.parse("http://user:@example.com")
2315
+ end
2316
+
2317
+ it "should use the 'http' scheme" do
2318
+ @uri.scheme.should == "http"
2319
+ end
2320
+
2321
+ it "should have a username of 'user'" do
2322
+ @uri.user.should == "user"
2323
+ end
2324
+
2325
+ it "should have a password of ''" do
2326
+ @uri.password.should == ""
2327
+ end
2328
+
2329
+ it "should have a normalized userinfo of 'user:'" do
2330
+ @uri.normalized_userinfo.should == "user:"
2331
+ end
2332
+
2333
+ it "should have a host of 'example.com'" do
2334
+ @uri.host.should == "example.com"
2335
+ end
2336
+
2337
+ it "should use port 80" do
2338
+ @uri.inferred_port.should == 80
2339
+ end
2340
+
2341
+ it "should have the correct username after assignment" do
2342
+ @uri.user = "newuser"
2343
+ @uri.user.should == "newuser"
2344
+ @uri.password.should == ""
2345
+ @uri.to_s.should == "http://newuser:@example.com"
2346
+ end
2347
+
2348
+ it "should have the correct password after assignment" do
2349
+ @uri.password = "newpass"
2350
+ @uri.password.should == "newpass"
2351
+ @uri.to_s.should == "http://user:newpass@example.com"
2352
+ end
2353
+
2354
+ it "should have the correct authority segment after assignment" do
2355
+ @uri.authority = "newuser:@example.com"
2356
+ @uri.authority.should == "newuser:@example.com"
2357
+ @uri.user.should == "newuser"
2358
+ @uri.password.should == ""
2359
+ @uri.host.should == "example.com"
2360
+ @uri.port.should == nil
2361
+ @uri.inferred_port.should == 80
2362
+ @uri.to_s.should == "http://newuser:@example.com"
2363
+ end
2364
+ end
2365
+
2366
+ describe Addressable::URI, "when parsed from " +
2367
+ "'http://:pass@example.com'" do
2368
+ before do
2369
+ @uri = Addressable::URI.parse("http://:pass@example.com")
2370
+ end
2371
+
2372
+ it "should use the 'http' scheme" do
2373
+ @uri.scheme.should == "http"
2374
+ end
2375
+
2376
+ it "should have a username of ''" do
2377
+ @uri.user.should == ""
2378
+ end
2379
+
2380
+ it "should have a password of 'pass'" do
2381
+ @uri.password.should == "pass"
2382
+ end
2383
+
2384
+ it "should have a userinfo of ':pass'" do
2385
+ @uri.userinfo.should == ":pass"
2386
+ end
2387
+
2388
+ it "should have a normalized userinfo of ':pass'" do
2389
+ @uri.normalized_userinfo.should == ":pass"
2390
+ end
2391
+
2392
+ it "should have a host of 'example.com'" do
2393
+ @uri.host.should == "example.com"
2394
+ end
2395
+
2396
+ it "should use port 80" do
2397
+ @uri.inferred_port.should == 80
2398
+ end
2399
+
2400
+ it "should have the correct username after assignment" do
2401
+ @uri.user = "newuser"
2402
+ @uri.user.should == "newuser"
2403
+ @uri.password.should == "pass"
2404
+ @uri.to_s.should == "http://newuser:pass@example.com"
2405
+ end
2406
+
2407
+ it "should have the correct password after assignment" do
2408
+ @uri.password = "newpass"
2409
+ @uri.password.should == "newpass"
2410
+ @uri.user.should == ""
2411
+ @uri.to_s.should == "http://:newpass@example.com"
2412
+ end
2413
+
2414
+ it "should have the correct authority segment after assignment" do
2415
+ @uri.authority = ":newpass@example.com"
2416
+ @uri.authority.should == ":newpass@example.com"
2417
+ @uri.user.should == ""
2418
+ @uri.password.should == "newpass"
2419
+ @uri.host.should == "example.com"
2420
+ @uri.port.should == nil
2421
+ @uri.inferred_port.should == 80
2422
+ @uri.to_s.should == "http://:newpass@example.com"
2423
+ end
2424
+ end
2425
+
2426
+ describe Addressable::URI, "when parsed from " +
2427
+ "'http://:@example.com'" do
2428
+ before do
2429
+ @uri = Addressable::URI.parse("http://:@example.com")
2430
+ end
2431
+
2432
+ it "should use the 'http' scheme" do
2433
+ @uri.scheme.should == "http"
2434
+ end
2435
+
2436
+ it "should have a username of ''" do
2437
+ @uri.user.should == ""
2438
+ end
2439
+
2440
+ it "should have a password of ''" do
2441
+ @uri.password.should == ""
2442
+ end
2443
+
2444
+ it "should have a normalized userinfo of nil" do
2445
+ @uri.normalized_userinfo.should == nil
2446
+ end
2447
+
2448
+ it "should have a host of 'example.com'" do
2449
+ @uri.host.should == "example.com"
2450
+ end
2451
+
2452
+ it "should use port 80" do
2453
+ @uri.inferred_port.should == 80
2454
+ end
2455
+
2456
+ it "should have the correct username after assignment" do
2457
+ @uri.user = "newuser"
2458
+ @uri.user.should == "newuser"
2459
+ @uri.password.should == ""
2460
+ @uri.to_s.should == "http://newuser:@example.com"
2461
+ end
2462
+
2463
+ it "should have the correct password after assignment" do
2464
+ @uri.password = "newpass"
2465
+ @uri.password.should == "newpass"
2466
+ @uri.user.should == ""
2467
+ @uri.to_s.should == "http://:newpass@example.com"
2468
+ end
2469
+
2470
+ it "should have the correct authority segment after assignment" do
2471
+ @uri.authority = ":@newexample.com"
2472
+ @uri.authority.should == ":@newexample.com"
2473
+ @uri.user.should == ""
2474
+ @uri.password.should == ""
2475
+ @uri.host.should == "newexample.com"
2476
+ @uri.port.should == nil
2477
+ @uri.inferred_port.should == 80
2478
+ @uri.to_s.should == "http://:@newexample.com"
2479
+ end
2480
+ end
2481
+
2482
+ describe Addressable::URI, "when parsed from " +
2483
+ "'#example'" do
2484
+ before do
2485
+ @uri = Addressable::URI.parse("#example")
2486
+ end
2487
+
2488
+ it "should be considered relative" do
2489
+ @uri.should be_relative
2490
+ end
2491
+
2492
+ it "should have a host of nil" do
2493
+ @uri.host.should == nil
2494
+ end
2495
+
2496
+ it "should have a path of ''" do
2497
+ @uri.path.should == ""
2498
+ end
2499
+
2500
+ it "should have a query string of nil" do
2501
+ @uri.query.should == nil
2502
+ end
2503
+
2504
+ it "should have a fragment of 'example'" do
2505
+ @uri.fragment.should == "example"
2506
+ end
2507
+ end
2508
+
2509
+ describe Addressable::URI, "when parsed from " +
2510
+ "the network-path reference '//example.com/'" do
2511
+ before do
2512
+ @uri = Addressable::URI.parse("//example.com/")
2513
+ end
2514
+
2515
+ it "should be considered relative" do
2516
+ @uri.should be_relative
2517
+ end
2518
+
2519
+ it "should have a host of 'example.com'" do
2520
+ @uri.host.should == "example.com"
2521
+ end
2522
+
2523
+ it "should have a path of '/'" do
2524
+ @uri.path.should == "/"
2525
+ end
2526
+
2527
+ it "should raise an error if routing is attempted" do
2528
+ (lambda do
2529
+ @uri.route_to("http://example.com/")
2530
+ end).should raise_error(ArgumentError, /\/\/example.com\//)
2531
+ (lambda do
2532
+ @uri.route_from("http://example.com/")
2533
+ end).should raise_error(ArgumentError, /\/\/example.com\//)
2534
+ end
2535
+ end
2536
+
2537
+ describe Addressable::URI, "when parsed from " +
2538
+ "'feed://http://example.com/'" do
2539
+ before do
2540
+ @uri = Addressable::URI.parse("feed://http://example.com/")
2541
+ end
2542
+
2543
+ it "should have a host of 'http'" do
2544
+ @uri.host.should == "http"
2545
+ end
2546
+
2547
+ it "should have a path of '//example.com/'" do
2548
+ @uri.path.should == "//example.com/"
2549
+ end
2550
+ end
2551
+
2552
+ describe Addressable::URI, "when parsed from " +
2553
+ "'feed:http://example.com/'" do
2554
+ before do
2555
+ @uri = Addressable::URI.parse("feed:http://example.com/")
2556
+ end
2557
+
2558
+ it "should have a path of 'http://example.com/'" do
2559
+ @uri.path.should == "http://example.com/"
2560
+ end
2561
+
2562
+ it "should normalize to 'http://example.com/'" do
2563
+ @uri.normalize.to_s.should == "http://example.com/"
2564
+ @uri.normalize!.to_s.should == "http://example.com/"
2565
+ end
2566
+ end
2567
+
2568
+ describe Addressable::URI, "when parsed from " +
2569
+ "'example://a/b/c/%7Bfoo%7D'" do
2570
+ before do
2571
+ @uri = Addressable::URI.parse("example://a/b/c/%7Bfoo%7D")
2572
+ end
2573
+
2574
+ # Section 6.2.2 of RFC 3986
2575
+ it "should be equivalent to eXAMPLE://a/./b/../b/%63/%7bfoo%7d" do
2576
+ @uri.should ==
2577
+ Addressable::URI.parse("eXAMPLE://a/./b/../b/%63/%7bfoo%7d")
2578
+ end
2579
+ end
2580
+
2581
+ describe Addressable::URI, "when parsed from " +
2582
+ "'http://example.com/indirect/path/./to/../resource/'" do
2583
+ before do
2584
+ @uri = Addressable::URI.parse(
2585
+ "http://example.com/indirect/path/./to/../resource/")
2586
+ end
2587
+
2588
+ it "should use the 'http' scheme" do
2589
+ @uri.scheme.should == "http"
2590
+ end
2591
+
2592
+ it "should have a host of 'example.com'" do
2593
+ @uri.host.should == "example.com"
2594
+ end
2595
+
2596
+ it "should use port 80" do
2597
+ @uri.inferred_port.should == 80
2598
+ end
2599
+
2600
+ it "should have a path of '/indirect/path/./to/../resource/'" do
2601
+ @uri.path.should == "/indirect/path/./to/../resource/"
2602
+ end
2603
+
2604
+ # Section 6.2.2.3 of RFC 3986
2605
+ it "should have a normalized path of '/indirect/path/resource/'" do
2606
+ @uri.normalize.path.should == "/indirect/path/resource/"
2607
+ @uri.normalize!.path.should == "/indirect/path/resource/"
2608
+ end
2609
+ end
2610
+
2611
+ describe Addressable::URI, "when parsed from " +
2612
+ "'http://under_score.example.com/'" do
2613
+ it "should not cause an error" do
2614
+ (lambda do
2615
+ Addressable::URI.parse("http://under_score.example.com/")
2616
+ end).should_not raise_error
2617
+ end
2618
+ end
2619
+
2620
+ describe Addressable::URI, "when parsed from " +
2621
+ "'./this:that'" do
2622
+ before do
2623
+ @uri = Addressable::URI.parse("./this:that")
2624
+ end
2625
+
2626
+ it "should be considered relative" do
2627
+ @uri.should be_relative
2628
+ end
2629
+
2630
+ it "should have no scheme" do
2631
+ @uri.scheme.should == nil
2632
+ end
2633
+ end
2634
+
2635
+ describe Addressable::URI, "when parsed from " +
2636
+ "'this:that'" do
2637
+ before do
2638
+ @uri = Addressable::URI.parse("this:that")
2639
+ end
2640
+
2641
+ it "should be considered absolute" do
2642
+ @uri.should be_absolute
2643
+ end
2644
+
2645
+ it "should have a scheme of 'this'" do
2646
+ @uri.scheme.should == "this"
2647
+ end
2648
+ end
2649
+
2650
+ describe Addressable::URI, "when parsed from '?'" do
2651
+ before do
2652
+ @uri = Addressable::URI.parse("?")
2653
+ end
2654
+
2655
+ it "should have the correct subscript notation query values" do
2656
+ @uri.query_values.should == {}
2657
+ @uri.query_values(:notation => :subscript).should == {}
2658
+ end
2659
+
2660
+ it "should have the correct dot notation query values" do
2661
+ @uri.query_values(:notation => :dot).should == {}
2662
+ end
2663
+
2664
+ it "should have the correct flat notation query values" do
2665
+ @uri.query_values(:notation => :flat).should == {}
2666
+ end
2667
+ end
2668
+
2669
+ describe Addressable::URI, "when parsed from '?one=1&two=2&three=3'" do
2670
+ before do
2671
+ @uri = Addressable::URI.parse("?one=1&two=2&three=3")
2672
+ end
2673
+
2674
+ it "should have the correct query values" do
2675
+ @uri.query_values.should == {"one" => "1", "two" => "2", "three" => "3"}
2676
+ end
2677
+
2678
+ it "should raise an error for invalid query value notations" do
2679
+ (lambda do
2680
+ @uri.query_values(:notation => :bogus)
2681
+ end).should raise_error(ArgumentError)
2682
+ end
2683
+ end
2684
+
2685
+ describe Addressable::URI, "when parsed from '?one[two][three]=four'" do
2686
+ before do
2687
+ @uri = Addressable::URI.parse("?one[two][three]=four")
2688
+ end
2689
+
2690
+ it "should have the correct query values" do
2691
+ @uri.query_values.should == {"one" => {"two" => {"three" => "four"}}}
2692
+ end
2693
+
2694
+ it "should have the correct flat notation query values" do
2695
+ @uri.query_values(:notation => :flat).should == {
2696
+ "one[two][three]" => "four"
2697
+ }
2698
+ end
2699
+ end
2700
+
2701
+ describe Addressable::URI, "when parsed from '?one.two.three=four'" do
2702
+ before do
2703
+ @uri = Addressable::URI.parse("?one.two.three=four")
2704
+ end
2705
+
2706
+ it "should have the correct dot notation query values" do
2707
+ @uri.query_values(:notation => :dot).should == {
2708
+ "one" => {"two" => {"three" => "four"}}
2709
+ }
2710
+ end
2711
+
2712
+ it "should have the correct flat notation query values" do
2713
+ @uri.query_values(:notation => :flat).should == {
2714
+ "one.two.three" => "four"
2715
+ }
2716
+ end
2717
+ end
2718
+
2719
+ describe Addressable::URI, "when parsed from " +
2720
+ "'?one[two][three]=four&one[two][five]=six'" do
2721
+ before do
2722
+ @uri = Addressable::URI.parse("?one[two][three]=four&one[two][five]=six")
2723
+ end
2724
+
2725
+ it "should have the correct dot notation query values" do
2726
+ @uri.query_values(:notation => :subscript).should == {
2727
+ "one" => {"two" => {"three" => "four", "five" => "six"}}
2728
+ }
2729
+ end
2730
+
2731
+ it "should have the correct flat notation query values" do
2732
+ @uri.query_values(:notation => :flat).should == {
2733
+ "one[two][three]" => "four",
2734
+ "one[two][five]" => "six"
2735
+ }
2736
+ end
2737
+ end
2738
+
2739
+ describe Addressable::URI, "when parsed from " +
2740
+ "'?one.two.three=four&one.two.five=six'" do
2741
+ before do
2742
+ @uri = Addressable::URI.parse("?one.two.three=four&one.two.five=six")
2743
+ end
2744
+
2745
+ it "should have the correct dot notation query values" do
2746
+ @uri.query_values(:notation => :dot).should == {
2747
+ "one" => {"two" => {"three" => "four", "five" => "six"}}
2748
+ }
2749
+ end
2750
+
2751
+ it "should have the correct flat notation query values" do
2752
+ @uri.query_values(:notation => :flat).should == {
2753
+ "one.two.three" => "four",
2754
+ "one.two.five" => "six"
2755
+ }
2756
+ end
2757
+ end
2758
+
2759
+ describe Addressable::URI, "when parsed from " +
2760
+ "'?one[two][three][]=four&one[two][three][]=five'" do
2761
+ before do
2762
+ @uri = Addressable::URI.parse(
2763
+ "?one[two][three][]=four&one[two][three][]=five"
2764
+ )
2765
+ end
2766
+
2767
+ it "should have the correct subscript notation query values" do
2768
+ @uri.query_values(:notation => :subscript).should == {
2769
+ "one" => {"two" => {"three" => ["four", "five"]}}
2770
+ }
2771
+ end
2772
+
2773
+ it "should raise an error if a key is repeated in the flat notation" do
2774
+ (lambda do
2775
+ @uri.query_values(:notation => :flat)
2776
+ end).should raise_error(ArgumentError)
2777
+ end
2778
+ end
2779
+
2780
+ describe Addressable::URI, "when parsed from " +
2781
+ "'?one[two][three][0]=four&one[two][three][1]=five'" do
2782
+ before do
2783
+ @uri = Addressable::URI.parse(
2784
+ "?one[two][three][0]=four&one[two][three][1]=five"
2785
+ )
2786
+ end
2787
+
2788
+ it "should have the correct subscript notation query values" do
2789
+ @uri.query_values(:notation => :subscript).should == {
2790
+ "one" => {"two" => {"three" => ["four", "five"]}}
2791
+ }
2792
+ end
2793
+ end
2794
+
2795
+ describe Addressable::URI, "when parsed from " +
2796
+ "'http://www.詹姆斯.com/'" do
2797
+ before do
2798
+ @uri = Addressable::URI.parse("http://www.詹姆斯.com/")
2799
+ end
2800
+
2801
+ it "should be equivalent to 'http://www.xn--8ws00zhy3a.com/'" do
2802
+ @uri.should ==
2803
+ Addressable::URI.parse("http://www.xn--8ws00zhy3a.com/")
2804
+ end
2805
+
2806
+ it "should not have domain name encoded during normalization" do
2807
+ Addressable::URI.normalized_encode(@uri.to_s).should ==
2808
+ "http://www.詹姆斯.com/"
2809
+ end
2810
+ end
2811
+
2812
+ describe Addressable::URI, "when parsed from " +
2813
+ "'http://www.詹姆斯.com/ some spaces /'" do
2814
+ before do
2815
+ @uri = Addressable::URI.parse("http://www.詹姆斯.com/ some spaces /")
2816
+ end
2817
+
2818
+ it "should be equivalent to " +
2819
+ "'http://www.xn--8ws00zhy3a.com/%20some%20spaces%20/'" do
2820
+ @uri.should ==
2821
+ Addressable::URI.parse(
2822
+ "http://www.xn--8ws00zhy3a.com/%20some%20spaces%20/")
2823
+ end
2824
+
2825
+ it "should not have domain name encoded during normalization" do
2826
+ Addressable::URI.normalized_encode(@uri.to_s).should ==
2827
+ "http://www.詹姆斯.com/%20some%20spaces%20/"
2828
+ end
2829
+ end
2830
+
2831
+ describe Addressable::URI, "when parsed from " +
2832
+ "'http://www.xn--8ws00zhy3a.com/'" do
2833
+ before do
2834
+ @uri = Addressable::URI.parse("http://www.xn--8ws00zhy3a.com/")
2835
+ end
2836
+
2837
+ it "should be displayed as http://www.詹姆斯.com/" do
2838
+ @uri.display_uri.to_s.should == "http://www.詹姆斯.com/"
2839
+ end
2840
+
2841
+ it "should properly force the encoding" do
2842
+ display_string = @uri.display_uri.to_str
2843
+ display_string.should == "http://www.詹姆斯.com/"
2844
+ if display_string.respond_to?(:encoding)
2845
+ display_string.encoding.to_s.should == Encoding::UTF_8.to_s
2846
+ end
2847
+ end
2848
+ end
2849
+
2850
+ describe Addressable::URI, "when parsed from " +
2851
+ "'http://www.詹姆斯.com/atomtests/iri/詹.html'" do
2852
+ before do
2853
+ @uri = Addressable::URI.parse("http://www.詹姆斯.com/atomtests/iri/詹.html")
2854
+ end
2855
+
2856
+ it "should normalize to " +
2857
+ "http://www.xn--8ws00zhy3a.com/atomtests/iri/%E8%A9%B9.html" do
2858
+ @uri.normalize.to_s.should ==
2859
+ "http://www.xn--8ws00zhy3a.com/atomtests/iri/%E8%A9%B9.html"
2860
+ @uri.normalize!.to_s.should ==
2861
+ "http://www.xn--8ws00zhy3a.com/atomtests/iri/%E8%A9%B9.html"
2862
+ end
2863
+ end
2864
+
2865
+ describe Addressable::URI, "when parsed from a percent-encoded IRI" do
2866
+ before do
2867
+ @uri = Addressable::URI.parse(
2868
+ "http://www.%E3%81%BB%E3%82%93%E3%81%A8%E3%81%86%E3%81%AB%E3%81%AA" +
2869
+ "%E3%81%8C%E3%81%84%E3%82%8F%E3%81%91%E3%81%AE%E3%82%8F%E3%81%8B%E3" +
2870
+ "%82%89%E3%81%AA%E3%81%84%E3%81%A9%E3%82%81%E3%81%84%E3%82%93%E3%82" +
2871
+ "%81%E3%81%84%E3%81%AE%E3%82%89%E3%81%B9%E3%82%8B%E3%81%BE%E3%81%A0" +
2872
+ "%E3%81%AA%E3%81%8C%E3%81%8F%E3%81%97%E3%81%AA%E3%81%84%E3%81%A8%E3" +
2873
+ "%81%9F%E3%82%8A%E3%81%AA%E3%81%84.w3.mag.keio.ac.jp"
2874
+ )
2875
+ end
2876
+
2877
+ it "should normalize to something sane" do
2878
+ @uri.normalize.to_s.should ==
2879
+ "http://www.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3f" +
2880
+ "g11amb5gzdb4wi9bya3kc6lra.w3.mag.keio.ac.jp/"
2881
+ @uri.normalize!.to_s.should ==
2882
+ "http://www.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3f" +
2883
+ "g11amb5gzdb4wi9bya3kc6lra.w3.mag.keio.ac.jp/"
2884
+ end
2885
+ end
2886
+
2887
+ describe Addressable::URI, "with a base uri of 'http://a/b/c/d;p?q'" do
2888
+ before do
2889
+ @uri = Addressable::URI.parse("http://a/b/c/d;p?q")
2890
+ end
2891
+
2892
+ # Section 5.4.1 of RFC 3986
2893
+ it "when joined with 'g:h' should resolve to g:h" do
2894
+ (@uri + "g:h").to_s.should == "g:h"
2895
+ Addressable::URI.join(@uri, "g:h").to_s.should == "g:h"
2896
+ end
2897
+
2898
+ # Section 5.4.1 of RFC 3986
2899
+ it "when joined with 'g' should resolve to http://a/b/c/g" do
2900
+ (@uri + "g").to_s.should == "http://a/b/c/g"
2901
+ Addressable::URI.join(@uri.to_s, "g").to_s.should == "http://a/b/c/g"
2902
+ end
2903
+
2904
+ # Section 5.4.1 of RFC 3986
2905
+ it "when joined with './g' should resolve to http://a/b/c/g" do
2906
+ (@uri + "./g").to_s.should == "http://a/b/c/g"
2907
+ Addressable::URI.join(@uri.to_s, "./g").to_s.should == "http://a/b/c/g"
2908
+ end
2909
+
2910
+ # Section 5.4.1 of RFC 3986
2911
+ it "when joined with 'g/' should resolve to http://a/b/c/g/" do
2912
+ (@uri + "g/").to_s.should == "http://a/b/c/g/"
2913
+ Addressable::URI.join(@uri.to_s, "g/").to_s.should == "http://a/b/c/g/"
2914
+ end
2915
+
2916
+ # Section 5.4.1 of RFC 3986
2917
+ it "when joined with '/g' should resolve to http://a/g" do
2918
+ (@uri + "/g").to_s.should == "http://a/g"
2919
+ Addressable::URI.join(@uri.to_s, "/g").to_s.should == "http://a/g"
2920
+ end
2921
+
2922
+ # Section 5.4.1 of RFC 3986
2923
+ it "when joined with '//g' should resolve to http://g" do
2924
+ (@uri + "//g").to_s.should == "http://g"
2925
+ Addressable::URI.join(@uri.to_s, "//g").to_s.should == "http://g"
2926
+ end
2927
+
2928
+ # Section 5.4.1 of RFC 3986
2929
+ it "when joined with '?y' should resolve to http://a/b/c/d;p?y" do
2930
+ (@uri + "?y").to_s.should == "http://a/b/c/d;p?y"
2931
+ Addressable::URI.join(@uri.to_s, "?y").to_s.should == "http://a/b/c/d;p?y"
2932
+ end
2933
+
2934
+ # Section 5.4.1 of RFC 3986
2935
+ it "when joined with 'g?y' should resolve to http://a/b/c/g?y" do
2936
+ (@uri + "g?y").to_s.should == "http://a/b/c/g?y"
2937
+ Addressable::URI.join(@uri.to_s, "g?y").to_s.should == "http://a/b/c/g?y"
2938
+ end
2939
+
2940
+ # Section 5.4.1 of RFC 3986
2941
+ it "when joined with '#s' should resolve to http://a/b/c/d;p?q#s" do
2942
+ (@uri + "#s").to_s.should == "http://a/b/c/d;p?q#s"
2943
+ Addressable::URI.join(@uri.to_s, "#s").to_s.should == "http://a/b/c/d;p?q#s"
2944
+ end
2945
+
2946
+ # Section 5.4.1 of RFC 3986
2947
+ it "when joined with 'g#s' should resolve to http://a/b/c/g#s" do
2948
+ (@uri + "g#s").to_s.should == "http://a/b/c/g#s"
2949
+ Addressable::URI.join(@uri.to_s, "g#s").to_s.should == "http://a/b/c/g#s"
2950
+ end
2951
+
2952
+ # Section 5.4.1 of RFC 3986
2953
+ it "when joined with 'g?y#s' should resolve to http://a/b/c/g?y#s" do
2954
+ (@uri + "g?y#s").to_s.should == "http://a/b/c/g?y#s"
2955
+ Addressable::URI.join(
2956
+ @uri.to_s, "g?y#s").to_s.should == "http://a/b/c/g?y#s"
2957
+ end
2958
+
2959
+ # Section 5.4.1 of RFC 3986
2960
+ it "when joined with ';x' should resolve to http://a/b/c/;x" do
2961
+ (@uri + ";x").to_s.should == "http://a/b/c/;x"
2962
+ Addressable::URI.join(@uri.to_s, ";x").to_s.should == "http://a/b/c/;x"
2963
+ end
2964
+
2965
+ # Section 5.4.1 of RFC 3986
2966
+ it "when joined with 'g;x' should resolve to http://a/b/c/g;x" do
2967
+ (@uri + "g;x").to_s.should == "http://a/b/c/g;x"
2968
+ Addressable::URI.join(@uri.to_s, "g;x").to_s.should == "http://a/b/c/g;x"
2969
+ end
2970
+
2971
+ # Section 5.4.1 of RFC 3986
2972
+ it "when joined with 'g;x?y#s' should resolve to http://a/b/c/g;x?y#s" do
2973
+ (@uri + "g;x?y#s").to_s.should == "http://a/b/c/g;x?y#s"
2974
+ Addressable::URI.join(
2975
+ @uri.to_s, "g;x?y#s").to_s.should == "http://a/b/c/g;x?y#s"
2976
+ end
2977
+
2978
+ # Section 5.4.1 of RFC 3986
2979
+ it "when joined with '' should resolve to http://a/b/c/d;p?q" do
2980
+ (@uri + "").to_s.should == "http://a/b/c/d;p?q"
2981
+ Addressable::URI.join(@uri.to_s, "").to_s.should == "http://a/b/c/d;p?q"
2982
+ end
2983
+
2984
+ # Section 5.4.1 of RFC 3986
2985
+ it "when joined with '.' should resolve to http://a/b/c/" do
2986
+ (@uri + ".").to_s.should == "http://a/b/c/"
2987
+ Addressable::URI.join(@uri.to_s, ".").to_s.should == "http://a/b/c/"
2988
+ end
2989
+
2990
+ # Section 5.4.1 of RFC 3986
2991
+ it "when joined with './' should resolve to http://a/b/c/" do
2992
+ (@uri + "./").to_s.should == "http://a/b/c/"
2993
+ Addressable::URI.join(@uri.to_s, "./").to_s.should == "http://a/b/c/"
2994
+ end
2995
+
2996
+ # Section 5.4.1 of RFC 3986
2997
+ it "when joined with '..' should resolve to http://a/b/" do
2998
+ (@uri + "..").to_s.should == "http://a/b/"
2999
+ Addressable::URI.join(@uri.to_s, "..").to_s.should == "http://a/b/"
3000
+ end
3001
+
3002
+ # Section 5.4.1 of RFC 3986
3003
+ it "when joined with '../' should resolve to http://a/b/" do
3004
+ (@uri + "../").to_s.should == "http://a/b/"
3005
+ Addressable::URI.join(@uri.to_s, "../").to_s.should == "http://a/b/"
3006
+ end
3007
+
3008
+ # Section 5.4.1 of RFC 3986
3009
+ it "when joined with '../g' should resolve to http://a/b/g" do
3010
+ (@uri + "../g").to_s.should == "http://a/b/g"
3011
+ Addressable::URI.join(@uri.to_s, "../g").to_s.should == "http://a/b/g"
3012
+ end
3013
+
3014
+ # Section 5.4.1 of RFC 3986
3015
+ it "when joined with '../..' should resolve to http://a/" do
3016
+ (@uri + "../..").to_s.should == "http://a/"
3017
+ Addressable::URI.join(@uri.to_s, "../..").to_s.should == "http://a/"
3018
+ end
3019
+
3020
+ # Section 5.4.1 of RFC 3986
3021
+ it "when joined with '../../' should resolve to http://a/" do
3022
+ (@uri + "../../").to_s.should == "http://a/"
3023
+ Addressable::URI.join(@uri.to_s, "../../").to_s.should == "http://a/"
3024
+ end
3025
+
3026
+ # Section 5.4.1 of RFC 3986
3027
+ it "when joined with '../../g' should resolve to http://a/g" do
3028
+ (@uri + "../../g").to_s.should == "http://a/g"
3029
+ Addressable::URI.join(@uri.to_s, "../../g").to_s.should == "http://a/g"
3030
+ end
3031
+
3032
+ # Section 5.4.2 of RFC 3986
3033
+ it "when joined with '../../../g' should resolve to http://a/g" do
3034
+ (@uri + "../../../g").to_s.should == "http://a/g"
3035
+ Addressable::URI.join(@uri.to_s, "../../../g").to_s.should == "http://a/g"
3036
+ end
3037
+
3038
+ it "when joined with '../.././../g' should resolve to http://a/g" do
3039
+ (@uri + "../.././../g").to_s.should == "http://a/g"
3040
+ Addressable::URI.join(@uri.to_s, "../.././../g").to_s.should == "http://a/g"
3041
+ end
3042
+
3043
+ # Section 5.4.2 of RFC 3986
3044
+ it "when joined with '../../../../g' should resolve to http://a/g" do
3045
+ (@uri + "../../../../g").to_s.should == "http://a/g"
3046
+ Addressable::URI.join(
3047
+ @uri.to_s, "../../../../g").to_s.should == "http://a/g"
3048
+ end
3049
+
3050
+ # Section 5.4.2 of RFC 3986
3051
+ it "when joined with '/./g' should resolve to http://a/g" do
3052
+ (@uri + "/./g").to_s.should == "http://a/g"
3053
+ Addressable::URI.join(@uri.to_s, "/./g").to_s.should == "http://a/g"
3054
+ end
3055
+
3056
+ # Section 5.4.2 of RFC 3986
3057
+ it "when joined with '/../g' should resolve to http://a/g" do
3058
+ (@uri + "/../g").to_s.should == "http://a/g"
3059
+ Addressable::URI.join(@uri.to_s, "/../g").to_s.should == "http://a/g"
3060
+ end
3061
+
3062
+ # Section 5.4.2 of RFC 3986
3063
+ it "when joined with 'g.' should resolve to http://a/b/c/g." do
3064
+ (@uri + "g.").to_s.should == "http://a/b/c/g."
3065
+ Addressable::URI.join(@uri.to_s, "g.").to_s.should == "http://a/b/c/g."
3066
+ end
3067
+
3068
+ # Section 5.4.2 of RFC 3986
3069
+ it "when joined with '.g' should resolve to http://a/b/c/.g" do
3070
+ (@uri + ".g").to_s.should == "http://a/b/c/.g"
3071
+ Addressable::URI.join(@uri.to_s, ".g").to_s.should == "http://a/b/c/.g"
3072
+ end
3073
+
3074
+ # Section 5.4.2 of RFC 3986
3075
+ it "when joined with 'g..' should resolve to http://a/b/c/g.." do
3076
+ (@uri + "g..").to_s.should == "http://a/b/c/g.."
3077
+ Addressable::URI.join(@uri.to_s, "g..").to_s.should == "http://a/b/c/g.."
3078
+ end
3079
+
3080
+ # Section 5.4.2 of RFC 3986
3081
+ it "when joined with '..g' should resolve to http://a/b/c/..g" do
3082
+ (@uri + "..g").to_s.should == "http://a/b/c/..g"
3083
+ Addressable::URI.join(@uri.to_s, "..g").to_s.should == "http://a/b/c/..g"
3084
+ end
3085
+
3086
+ # Section 5.4.2 of RFC 3986
3087
+ it "when joined with './../g' should resolve to http://a/b/g" do
3088
+ (@uri + "./../g").to_s.should == "http://a/b/g"
3089
+ Addressable::URI.join(@uri.to_s, "./../g").to_s.should == "http://a/b/g"
3090
+ end
3091
+
3092
+ # Section 5.4.2 of RFC 3986
3093
+ it "when joined with './g/.' should resolve to http://a/b/c/g/" do
3094
+ (@uri + "./g/.").to_s.should == "http://a/b/c/g/"
3095
+ Addressable::URI.join(@uri.to_s, "./g/.").to_s.should == "http://a/b/c/g/"
3096
+ end
3097
+
3098
+ # Section 5.4.2 of RFC 3986
3099
+ it "when joined with 'g/./h' should resolve to http://a/b/c/g/h" do
3100
+ (@uri + "g/./h").to_s.should == "http://a/b/c/g/h"
3101
+ Addressable::URI.join(@uri.to_s, "g/./h").to_s.should == "http://a/b/c/g/h"
3102
+ end
3103
+
3104
+ # Section 5.4.2 of RFC 3986
3105
+ it "when joined with 'g/../h' should resolve to http://a/b/c/h" do
3106
+ (@uri + "g/../h").to_s.should == "http://a/b/c/h"
3107
+ Addressable::URI.join(@uri.to_s, "g/../h").to_s.should == "http://a/b/c/h"
3108
+ end
3109
+
3110
+ # Section 5.4.2 of RFC 3986
3111
+ it "when joined with 'g;x=1/./y' " +
3112
+ "should resolve to http://a/b/c/g;x=1/y" do
3113
+ (@uri + "g;x=1/./y").to_s.should == "http://a/b/c/g;x=1/y"
3114
+ Addressable::URI.join(
3115
+ @uri.to_s, "g;x=1/./y").to_s.should == "http://a/b/c/g;x=1/y"
3116
+ end
3117
+
3118
+ # Section 5.4.2 of RFC 3986
3119
+ it "when joined with 'g;x=1/../y' should resolve to http://a/b/c/y" do
3120
+ (@uri + "g;x=1/../y").to_s.should == "http://a/b/c/y"
3121
+ Addressable::URI.join(
3122
+ @uri.to_s, "g;x=1/../y").to_s.should == "http://a/b/c/y"
3123
+ end
3124
+
3125
+ # Section 5.4.2 of RFC 3986
3126
+ it "when joined with 'g?y/./x' " +
3127
+ "should resolve to http://a/b/c/g?y/./x" do
3128
+ (@uri + "g?y/./x").to_s.should == "http://a/b/c/g?y/./x"
3129
+ Addressable::URI.join(
3130
+ @uri.to_s, "g?y/./x").to_s.should == "http://a/b/c/g?y/./x"
3131
+ end
3132
+
3133
+ # Section 5.4.2 of RFC 3986
3134
+ it "when joined with 'g?y/../x' " +
3135
+ "should resolve to http://a/b/c/g?y/../x" do
3136
+ (@uri + "g?y/../x").to_s.should == "http://a/b/c/g?y/../x"
3137
+ Addressable::URI.join(
3138
+ @uri.to_s, "g?y/../x").to_s.should == "http://a/b/c/g?y/../x"
3139
+ end
3140
+
3141
+ # Section 5.4.2 of RFC 3986
3142
+ it "when joined with 'g#s/./x' " +
3143
+ "should resolve to http://a/b/c/g#s/./x" do
3144
+ (@uri + "g#s/./x").to_s.should == "http://a/b/c/g#s/./x"
3145
+ Addressable::URI.join(
3146
+ @uri.to_s, "g#s/./x").to_s.should == "http://a/b/c/g#s/./x"
3147
+ end
3148
+
3149
+ # Section 5.4.2 of RFC 3986
3150
+ it "when joined with 'g#s/../x' " +
3151
+ "should resolve to http://a/b/c/g#s/../x" do
3152
+ (@uri + "g#s/../x").to_s.should == "http://a/b/c/g#s/../x"
3153
+ Addressable::URI.join(
3154
+ @uri.to_s, "g#s/../x").to_s.should == "http://a/b/c/g#s/../x"
3155
+ end
3156
+
3157
+ # Section 5.4.2 of RFC 3986
3158
+ it "when joined with 'http:g' should resolve to http:g" do
3159
+ (@uri + "http:g").to_s.should == "http:g"
3160
+ Addressable::URI.join(@uri.to_s, "http:g").to_s.should == "http:g"
3161
+ end
3162
+
3163
+ # Edge case to be sure
3164
+ it "when joined with '//example.com/' should " +
3165
+ "resolve to http://example.com/" do
3166
+ (@uri + "//example.com/").to_s.should == "http://example.com/"
3167
+ Addressable::URI.join(
3168
+ @uri.to_s, "//example.com/").to_s.should == "http://example.com/"
3169
+ end
3170
+
3171
+ it "when joined with a bogus object a TypeError should be raised" do
3172
+ (lambda do
3173
+ Addressable::URI.join(@uri, 42)
3174
+ end).should raise_error(TypeError)
3175
+ end
3176
+ end
3177
+
3178
+ describe Addressable::URI, "when converting the path " +
3179
+ "'relative/path/to/something'" do
3180
+ before do
3181
+ @path = 'relative/path/to/something'
3182
+ end
3183
+
3184
+ it "should convert to " +
3185
+ "\'relative/path/to/something\'" do
3186
+ @uri = Addressable::URI.convert_path(@path)
3187
+ @uri.to_str.should == "relative/path/to/something"
3188
+ end
3189
+
3190
+ it "should join with an absolute file path correctly" do
3191
+ @base = Addressable::URI.convert_path("/absolute/path/")
3192
+ @uri = Addressable::URI.convert_path(@path)
3193
+ (@base + @uri).to_str.should ==
3194
+ "file:///absolute/path/relative/path/to/something"
3195
+ end
3196
+ end
3197
+
3198
+ describe Addressable::URI, "when converting a bogus path" do
3199
+ it "should raise a TypeError" do
3200
+ (lambda do
3201
+ Addressable::URI.convert_path(42)
3202
+ end).should raise_error(TypeError)
3203
+ end
3204
+ end
3205
+
3206
+ describe Addressable::URI, "when given the root directory" do
3207
+ before do
3208
+ if RUBY_PLATFORM =~ /mswin/
3209
+ @path = "C:\\"
3210
+ else
3211
+ @path = "/"
3212
+ end
3213
+ end
3214
+
3215
+ if RUBY_PLATFORM =~ /mswin/
3216
+ it "should convert to \'file:///c:/\'" do
3217
+ @uri = Addressable::URI.convert_path(@path)
3218
+ @uri.to_str.should == "file:///c:/"
3219
+ end
3220
+ else
3221
+ it "should convert to \'file:///\'" do
3222
+ @uri = Addressable::URI.convert_path(@path)
3223
+ @uri.to_str.should == "file:///"
3224
+ end
3225
+ end
3226
+ end
3227
+
3228
+ describe Addressable::URI, "when given the path '/home/user/'" do
3229
+ before do
3230
+ @path = '/home/user/'
3231
+ end
3232
+
3233
+ it "should convert to " +
3234
+ "\'file:///home/user/\'" do
3235
+ @uri = Addressable::URI.convert_path(@path)
3236
+ @uri.to_str.should == "file:///home/user/"
3237
+ end
3238
+ end
3239
+
3240
+ describe Addressable::URI, "when given the path " +
3241
+ "'c:\\windows\\My Documents 100%20\\foo.txt'" do
3242
+ before do
3243
+ @path = "c:\\windows\\My Documents 100%20\\foo.txt"
3244
+ end
3245
+
3246
+ it "should convert to " +
3247
+ "\'file:///c:/windows/My%20Documents%20100%20/foo.txt\'" do
3248
+ @uri = Addressable::URI.convert_path(@path)
3249
+ @uri.to_str.should == "file:///c:/windows/My%20Documents%20100%20/foo.txt"
3250
+ end
3251
+ end
3252
+
3253
+ describe Addressable::URI, "when given the path " +
3254
+ "'file://c:\\windows\\My Documents 100%20\\foo.txt'" do
3255
+ before do
3256
+ @path = "file://c:\\windows\\My Documents 100%20\\foo.txt"
3257
+ end
3258
+
3259
+ it "should convert to " +
3260
+ "\'file:///c:/windows/My%20Documents%20100%20/foo.txt\'" do
3261
+ @uri = Addressable::URI.convert_path(@path)
3262
+ @uri.to_str.should == "file:///c:/windows/My%20Documents%20100%20/foo.txt"
3263
+ end
3264
+ end
3265
+
3266
+ describe Addressable::URI, "when given the path " +
3267
+ "'file:c:\\windows\\My Documents 100%20\\foo.txt'" do
3268
+ before do
3269
+ @path = "file:c:\\windows\\My Documents 100%20\\foo.txt"
3270
+ end
3271
+
3272
+ it "should convert to " +
3273
+ "\'file:///c:/windows/My%20Documents%20100%20/foo.txt\'" do
3274
+ @uri = Addressable::URI.convert_path(@path)
3275
+ @uri.to_str.should == "file:///c:/windows/My%20Documents%20100%20/foo.txt"
3276
+ end
3277
+ end
3278
+
3279
+ describe Addressable::URI, "when given the path " +
3280
+ "'file:/c:\\windows\\My Documents 100%20\\foo.txt'" do
3281
+ before do
3282
+ @path = "file:/c:\\windows\\My Documents 100%20\\foo.txt"
3283
+ end
3284
+
3285
+ it "should convert to " +
3286
+ "\'file:///c:/windows/My%20Documents%20100%20/foo.txt\'" do
3287
+ @uri = Addressable::URI.convert_path(@path)
3288
+ @uri.to_str.should == "file:///c:/windows/My%20Documents%20100%20/foo.txt"
3289
+ end
3290
+ end
3291
+
3292
+ describe Addressable::URI, "when given the path " +
3293
+ "'file:///c|/windows/My%20Documents%20100%20/foo.txt'" do
3294
+ before do
3295
+ @path = "file:///c|/windows/My%20Documents%20100%20/foo.txt"
3296
+ end
3297
+
3298
+ it "should convert to " +
3299
+ "\'file:///c:/windows/My%20Documents%20100%20/foo.txt\'" do
3300
+ @uri = Addressable::URI.convert_path(@path)
3301
+ @uri.to_str.should == "file:///c:/windows/My%20Documents%20100%20/foo.txt"
3302
+ end
3303
+ end
3304
+
3305
+ describe Addressable::URI, "when given an http protocol URI" do
3306
+ before do
3307
+ @path = "http://example.com/"
3308
+ end
3309
+
3310
+ it "should not do any conversion at all" do
3311
+ @uri = Addressable::URI.convert_path(@path)
3312
+ @uri.to_str.should == "http://example.com/"
3313
+ end
3314
+ end
3315
+
3316
+ class SuperString
3317
+ def initialize(string)
3318
+ @string = string.to_s
3319
+ end
3320
+
3321
+ def to_str
3322
+ return @string
3323
+ end
3324
+ end
3325
+
3326
+ describe Addressable::URI, "when parsing a non-String object" do
3327
+ it "should correctly parse anything with a 'to_str' method" do
3328
+ Addressable::URI.parse(SuperString.new(42))
3329
+ end
3330
+
3331
+ it "should raise a TypeError for objects than cannot be converted" do
3332
+ (lambda do
3333
+ Addressable::URI.parse(42)
3334
+ end).should raise_error(TypeError, "Can't convert Fixnum into String.")
3335
+ end
3336
+
3337
+ it "should correctly parse heuristically anything with a 'to_str' method" do
3338
+ Addressable::URI.heuristic_parse(SuperString.new(42))
3339
+ end
3340
+
3341
+ it "should raise a TypeError for objects than cannot be converted" do
3342
+ (lambda do
3343
+ Addressable::URI.heuristic_parse(42)
3344
+ end).should raise_error(TypeError, "Can't convert Fixnum into String.")
3345
+ end
3346
+ end
3347
+
3348
+ describe Addressable::URI, "when encoding a multibyte string" do
3349
+ it "should result in correct percent encoded sequence" do
3350
+ Addressable::URI.encode_component("günther").should == "g%C3%BCnther"
3351
+ end
3352
+
3353
+ it "should result in correct percent encoded sequence" do
3354
+ Addressable::URI.encode_component(
3355
+ "günther", /[^a-zA-Z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\-\.\_\~]/
3356
+ ).should == "g%C3%BCnther"
3357
+ end
3358
+ end
3359
+
3360
+ describe Addressable::URI, "when encoding a string with ASCII chars 0-15" do
3361
+ it "should result in correct percent encoded sequence" do
3362
+ Addressable::URI.encode_component("one\ntwo").should == "one%0Atwo"
3363
+ end
3364
+
3365
+ it "should result in correct percent encoded sequence" do
3366
+ Addressable::URI.encode_component(
3367
+ "one\ntwo", /[^a-zA-Z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\-\.\_\~]/
3368
+ ).should == "one%0Atwo"
3369
+ end
3370
+ end
3371
+
3372
+ describe Addressable::URI, "when unencoding a multibyte string" do
3373
+ it "should result in correct percent encoded sequence" do
3374
+ Addressable::URI.unencode_component("g%C3%BCnther").should == "günther"
3375
+ end
3376
+
3377
+ it "should result in correct percent encoded sequence as a URI" do
3378
+ Addressable::URI.unencode(
3379
+ "/path?g%C3%BCnther", ::Addressable::URI
3380
+ ).should == Addressable::URI.new(
3381
+ :path => "/path", :query => "günther"
3382
+ )
3383
+ end
3384
+ end
3385
+
3386
+ describe Addressable::URI, "when unencoding a bogus object" do
3387
+ it "should raise a TypeError" do
3388
+ (lambda do
3389
+ Addressable::URI.unencode_component(42)
3390
+ end).should raise_error(TypeError)
3391
+ end
3392
+
3393
+ it "should raise a TypeError" do
3394
+ (lambda do
3395
+ Addressable::URI.unencode("/path?g%C3%BCnther", Integer)
3396
+ end).should raise_error(TypeError)
3397
+ end
3398
+ end
3399
+
3400
+ describe Addressable::URI, "when encoding a bogus object" do
3401
+ it "should raise a TypeError" do
3402
+ (lambda do
3403
+ Addressable::URI.encode(42)
3404
+ end).should raise_error(TypeError)
3405
+ end
3406
+
3407
+ it "should raise a TypeError" do
3408
+ (lambda do
3409
+ Addressable::URI.normalized_encode(42)
3410
+ end).should raise_error(TypeError)
3411
+ end
3412
+
3413
+ it "should raise a TypeError" do
3414
+ (lambda do
3415
+ Addressable::URI.encode_component("günther", 42)
3416
+ end).should raise_error(TypeError)
3417
+ end
3418
+
3419
+ it "should raise a TypeError" do
3420
+ (lambda do
3421
+ Addressable::URI.encode_component(42)
3422
+ end).should raise_error(TypeError)
3423
+ end
3424
+ end
3425
+
3426
+ describe Addressable::URI, "when given the input " +
3427
+ "'http://example.com/'" do
3428
+ before do
3429
+ @input = "http://example.com/"
3430
+ end
3431
+
3432
+ it "should heuristically parse to 'http://example.com/'" do
3433
+ @uri = Addressable::URI.heuristic_parse(@input)
3434
+ @uri.to_s.should == "http://example.com/"
3435
+ end
3436
+ end
3437
+
3438
+
3439
+ describe Addressable::URI, "when given the input " +
3440
+ "'http:example.com/'" do
3441
+ before do
3442
+ @input = "http:example.com/"
3443
+ end
3444
+
3445
+ it "should heuristically parse to 'http://example.com/'" do
3446
+ @uri = Addressable::URI.heuristic_parse(@input)
3447
+ @uri.to_s.should == "http://example.com/"
3448
+ end
3449
+
3450
+ it "should heuristically parse to 'http://example.com/' " +
3451
+ "even with a scheme hint of 'ftp'" do
3452
+ @uri = Addressable::URI.heuristic_parse(@input, {:scheme => 'ftp'})
3453
+ @uri.to_s.should == "http://example.com/"
3454
+ end
3455
+ end
3456
+
3457
+ describe Addressable::URI, "when given the input " +
3458
+ "'http://example.com/example.com/'" do
3459
+ before do
3460
+ @input = "http://example.com/example.com/"
3461
+ end
3462
+
3463
+ it "should heuristically parse to 'http://example.com/example.com/'" do
3464
+ @uri = Addressable::URI.heuristic_parse(@input)
3465
+ @uri.to_s.should == "http://example.com/example.com/"
3466
+ end
3467
+ end
3468
+
3469
+ describe Addressable::URI, "when given the input " +
3470
+ "'/path/to/resource'" do
3471
+ before do
3472
+ @input = "/path/to/resource"
3473
+ end
3474
+
3475
+ it "should heuristically parse to '/path/to/resource'" do
3476
+ @uri = Addressable::URI.heuristic_parse(@input)
3477
+ @uri.to_s.should == "/path/to/resource"
3478
+ end
3479
+ end
3480
+
3481
+ describe Addressable::URI, "when given the input " +
3482
+ "'relative/path/to/resource'" do
3483
+ before do
3484
+ @input = "relative/path/to/resource"
3485
+ end
3486
+
3487
+ it "should heuristically parse to 'relative/path/to/resource'" do
3488
+ @uri = Addressable::URI.heuristic_parse(@input)
3489
+ @uri.to_s.should == "relative/path/to/resource"
3490
+ end
3491
+ end
3492
+
3493
+ describe Addressable::URI, "when given the input " +
3494
+ "'example.com'" do
3495
+ before do
3496
+ @input = "example.com"
3497
+ end
3498
+
3499
+ it "should heuristically parse to 'http://example.com'" do
3500
+ @uri = Addressable::URI.heuristic_parse(@input)
3501
+ @uri.to_s.should == "http://example.com"
3502
+ end
3503
+ end
3504
+
3505
+ describe Addressable::URI, "when given the input " +
3506
+ "'example.com' and a scheme hint of 'ftp'" do
3507
+ before do
3508
+ @input = "example.com"
3509
+ @hints = {:scheme => 'ftp'}
3510
+ end
3511
+
3512
+ it "should heuristically parse to 'http://example.com'" do
3513
+ @uri = Addressable::URI.heuristic_parse(@input, @hints)
3514
+ @uri.to_s.should == "ftp://example.com"
3515
+ end
3516
+ end
3517
+
3518
+ describe Addressable::URI, "when given the input " +
3519
+ "'example.com:21' and a scheme hint of 'ftp'" do
3520
+ before do
3521
+ @input = "example.com:21"
3522
+ @hints = {:scheme => 'ftp'}
3523
+ end
3524
+
3525
+ it "should heuristically parse to 'http://example.com:21'" do
3526
+ @uri = Addressable::URI.heuristic_parse(@input, @hints)
3527
+ @uri.to_s.should == "ftp://example.com:21"
3528
+ end
3529
+ end
3530
+
3531
+ describe Addressable::URI, "when given the input " +
3532
+ "'example.com/path/to/resource'" do
3533
+ before do
3534
+ @input = "example.com/path/to/resource"
3535
+ end
3536
+
3537
+ it "should heuristically parse to 'http://example.com/path/to/resource'" do
3538
+ @uri = Addressable::URI.heuristic_parse(@input)
3539
+ @uri.to_s.should == "http://example.com/path/to/resource"
3540
+ end
3541
+ end
3542
+
3543
+ describe Addressable::URI, "when given the input " +
3544
+ "'http:///example.com'" do
3545
+ before do
3546
+ @input = "http:///example.com"
3547
+ end
3548
+
3549
+ it "should heuristically parse to 'http://example.com'" do
3550
+ @uri = Addressable::URI.heuristic_parse(@input)
3551
+ @uri.to_s.should == "http://example.com"
3552
+ end
3553
+ end
3554
+
3555
+ describe Addressable::URI, "when given the input " +
3556
+ "'feed:///example.com'" do
3557
+ before do
3558
+ @input = "feed:///example.com"
3559
+ end
3560
+
3561
+ it "should heuristically parse to 'feed://example.com'" do
3562
+ @uri = Addressable::URI.heuristic_parse(@input)
3563
+ @uri.to_s.should == "feed://example.com"
3564
+ end
3565
+ end
3566
+
3567
+ describe Addressable::URI, "when given the input " +
3568
+ "'file://path/to/resource/'" do
3569
+ before do
3570
+ @input = "file://path/to/resource/"
3571
+ end
3572
+
3573
+ it "should heuristically parse to 'file:///path/to/resource/'" do
3574
+ @uri = Addressable::URI.heuristic_parse(@input)
3575
+ @uri.to_s.should == "file:///path/to/resource/"
3576
+ end
3577
+ end
3578
+
3579
+ describe Addressable::URI, "when given the input " +
3580
+ "'feed://http://example.com'" do
3581
+ before do
3582
+ @input = "feed://http://example.com"
3583
+ end
3584
+
3585
+ it "should heuristically parse to 'feed:http://example.com'" do
3586
+ @uri = Addressable::URI.heuristic_parse(@input)
3587
+ @uri.to_s.should == "feed:http://example.com"
3588
+ end
3589
+ end
3590
+
3591
+ describe Addressable::URI, "when assigning query values" do
3592
+ before do
3593
+ @uri = Addressable::URI.new
3594
+ end
3595
+
3596
+ it "should correctly assign {:a => 'a', :b => ['c', 'd', 'e']}" do
3597
+ @uri.query_values = {:a => "a", :b => ["c", "d", "e"]}
3598
+ @uri.query.should == "a=a&b[0]=c&b[1]=d&b[2]=e"
3599
+ end
3600
+
3601
+ it "should correctly assign " +
3602
+ "{:a => 'a', :b => [{:c => 'c', :d => 'd'}, {:e => 'e', :f => 'f'}]}" do
3603
+ @uri.query_values = {
3604
+ :a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]
3605
+ }
3606
+ @uri.query.should == "a=a&b[0][c]=c&b[0][d]=d&b[1][e]=e&b[1][f]=f"
3607
+ end
3608
+
3609
+ it "should correctly assign " +
3610
+ "{:a => 'a', :b => [{:c => true, :d => 'd'}, {:e => 'e', :f => 'f'}]}" do
3611
+ @uri.query_values = {
3612
+ :a => 'a', :b => [{:c => true, :d => 'd'}, {:e => 'e', :f => 'f'}]
3613
+ }
3614
+ @uri.query.should == "a=a&b[0][c]&b[0][d]=d&b[1][e]=e&b[1][f]=f"
3615
+ end
3616
+
3617
+ it "should correctly assign " +
3618
+ "{:a => 'a', :b => {:c => true, :d => 'd'}}" do
3619
+ @uri.query_values = {
3620
+ :a => 'a', :b => {:c => true, :d => 'd'}
3621
+ }
3622
+ @uri.query.should == "a=a&b[c]&b[d]=d"
3623
+ end
3624
+
3625
+ it "should correctly assign " +
3626
+ "{:a => 'a', :b => {:c => true, :d => 'd'}, :e => []}" do
3627
+ @uri.query_values = {
3628
+ :a => 'a', :b => {:c => true, :d => 'd'}
3629
+ }
3630
+ @uri.query.should == "a=a&b[c]&b[d]=d"
3631
+ end
3632
+ end