sunstone 0.1.0 → 1.0.0

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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/active_record/connection_adapters/sunstone/column.rb +19 -0
  3. data/lib/active_record/connection_adapters/sunstone/database_statements.rb +40 -0
  4. data/lib/active_record/connection_adapters/sunstone/schema_statements.rb +95 -0
  5. data/lib/active_record/connection_adapters/sunstone/type/date_time.rb +22 -0
  6. data/lib/active_record/connection_adapters/sunstone_adapter.rb +177 -0
  7. data/lib/arel/collectors/sunstone.rb +75 -0
  8. data/lib/arel/visitors/sunstone.rb +769 -0
  9. data/lib/ext/active_record/associations/builder/has_and_belongs_to_many.rb +48 -0
  10. data/lib/ext/active_record/relation.rb +26 -0
  11. data/lib/ext/active_record/statement_cache.rb +24 -0
  12. data/lib/sunstone.rb +37 -347
  13. data/lib/sunstone/connection.rb +337 -0
  14. data/sunstone.gemspec +3 -2
  15. data/test/sunstone/connection_test.rb +319 -0
  16. data/test/sunstone/parser_test.rb +21 -21
  17. metadata +30 -36
  18. data/lib/sunstone/model.rb +0 -23
  19. data/lib/sunstone/model/attributes.rb +0 -99
  20. data/lib/sunstone/model/persistence.rb +0 -168
  21. data/lib/sunstone/schema.rb +0 -38
  22. data/lib/sunstone/type/boolean.rb +0 -19
  23. data/lib/sunstone/type/date_time.rb +0 -20
  24. data/lib/sunstone/type/decimal.rb +0 -19
  25. data/lib/sunstone/type/integer.rb +0 -17
  26. data/lib/sunstone/type/mutable.rb +0 -16
  27. data/lib/sunstone/type/string.rb +0 -18
  28. data/lib/sunstone/type/value.rb +0 -97
  29. data/test/sunstone/model/associations_test.rb +0 -55
  30. data/test/sunstone/model/attributes_test.rb +0 -60
  31. data/test/sunstone/model/persistence_test.rb +0 -173
  32. data/test/sunstone/model_test.rb +0 -11
  33. data/test/sunstone/schema_test.rb +0 -25
  34. data/test/sunstone/type/boolean_test.rb +0 -24
  35. data/test/sunstone/type/date_time_test.rb +0 -31
  36. data/test/sunstone/type/decimal_test.rb +0 -27
  37. data/test/sunstone/type/integer_test.rb +0 -29
  38. data/test/sunstone/type/string_test.rb +0 -54
  39. data/test/sunstone/type/value_test.rb +0 -27
@@ -0,0 +1,337 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'net/https'
4
+
5
+ # # _Sunstone_ is a low-level API. It provides basic HTTP #get, #post, #put, and
6
+ # # #delete calls to the Sunstone Server. It can also provides basic error
7
+ # # checking of responses.
8
+ module Sunstone
9
+ class Connection
10
+
11
+ # Set the User-Agent of the client. Will be joined with other User-Agent info
12
+ attr_writer :user_agent
13
+ attr_accessor :api_key, :host, :port, :use_ssl
14
+
15
+ # Initialize a connection a Sunstone API server.
16
+ #
17
+ # Options:
18
+ #
19
+ # * <tt>:site</tt> - An optional url used to set the protocol, host, port,
20
+ # and api_key
21
+ # * <tt>:host</tt> - The default is to connect to 127.0.0.1.
22
+ # * <tt>:port</tt> - Defaults to 80.
23
+ # * <tt>:use_ssl</tt> - Defaults to false.
24
+ # * <tt>:api_key</tt> - An optional token to send in the `Api-Key` header
25
+ def initialize(config)
26
+ if config[:site]
27
+ uri = URI.parse(config.delete(:site))
28
+ config[:api_key] ||= (uri.user ? CGI.unescape(uri.user) : nil)
29
+ config[:host] ||= uri.host
30
+ config[:port] ||= uri.port
31
+ config[:use_ssl] ||= (uri.scheme == 'https')
32
+ end
33
+
34
+ [:api_key, :host, :port, :use_ssl, :user_agent].each do |key|
35
+ self.send(:"#{key}=", config[key])
36
+ end
37
+
38
+ @connection = Net::HTTP.new(host, port)
39
+ @connection.use_ssl = use_ssl
40
+ end
41
+
42
+ # Ping the Sunstone. If everything is configured and operating correctly
43
+ # <tt>"pong"</tt> will be returned. Otherwise and Sunstone::Exception should be
44
+ # thrown.
45
+ #
46
+ # #!ruby
47
+ # Sunstone.ping # => "pong"
48
+ #
49
+ # Sunstone.ping # raises Sunstone::Exception::ServiceUnavailable if a 503 is returned
50
+ def ping
51
+ get('/ping').body
52
+ end
53
+
54
+ # Returns the User-Agent of the client. Defaults to:
55
+ # "sunstone-ruby/SUNSTONE_VERSION RUBY_VERSION-pPATCH_LEVEL PLATFORM"
56
+ def user_agent
57
+ [
58
+ @user_agent,
59
+ "Sunstone/#{Sunstone::VERSION}",
60
+ "Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}",
61
+ RUBY_PLATFORM
62
+ ].compact.join(' ')
63
+ end
64
+
65
+ # Sends a Net::HTTPRequest to the server. The headers returned from
66
+ # Sunestone#headers are automatically added to the request. The appropriate
67
+ # error is raised if the response is not in the 200..299 range.
68
+ #
69
+ # Paramaters::
70
+ #
71
+ # * +request+ - A Net::HTTPRequest to send to the server
72
+ # * +body+ - Optional, a String, IO Object, or a Ruby object which is
73
+ # converted into JSON and sent as the body
74
+ # * +block+ - An optional block to call with the +Net::HTTPResponse+ object.
75
+ #
76
+ # Return Value::
77
+ #
78
+ # Returns the return value of the <tt>&block</tt> if given, otherwise the
79
+ # response object (a Net::HTTPResponse)
80
+ #
81
+ # Examples:
82
+ #
83
+ # #!ruby
84
+ # Sunstone.send_request(#<Net::HTTP::Get>) # => #<Net::HTTP::Response>
85
+ #
86
+ # Sunstone.send_request(#<Net::HTTP::Get @path="/404">) # => raises Sunstone::Exception::NotFound
87
+ #
88
+ # # this will still raise an exception if the response_code is not valid
89
+ # # and the block will not be called
90
+ # Sunstone.send_request(#<Net::HTTP::Get>) do |response|
91
+ # # ...
92
+ # end
93
+ #
94
+ # # The following example shows how to stream a response:
95
+ # Sunstone.send_request(#<Net::HTTP::Get>) do |response|
96
+ # response.read_body do |chunk|
97
+ # io.write(chunk)
98
+ # end
99
+ # end
100
+ def send_request(request, body=nil, &block)
101
+ request_headers.each { |k, v| request[k] = v }
102
+
103
+ if body.is_a?(IO)
104
+ request['Transfer-Encoding'] = 'chunked'
105
+ request.body_stream = body
106
+ elsif body.is_a?(String)
107
+ request.body = body
108
+ elsif body
109
+ request.body = Wankel.encode(body)
110
+ end
111
+
112
+ return_value = nil
113
+ @connection.request(request) do |response|
114
+
115
+ if response['X-42Floors-API-Version-Deprecated']
116
+ logger.warn("DEPRECATION WARNING: API v#{API_VERSION} is being phased out")
117
+ end
118
+
119
+ validate_response_code(response)
120
+
121
+ # Get the cookies
122
+ response.each_header do |key, value|
123
+ if key.downcase == 'set-cookie' && Thread.current[:sunstone_cookie_store]
124
+ Thread.current[:sunstone_cookie_store].set_cookie("#{site}#{request.path}", value)
125
+ end
126
+ end
127
+
128
+ if block_given?
129
+ return_value =yield(response)
130
+ else
131
+ return_value =response
132
+ end
133
+ end
134
+
135
+
136
+ return_value
137
+ end
138
+
139
+ # Send a GET request to +path+ on the Sunstone Server via +Sunstone#send_request+.
140
+ # See +Sunstone#send_request+ for more details on how the response is handled.
141
+ #
142
+ # Paramaters::
143
+ #
144
+ # * +path+ - The +path+ on the server to GET to.
145
+ # * +params+ - Either a String, Hash, or Ruby Object that responds to
146
+ # #to_param. Appended on the URL as query params
147
+ # * +block+ - An optional block to call with the +Net::HTTPResponse+ object.
148
+ #
149
+ # Return Value::
150
+ #
151
+ # See +Sunstone#send_request+
152
+ #
153
+ # Examples:
154
+ #
155
+ # #!ruby
156
+ # Sunstone.get('/example') # => #<Net::HTTP::Response>
157
+ #
158
+ # Sunstone.get('/example', 'query=stuff') # => #<Net::HTTP::Response>
159
+ #
160
+ # Sunstone.get('/example', {:query => 'stuff'}) # => #<Net::HTTP::Response>
161
+ #
162
+ # Sunstone.get('/404') # => raises Sunstone::Exception::NotFound
163
+ #
164
+ # Sunstone.get('/act') do |response|
165
+ # # ...
166
+ # end
167
+ def get(path, params='', &block)
168
+ params ||= ''
169
+ request = Net::HTTP::Get.new(path + '?' + params.to_param)
170
+
171
+ send_request(request, nil, &block)
172
+ end
173
+
174
+ # Send a POST request to +path+ on the Sunstone Server via +Sunstone#send_request+.
175
+ # See +Sunstone#send_request+ for more details on how the response is handled.
176
+ #
177
+ # Paramaters::
178
+ #
179
+ # * +path+ - The +path+ on the server to POST to.
180
+ # * +body+ - Optional, See +Sunstone#send_request+.
181
+ # * +block+ - Optional, See +Sunstone#send_request+
182
+ #
183
+ # Return Value::
184
+ #
185
+ # See +Sunstone#send_request+
186
+ #
187
+ # Examples:
188
+ #
189
+ # #!ruby
190
+ # Sunstone.post('/example') # => #<Net::HTTP::Response>
191
+ #
192
+ # Sunstone.post('/example', 'body') # => #<Net::HTTP::Response>
193
+ #
194
+ # Sunstone.post('/example', #<IO Object>) # => #<Net::HTTP::Response>
195
+ #
196
+ # Sunstone.post('/example', {:example => 'data'}) # => #<Net::HTTP::Response>
197
+ #
198
+ # Sunstone.post('/404') # => raises Sunstone::Exception::NotFound
199
+ #
200
+ # Sunstone.post('/act') do |response|
201
+ # # ...
202
+ # end
203
+ def post(path, body=nil, &block)
204
+ request = Net::HTTP::Post.new(path)
205
+
206
+ send_request(request, body, &block)
207
+ end
208
+
209
+ # Send a PUT request to +path+ on the Sunstone Server via +Sunstone#send_request+.
210
+ # See +Sunstone#send_request+ for more details on how the response is handled.
211
+ #
212
+ # Paramaters::
213
+ #
214
+ # * +path+ - The +path+ on the server to POST to.
215
+ # * +body+ - Optional, See +Sunstone#send_request+.
216
+ # * +block+ - Optional, See +Sunstone#send_request+
217
+ #
218
+ # Return Value::
219
+ #
220
+ # See +Sunstone#send_request+
221
+ #
222
+ # Examples:
223
+ #
224
+ # #!ruby
225
+ # Sunstone.put('/example') # => #<Net::HTTP::Response>
226
+ #
227
+ # Sunstone.put('/example', 'body') # => #<Net::HTTP::Response>
228
+ #
229
+ # Sunstone.put('/example', #<IO Object>) # => #<Net::HTTP::Response>
230
+ #
231
+ # Sunstone.put('/example', {:example => 'data'}) # => #<Net::HTTP::Response>
232
+ #
233
+ # Sunstone.put('/404') # => raises Sunstone::Exception::NotFound
234
+ #
235
+ # Sunstone.put('/act') do |response|
236
+ # # ...
237
+ # end
238
+ def put(path, body=nil, *valid_response_codes, &block)
239
+ request = Net::HTTP::Put.new(path)
240
+
241
+ send_request(request, body, &block)
242
+ end
243
+
244
+ # Send a DELETE request to +path+ on the Sunstone Server via +Sunstone#send_request+.
245
+ # See +Sunstone#send_request+ for more details on how the response is handled
246
+ #
247
+ # Paramaters::
248
+ #
249
+ # * +path+ - The +path+ on the server to POST to.
250
+ # * +block+ - Optional, See +Sunstone#send_request+
251
+ #
252
+ # Return Value::
253
+ #
254
+ # See +Sunstone#send_request+
255
+ #
256
+ # Examples:
257
+ #
258
+ # #!ruby
259
+ # Sunstone.delete('/example') # => #<Net::HTTP::Response>
260
+ #
261
+ # Sunstone.delete('/404') # => raises Sunstone::Exception::NotFound
262
+ #
263
+ # Sunstone.delete('/act') do |response|
264
+ # # ...
265
+ # end
266
+ def delete(path, &block)
267
+ request = Net::HTTP::Delete.new(path)
268
+
269
+ send_request(request, nil, &block)
270
+ end
271
+
272
+ def server_config
273
+ @server_config ||= Wankel.parse(get('/config').body, :symbolize_keys => true)
274
+ end
275
+
276
+ private
277
+
278
+ def request_headers
279
+ headers = {
280
+ 'Content-Type' => 'application/json',
281
+ 'User-Agent' => user_agent,
282
+ 'Api-Version' => '0.1.0'
283
+ }
284
+
285
+ headers['Api-Key'] = api_key if api_key
286
+
287
+ headers
288
+ end
289
+
290
+ # Raise an Sunstone::Exception based on the response_code, unless the response_code
291
+ # is include in the valid_response_codes Array
292
+ #
293
+ # Paramaters::
294
+ #
295
+ # * +response+ - The Net::HTTP::Response object
296
+ #
297
+ # Return Value::
298
+ #
299
+ # If an exception is not raised the +response+ is returned
300
+ #
301
+ # Examples:
302
+ #
303
+ # #!ruby
304
+ # Sunstone.validate_response_code(<Net::HTTP::Response @code=200>) # => <Net::HTTP::Response @code=200>
305
+ #
306
+ # Sunstone.validate_response_code(<Net::HTTP::Response @code=404>) # => raises Sunstone::Exception::NotFound
307
+ #
308
+ # Sunstone.validate_response_code(<Net::HTTP::Response @code=500>) # => raises Sunstone::Exception
309
+ def validate_response_code(response)
310
+ code = response.code.to_i
311
+
312
+ if !(200..299).include?(code)
313
+ case code
314
+ when 400
315
+ raise Sunstone::Exception::BadRequest, response
316
+ when 401
317
+ raise Sunstone::Exception::Unauthorized, response
318
+ when 404
319
+ raise Sunstone::Exception::NotFound, response
320
+ when 410
321
+ raise Sunstone::Exception::Gone, response
322
+ when 422
323
+ raise Sunstone::Exception::ApiVersionUnsupported, response
324
+ when 503
325
+ raise Sunstone::Exception::ServiceUnavailable, response
326
+ when 301
327
+ raise Sunstone::Exception::MovedPermanently, response
328
+ when 300..599
329
+ raise Sunstone::Exception, response
330
+ else
331
+ raise Sunstone::Exception, response
332
+ end
333
+ end
334
+ end
335
+
336
+ end
337
+ end
data/sunstone.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "sunstone"
3
- s.version = '0.1.0'
3
+ s.version = '1.0.0'
4
4
  s.authors = ["Jon Bracy"]
5
5
  s.email = ["jonbracy@gmail.com"]
6
6
  s.homepage = "http://sunstonerb.com"
@@ -28,7 +28,8 @@ Gem::Specification.new do |s|
28
28
  # Runtime
29
29
  s.add_runtime_dependency 'wankel'
30
30
  s.add_runtime_dependency 'cookie_store'
31
+ s.add_runtime_dependency 'arel'
31
32
  s.add_runtime_dependency 'activesupport'
32
33
  s.add_runtime_dependency 'activemodel'
33
- s.add_runtime_dependency 'connection_pool'
34
+ s.add_runtime_dependency 'activerecord'
34
35
  end
@@ -0,0 +1,319 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::ConnectionTest < Minitest::Test
4
+
5
+ # ::new =====================================================================
6
+ test "setting the site sets the api_key" do
7
+ connection = Sunstone::Connection.new(:site => 'https://my_api_key@localhost')
8
+ assert_equal('my_api_key', connection.api_key)
9
+ end
10
+
11
+ test "setting the site sets the host" do
12
+ connection = Sunstone::Connection.new(:site => 'https://my_api_key@example.com')
13
+ assert_equal('example.com', connection.host)
14
+ end
15
+
16
+ test "setting the site sets the port" do
17
+ connection = Sunstone::Connection.new(:site => 'http://my_api_key@localhost')
18
+ assert_equal(80, connection.port)
19
+
20
+ connection = Sunstone::Connection.new(:site => 'https://my_api_key@localhost')
21
+ assert_equal(443, connection.port)
22
+
23
+ connection = Sunstone::Connection.new(:site => 'https://my_api_key@localhost:4321')
24
+ assert_equal(4321, connection.port)
25
+ end
26
+
27
+ test "setting the site sets the use_ssl option" do
28
+ connection = Sunstone::Connection.new(:site => 'http://my_api_key@localhost')
29
+ assert_equal(false, connection.use_ssl)
30
+
31
+ connection = Sunstone::Connection.new(:site => 'https://my_api_key@localhost')
32
+ assert_equal(true, connection.use_ssl)
33
+ end
34
+
35
+ test "setting the user_agent appends it to the User-Agent" do
36
+ connection = Sunstone::Connection.new(:site => 'http://my_api_key@localhost')
37
+ assert_equal("Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", connection.user_agent)
38
+
39
+ connection = Sunstone::Connection.new(:site => 'http://my_api_key@localhost', :user_agent => "MyGem/3.14")
40
+ assert_equal("MyGem/3.14 Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", connection.user_agent)
41
+ end
42
+
43
+ #send_request =============================================================
44
+
45
+ test '#send_request(#<Net::HTTPRequest>)' do
46
+ stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
47
+
48
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
49
+ assert_equal('get', connection.send_request(Net::HTTP::Get.new('/test')).body)
50
+ end
51
+
52
+ test '#send_request(#<Net::HTTPRequest>, body) with string body' do
53
+ stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
54
+
55
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
56
+ assert_equal('post', connection.send_request(Net::HTTP::Post.new('/test'), '{"key":"value"}').body)
57
+ end
58
+
59
+ test '#send_request(#<Net::HTTPRequest>, body) with IO body' do
60
+ stub_request(:post, "http://testhost.com/test").with { |request|
61
+ request.headers['Transfer-Encoding'] == "chunked" && request.body == '{"key":"value"}'
62
+ }.to_return(:body => "post")
63
+
64
+ rd, wr = IO.pipe
65
+ wr.write('{"key":"value"}')
66
+ wr.close
67
+
68
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
69
+ assert_equal('post', connection.send_request(Net::HTTP::Post.new('/test'), rd).body)
70
+ end
71
+
72
+ test '#send_request(#<Net::HTTPRequest>, body) with Ruby Object body' do
73
+ stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
74
+
75
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
76
+ assert_equal('post', connection.send_request(Net::HTTP::Post.new('/test'), {:key => 'value'}).body)
77
+ end
78
+
79
+ test '#send_request(#<Net::HTTPRequest>) raises Sunstone::Exceptions on non-200 responses' do
80
+ stub_request(:get, "http://testhost.com/400").to_return(:status => 400)
81
+ stub_request(:get, "http://testhost.com/401").to_return(:status => 401)
82
+ stub_request(:get, "http://testhost.com/404").to_return(:status => 404)
83
+ stub_request(:get, "http://testhost.com/410").to_return(:status => 410)
84
+ stub_request(:get, "http://testhost.com/422").to_return(:status => 422)
85
+ stub_request(:get, "http://testhost.com/450").to_return(:status => 450)
86
+ stub_request(:get, "http://testhost.com/503").to_return(:status => 503)
87
+ stub_request(:get, "http://testhost.com/550").to_return(:status => 550)
88
+
89
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
90
+ assert_raises(Sunstone::Exception::BadRequest) { connection.send_request(Net::HTTP::Get.new('/400')) }
91
+ assert_raises(Sunstone::Exception::Unauthorized) { connection.send_request(Net::HTTP::Get.new('/401')) }
92
+ assert_raises(Sunstone::Exception::NotFound) { connection.send_request(Net::HTTP::Get.new('/404')) }
93
+ assert_raises(Sunstone::Exception::Gone) { connection.send_request(Net::HTTP::Get.new('/410')) }
94
+ assert_raises(Sunstone::Exception::ApiVersionUnsupported) { connection.send_request(Net::HTTP::Get.new('/422')) }
95
+ assert_raises(Sunstone::Exception) { connection.send_request(Net::HTTP::Get.new('/450')) }
96
+ assert_raises(Sunstone::Exception::ServiceUnavailable) { connection.send_request(Net::HTTP::Get.new('/503')) }
97
+ assert_raises(Sunstone::Exception) { connection.send_request(Net::HTTP::Get.new('/550')) }
98
+ end
99
+
100
+ test '#send_request(#<Net::HTTPRequest>, &block) returns value returned from &block' do
101
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
102
+
103
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
104
+ value = connection.send_request(Net::HTTP::Get.new('/test')) do |response|
105
+ 3215
106
+ end
107
+
108
+ assert_equal 3215, value
109
+ end
110
+
111
+ test '#send_request(#<Net::HTTPRequest>, &block)' do
112
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
113
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
114
+
115
+ connection.send_request(Net::HTTP::Get.new('/test')) do |response|
116
+ assert_equal 'get', response.body
117
+ end
118
+
119
+ # make sure block is not called when not in valid_response_codes
120
+ stub_request(:get, "http://testhost.com/test").to_return(:status => 401, :body => 'get')
121
+
122
+ assert_raises(Sunstone::Exception::Unauthorized) {
123
+ connection.send_request(Net::HTTP::Get.new('/test')) do |response|
124
+ raise Sunstone::Exception, 'Should not get here'
125
+ end
126
+ }
127
+ end
128
+
129
+ test '#send_request(#<Net::HTTPRequest>, &block) with block reading chunks' do
130
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
131
+
132
+ rd, wr = IO.pipe
133
+ rd = Net::BufferedIO.new(rd)
134
+ wr.write(<<-DATA.gsub(/^ +/, '').gsub(/\n/, "\r\n"))
135
+ HTTP/1.1 200 OK
136
+ Content-Length: 5
137
+
138
+ hello
139
+ DATA
140
+
141
+ res = Net::HTTPResponse.read_new(rd)
142
+ mock_connection = mock('connection')
143
+ mock_connection.stubs(:request).yields(res)
144
+ connection.instance_variable_set(:@connection, mock_connection)
145
+
146
+ res.reading_body(rd, true) do
147
+ connection.send_request(Net::HTTP::Get.new('/test')) do |response|
148
+ response.read_body do |chunk|
149
+ assert_equal('hello', chunk)
150
+ end
151
+ end
152
+ end
153
+ end
154
+ #
155
+ # test '#send_request(#<Net::HTTPRequest) adds cookies to the cookie store if present' do
156
+ # store = CookieStore::HashStore.new
157
+ # stub_request(:get, "http://testhost.com/test").to_return(:body => 'get', :headers => {'Set-Cookie' => 'foo=bar; Max-Age=3600'})
158
+ #
159
+ # Sunstone.with_cookie_store(store) do
160
+ # Sunstone.send_request(Net::HTTP::Get.new('/test'))
161
+ # end
162
+ #
163
+ # assert_equal 1, store.instance_variable_get(:@domains).size
164
+ # assert_equal 1, store.instance_variable_get(:@domains)['testhost.com'].size
165
+ # assert_equal 1, store.instance_variable_get(:@domains)['testhost.com']['/test'].size
166
+ # assert_equal 'bar', store.instance_variable_get(:@domains)['testhost.com']['/test']['foo'].value
167
+ # end
168
+ #
169
+ # test '#send_request(#<Net::HTTPRequest>) includes the headers' do
170
+ # stub_request(:get, "http://testhost.com/test").with(:headers => {
171
+ # 'Api-Key' => 'test_api_key',
172
+ # 'Content-Type' => 'application/json',
173
+ # 'User-Agent' => Sunstone.user_agent
174
+ # }).to_return(:body => "get")
175
+ #
176
+ # assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
177
+ #
178
+ # # Test without api key
179
+ # Sunstone.site = "http://testhost.com"
180
+ # stub_request(:get, "http://testhost.com/test").with(:headers => {
181
+ # 'Content-Type'=>'application/json',
182
+ # 'User-Agent'=>'Sunstone/0.1 Ruby/2.1.1-p76 x86_64-darwin13.0'
183
+ # }).to_return(:body => "get")
184
+ #
185
+ # assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
186
+ # end
187
+
188
+ #
189
+ # # Sunstone.with_cookie_store ================================================
190
+ #
191
+ # test '#with_cookie_store(store, &block) sets the cookie-store' do
192
+ # assert_nil Thread.current[:sunstone_cookie_store]
193
+ # Sunstone.with_cookie_store('my_store') do
194
+ # assert_equal 'my_store', Thread.current[:sunstone_cookie_store]
195
+ # end
196
+ # assert_nil Thread.current[:sunstone_cookie_store]
197
+ # end
198
+
199
+ # Sunstone.get ==============================================================
200
+
201
+ test '#get(path)' do
202
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
203
+ stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
204
+
205
+ assert_equal('get', connection.get('/test').body)
206
+ end
207
+
208
+ test '#get(path, params) with params as string' do
209
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
210
+ stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
211
+
212
+ assert_equal 'get', connection.get('/test', 'key=value').body
213
+ end
214
+
215
+ test '#get(path, params) with params as hash' do
216
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
217
+ stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
218
+
219
+ assert_equal 'get', connection.get('/test', {:key => 'value'}).body
220
+ end
221
+
222
+ test '#get(path, &block)' do
223
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
224
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
225
+
226
+ connection.get('/test') do |response|
227
+ assert_equal 'get', response.body
228
+ end
229
+ end
230
+
231
+ # Sunstone.post =============================================================
232
+
233
+ test '#post(path)' do
234
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
235
+ stub_request(:post, "http://testhost.com/test").to_return(:body => "post")
236
+
237
+ assert_equal('post', connection.post('/test').body)
238
+ end
239
+
240
+ test '#post(path, body)' do
241
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
242
+ stub_request(:post, "http://testhost.com/test").with(:body => 'body').to_return(:body => "post")
243
+
244
+ assert_equal('post', connection.post('/test', 'body').body)
245
+ end
246
+
247
+ test '#post(path, &block)' do
248
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
249
+ stub_request(:post, "http://testhost.com/test").to_return(:body => 'post')
250
+
251
+ connection.post('/test') do |response|
252
+ assert_equal 'post', response.body
253
+ end
254
+ end
255
+
256
+ # Sunstone.put ==============================================================
257
+
258
+ test '#put(path)' do
259
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
260
+ stub_request(:put, "http://testhost.com/test").to_return(:body => "put")
261
+
262
+ assert_equal('put', connection.put('/test').body)
263
+ end
264
+
265
+ test '#put(path, body)' do
266
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
267
+ stub_request(:put, "http://testhost.com/test").with(:body => 'body').to_return(:body => "put")
268
+
269
+ assert_equal('put', connection.put('/test', 'body').body)
270
+ end
271
+
272
+ test '#put(path, &block)' do
273
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
274
+ stub_request(:put, "http://testhost.com/test").to_return(:body => 'put')
275
+
276
+ connection.put('/test') do |response|
277
+ assert_equal 'put', response.body
278
+ end
279
+ end
280
+
281
+ # Sunstone.delete ===========================================================
282
+
283
+ test '#delete' do
284
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
285
+ stub_request(:delete, "http://testhost.com/test").to_return(:body => "delete")
286
+
287
+ assert_equal('delete', connection.delete('/test').body)
288
+ end
289
+
290
+ test '#delete(path, &block)' do
291
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
292
+ stub_request(:delete, "http://testhost.com/test").to_return(:body => 'delete')
293
+
294
+ connection.delete('/test') do |response|
295
+ assert_equal 'delete', response.body
296
+ end
297
+ end
298
+
299
+ # #ping =====================================================================
300
+
301
+ test '#ping' do
302
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
303
+ stub_request(:get, "http://testhost.com/ping").to_return(:body => 'pong')
304
+
305
+ assert_equal( 'pong', connection.ping )
306
+ end
307
+
308
+ # #server_config ===========================================================
309
+
310
+ test '#config' do
311
+ connection = Sunstone::Connection.new(:site => "http://test_api_key@testhost.com")
312
+ stub_request(:get, "http://testhost.com/config").to_return(:body => '{"server": "configs"}')
313
+
314
+ assert_equal( {:server => "configs"}, connection.server_config )
315
+ end
316
+
317
+
318
+
319
+ end