sunstone 2.0.0 → 2.0.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.
- checksums.yaml +4 -4
- data/README.md +9 -2
- data/ext/active_record/associations/builder/has_and_belongs_to_many.rb +52 -48
- data/ext/active_record/batches.rb +2 -2
- data/ext/active_record/calculations.rb +28 -28
- data/ext/active_record/finder_methods.rb +36 -22
- data/ext/active_record/query_methods.rb +23 -0
- data/ext/active_record/relation.rb +6 -8
- data/ext/active_record/relation/predicate_builder.rb +29 -23
- data/ext/active_record/statement_cache.rb +21 -8
- data/lib/active_record/connection_adapters/sunstone_adapter.rb +0 -2
- data/lib/arel/visitors/sunstone.rb +9 -5
- data/lib/sunstone.rb +9 -3
- data/lib/sunstone/connection.rb +2 -3
- data/lib/sunstone/version.rb +1 -1
- data/sunstone.gemspec +3 -3
- data/test/models/ship.rb +14 -0
- data/test/query_test.rb +128 -0
- data/test/sunstone_test.rb +296 -305
- data/test/test_helper.rb +11 -25
- metadata +13 -8
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'active_record/connection_adapters/abstract_adapter'
|
2
2
|
require 'active_record/connection_adapters/statement_pool'
|
3
3
|
|
4
|
-
require 'arel/visitors/sunstone'
|
5
|
-
require 'arel/collectors/sunstone'
|
6
4
|
|
7
5
|
require 'active_record/connection_adapters/sunstone/database_statements'
|
8
6
|
require 'active_record/connection_adapters/sunstone/schema_statements'
|
@@ -397,11 +397,15 @@ module Arel
|
|
397
397
|
collector.columns << {:count => (o.expressions.first.is_a?(Arel::Attributes::Attribute) ? o.expressions.first.name : o.expressions.first) }
|
398
398
|
# collector.columns = visit o.expressions.first, collector
|
399
399
|
end
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
400
|
+
|
401
|
+
def visit_Arel_Nodes_Sum o, collector
|
402
|
+
collector.operation = :calculate
|
403
|
+
|
404
|
+
collector.columns ||= []
|
405
|
+
collector.columns << {:sum => (o.expressions.first.is_a?(Arel::Attributes::Attribute) ? o.expressions.first.name : o.expressions.first) }
|
406
|
+
# collector.columns = visit o.expressions.first, collector
|
407
|
+
end
|
408
|
+
|
405
409
|
def visit_Arel_Nodes_Max o, collector
|
406
410
|
collector.operation = :calculate
|
407
411
|
|
data/lib/sunstone.rb
CHANGED
@@ -6,11 +6,17 @@ require File.expand_path(File.join(__FILE__, '../sunstone/version'))
|
|
6
6
|
require File.expand_path(File.join(__FILE__, '../sunstone/connection'))
|
7
7
|
require File.expand_path(File.join(__FILE__, '../sunstone/exception'))
|
8
8
|
|
9
|
+
require File.expand_path(File.join(__FILE__, '../arel/visitors/sunstone'))
|
10
|
+
require File.expand_path(File.join(__FILE__, '../arel/collectors/sunstone'))
|
11
|
+
|
12
|
+
require File.expand_path(File.join(__FILE__, '../active_record/connection_adapters/sunstone_adapter'))
|
13
|
+
|
9
14
|
require File.expand_path(File.join(__FILE__, '../../ext/active_record/statement_cache'))
|
10
15
|
require File.expand_path(File.join(__FILE__, '../../ext/active_record/relation'))
|
11
|
-
require File.expand_path(File.join(__FILE__, '../../ext/active_record/relation/predicate_builder'))
|
12
|
-
require File.expand_path(File.join(__FILE__, '../../ext/active_record/calculations'))
|
13
|
-
require File.expand_path(File.join(__FILE__, '../../ext/active_record/
|
16
|
+
# require File.expand_path(File.join(__FILE__, '../../ext/active_record/relation/predicate_builder'))
|
17
|
+
# require File.expand_path(File.join(__FILE__, '../../ext/active_record/calculations'))
|
18
|
+
require File.expand_path(File.join(__FILE__, '../../ext/active_record/query_methods'))
|
19
|
+
# require File.expand_path(File.join(__FILE__, '../../ext/active_record/associations/builder/has_and_belongs_to_many'))
|
14
20
|
|
15
21
|
require File.expand_path(File.join(__FILE__, '../../ext/arel/select_manager'))
|
16
22
|
require File.expand_path(File.join(__FILE__, '../../ext/arel/nodes/eager_load'))
|
data/lib/sunstone/connection.rb
CHANGED
@@ -9,8 +9,7 @@ module Sunstone
|
|
9
9
|
class Connection
|
10
10
|
|
11
11
|
# Set the User-Agent of the client. Will be joined with other User-Agent info
|
12
|
-
|
13
|
-
attr_accessor :api_key, :host, :port, :use_ssl
|
12
|
+
attr_reader :api_key, :host, :port, :use_ssl
|
14
13
|
|
15
14
|
# Initialize a connection a Sunstone API server.
|
16
15
|
#
|
@@ -32,7 +31,7 @@ module Sunstone
|
|
32
31
|
end
|
33
32
|
|
34
33
|
[:api_key, :host, :port, :use_ssl, :user_agent].each do |key|
|
35
|
-
self.
|
34
|
+
self.instance_variable_set(:"@#{key}", config[key])
|
36
35
|
end
|
37
36
|
|
38
37
|
@connection = Net::HTTP.new(host, port)
|
data/lib/sunstone/version.rb
CHANGED
data/sunstone.gemspec
CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
s.add_runtime_dependency 'wankel'
|
32
32
|
s.add_runtime_dependency 'cookie_store'
|
33
33
|
s.add_runtime_dependency 'arel', '~> 6.0'
|
34
|
-
s.add_runtime_dependency 'activesupport', '>= 4.2.
|
35
|
-
s.add_runtime_dependency 'activemodel', '>= 4.2.
|
36
|
-
s.add_runtime_dependency 'activerecord', '>= 4.2.
|
34
|
+
s.add_runtime_dependency 'activesupport', '>= 4.2.4'
|
35
|
+
s.add_runtime_dependency 'activemodel', '>= 4.2.4'
|
36
|
+
s.add_runtime_dependency 'activerecord', '>= 4.2.4'
|
37
37
|
end
|
data/test/models/ship.rb
ADDED
data/test/query_test.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class QueryTest < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
stub_request(:get, "malomalo.io/ping").to_return(:body => "pong")
|
7
|
+
stub_request(:get, "malomalo.io/tables").to_return(:body => ['ships'].to_json)
|
8
|
+
|
9
|
+
stub_request(:get, "malomalo.io/ships/schema").to_return(:body => {
|
10
|
+
id: {type: 'integer', primary_key: true, null: false, array: false},
|
11
|
+
fleet_id: {type: 'integer', primary_key: false, null: true, array: false},
|
12
|
+
name: {type: 'string', primary_key: false, null: true, array: false}
|
13
|
+
}.to_json)
|
14
|
+
|
15
|
+
stub_request(:get, "malomalo.io/fleets/schema").to_return(:body => {
|
16
|
+
id: {type: 'integer', primary_key: true, null: false, array: false},
|
17
|
+
name: {type: 'string', primary_key: false, null: true, array: false}
|
18
|
+
}.to_json)
|
19
|
+
|
20
|
+
stub_request(:get, "malomalo.io/sailors/schema").to_return(:body => {
|
21
|
+
id: {type: 'integer', primary_key: true, null: false, array: false},
|
22
|
+
name: {type: 'string', primary_key: false, null: true, array: false}
|
23
|
+
}.to_json)
|
24
|
+
|
25
|
+
ActiveRecord::Base.establish_connection(
|
26
|
+
:adapter => 'sunstone',
|
27
|
+
:site => 'http://malomalo.io'
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
test '::find' do
|
32
|
+
stub_request(:get, URI::escape('malomalo.io/ships?limit=1&where[id]=42')).to_return(body: [{id: 42}].to_json)
|
33
|
+
|
34
|
+
assert_equal 42, Ship.find(42).id
|
35
|
+
end
|
36
|
+
|
37
|
+
test '::first' do
|
38
|
+
stub_request(:get, URI::escape('malomalo.io/ships?limit=1&order[][id]=asc')).to_return(body: [].to_json)
|
39
|
+
|
40
|
+
assert_nil Ship.first
|
41
|
+
end
|
42
|
+
|
43
|
+
test '::last' do
|
44
|
+
stub_request(:get, URI::escape('malomalo.io/ships?limit=1&order[][id]=desc')).to_return(body: [].to_json)
|
45
|
+
|
46
|
+
assert_nil Ship.last
|
47
|
+
end
|
48
|
+
|
49
|
+
test '::find_each' do
|
50
|
+
stub_request(:get, URI::escape('malomalo.io/ships?limit=1000&order[][id]=asc')).to_return(body: [].to_json)
|
51
|
+
|
52
|
+
assert_nil Ship.find_each { |s| s }
|
53
|
+
end
|
54
|
+
|
55
|
+
test '::where on columns' do
|
56
|
+
stub_request(:get, URI::escape('malomalo.io/ships?where[id]=10')).to_return(body: [].to_json)
|
57
|
+
|
58
|
+
assert_equal [], Ship.where(:id => 10).to_a
|
59
|
+
end
|
60
|
+
|
61
|
+
test '::where on belongs_to relation' do
|
62
|
+
stub_request(:get, URI::escape('malomalo.io/ships?where[fleet][id]=1')).to_return(body: [].to_json)
|
63
|
+
|
64
|
+
assert_equal [], Ship.where(:fleet => {id: 1}).to_a
|
65
|
+
end
|
66
|
+
|
67
|
+
test '::where on has_many relation' do
|
68
|
+
stub_request(:get, URI::escape('malomalo.io/fleets?where[ships][id]=1')).to_return(body: [].to_json)
|
69
|
+
|
70
|
+
assert_equal [], Fleet.where(:ships => {id: 1}).to_a
|
71
|
+
end
|
72
|
+
|
73
|
+
test '::where on has_and_belongs_to_many relation' do
|
74
|
+
stub_request(:get, URI::escape('malomalo.io/ships?where[sailors][id]=1')).to_return(body: [].to_json)
|
75
|
+
|
76
|
+
assert_equal [], Ship.where(:sailors => {id: 1}).to_a
|
77
|
+
end
|
78
|
+
|
79
|
+
test '::count' do
|
80
|
+
stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][count]=*')).to_return(body: '[10]')
|
81
|
+
|
82
|
+
assert_equal 10, Ship.count
|
83
|
+
end
|
84
|
+
|
85
|
+
test '::count(:column)' do
|
86
|
+
stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][count]=id')).to_return(body: '[10]')
|
87
|
+
|
88
|
+
assert_equal 10, Ship.count(:id)
|
89
|
+
end
|
90
|
+
|
91
|
+
test '::sum(:column)' do
|
92
|
+
stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][sum]=weight')).to_return(body: '[10]')
|
93
|
+
|
94
|
+
assert_equal 10, Ship.sum(:weight)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Relation test
|
98
|
+
|
99
|
+
test '#to_sql' do
|
100
|
+
stub_request(:get, URI::escape('malomalo.io/ships?where[id]=10')).to_return(body: [].to_json)
|
101
|
+
|
102
|
+
assert_equal "SELECT ships.* FROM ships WHERE ships.id = 10", Ship.where(:id => 10).to_sql
|
103
|
+
end
|
104
|
+
|
105
|
+
# Preload test
|
106
|
+
|
107
|
+
test '#preload' do
|
108
|
+
stub_request(:get, URI::escape('malomalo.io/fleets')).to_return(body: [{id: 1}].to_json)
|
109
|
+
stub_request(:get, URI::escape('malomalo.io/ships?where[fleet_id][0]=1')).to_return(body: [{id: 1, fleet_id: 1}].to_json)
|
110
|
+
|
111
|
+
fleets = Fleet.preload(:ships)
|
112
|
+
assert_equal [1], fleets.map(&:id)
|
113
|
+
assert_equal [1], fleets.first.ships.map(&:id)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Eagerload test
|
117
|
+
|
118
|
+
test '#eager_load' do
|
119
|
+
stub_request(:get, URI::escape('malomalo.io/fleets?include[]=ships')).to_return(body: [{
|
120
|
+
id: 1, ships: [{id: 1, fleet_id: 1}]
|
121
|
+
}].to_json)
|
122
|
+
|
123
|
+
fleets = Fleet.eager_load(:ships)
|
124
|
+
assert_equal [1], fleets.map(&:id)
|
125
|
+
assert_equal [1], fleets.first.ships.map(&:id)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
data/test/sunstone_test.rb
CHANGED
@@ -3,310 +3,301 @@ require 'test_helper'
|
|
3
3
|
class SunstoneTest < Minitest::Test
|
4
4
|
|
5
5
|
def setup
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
assert_equal
|
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
|
-
test '#send_request(#<Net::HTTPRequest
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
test '#send_request(#<Net::HTTPRequest>, body) with
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
test '#send_request(#<Net::HTTPRequest
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
#
|
206
|
-
|
207
|
-
test '#get(path)' do
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
end
|
212
|
-
|
213
|
-
test '#get(path, params) with params as
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
end
|
218
|
-
|
219
|
-
test '#get(path,
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
#
|
234
|
-
|
235
|
-
test '#post(path)' do
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
end
|
240
|
-
|
241
|
-
test '#post(path,
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
#
|
256
|
-
|
257
|
-
test '#put(path)' do
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
end
|
262
|
-
|
263
|
-
test '#put(path,
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
#
|
278
|
-
|
279
|
-
test '#delete' do
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
#
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
#
|
302
|
-
|
303
|
-
test '#config' do
|
304
|
-
stub_request(:get, "http://testhost.com/config").to_return(:body => '{"server": "configs"}')
|
305
|
-
|
306
|
-
assert_equal( {:server => "configs"}, Sunstone.config )
|
307
|
-
end
|
308
|
-
|
309
|
-
|
310
|
-
|
6
|
+
stub_request(:get, "malomalo.io/ping").to_return(:body => "pong")
|
7
|
+
end
|
8
|
+
|
9
|
+
test "connection configuration" do
|
10
|
+
ActiveRecord::Base.establish_connection(
|
11
|
+
:adapter => 'sunstone',
|
12
|
+
:site => 'http://malomalo.io'
|
13
|
+
)
|
14
|
+
|
15
|
+
assert_equal ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter, ActiveRecord::Base.connection.class
|
16
|
+
|
17
|
+
|
18
|
+
assert_equal 'malomalo.io', ActiveRecord::Base.connection.instance_variable_get(:@connection).host
|
19
|
+
assert_equal 80, ActiveRecord::Base.connection.instance_variable_get(:@connection).port
|
20
|
+
assert_equal false, ActiveRecord::Base.connection.instance_variable_get(:@connection).use_ssl
|
21
|
+
end
|
22
|
+
|
23
|
+
test "connection with user_agent" do
|
24
|
+
ActiveRecord::Base.establish_connection(
|
25
|
+
:adapter => 'sunstone',
|
26
|
+
:site => 'http://malomalo.io',
|
27
|
+
:user_agent => 'MyAgent'
|
28
|
+
)
|
29
|
+
|
30
|
+
assert ActiveRecord::Base.connection.instance_variable_get(:@connection).user_agent.start_with?('MyAgent')
|
31
|
+
end
|
32
|
+
|
33
|
+
test 'connection with api key' do
|
34
|
+
ActiveRecord::Base.establish_connection(
|
35
|
+
:adapter => 'sunstone',
|
36
|
+
:site => 'http://myapikey@malomalo.io'
|
37
|
+
)
|
38
|
+
|
39
|
+
assert_equal 'myapikey', ActiveRecord::Base.connection.instance_variable_get(:@connection).api_key
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
# # Sunstone.with_cookie_store ================================================
|
44
|
+
#
|
45
|
+
# test '#with_cookie_store(store, &block) sets the cookie-store' do
|
46
|
+
# assert_nil Thread.current[:sunstone_cookie_store]
|
47
|
+
# Sunstone.with_cookie_store('my_store') do
|
48
|
+
# assert_equal 'my_store', Thread.current[:sunstone_cookie_store]
|
49
|
+
# end
|
50
|
+
# assert_nil Thread.current[:sunstone_cookie_store]
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# # Sunstone.with_api_key ================================================
|
54
|
+
#
|
55
|
+
# test '#with_api_key(key, &block) sets the Api-Key' do
|
56
|
+
# assert_nil Thread.current[:sunstone_api_key]
|
57
|
+
# Sunstone.with_api_key('my_api_key') do
|
58
|
+
# assert_equal 'my_api_key', Thread.current[:sunstone_api_key]
|
59
|
+
# end
|
60
|
+
# assert_nil Thread.current[:sunstone_api_key]
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# # Sunstone.send_request =====================================================
|
64
|
+
#
|
65
|
+
# test '#send_request(#<Net::HTTPRequest>)' do
|
66
|
+
# stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
|
67
|
+
#
|
68
|
+
# assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# test '#send_request(#<Net::HTTPRequest>, body) with string body' do
|
72
|
+
# stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
|
73
|
+
#
|
74
|
+
# assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), '{"key":"value"}').body)
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# test '#send_request(#<Net::HTTPRequest>, body) with IO body' do
|
78
|
+
# stub_request(:post, "http://testhost.com/test").with { |request|
|
79
|
+
# request.headers['Transfer-Encoding'] == "chunked" && request.body == '{"key":"value"}'
|
80
|
+
# }.to_return(:body => "post")
|
81
|
+
#
|
82
|
+
# rd, wr = IO.pipe
|
83
|
+
# wr.write('{"key":"value"}')
|
84
|
+
# wr.close
|
85
|
+
#
|
86
|
+
# assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), rd).body)
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# test '#send_request(#<Net::HTTPRequest>, body) with Ruby Object body' do
|
90
|
+
# stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
|
91
|
+
#
|
92
|
+
# assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), {:key => 'value'}).body)
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# test '#send_request(#<Net::HTTPRequest>) raises Sunstone::Exceptions on non-200 responses' do
|
96
|
+
# stub_request(:get, "http://testhost.com/400").to_return(:status => 400)
|
97
|
+
# stub_request(:get, "http://testhost.com/401").to_return(:status => 401)
|
98
|
+
# stub_request(:get, "http://testhost.com/404").to_return(:status => 404)
|
99
|
+
# stub_request(:get, "http://testhost.com/410").to_return(:status => 410)
|
100
|
+
# stub_request(:get, "http://testhost.com/422").to_return(:status => 422)
|
101
|
+
# stub_request(:get, "http://testhost.com/450").to_return(:status => 450)
|
102
|
+
# stub_request(:get, "http://testhost.com/503").to_return(:status => 503)
|
103
|
+
# stub_request(:get, "http://testhost.com/550").to_return(:status => 550)
|
104
|
+
#
|
105
|
+
# assert_raises(Sunstone::Exception::BadRequest) { Sunstone.send_request(Net::HTTP::Get.new('/400')) }
|
106
|
+
# assert_raises(Sunstone::Exception::Unauthorized) { Sunstone.send_request(Net::HTTP::Get.new('/401')) }
|
107
|
+
# assert_raises(Sunstone::Exception::NotFound) { Sunstone.send_request(Net::HTTP::Get.new('/404')) }
|
108
|
+
# assert_raises(Sunstone::Exception::Gone) { Sunstone.send_request(Net::HTTP::Get.new('/410')) }
|
109
|
+
# assert_raises(Sunstone::Exception::ApiVersionUnsupported) { Sunstone.send_request(Net::HTTP::Get.new('/422')) }
|
110
|
+
# assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/450')) }
|
111
|
+
# assert_raises(Sunstone::Exception::ServiceUnavailable) { Sunstone.send_request(Net::HTTP::Get.new('/503')) }
|
112
|
+
# assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/550')) }
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
# test '#send_request(#<Net::HTTPRequest>, &block) returns value returned from &block' do
|
116
|
+
# stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
|
117
|
+
#
|
118
|
+
# value = Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
|
119
|
+
# 3215
|
120
|
+
# end
|
121
|
+
#
|
122
|
+
# assert_equal 3215, value
|
123
|
+
# end
|
124
|
+
#
|
125
|
+
# test '#send_request(#<Net::HTTPRequest>, &block)' do
|
126
|
+
# stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
|
127
|
+
#
|
128
|
+
# Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
|
129
|
+
# assert_equal 'get', response.body
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
# # make sure block is not called when not in valid_response_codes
|
133
|
+
# stub_request(:get, "http://testhost.com/test").to_return(:status => 401, :body => 'get')
|
134
|
+
#
|
135
|
+
# assert_raises(Sunstone::Exception::Unauthorized) {
|
136
|
+
# Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
|
137
|
+
# raise Sunstone::Exception, 'Should not get here'
|
138
|
+
# end
|
139
|
+
# }
|
140
|
+
# end
|
141
|
+
#
|
142
|
+
# test '#send_request(#<Net::HTTPRequest>, &block) with block reading chunks' do
|
143
|
+
# rd, wr = IO.pipe
|
144
|
+
# rd = Net::BufferedIO.new(rd)
|
145
|
+
# wr.write(<<-DATA.gsub(/^ +/, '').gsub(/\n/, "\r\n"))
|
146
|
+
# HTTP/1.1 200 OK
|
147
|
+
# Content-Length: 5
|
148
|
+
#
|
149
|
+
# hello
|
150
|
+
# DATA
|
151
|
+
#
|
152
|
+
# res = Net::HTTPResponse.read_new(rd)
|
153
|
+
# connection = mock('connection')
|
154
|
+
# connection.stubs(:request).yields(res)
|
155
|
+
# Sunstone.stubs(:with_connection).yields(connection)
|
156
|
+
#
|
157
|
+
# res.reading_body(rd, true) do
|
158
|
+
# Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
|
159
|
+
# response.read_body do |chunk|
|
160
|
+
# assert_equal('hello', chunk)
|
161
|
+
# end
|
162
|
+
# end
|
163
|
+
# end
|
164
|
+
# end
|
165
|
+
#
|
166
|
+
# test '#send_request(#<Net::HTTPRequest) adds cookies to the cookie store if present' do
|
167
|
+
# store = CookieStore::HashStore.new
|
168
|
+
# stub_request(:get, "http://testhost.com/test").to_return(:body => 'get', :headers => {'Set-Cookie' => 'foo=bar; Max-Age=3600'})
|
169
|
+
#
|
170
|
+
# Sunstone.with_cookie_store(store) do
|
171
|
+
# Sunstone.send_request(Net::HTTP::Get.new('/test'))
|
172
|
+
# end
|
173
|
+
#
|
174
|
+
# assert_equal 1, store.instance_variable_get(:@domains).size
|
175
|
+
# assert_equal 1, store.instance_variable_get(:@domains)['testhost.com'].size
|
176
|
+
# assert_equal 1, store.instance_variable_get(:@domains)['testhost.com']['/test'].size
|
177
|
+
# assert_equal 'bar', store.instance_variable_get(:@domains)['testhost.com']['/test']['foo'].value
|
178
|
+
# end
|
179
|
+
#
|
180
|
+
# test '#send_request(#<Net::HTTPRequest>) includes the headers' do
|
181
|
+
# stub_request(:get, "http://testhost.com/test").with(:headers => {
|
182
|
+
# 'Api-Key' => 'test_api_key',
|
183
|
+
# 'Content-Type' => 'application/json',
|
184
|
+
# 'User-Agent' => Sunstone.user_agent
|
185
|
+
# }).to_return(:body => "get")
|
186
|
+
#
|
187
|
+
# assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
|
188
|
+
#
|
189
|
+
# # Test without api key
|
190
|
+
# Sunstone.site = "http://testhost.com"
|
191
|
+
# stub_request(:get, "http://testhost.com/test").with(:headers => {
|
192
|
+
# 'Content-Type'=>'application/json',
|
193
|
+
# 'User-Agent'=>'Sunstone/0.1 Ruby/2.1.1-p76 x86_64-darwin13.0'
|
194
|
+
# }).to_return(:body => "get")
|
195
|
+
#
|
196
|
+
# assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
|
197
|
+
# end
|
198
|
+
#
|
199
|
+
# # Sunstone.get ==============================================================
|
200
|
+
#
|
201
|
+
# test '#get(path)' do
|
202
|
+
# stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
|
203
|
+
#
|
204
|
+
# assert_equal('get', Sunstone.get('/test').body)
|
205
|
+
# end
|
206
|
+
#
|
207
|
+
# test '#get(path, params) with params as string' do
|
208
|
+
# stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
|
209
|
+
#
|
210
|
+
# assert_equal 'get', Sunstone.get('/test', 'key=value').body
|
211
|
+
# end
|
212
|
+
#
|
213
|
+
# test '#get(path, params) with params as hash' do
|
214
|
+
# stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
|
215
|
+
#
|
216
|
+
# assert_equal 'get', Sunstone.get('/test', {:key => 'value'}).body
|
217
|
+
# end
|
218
|
+
#
|
219
|
+
# test '#get(path, &block)' do
|
220
|
+
# stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
|
221
|
+
#
|
222
|
+
# Sunstone.get('/test') do |response|
|
223
|
+
# assert_equal 'get', response.body
|
224
|
+
# end
|
225
|
+
# end
|
226
|
+
#
|
227
|
+
# # Sunstone.post =============================================================
|
228
|
+
#
|
229
|
+
# test '#post(path)' do
|
230
|
+
# stub_request(:post, "http://testhost.com/test").to_return(:body => "post")
|
231
|
+
#
|
232
|
+
# assert_equal('post', Sunstone.post('/test').body)
|
233
|
+
# end
|
234
|
+
#
|
235
|
+
# test '#post(path, body)' do
|
236
|
+
# stub_request(:post, "http://testhost.com/test").with(:body => 'body').to_return(:body => "post")
|
237
|
+
#
|
238
|
+
# assert_equal('post', Sunstone.post('/test', 'body').body)
|
239
|
+
# end
|
240
|
+
#
|
241
|
+
# test '#post(path, &block)' do
|
242
|
+
# stub_request(:post, "http://testhost.com/test").to_return(:body => 'post')
|
243
|
+
#
|
244
|
+
# Sunstone.post('/test') do |response|
|
245
|
+
# assert_equal 'post', response.body
|
246
|
+
# end
|
247
|
+
# end
|
248
|
+
#
|
249
|
+
# # Sunstone.put ==============================================================
|
250
|
+
#
|
251
|
+
# test '#put(path)' do
|
252
|
+
# stub_request(:put, "http://testhost.com/test").to_return(:body => "put")
|
253
|
+
#
|
254
|
+
# assert_equal('put', Sunstone.put('/test').body)
|
255
|
+
# end
|
256
|
+
#
|
257
|
+
# test '#put(path, body)' do
|
258
|
+
# stub_request(:put, "http://testhost.com/test").with(:body => 'body').to_return(:body => "put")
|
259
|
+
#
|
260
|
+
# assert_equal('put', Sunstone.put('/test', 'body').body)
|
261
|
+
# end
|
262
|
+
#
|
263
|
+
# test '#put(path, &block)' do
|
264
|
+
# stub_request(:put, "http://testhost.com/test").to_return(:body => 'put')
|
265
|
+
#
|
266
|
+
# Sunstone.put('/test') do |response|
|
267
|
+
# assert_equal 'put', response.body
|
268
|
+
# end
|
269
|
+
# end
|
270
|
+
#
|
271
|
+
# # Sunstone.delete ===========================================================
|
272
|
+
#
|
273
|
+
# test '#delete' do
|
274
|
+
# stub_request(:delete, "http://testhost.com/test").to_return(:body => "delete")
|
275
|
+
#
|
276
|
+
# assert_equal('delete', Sunstone.delete('/test').body)
|
277
|
+
# end
|
278
|
+
#
|
279
|
+
# test '#delete(path, &block)' do
|
280
|
+
# stub_request(:delete, "http://testhost.com/test").to_return(:body => 'delete')
|
281
|
+
#
|
282
|
+
# Sunstone.delete('/test') do |response|
|
283
|
+
# assert_equal 'delete', response.body
|
284
|
+
# end
|
285
|
+
# end
|
286
|
+
#
|
287
|
+
# # Sunstone.ping =============================================================
|
288
|
+
#
|
289
|
+
# test '#ping' do
|
290
|
+
# stub_request(:get, "http://testhost.com/ping").to_return(:body => 'pong')
|
291
|
+
#
|
292
|
+
# assert_equal( 'pong', Sunstone.ping )
|
293
|
+
# end
|
294
|
+
#
|
295
|
+
# # Sunstone.config ===========================================================
|
296
|
+
#
|
297
|
+
# test '#config' do
|
298
|
+
# stub_request(:get, "http://testhost.com/config").to_return(:body => '{"server": "configs"}')
|
299
|
+
#
|
300
|
+
# assert_equal( {:server => "configs"}, Sunstone.config )
|
301
|
+
# end
|
311
302
|
|
312
303
|
end
|