-
Notifications
You must be signed in to change notification settings - Fork 10
/
uri_spec.rb
108 lines (93 loc) · 3.7 KB
/
uri_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require 'spec_helper'
describe Springboard::Client::URI do
let(:uri) { described_class.parse('/relative/path') }
describe "subpath" do
it "should return a new URI with the path relative to the receiver" do
expect(uri.subpath('other')).to eq(described_class.parse('/relative/path/other'))
expect(uri.subpath('/other')).to eq(described_class.parse('/relative/path/other'))
uri.subpath(described_class.parse('/other')) == described_class.parse('/relative/path/other')
end
end
describe "parse" do
describe "when called with a URI object" do
it "should return a cloned URI object" do
parsed_uri = described_class.parse(uri)
expect(uri.class).to eq(parsed_uri.class)
expect(uri.object_id).not_to eq(parsed_uri.object_id)
end
end
describe "when called with a URI string" do
it "should return a URI based on the given string" do
uri = described_class.parse('/some_path')
expect(uri).to be_a(described_class)
expect(uri.to_s).to eq('/some_path')
end
end
end
describe "dup" do
it "should return a duplicate URI object" do
dup_uri = uri.dup
expect(uri.class).to eq(dup_uri.class)
expect(uri.to_s).to eq(dup_uri.to_s)
expect(uri.object_id).not_to eq(dup_uri.object_id)
end
describe "when mutating the copy" do
it "should not affect the original" do
dup_uri = uri.dup
dup_uri.query_values = {per_page: 1}
expect(uri.to_s).to eq('/relative/path')
expect(dup_uri.to_s).to eq('/relative/path?per_page=1')
end
end
end
describe "==" do
it "should consider two URIs parsed from the same string equal" do
expect(
described_class.parse('http://example.com/some_path?a=1&b=2') ==
described_class.parse('http://example.com/some_path?a=1&b=2')
).to be(true)
end
it "should consider two URIs parsed from different strings not equal" do
expect(
described_class.parse('http://example.com/some_path?a=1&b=2') ==
described_class.parse('http://example.com/some_path?a=1&c=3')
).to be(false)
expect(
described_class.parse('http://foo.example.com') ==
described_class.parse('http://bar.example.com')
).to be(false)
end
end
describe "merge_query_values!" do
it "should call springboard_query_values=" do
uri.query_values = {'a' => '1'}
expect(uri).to receive(:springboard_query_values=).with({'a' => '1', 'b' => '2'})
uri.merge_query_values! 'b' => '2'
end
it "should merge the given values with the existing query_values" do
uri.query_values = {'a' => '1', 'b' => '2'}
uri.merge_query_values! 'b' => '20', 'c' => '30'
expect(uri.query_values).to eq({'a' => '1', 'b' => '20', 'c' => '30'})
end
it "should set the given values if there are no existing query_values" do
expect(uri.query_values).to be_nil
uri.merge_query_values! 'b' => '20', 'c' => '30'
expect(uri.query_values).to eq({'b' => '20', 'c' => '30'})
end
end
describe "springboard_query_values=" do
it "should preserve empty bracket notation for array params" do
uri.query = 'sort[]=f1&sort[]=f2'
uri.__send__(:springboard_query_values=, uri.query_values)
expect(uri.to_s).to eq('/relative/path?sort[]=f1&sort[]=f2')
end
it "should stringify boolean param values" do
uri.__send__(:springboard_query_values=, {:p1 => true, :p2 => false})
expect(uri.to_s).to eq('/relative/path?p1=true&p2=false')
end
it "should support hash param values" do
uri.__send__(:springboard_query_values=, {:a => {:b => {:c => 123}}})
expect(uri.to_s).to eq('/relative/path?a[b][c]=123')
end
end
end