bubble-wrap 0.4.0 → 1.0.0.pre

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.
File without changes
@@ -13,13 +13,13 @@ describe BubbleWrap::App do
13
13
 
14
14
  describe '.notification_center' do
15
15
  it 'should be a NSNotificationCenter' do
16
- BW::App.notification_center.class.should == NSNotificationCenter
16
+ BW::App.notification_center.should == NSNotificationCenter.defaultCenter
17
17
  end
18
18
  end
19
19
 
20
20
  describe '.user_cache' do
21
21
  it 'should be a NSUserDefaults' do
22
- BW::App.user_cache.class.should == NSUserDefaults
22
+ BW::App.user_cache.should == NSUserDefaults.standardUserDefaults
23
23
  end
24
24
  end
25
25
 
@@ -55,6 +55,9 @@ describe BubbleWrap::App do
55
55
  it 'returns a hash' do
56
56
  BW::App.states.class.should == Hash
57
57
  end
58
+ it "returns the real instance variable" do
59
+ BW::App.states.should == BW::App.instance_variable_get(:@states)
60
+ end
58
61
  end
59
62
 
60
63
  describe '.name' do
@@ -70,14 +73,21 @@ describe BubbleWrap::App do
70
73
  end
71
74
 
72
75
  describe '.frame' do
73
- it 'returns a CGRect' do
74
- BW::App.frame.class.should == CGRect
76
+ it 'returns Application Frame' do
77
+ BW::App.frame.should == UIScreen.mainScreen.applicationFrame
75
78
  end
76
79
  end
77
80
 
81
+ describe '.bounds' do
82
+ it 'returns Main Screen bounds' do
83
+ BW::App.bounds.should == UIScreen.mainScreen.bounds
84
+ end
85
+ end
86
+
87
+
78
88
  describe '.delegate' do
79
89
  it 'returns a TestSuiteDelegate' do
80
- BW::App.delegate.class.should == TestSuiteDelegate
90
+ BW::App.delegate.should == UIApplication.sharedApplication.delegate
81
91
  end
82
92
  end
83
93
 
@@ -111,42 +111,42 @@ describe BubbleWrap::Device::Screen do
111
111
  end
112
112
  end
113
113
 
114
- describe '.widthForOrientation' do
114
+ describe '.width_for_orientation' do
115
115
  describe ':landscape_left' do
116
116
  it 'returns the current device screen height' do
117
- BW::Device::Screen.widthForOrientation(:landscape_left).should == BW::Device::Screen.height
117
+ BW::Device::Screen.width_for_orientation(:landscape_left).should == BW::Device::Screen.height
118
118
  end
119
119
  end
120
120
 
121
121
  describe ':landscape_right' do
122
122
  it 'returns the current device screen height' do
123
- BW::Device::Screen.widthForOrientation(:landscape_right).should == BW::Device::Screen.height
123
+ BW::Device::Screen.width_for_orientation(:landscape_right).should == BW::Device::Screen.height
124
124
  end
125
125
  end
126
126
 
127
127
  describe 'default' do
128
128
  it 'returns the current device screen width' do
129
- BW::Device::Screen.widthForOrientation.should == BW::Device::Screen.width
129
+ BW::Device::Screen.width_for_orientation.should == BW::Device::Screen.width
130
130
  end
131
131
  end
132
132
  end
133
133
 
134
- describe '.heightForOrientation' do
134
+ describe '.height_for_orientation' do
135
135
  describe ':landscape_left' do
136
136
  it 'returns the current device screen width' do
137
- BW::Device::Screen.heightForOrientation(:landscape_left).should == BW::Device::Screen.width
137
+ BW::Device::Screen.height_for_orientation(:landscape_left).should == BW::Device::Screen.width
138
138
  end
139
139
  end
140
140
 
141
141
  describe ':landscape_right' do
142
142
  it 'returns the current device screen width' do
143
- BW::Device::Screen.heightForOrientation(:landscape_right).should == BW::Device::Screen.width
143
+ BW::Device::Screen.height_for_orientation(:landscape_right).should == BW::Device::Screen.width
144
144
  end
145
145
  end
146
146
 
147
147
  describe 'default' do
148
148
  it 'returns the current device screen height' do
149
- BW::Device::Screen.heightForOrientation.should == BW::Device::Screen.height
149
+ BW::Device::Screen.height_for_orientation.should == BW::Device::Screen.height
150
150
  end
151
151
  end
152
152
  end
@@ -0,0 +1,191 @@
1
+ describe "KVOHelper" do
2
+
3
+ class KvoExample
4
+ include BW::KVO
5
+
6
+ attr_accessor :label
7
+ attr_accessor :items
8
+
9
+ def initialize
10
+ @items = [ "Apple", "Banana", "Chickpeas" ]
11
+
12
+ @label = UILabel.alloc.initWithFrame [[0,0],[320, 30]]
13
+ @label.text = "Foo"
14
+ end
15
+
16
+ # Test helper
17
+
18
+ def observe_label(&block)
19
+ observe(@label, "text", &block)
20
+ end
21
+
22
+ def observe_collection(&block)
23
+ observe(self, "items", &block)
24
+ end
25
+
26
+ def unobserve_label
27
+ unobserve(@label, "text")
28
+ end
29
+
30
+ # def unobserve_all
31
+ #unobserve(@label, "text")
32
+ #unobserve(self, "items")
33
+ #end
34
+
35
+ end
36
+
37
+
38
+ describe "Callbacks" do
39
+ before do
40
+ @example = KvoExample.new
41
+ end
42
+
43
+ after do
44
+ @example = nil
45
+ end
46
+
47
+ # add
48
+
49
+ it "should add an observer block" do
50
+ target = Object.new
51
+ block = lambda { |old_value, new_value| }
52
+ @example.send(:add_observer_block, target, "key_path", &block)
53
+ @example.send(:registered?, target, "key_path").should == true
54
+ end
55
+
56
+ it "should not add an observer block if the key path is not present" do
57
+ target = Object.new
58
+ block = lambda { |old_value, new_value| }
59
+ @example.send(:add_observer_block, target, nil, &block)
60
+ @example.send(:registered?, target, nil).should == false
61
+ end
62
+
63
+ it "should not add an observer block if the block is not present" do
64
+ target = Object.new
65
+ @example.send(:add_observer_block, target, "key_path")
66
+ @example.send(:registered?, target, "key_path").should == false
67
+ end
68
+
69
+ # remove
70
+
71
+ it "should remove an observer block" do
72
+ target = Object.new
73
+ block = lambda { |old_value, new_value| }
74
+ @example.send(:add_observer_block, target, "key_path", &block)
75
+ @example.send(:remove_observer_block, target, "key_path")
76
+ @example.send(:registered?, target, "key_path").should == false
77
+ end
78
+
79
+ it "should not remove an observer block if the target is not present" do
80
+ target = Object.new
81
+ block = lambda { |old_value, new_value| }
82
+ @example.send(:add_observer_block, target, "key_path", &block)
83
+ @example.send(:remove_observer_block, nil, "key_path")
84
+ @example.send(:registered?, target, "key_path").should == true
85
+ end
86
+
87
+ it "should not remove an observer block if the key path is not present" do
88
+ target = Object.new
89
+ block = lambda { |old_value, new_value| }
90
+ @example.send(:add_observer_block, target, "key_path", &block)
91
+ @example.send(:remove_observer_block, target, nil)
92
+ @example.send(:registered?, target, "key_path").should == true
93
+ end
94
+
95
+ it "should remove only one observer block" do
96
+ target = Object.new
97
+ block = lambda { |old_value, new_value| }
98
+ @example.send(:add_observer_block, target, "key_path1", &block)
99
+ @example.send(:add_observer_block, target, "key_path2", &block)
100
+ @example.send(:remove_observer_block, target, "key_path1")
101
+ @example.send(:registered?, target, "key_path1").should == false
102
+ @example.send(:registered?, target, "key_path2").should == true
103
+ end
104
+
105
+ # remove all
106
+
107
+ it "should remove all observer blocks" do
108
+ target = Object.new
109
+ block = lambda { |old_value, new_value| }
110
+ @example.send(:add_observer_block, target, "key_path1", &block)
111
+ @example.send(:add_observer_block, target, "key_path2", &block)
112
+ @example.send(:remove_all_observer_blocks)
113
+ @example.send(:registered?, target, "key_path1").should == false
114
+ @example.send(:registered?, target, "key_path2").should == false
115
+ end
116
+
117
+ end
118
+
119
+ describe "API" do
120
+ before do
121
+ @example = KvoExample.new
122
+ end
123
+
124
+ after do
125
+ @example.unobserve_all
126
+ @example = nil
127
+ end
128
+
129
+ # observe
130
+
131
+ it "should observe a key path" do
132
+ observed = false
133
+ @example.observe_label do |old_value, new_value|
134
+ observed = true
135
+ old_value.should == "Foo"
136
+ new_value.should == "Bar"
137
+ end
138
+
139
+ @example.label.text = "Bar"
140
+ observed.should == true
141
+ end
142
+
143
+ it "should observe a key path with more than one block" do
144
+ observed_one = false
145
+ observed_two = false
146
+ observed_three = false
147
+ @example.observe_label do |old_value, new_value|
148
+ observed_one = true
149
+ end
150
+ @example.observe_label do |old_value, new_value|
151
+ observed_two = true
152
+ end
153
+ @example.observe_label do |old_value, new_value|
154
+ observed_three = true
155
+ end
156
+
157
+ @example.label.text = "Bar"
158
+ observed_one.should == true
159
+ observed_two.should == true
160
+ observed_three.should == true
161
+ end
162
+
163
+ # unobserve
164
+
165
+ it "should unobserve a key path" do
166
+ observed = false
167
+ @example.observe_label do |old_value, new_value|
168
+ observed = true
169
+ end
170
+ @example.unobserve_label
171
+
172
+ @example.label.text = "Bar"
173
+ observed.should == false
174
+ end
175
+
176
+ end
177
+
178
+ =begin
179
+ it "should be able to observe a collection" do
180
+ observed = false
181
+ @example.observe_collection do |old_value, new_value, indexes|
182
+ puts "#{collection} #{old_value} #{new_value} #{indexes}"
183
+ observed = true
184
+ end
185
+
186
+ @example.items << "Dragonfruit"
187
+ observed.should == true
188
+ end
189
+ =end
190
+
191
+ end
@@ -9,7 +9,6 @@ describe "NSIndexPathWrap" do
9
9
  end
10
10
 
11
11
  it "should support the each iterator" do
12
- puts @index.inspect
13
12
  i = []
14
13
  @index.each do |idx|
15
14
  i << idx
@@ -1,20 +1,42 @@
1
1
  describe BubbleWrap::Persistence do
2
2
 
3
3
  describe '.app_key' do
4
+
5
+ it "caches the @app_key" do
6
+ BubbleWrap::Persistence.instance_variable_get(:@app_key).should.equal nil
7
+ BubbleWrap::Persistence.app_key
8
+ BubbleWrap::Persistence.instance_variable_get(:@app_key).should.not.equal nil
9
+ end
10
+
4
11
  it 'delegates to BubbleWrap::App.idenfitier' do
5
12
  BubbleWrap::Persistence.app_key.should == BubbleWrap::App.identifier
6
13
  end
14
+
7
15
  end
8
16
 
9
- it 'can persist simple objects' do
10
- lambda do
17
+
18
+ describe "storing objects" do
19
+ it 'can persist simple objects' do
20
+ lambda do
21
+ BubbleWrap::Persistence['arbitraryNumber'] = 42
22
+ end.
23
+ should.not.raise(Exception)
24
+ end
25
+
26
+ it "must call synchronize" do
27
+ storage = NSUserDefaults.standardUserDefaults
28
+ def storage.synchronize; @sync_was_called = true; end
29
+
11
30
  BubbleWrap::Persistence['arbitraryNumber'] = 42
12
- end.
13
- should.not.raise(Exception)
31
+ storage.instance_variable_get(:@sync_was_called).should.equal true
32
+ end
33
+
14
34
  end
15
35
 
16
- it 'can retrieve persisted objects' do
17
- BubbleWrap::Persistence['arbitraryNumber'].should == 42
36
+ describe "retrieving objects" do
37
+ it 'can retrieve persisted objects' do
38
+ BubbleWrap::Persistence['arbitraryNumber'].should == 42
39
+ end
18
40
  end
19
41
 
20
42
  end
@@ -1,5 +1,20 @@
1
1
  describe "Time" do
2
2
 
3
+ describe "Caching the date formatter" do
4
+
5
+ it "should reuse the created formatter" do
6
+ #this is a bad test, relies on implementation.
7
+ #I'll rewrite it when the better idea comes on my mind..
8
+ 100.times do
9
+ Time.iso8601("2011-04-11T13:22:21Z")
10
+ end
11
+ Thread.current[:date_formatters].count.should.equal 1
12
+ Thread.current[:date_formatters]["yyyy-MM-dd'T'HH:mm:ss'Z'"].should.not.equal nil
13
+ end
14
+
15
+ end
16
+
17
+
3
18
  describe "parsing an iso8601 formatted time to a Time object" do
4
19
  before do
5
20
  @time = Time.iso8601("2012-05-31T19:41:33Z")
data/spec/http_spec.rb CHANGED
@@ -1,385 +1,539 @@
1
1
  describe "HTTP" do
2
2
 
3
-
4
-
5
- describe "HTTP::Response" do
6
3
  before do
7
- @response = BubbleWrap::HTTP::Response.new({ status_code: 200, url: 'http://localhost' })
4
+ @localhost_url = 'http://localhost'
8
5
  end
9
6
 
10
- it 'should turn the initialization Hash to instance variables' do
11
- @response.instance_variable_get(:@status_code).should == 200
12
- @response.instance_variable_get(:@url).should == 'http://localhost'
13
- end
7
+ describe "Core HTTP method calls" do
14
8
 
9
+ def test_http_method(method)
10
+ called = false
11
+ delegator = Proc.new { |r, q| @the_response = r; @the_query = q; called = true }
12
+ query = BubbleWrap::HTTP.send(method, @localhost_url, { name: 'bubble-wrap', action: delegator })
13
+ query.should.not.equal nil
14
+ query.method.should.equal method.to_s.upcase
15
+ query.options[:name].should.equal 'bubble-wrap'
16
+ query.instance_variable_get(:@delegator).should.equal delegator
15
17
 
16
- it "says OK status code 20x" do
17
- @response.ok?.should.equal true
18
- (200..206).each do |code|
19
- BubbleWrap::HTTP::Response.new({status_code: code}).ok?.should.be.true
20
- end
21
- [100..101, 300..307, 400..417, 500..505].inject([]){|codes, rg| codes += rg.to_a}.each do |code|
22
- BubbleWrap::HTTP::Response.new({status_code: code}).ok?.should.be.false
18
+ query.connectionDidFinishLoading(query.connection)
19
+ query.should.be.same_as @the_query
20
+ query.response.should.be.same_as @the_response
21
+ called.should.equal true
23
22
  end
24
- end
25
23
 
26
-
27
- it "has appropriate attributes" do
28
- @response.should.respond_to :body
29
- @response.should.respond_to :headers
30
- @response.should.respond_to :url
31
- @response.should.respond_to :status_code=
32
- @response.should.respond_to :error_message=
33
- end
24
+ it ".get .post .put .delete .head .patch should properly generate the HTTP::Query" do
25
+ [:get, :post, :put, :delete, :head, :patch].each do |method|
26
+ test_http_method method
27
+ end
28
+ end
34
29
 
35
- end
30
+ it "uses the block instead of action passed in " do
31
+ [:get, :post, :put, :delete, :head, :patch].each do |method|
32
+ called = false
33
+ expected_delegator = Proc.new {|response| called = true }
36
34
 
37
- describe "HTTP::Query" do
35
+ query = BubbleWrap::HTTP.send(method, @localhost_url, { action: 'not_valid' }, &expected_delegator)
36
+ query.instance_variable_get(:@delegator).should.equal expected_delegator
37
+ query.connectionDidFinishLoading(query.connection)
38
+ called.should.equal true
39
+ end
40
+ end
38
41
 
39
- before do
40
- @credentials = { credit_card: 23423948234 }
41
- @payload = {
42
- user: { name: 'marin', surname: 'usalj' },
43
- twitter: '@mneorr',
44
- website: 'mneorr.com',
45
- values: [1, 2, 3],
46
- credentials: @credentials
47
- }
48
- @action = lambda{|fa, ke|}
49
- @cache_policy = 24234
50
- @leftover_option = 'trololo'
51
- @headers = { 'User-Agent' => "Mozilla/5.0 (X11; Linux x86_64; rv:12.0) \n Gecko/20100101 Firefox/12.0" }
52
- @options = { action: @action,
53
- payload: @payload,
54
- credentials: @credentials,
55
- headers: @headers,
56
- cache_policy: @cache_policy,
57
- leftover_option: @leftover_option
58
- }
59
- @query = BubbleWrap::HTTP::Query.new( 'http://localhost' , :get, @options )
60
- end
42
+ [:get, :post, :put, :delete, :head, :patch].each do |verb|
43
+ it "has access to the proper response scope for #{verb} request" do
44
+ class WatchedObj; attr_accessor :test_value end
45
+ @watched_object = WatchedObj.new
46
+ @name = 'Matt'
47
+ query = BubbleWrap::HTTP.send(verb, @localhost_url) do |response|
48
+ @watched_object.test_value = @name
49
+ end
50
+ wait_for_change(@watched_object, 'test_value') do
51
+ @watched_object.test_value.should == 'Matt'
52
+ end
53
+ end
54
+ end
61
55
 
62
- it "has appropriate attributes" do
63
- @query.should.respond_to :request=
64
- @query.should.respond_to :connection=
65
- @query.should.respond_to :credentials=
66
- @query.should.respond_to :proxy_credential=
67
- @query.should.respond_to :post_data=
68
-
69
- @query.should.respond_to :method
70
- @query.should.respond_to :response
71
- @query.should.respond_to :status_code
72
- @query.should.respond_to :response_headers
73
- @query.should.respond_to :response_size
74
- @query.should.respond_to :options
75
56
  end
76
57
 
77
- describe "When initialized" do
78
58
 
79
- it "should upcase the HTTP method" do
80
- @query.method.should.equal "GET"
59
+ describe "HTTP::Response" do
60
+ before do
61
+ @response = BubbleWrap::HTTP::Response.new({ status_code: 200, url: 'http://localhost' })
81
62
  end
82
63
 
83
- it "should set the deleted delegator from options" do
84
- @query.instance_variable_get(:@delegator).should.equal @action
85
- @options.should.not.has_key? :action
64
+ it 'should turn the initialization Hash to instance variables' do
65
+ @response.instance_variable_get(:@status_code).should == 200
66
+ @response.instance_variable_get(:@url).should == 'http://localhost'
86
67
  end
87
68
 
88
- it "should set self as the delegator if action not passed in" do
89
- new_query = BubbleWrap::HTTP::Query.new( 'http://localhost', :get, {})
90
- new_query.instance_variable_get(:@delegator).should.equal new_query
69
+ it "says OK status code 20x" do
70
+ @response.ok?.should.equal true
71
+ (200..209).each do |code|
72
+ BubbleWrap::HTTP::Response.new({status_code: code}).ok?.should.be.true
73
+ end
74
+ [100..101, 300..307, 400..417, 500..505].inject([]){|codes, rg| codes += rg.to_a}.each do |code|
75
+ BubbleWrap::HTTP::Response.new({status_code: code}).ok?.should.be.false
76
+ end
91
77
  end
92
78
 
93
- it "should merge :username and :password in loaded credentials" do
94
- @query.credentials.should.equal @credentials.merge({:username => '', :password => ''})
79
+ it "updates ivars when calling update" do
80
+ @response.update( { one: 'one', two: 'two' } )
81
+ @response.instance_variable_get(:@one).should.equal 'one'
82
+ @response.instance_variable_get(:@two).should.equal 'two'
95
83
 
96
- new_credentials = {:username => 'user', :password => 'pass'}
97
- options = { credentials: new_credentials }
98
- new_query = BubbleWrap::HTTP::Query.new( 'http://localhost', :get, options)
99
-
100
- new_query.credentials.should.equal new_credentials
101
- options.should.be.empty
84
+ @response.update( { one: 'three', two: 'four' } )
85
+ @response.instance_variable_get(:@one).should.equal 'three'
86
+ @response.instance_variable_get(:@two).should.equal 'four'
102
87
  end
103
88
 
104
- it "should set payload from options{} to @payload" do
105
- payload = "user[name]=marin&user[surname]=usalj&twitter=@mneorr&website=mneorr.com&values=[1, 2, 3]&credentials[credit_card]=23423948234"
106
- @query.instance_variable_get(:@payload).should.equal payload
107
- @options.should.not.has_key? :payload
89
+ it "has appropriate attributes" do
90
+ @response.should.respond_to :body
91
+ @response.should.respond_to :headers
92
+ @response.should.respond_to :url
93
+ @response.should.respond_to :status_code=
94
+ @response.should.respond_to :error_message=
108
95
  end
109
96
 
110
- it "should set default timeout to 30s or the one from hash" do
111
- @query.instance_variable_get(:@timeout).should == 30
112
-
113
- options = {timeout: 10}
114
- new_query = BubbleWrap::HTTP::Query.new( 'http://localhost/', :get, options)
115
-
116
- new_query.instance_variable_get(:@timeout).should == 10
117
- options.should.be.empty
118
- end
97
+ end
119
98
 
120
- it "should delete :headers from options and escape Line Feeds" do
121
- gsubbed = @headers['User-Agent'].gsub("\n", '\\n')
122
- @headers['User-Agent'] = gsubbed
123
- @query.instance_variable_get(:@headers).should.equal @headers
124
- end
99
+ describe "HTTP::Query" do
100
+
101
+ before do
102
+ @credentials = { username: 'mneorr', password: '123456xx!@crazy' }
103
+ @payload = {
104
+ user: { name: 'marin', surname: 'usalj' },
105
+ twitter: '@mneorr',
106
+ website: 'mneorr.com',
107
+ values: ['apple', 'orange', 'peach'],
108
+ credentials: @credentials
109
+ }
110
+ @action = lambda{|fa, ke|}
111
+ @cache_policy = 24234
112
+ @leftover_option = 'trololo'
113
+ @headers = { 'User-Agent' => "Mozilla/5.0 (X11; Linux x86_64; rv:12.0) \n Gecko/20100101 Firefox/12.0" }
114
+ @options = { action: @action,
115
+ payload: @payload,
116
+ credentials: @credentials,
117
+ headers: @headers,
118
+ cache_policy: @cache_policy,
119
+ leftover_option: @leftover_option
120
+ }
121
+ @query = BubbleWrap::HTTP::Query.new( @localhost_url , :get, @options )
122
+ end
123
+
124
+ it "has appropriate attributes" do
125
+ @query.should.respond_to :request=
126
+ @query.should.respond_to :connection=
127
+ @query.should.respond_to :credentials=
128
+ @query.should.respond_to :proxy_credential=
129
+ @query.should.respond_to :post_data=
130
+
131
+ @query.should.respond_to :method
132
+ @query.should.respond_to :response
133
+ @query.should.respond_to :status_code
134
+ @query.should.respond_to :response_headers
135
+ @query.should.respond_to :response_size
136
+ @query.should.respond_to :options
137
+ end
138
+
139
+ describe "When initialized" do
140
+
141
+ it "should upcase the HTTP method" do
142
+ @query.method.should.equal "GET"
143
+ end
144
+
145
+ it "should set the deleted delegator from options" do
146
+ @query.instance_variable_get(:@delegator).should.equal @action
147
+ @options.should.not.has_key? :action
148
+ end
149
+
150
+ it "should set self as the delegator if action was not passed in" do
151
+ new_query = BubbleWrap::HTTP::Query.new( 'http://localhost', :get, {})
152
+ new_query.instance_variable_get(:@delegator).should.equal new_query
153
+ end
154
+
155
+ it "should merge :username and :password in loaded credentials" do
156
+ @query.credentials.should.equal @credentials
157
+
158
+ options = { credentials: {} }
159
+ new_query = BubbleWrap::HTTP::Query.new( @localhost_url, :get, options)
160
+
161
+ generated_credentials = {:username => '', :password => ''}
162
+ new_query.credentials.should.equal generated_credentials
163
+ options.should.be.empty
164
+ end
165
+
166
+ it "should set payload from options{} to @payload" do
167
+ payload = "user[name]=marin&user[surname]=usalj&twitter=@mneorr&website=mneorr.com&values[]=apple&values[]=orange&values[]=peach&credentials[username]=mneorr&credentials[password]=123456xx!@crazy"
168
+ @query.instance_variable_get(:@payload).should.equal payload
169
+ @options.should.not.has_key? :payload
170
+ end
171
+
172
+ it "should set default timeout to 30s or the one from hash" do
173
+ @query.instance_variable_get(:@timeout).should == 30
174
+
175
+ options = {timeout: 10}
176
+ new_query = BubbleWrap::HTTP::Query.new( @localhost_url, :get, options)
177
+
178
+ new_query.instance_variable_get(:@timeout).should == 10
179
+ options.should.be.empty
180
+ end
181
+
182
+ it "should delete :headers from options and escape Line Feeds" do
183
+ escaped_lf = {"User-Agent"=>"Mozilla/5.0 (X11; Linux x86_64; rv:12.0) \\n Gecko/20100101 Firefox/12.0"}
184
+ @query.instance_variable_get(:@headers).should.equal escaped_lf
185
+ end
186
+
187
+ it "should delete :cache_policy or set NSURLRequestUseProtocolCachePolicy" do
188
+ @query.instance_variable_get(:@cache_policy).should.equal @cache_policy
189
+ @options.should.not.has_key? :cache_policy
190
+
191
+ new_query = BubbleWrap::HTTP::Query.new( @localhost_url, :get, {})
192
+ new_query.instance_variable_get(:@cache_policy).should.equal NSURLRequestUseProtocolCachePolicy
193
+ end
194
+
195
+ it "should set the rest of options{} to ivar @options" do
196
+ @query.options.size.should.equal 1
197
+ @query.options.values[0].should.equal @leftover_option
198
+ end
199
+
200
+ it "should create a new response before instantiating a new request" do
201
+ @query.response.should.not.equal nil
202
+ end
203
+
204
+ it "should call initiate_request with the URL passed in" do
205
+ processed_url = "http://localhost?user%5Bname%5D=marin&user%5Bsurname%5D=usalj&twitter=@mneorr&website=mneorr.com&values%5B%5D=apple&values%5B%5D=orange&values%5B%5D=peach&credentials%5Busername%5D=mneorr&credentials%5Bpassword%5D=123456xx!@crazy"
206
+ @query.instance_variable_get(:@url).description.should.equal processed_url
207
+ end
208
+
209
+ it "should start the connection" do
210
+ @query.connection.was_started.should.equal true
211
+ end
212
+
213
+ it "should turn on the network indicator" do
214
+ UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should.equal true
215
+ end
125
216
 
126
- it "should delete :cache_policy or set NSURLRequestUseProtocolCachePolicy" do
127
- @query.instance_variable_get(:@cachePolicy).should.equal @cache_policy
128
- @options.should.not.has_key? :cache_policy
129
-
130
- new_query = BubbleWrap::HTTP::Query.new( 'http://fakehost.local/', :get, {})
131
- new_query.instance_variable_get(:@cachePolicy).should.equal NSURLRequestUseProtocolCachePolicy
132
217
  end
133
218
 
134
- it "should set the rest of options{} to ivar @options" do
135
- @query.options.size.should.equal 1
136
- @query.options.values[0].should.equal @leftover_option
137
- end
219
+ describe "initiate request" do
138
220
 
139
- it "should create a new response before instantiating a new request" do
140
- @query.response.should.not.equal nil
141
- end
221
+ before do
222
+ @url_string = 'http://initiated-request.dev/to convert'
223
+ @payload = { name: 'apple', model: 'macbook'}
224
+ @headers = { fake: 'headers' }
225
+ @get_query = BubbleWrap::HTTP::Query.new( @url_string , :get, { headers: @headers, payload: @payload } )
226
+ @get_query.initiate_request @url_string
227
+ end
142
228
 
143
- it "should call initiate_request with the URL passed in" do
144
- processed_url = "http://localhost?user%5Bname%5D=marin&user%5Bsurname%5D=usalj&twitter=@mneorr&website=mneorr.com&values=%5B1,%202,%203%5D&credentials%5Bcredit_card%5D=23423948234"
145
- @query.instance_variable_get(:@url).description.should.equal processed_url
146
- end
229
+ it "should check if @payload is a hash before generating params" do
230
+ @get_query.instance_variable_get(:@payload).should.equal 'name=apple&model=macbook'
147
231
 
148
- it "should start the connection" do
149
- @query.connection.was_started.should.equal true
150
- end
232
+ query_string_payload = BubbleWrap::HTTP::Query.new( 'nil' , :get, { payload: "name=apple&model=macbook"} )
233
+ query_string_payload.instance_variable_get(:@payload).should.equal 'name=apple&model=macbook'
234
+ end
151
235
 
152
- it "should turn on the network indicator" do
153
- UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should.equal true
154
- end
236
+ it "should check if payload is nil" do
237
+ nil_payload = BubbleWrap::HTTP::Query.new( 'nil' , :post, {} )
238
+ lambda{ nil_payload.initiate_request('fake') }.should.not.raise NoMethodError
239
+ end
155
240
 
156
- end
241
+ it "should set the payload in URL only for GET request" do
242
+ [:post, :put, :delete, :head, :patch].each do |method|
243
+ query = BubbleWrap::HTTP::Query.new( @localhost_url , method, { payload: @payload } )
244
+ query.instance_variable_get(:@url).description.should.equal @localhost_url
245
+ end
246
+ end
157
247
 
158
- describe "initiate request" do
248
+ it "sets the HTTPBody DATA to @request for all methods except GET" do
159
249
 
160
- before do
161
- @url_string = 'http://initiated-request.dev'
162
-
163
- @get_query = BubbleWrap::HTTP::Query.new( 'nil' , :get, { payload: {name: 'apple', model: 'macbook'} } )
164
- @get_query.initiate_request @url_string
165
-
166
- @post_query = BubbleWrap::HTTP::Query.new( 'nil' , :post, { payload: {name: 'apple', model: 'macbook'}} )
167
- @post_query.initiate_request @url_string
168
- end
250
+ [:post, :put, :delete, :head, :patch].each do |method|
251
+ query = BubbleWrap::HTTP::Query.new( 'nil' , method, { payload: @payload } )
252
+ real_payload = NSString.alloc.initWithData(query.request.HTTPBody, encoding:NSUTF8StringEncoding)
169
253
 
170
- it "should check if @payload is a hash before generating params" do
171
- @get_query.instance_variable_get(:@payload).should.equal 'name=apple&model=macbook'
254
+ real_payload.should.equal 'name=apple&model=macbook'
255
+ end
172
256
 
173
- query_string_payload = BubbleWrap::HTTP::Query.new( 'nil' , :get, { payload: "name=apple&model=macbook"} )
174
- query_string_payload.instance_variable_get(:@payload).should.equal 'name=apple&model=macbook'
175
- end
257
+ end
176
258
 
177
- end
259
+ it "should add UTF8 escaping on the URL string" do
260
+ @get_query.instance_variable_get(:@url).description.should.equal 'http://initiated-request.dev/to%20convert?name=apple&model=macbook'
261
+ end
178
262
 
179
- describe "Generating GET params" do
180
-
181
- it "should create params with nested hashes with prefix[key]=value" do
182
- expected_params = [
183
- 'user[name]=marin',
184
- 'user[surname]=usalj',
185
- 'twitter=@mneorr',
186
- 'website=mneorr.com',
187
- 'values=[1, 2, 3]',
188
- 'credentials[credit_card]=23423948234'
189
- ]
190
- @query.generate_get_params(@payload).should.equal expected_params
191
- end
263
+ it "should create a new request with HTTP method & header fields" do
264
+ @query.request.HTTPMethod.should.equal @query.method
265
+ @get_query.request.allHTTPHeaderFields.should.equal @headers
266
+ end
192
267
 
193
- end
268
+ it "creates a new NSURLConnection and sets itself as a delegate" do
269
+ @query.connection.delegate.should.equal @query
270
+ end
194
271
 
195
- describe "when didReceiveResponse:" do
272
+ it "should pass the new request in the new connection" do
273
+ @query.connection.request.URL.description.should.equal @query.request.URL.description
274
+ end
196
275
 
197
- it "should assign status_code, headers and response_size" do
198
- headers = { foo: 'bar' }
199
- status_code = 234
200
- length = 123.53
276
+ it "should patch the NSURLRequest with done_loading and done_loading!" do
277
+ @query.request.done_loading.should.equal @query.request.instance_variable_get(:@done_loading)
278
+
279
+ @query.request.instance_variable_set(:@done_loading, false)
280
+ @query.request.done_loading.should.equal false
281
+ @query.request.done_loading!
282
+ @query.request.done_loading.should.equal true
283
+ end
201
284
 
202
- response = FakeURLResponse.new(status_code, headers, length)
203
- @query.connection(nil, didReceiveResponse:response)
285
+ it "should pass the right arguments when creating new request" do
286
+ @query.request.cachePolicy.should.equal @query.instance_variable_get(:@cache_policy)
287
+ @query.request.timeoutInterval.should.equal @query.instance_variable_get(:@timeout)
288
+ end
204
289
 
205
- @query.status_code.should.equal status_code
206
- @query.response_headers.should.equal headers
207
- @query.response_size.should.equal length
208
290
  end
209
291
 
210
- end
292
+ describe "Generating GET params" do
211
293
 
212
- describe "when didRecieveData:" do
294
+ it "should create params with nested hashes with prefix[key]=value" do
295
+ expected_params = [
296
+ 'user[name]=marin',
297
+ 'user[surname]=usalj',
298
+ 'twitter=@mneorr',
299
+ 'website=mneorr.com',
300
+ 'values[]=apple',
301
+ 'values[]=orange',
302
+ 'values[]=peach',
303
+ "credentials[username]=mneorr",
304
+ "credentials[password]=123456xx!@crazy"
305
+ ]
306
+ @query.generate_params(@payload).should.equal expected_params
307
+ end
213
308
 
214
- def query_received_data
215
- @query.instance_variable_get(:@received_data)
216
309
  end
217
310
 
218
- it "should initialize @received_data and append the received data" do
219
- query_received_data.should.equal nil
220
- data = NSData.dataWithBytesNoCopy(Pointer.new(:char, 'abc'), length:24)
311
+ describe "when didReceiveResponse:" do
221
312
 
222
- @query.connection(nil, didReceiveData:nil)
223
- query_received_data.should.not.equal nil
313
+ it "should assign status_code, headers and response_size" do
314
+ headers = { foo: 'bar' }
315
+ status_code = 234
316
+ length = 123.53
224
317
 
225
- @query.connection(nil, didReceiveData:data)
226
- query_received_data.length.should.equal 24
318
+ response = FakeURLResponse.new(status_code, headers, length)
319
+ @query.connection(nil, didReceiveResponse:response)
227
320
 
228
- @query.connection(nil, didReceiveData:data)
229
- query_received_data.length.should.equal 48
230
- end
321
+ @query.status_code.should.equal status_code
322
+ @query.response_headers.should.equal headers
323
+ @query.response_size.should.equal length
324
+ end
231
325
 
232
- end
326
+ end
233
327
 
328
+ describe "when didRecieveData:" do
234
329
 
330
+ def query_received_data
331
+ @query.instance_variable_get(:@received_data)
332
+ end
235
333
 
236
- describe "when requestDidFailWithError:" do
237
- before do
238
- @fake_error = NSError.errorWithDomain('testing', code:7768, userInfo:nil)
239
- end
334
+ it "should initialize @received_data and append the received data" do
335
+ query_received_data.should.equal nil
336
+ data = NSData.dataWithBytesNoCopy(Pointer.new(:char, 'abc'), length:24)
240
337
 
241
- it "should turn off network indicator" do
242
- UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == true
338
+ @query.connection(nil, didReceiveData:nil)
339
+ query_received_data.should.not.equal nil
243
340
 
244
- @query.connection(nil, didFailWithError:@fake_error)
245
- UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == false
246
- end
341
+ @query.connection(nil, didReceiveData:data)
342
+ query_received_data.length.should.equal 24
247
343
 
248
- it "should set request_done to true" do
249
- @query.request.done_loading.should == false
344
+ @query.connection(nil, didReceiveData:data)
345
+ query_received_data.length.should.equal 48
346
+ end
250
347
 
251
- @query.connection(nil, didFailWithError:@fake_error)
252
- @query.request.done_loading.should == true
253
348
  end
254
349
 
255
- it "should set the error message to response object" do
256
- @query.response.error_message.should.equal nil
257
350
 
258
- @query.connection(nil, didFailWithError:@fake_error)
259
- @query.response.error_message.should.equal @fake_error.localizedDescription
260
- end
261
351
 
262
- it "should check if there's a callback block and pass the response in" do
263
- expected_response = BubbleWrap::HTTP::Response.new
264
- real_response = nil
265
- block = lambda{ |response, query| real_response = response }
352
+ describe "when requestDidFailWithError:" do
353
+ before do
354
+ @fake_error = NSError.errorWithDomain('testing', code:7768, userInfo:nil)
355
+ end
266
356
 
267
- query = BubbleWrap::HTTP::Query.new( 'http://localhost', :get, { :action => block })
268
- query.instance_variable_set(:@response, expected_response)
357
+ it "should turn off network indicator" do
358
+ UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == true
359
+ @query.connection(nil, didFailWithError:@fake_error)
360
+ UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == false
361
+ end
269
362
 
270
- query.connection(nil, didFailWithError:@fake_error)
271
- real_response.should.equal expected_response
272
- end
363
+ it "should set request_done to true" do
364
+ @query.request.done_loading.should == false
365
+ @query.connection(nil, didFailWithError:@fake_error)
366
+ @query.request.done_loading.should == true
367
+ end
273
368
 
274
- end
369
+ it "should set the error message to response object" do
370
+ @query.response.error_message.should.equal nil
371
+ @query.connection(nil, didFailWithError:@fake_error)
372
+ @query.response.error_message.should.equal @fake_error.localizedDescription
373
+ end
275
374
 
276
- describe "when connectionDidFinishLoading:" do
375
+ it "should check if there's a callback block and pass the response in" do
376
+ expected_response = BubbleWrap::HTTP::Response.new
377
+ real_response = nil
378
+ block = lambda{ |response, query| real_response = response }
277
379
 
278
- it "should turn off the network indicator" do
279
- UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == true
380
+ query = BubbleWrap::HTTP::Query.new(@localhost_url, :get, { :action => block })
381
+ query.instance_variable_set(:@response, expected_response)
382
+
383
+ query.connection(nil, didFailWithError:@fake_error)
384
+ real_response.should.equal expected_response
385
+ end
280
386
 
281
- @query.connectionDidFinishLoading(nil)
282
- UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == false
283
387
  end
284
388
 
285
- it "should set request_done to true" do
286
- @query.request.done_loading.should == false
389
+ describe "when connectionDidFinishLoading:" do
287
390
 
288
- @query.connectionDidFinishLoading(nil)
289
- @query.request.done_loading.should == true
290
- end
391
+ it "should turn off the network indicator" do
392
+ UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == true
291
393
 
292
- it "should set response_body to @received data if not nil" do
293
- data = NSData.dataWithBytesNoCopy(Pointer.new(:char, 'abc'), length:24)
294
- headers = { foo: 'bar' }
295
- status_code = 234
296
- response = FakeURLResponse.new(status_code, headers, 65456)
394
+ @query.connectionDidFinishLoading(nil)
395
+ UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == false
396
+ end
297
397
 
298
- @query.connection(nil, didReceiveResponse:response)
299
- @query.connection(nil, didReceiveData:data)
300
- @query.connectionDidFinishLoading(nil)
398
+ it "should set request_done to true" do
399
+ @query.request.done_loading.should == false
301
400
 
302
- @query.response.body.should.equal data
303
- @query.response.status_code.should.equal status_code
304
- @query.response.headers.should.equal headers
305
- @query.response.url.should.equal @query.instance_variable_get(:@url)
306
- end
401
+ @query.connectionDidFinishLoading(nil)
402
+ @query.request.done_loading.should == true
403
+ end
307
404
 
308
- it "should check if there's a callback block and pass the response in" do
309
- expected_response = BubbleWrap::HTTP::Response.new
310
- real_response = nil
311
- block = lambda{ |response, query| real_response = response }
405
+ it "should set response_body to @received data if not nil" do
406
+ data = NSData.dataWithBytesNoCopy(Pointer.new(:char, 'abc'), length:24)
407
+ headers = { foo: 'bar' }
408
+ status_code = 234
409
+ response = FakeURLResponse.new(status_code, headers, 65456)
312
410
 
313
- query = BubbleWrap::HTTP::Query.new( 'http://localhost', :get, { :action => block })
314
- query.instance_variable_set(:@response, expected_response)
411
+ @query.connection(nil, didReceiveResponse:response)
412
+ @query.connection(nil, didReceiveData:data)
413
+ @query.connectionDidFinishLoading(nil)
315
414
 
316
- query.connectionDidFinishLoading(nil)
317
- real_response.should.equal expected_response
318
- end
415
+ @query.response.body.should.equal data
416
+ @query.response.status_code.should.equal status_code
417
+ @query.response.headers.should.equal headers
418
+ @query.response.url.should.equal @query.instance_variable_get(:@url)
419
+ end
319
420
 
320
- end
421
+ it "should check if there's a callback block and pass the response in" do
422
+ expected_response = BubbleWrap::HTTP::Response.new
423
+ real_response = nil
424
+ block = lambda{ |response, query| real_response = response }
425
+ query = BubbleWrap::HTTP::Query.new(@localhost_url, :get, { :action => block })
426
+ query.instance_variable_set(:@response, expected_response)
427
+
428
+ query.connectionDidFinishLoading(nil)
429
+ real_response.should.equal expected_response
430
+ end
321
431
 
322
- describe "when connection:willSendRequest:redirectResponse:" do
323
- before do
324
- @request = NSURLRequest.requestWithURL NSURL.URLWithString('http://fakehost.local/')
325
432
  end
326
433
 
327
- it "should make a mutableCopy of passed in request and set headers from @headers" do
328
- expected_headers = { new_header: 'should_be_here' }
329
- @query.instance_variable_set(:@headers, expected_headers)
434
+ describe "when connection:willSendRequest:redirectResponse:" do
435
+ before do
436
+ @request = NSURLRequest.requestWithURL NSURL.URLWithString('http://fakehost.local/')
437
+ end
330
438
 
331
- new_request = @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
439
+ it "should make a mutableCopy of passed in request and set headers from @headers" do
440
+ expected_headers = { new_header: 'should_be_here' }
441
+ @query.instance_variable_set(:@headers, expected_headers)
332
442
 
333
- @query.request.should.not.be.equal @request
334
- new_request.URL.description.should.equal @request.URL.description
335
- new_request.allHTTPHeaderFields.should.equal expected_headers
336
- end
443
+ new_request = @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
337
444
 
338
- it "should create a new Connection with the request passed in" do
339
- old_connection = @query.connection
340
- @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
445
+ @query.request.should.not.be.equal @request
446
+ new_request.URL.description.should.equal @request.URL.description
447
+ new_request.allHTTPHeaderFields.should.equal expected_headers
448
+ end
341
449
 
342
- old_connection.should.not.equal @query.connection
343
- end
450
+ it "should create a new Connection with the request passed in" do
451
+ old_connection = @query.connection
452
+ @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
453
+ old_connection.should.not.equal @query.connection
454
+ end
344
455
 
345
- it "should set itself as a delegate of new NSURLConnection" do
346
- @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
347
- @query.connection.delegate.should.equal @query
456
+ it "should set itself as a delegate of new NSURLConnection" do
457
+ @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
458
+ @query.connection.delegate.should.equal @query
459
+ end
460
+
461
+ it "should pass the new request in the new connection" do
462
+ @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
463
+ @query.connection.request.URL.description.should.equal @request.URL.description
464
+ end
348
465
  end
349
466
 
350
- it "should pass the new request in the new connection" do
351
- @query.connection(nil, willSendRequest:@request, redirectResponse:nil)
352
- @query.connection.request.URL.description.should.equal @request.URL.description
467
+ describe "didReceiveAuthenticationChallenge" do
468
+ before do
469
+ @challenge = FakeChallenge.new
470
+ @challenge.previousFailureCount = 0
471
+ @query.connection(nil, didReceiveAuthenticationChallenge:@challenge)
472
+ end
473
+
474
+ it "should cancel the authentication if the failure count was not 0" do
475
+ @challenge.previousFailureCount = 1
476
+ @query.connection(nil, didReceiveAuthenticationChallenge:@challenge)
477
+ @challenge.sender.was_cancelled.should.equal true
478
+ end
479
+
480
+ it "should pass in Credentials and the challenge itself to the sender" do
481
+ @challenge.sender.challenge.should.equal @challenge
482
+ @challenge.sender.credential.user.should.equal @credentials[:username]
483
+ @challenge.sender.credential.password.should.equal @credentials[:password]
484
+ end
485
+
486
+ it "always uses NSURLCredentialPersistenceForSession" do
487
+ @challenge.sender.credential.persistence.should.equal NSURLCredentialPersistenceForSession
488
+ end
489
+
353
490
  end
354
491
 
355
- end
492
+ class FakeSender
493
+ attr_reader :challenge, :credential, :was_cancelled
494
+ def cancelAuthenticationChallenge(challenge)
495
+ @was_cancelled = true
496
+ end
497
+ def useCredential(credential, forAuthenticationChallenge:challenge)
498
+ @challenge = challenge
499
+ @credential = credential
500
+ end
501
+ end
356
502
 
357
- class BubbleWrap::HTTP::Query
358
- def create_connection(request, delegate); FakeURLConnection.new(request, delegate); end
359
- end
503
+ class FakeChallenge
504
+ attr_accessor :previousFailureCount
505
+
506
+ def sender
507
+ @fake_sender ||= FakeSender.new
508
+ end
509
+ end
360
510
 
361
- class FakeURLConnection < NSURLConnection
362
- attr_reader :delegate, :request, :was_started
363
- def initialize(request, delegate)
364
- @request = request
365
- @delegate = delegate
366
- self.class.connectionWithRequest(request, delegate:delegate)
511
+ class BubbleWrap::HTTP::Query
512
+ def create_connection(request, delegate); FakeURLConnection.new(request, delegate); end
367
513
  end
368
- def start
369
- @was_started = true
370
- super
514
+
515
+ class FakeURLConnection < NSURLConnection
516
+ attr_reader :delegate, :request, :was_started
517
+ def initialize(request, delegate)
518
+ @request = request
519
+ @delegate = delegate
520
+ self.class.connectionWithRequest(request, delegate:delegate)
521
+ end
522
+ def start
523
+ @was_started = true
524
+ super
525
+ end
371
526
  end
372
- end
373
527
 
374
- class FakeURLResponse
375
- attr_reader :statusCode, :allHeaderFields, :expectedContentLength
376
- def initialize(status_code, headers, length)
377
- @statusCode = status_code
378
- @allHeaderFields = headers
379
- @expectedContentLength = length
528
+ class FakeURLResponse
529
+ attr_reader :statusCode, :allHeaderFields, :expectedContentLength
530
+ def initialize(status_code, headers, length)
531
+ @statusCode = status_code
532
+ @allHeaderFields = headers
533
+ @expectedContentLength = length
534
+ end
380
535
  end
536
+
381
537
  end
382
538
 
383
539
  end
384
-
385
- end