sunstone 5.0.0.beta3 → 5.0.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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.tm_properties +1 -0
  4. data/.travis.yml +36 -0
  5. data/README.md +1 -2
  6. data/Rakefile.rb +1 -1
  7. data/ext/active_record/associations/collection_association.rb +48 -6
  8. data/ext/active_record/attribute_methods.rb +25 -21
  9. data/ext/active_record/callbacks.rb +17 -0
  10. data/ext/active_record/finder_methods.rb +44 -2
  11. data/ext/active_record/persistence.rb +127 -1
  12. data/ext/active_record/relation.rb +13 -5
  13. data/ext/active_record/relation/calculations.rb +25 -0
  14. data/ext/active_record/statement_cache.rb +3 -2
  15. data/ext/active_record/transactions.rb +60 -0
  16. data/ext/arel/attributes/empty_relation.rb +31 -0
  17. data/ext/arel/attributes/relation.rb +3 -2
  18. data/lib/active_record/connection_adapters/sunstone/database_statements.rb +13 -2
  19. data/lib/active_record/connection_adapters/sunstone/schema_dumper.rb +16 -0
  20. data/lib/active_record/connection_adapters/sunstone/schema_statements.rb +2 -2
  21. data/lib/active_record/connection_adapters/sunstone/type/uuid.rb +21 -0
  22. data/lib/active_record/connection_adapters/sunstone_adapter.rb +54 -30
  23. data/lib/arel/collectors/sunstone.rb +6 -4
  24. data/lib/arel/visitors/sunstone.rb +61 -39
  25. data/lib/sunstone.rb +18 -11
  26. data/lib/sunstone/connection.rb +62 -22
  27. data/lib/sunstone/exception.rb +3 -0
  28. data/lib/sunstone/gis.rb +1 -0
  29. data/lib/sunstone/version.rb +2 -2
  30. data/sunstone.gemspec +4 -5
  31. data/test/active_record/associations/has_and_belongs_to_many_test.rb +12 -0
  32. data/test/active_record/associations/has_many_test.rb +72 -0
  33. data/test/active_record/eager_loading_test.rb +15 -0
  34. data/test/active_record/persistance_test.rb +190 -0
  35. data/test/active_record/preload_test.rb +16 -0
  36. data/test/active_record/query_test.rb +91 -0
  37. data/test/models.rb +91 -0
  38. data/test/sunstone/connection/configuration_test.rb +44 -0
  39. data/test/sunstone/connection/cookie_store_test.rb +37 -0
  40. data/test/sunstone/connection/request_helper_test.rb +105 -0
  41. data/test/sunstone/connection/send_request_test.rb +164 -0
  42. data/test/sunstone/connection_test.rb +2 -298
  43. data/test/test_helper.rb +45 -2
  44. metadata +52 -47
  45. data/ext/active_record/associations/builder/has_and_belongs_to_many.rb +0 -48
  46. data/ext/active_record/calculations.rb +0 -32
  47. data/ext/active_record/query_methods.rb +0 -30
  48. data/ext/active_record/relation/predicate_builder.rb +0 -23
  49. data/test/models/ship.rb +0 -14
  50. data/test/query_test.rb +0 -134
  51. data/test/sunstone/parser_test.rb +0 -124
  52. data/test/sunstone_test.rb +0 -303
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveRecord::PreloadTest < Minitest::Test
4
+
5
+ test '#preload' do
6
+ webmock(:get, "/fleets").to_return(body: [{id: 1}].to_json)
7
+ webmock(:get, "/ships", where: {fleet_id: 1}).to_return(body: [{id: 1, fleet_id: 1}].to_json)
8
+ webmock(:get, "/sailors_ships", where: {ship_id: 1}).to_return(body: [{ship_id: 1, sailor_id: 1}].to_json)
9
+ webmock(:get, "/sailors", where: {id: 1}).to_return(body: [{id: 1}].to_json)
10
+
11
+ fleets = Fleet.preload(:ships => :sailors)
12
+ assert_equal [1], fleets.map(&:id)
13
+ assert_equal [1], fleets.first.ships.map(&:id)
14
+ end
15
+
16
+ end
@@ -0,0 +1,91 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveRecord::QueryTest < Minitest::Test
4
+
5
+ test '::find' do
6
+ webmock(:get, "/ships", { where: {id: 42}, limit: 1 }).to_return(body: [{id: 42}].to_json)
7
+
8
+ assert_equal 42, Ship.find(42).id
9
+ end
10
+
11
+ test '::first' do
12
+ webmock(:get, "/ships", { limit: 1, order: [{id: :asc}] }).to_return(body: [].to_json)
13
+
14
+ assert_nil Ship.first
15
+ end
16
+
17
+ test '::last' do
18
+ webmock(:get, "/ships", { limit: 1, order: [{id: :desc}] }).to_return(body: [].to_json)
19
+
20
+ assert_nil Ship.last
21
+ end
22
+
23
+ test '::find_each' do
24
+ webmock(:get, "/ships", { limit: 1000, order: [{id: :asc}] }).to_return(body: [].to_json)
25
+
26
+ assert_nil Ship.find_each { |s| s }
27
+ end
28
+
29
+ test '::where on columns' do
30
+ webmock(:get, "/ships", { where: {id: 10} }).to_return(body: [].to_json)
31
+
32
+ assert_equal [], Ship.where(:id => 10).to_a
33
+ end
34
+
35
+ test '::where column is nil' do
36
+ webmock(:get, "/ships", { where: {leased_at: nil} }).to_return(body: [].to_json)
37
+
38
+ assert_equal [], Ship.where(:leased_at => nil).to_a
39
+ end
40
+
41
+ test '::where on belongs_to relation' do
42
+ webmock(:get, "/ships", where: {fleet: { id: {eq: 1} } }).to_return(body: [].to_json)
43
+
44
+ assert_equal [], Ship.where(:fleet => {id: 1}).to_a
45
+ end
46
+
47
+ test '::where on has_many relation' do
48
+ webmock(:get, "/fleets", where: {ships: { id: {eq: 1} } }).to_return(body: [].to_json)
49
+
50
+ assert_equal [], Fleet.where(:ships => {id: 1}).to_a
51
+ end
52
+
53
+ test '::where on has_and_belongs_to_many relation' do
54
+ webmock(:get, "/ships", where: {sailors: { id: {eq: 1} } }).to_return(body: [].to_json)
55
+
56
+ assert_equal [], Ship.where(:sailors => {id: 1}).to_a
57
+ end
58
+
59
+ # Polymorphic
60
+ test '::where on a has_many throught a polymorphic source' do
61
+ webmock(:get, "/ships", where: { nations: { id: {eq: 1} } }, limit: 10).to_return(body: [].to_json)
62
+
63
+ assert_equal [], Ship.where(nations: {id: 1}).limit(10).to_a
64
+ end
65
+ ### end polymorphic test
66
+
67
+ test '::count' do
68
+ webmock(:get, "/ships/calculate", select: [{count: "*"}]).to_return(body: [10].to_json)
69
+
70
+ assert_equal 10, Ship.count
71
+ end
72
+
73
+ test '::count(:column)' do
74
+ webmock(:get, "/ships/calculate", select: [{count: "id"}]).to_return(body: [10].to_json)
75
+
76
+ assert_equal 10, Ship.count(:id)
77
+ end
78
+
79
+ test '::sum(:column)' do
80
+ webmock(:get, "/ships/calculate", select: [{sum: "weight"}]).to_return(body: [10].to_json)
81
+
82
+ assert_equal 10, Ship.sum(:weight)
83
+ end
84
+
85
+ # Relation test
86
+
87
+ test '#to_sql' do
88
+ assert_equal "SELECT ships.* FROM ships WHERE ships.id = 10", Ship.where(:id => 10).to_sql
89
+ end
90
+
91
+ end
@@ -0,0 +1,91 @@
1
+
2
+
3
+ WebMock::StubRegistry.instance.global_stubs.push(
4
+ WebMock::RequestStub.new(:get, "http://example.com/ping").to_return(
5
+ body: "pong"
6
+ ),
7
+
8
+ WebMock::RequestStub.new(:get, "http://example.com/tables").to_return(
9
+ body: %w(ships fleets sailors).to_json
10
+ ),
11
+
12
+ WebMock::RequestStub.new(:get, "http://example.com/ships/schema").to_return(
13
+ body: {
14
+ id: {type: 'integer', primary_key: true, null: false, array: false},
15
+ fleet_id: {type: 'integer', primary_key: false, null: true, array: false},
16
+ name: {type: 'string', primary_key: false, null: true, array: false}
17
+ }.to_json
18
+ ),
19
+
20
+ WebMock::RequestStub.new(:get, "http://example.com/fleets/schema").to_return(
21
+ body: {
22
+ id: {type: 'integer', primary_key: true, null: false, array: false},
23
+ name: {type: 'string', primary_key: false, null: true, array: false}
24
+ }.to_json
25
+ ),
26
+
27
+ WebMock::RequestStub.new(:get, "http://example.com/sailors/schema").to_return(
28
+ body: {
29
+ id: {type: 'integer', primary_key: true, null: false, array: false},
30
+ name: {type: 'string', primary_key: false, null: true, array: false}
31
+ }.to_json
32
+ ),
33
+
34
+ WebMock::RequestStub.new(:get, "http://example.com/sailors_ships/schema").to_return(
35
+ body: {
36
+ sailor_id: {type: 'integer', primary_key: false, null: false, array: false},
37
+ ship_id: {type: 'integer', primary_key: false, null: true, array: false}
38
+ }.to_json
39
+ ),
40
+
41
+ WebMock::RequestStub.new(:get, "http://example.com/countries/schema").to_return(
42
+ body: {
43
+ id: {type: 'integer', primary_key: true, null: false, array: false},
44
+ name: {type: 'string', primary_key: false, null: true, array: false}
45
+ }.to_json
46
+ ),
47
+
48
+ WebMock::RequestStub.new(:get, "http://example.com/ownerships/schema").to_return(
49
+ body: {
50
+ country_id: {type: 'integer', primary_key: false, null: false, array: false},
51
+ asset_type: {type: 'string', primary_key: false, null: false, array: false},
52
+ asset_id: {type: 'integer', primary_key: false, null: true, array: false}
53
+ }.to_json
54
+ )
55
+ )
56
+
57
+ class ExampleRecord < ActiveRecord::Base
58
+ self.abstract_class = true
59
+ end
60
+
61
+ ExampleRecord.establish_connection(
62
+ adapter: 'sunstone',
63
+ url: 'http://example.com'
64
+ )
65
+
66
+ class Fleet < ExampleRecord
67
+ has_many :ships
68
+ end
69
+
70
+ class Ship < ExampleRecord
71
+ belongs_to :fleet
72
+
73
+ has_and_belongs_to_many :sailors
74
+
75
+ has_many :ownerships, as: :asset
76
+ has_many :nations, through: :ownerships, source: :country, inverse_of: :ships
77
+ end
78
+
79
+ class Sailor < ExampleRecord
80
+ has_and_belongs_to_many :ships
81
+ end
82
+
83
+ class Country < ExampleRecord
84
+ has_many :ownerships
85
+ has_many :fleets, through: :ownerships
86
+ end
87
+
88
+ class Ownership < ExampleRecord
89
+ belongs_to :country
90
+ belongs_to :asset, polymorphic: true
91
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Connection::ConfigurationTest < Minitest::Test
4
+
5
+ test "setting the url sets the api_key" do
6
+ connection = Sunstone::Connection.new(url: 'http://my_api_key@localhost')
7
+ assert_equal('my_api_key', connection.api_key)
8
+ end
9
+
10
+ test "setting the url sets the host" do
11
+ connection = Sunstone::Connection.new(url: 'https://example.com')
12
+ assert_equal('example.com', connection.host)
13
+ end
14
+
15
+ test "setting the url sets the port" do
16
+ connection = Sunstone::Connection.new(url: 'http://localhost')
17
+ assert_equal(80, connection.port)
18
+
19
+ connection = Sunstone::Connection.new(url: 'https://localhost')
20
+ assert_equal(443, connection.port)
21
+
22
+ connection = Sunstone::Connection.new(url: 'https://localhost:4321')
23
+ assert_equal(4321, connection.port)
24
+ end
25
+
26
+ test "setting the url sets the use_ssl option" do
27
+ connection = Sunstone::Connection.new(url: 'http://localhost')
28
+ assert_equal(false, connection.use_ssl)
29
+
30
+ connection = Sunstone::Connection.new(url: 'https://localhost')
31
+ assert_equal(true, connection.use_ssl)
32
+ end
33
+
34
+ test "setting the user_agent appends it to the User-Agent" do
35
+ connection = Sunstone::Connection.new(url: 'http://localhost')
36
+ assert_equal("Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", connection.user_agent)
37
+
38
+ connection = Sunstone::Connection.new(url: 'http://localhost', user_agent: "MyGem/3.14")
39
+ assert_equal("MyGem/3.14 Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", connection.user_agent)
40
+ end
41
+
42
+ end
43
+
44
+
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Connection::CookieStoreTest < Minitest::Test
4
+
5
+ test '#send_request(#<Net::HTTPRequest) adds cookies to the cookie store if present' do
6
+ store = CookieStore::HashStore.new
7
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
8
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get', :headers => {'Set-Cookie' => 'foo=bar; Max-Age=3600'})
9
+
10
+ Sunstone::Connection.with_cookie_store(store) { connection.get('/test') }
11
+
12
+ assert_equal 1, store.instance_variable_get(:@domains).size
13
+ assert_equal 1, store.instance_variable_get(:@domains)['testhost.com'].size
14
+ assert_equal 1, store.instance_variable_get(:@domains)['testhost.com']['/test'].size
15
+ assert_equal 'bar', store.instance_variable_get(:@domains)['testhost.com']['/test']['foo'].value
16
+ end
17
+
18
+ test '#send_request(#<Net::HTTPRequest) sends cookie header if cookie store is present' do
19
+ store = CookieStore::HashStore.new
20
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
21
+ stub_request(:get, "http://testhost.com/test").to_return(
22
+ headers: {
23
+ 'Set-Cookie' => 'foo=bar; Path="/" Max-Age=3600'
24
+ },
25
+ body: 'get'
26
+ )
27
+ cookie_stub = stub_request(:get, "http://testhost.com/verify").with { |req|
28
+ req.headers['Cookie'] == 'foo=bar'
29
+ }.to_return(body: 'verify')
30
+
31
+ Sunstone::Connection.with_cookie_store(store) { connection.get('/test') }
32
+ Sunstone::Connection.with_cookie_store(store) { connection.get('/verify') }
33
+
34
+ assert_requested(cookie_stub)
35
+ end
36
+
37
+ end
@@ -0,0 +1,105 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Connection::RequestHelpersTest < Minitest::Test
4
+
5
+ # Sunstone.get ==============================================================
6
+
7
+ test '#get(path)' do
8
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
9
+ stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
10
+
11
+ assert_equal('get', connection.get('/test').body)
12
+ end
13
+
14
+ test '#get(path, params) with params as string' do
15
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
16
+ stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
17
+
18
+ assert_equal 'get', connection.get('/test', 'key=value').body
19
+ end
20
+
21
+ test '#get(path, params) with params as hash' do
22
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
23
+ stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
24
+
25
+ assert_equal 'get', connection.get('/test', {:key => 'value'}).body
26
+ end
27
+
28
+ test '#get(path, &block)' do
29
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
30
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
31
+
32
+ connection.get('/test') do |response|
33
+ assert_equal 'get', response.body
34
+ end
35
+ end
36
+
37
+ # Sunstone.post =============================================================
38
+
39
+ test '#post(path)' do
40
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
41
+ stub_request(:post, "http://testhost.com/test").to_return(:body => "post")
42
+
43
+ assert_equal('post', connection.post('/test').body)
44
+ end
45
+
46
+ test '#post(path, body)' do
47
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
48
+ stub_request(:post, "http://testhost.com/test").with(:body => 'body').to_return(:body => "post")
49
+
50
+ assert_equal('post', connection.post('/test', 'body').body)
51
+ end
52
+
53
+ test '#post(path, &block)' do
54
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
55
+ stub_request(:post, "http://testhost.com/test").to_return(:body => 'post')
56
+
57
+ connection.post('/test') do |response|
58
+ assert_equal 'post', response.body
59
+ end
60
+ end
61
+
62
+ # Sunstone.put ==============================================================
63
+
64
+ test '#put(path)' do
65
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
66
+ stub_request(:put, "http://testhost.com/test").to_return(:body => "put")
67
+
68
+ assert_equal('put', connection.put('/test').body)
69
+ end
70
+
71
+ test '#put(path, body)' do
72
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
73
+ stub_request(:put, "http://testhost.com/test").with(:body => 'body').to_return(:body => "put")
74
+
75
+ assert_equal('put', connection.put('/test', 'body').body)
76
+ end
77
+
78
+ test '#put(path, &block)' do
79
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
80
+ stub_request(:put, "http://testhost.com/test").to_return(:body => 'put')
81
+
82
+ connection.put('/test') do |response|
83
+ assert_equal 'put', response.body
84
+ end
85
+ end
86
+
87
+ # Sunstone.delete ===========================================================
88
+
89
+ test '#delete' do
90
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
91
+ stub_request(:delete, "http://testhost.com/test").to_return(:body => "delete")
92
+
93
+ assert_equal('delete', connection.delete('/test').body)
94
+ end
95
+
96
+ test '#delete(path, &block)' do
97
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
98
+ stub_request(:delete, "http://testhost.com/test").to_return(:body => 'delete')
99
+
100
+ connection.delete('/test') do |response|
101
+ assert_equal 'delete', response.body
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,164 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Connection::SendRequestTest < Minitest::Test
4
+
5
+ test '#send_request(#<Net::HTTPRequest>) includes the api-key header when present' do
6
+ connection = Sunstone::Connection.new(url: "http://my_api_key@example.com")
7
+
8
+ test_stub = stub_request(:get, "http://example.com/verify").with { |req|
9
+ req.headers['Api-Key'] == 'my_api_key'
10
+ }
11
+ connection.get('/verify')
12
+ assert_requested(test_stub)
13
+ end
14
+
15
+ test '#send_request(#<Net::HTTPRequest>) includes the user_agent' do
16
+ connection = Sunstone::Connection.new(url: "http://example.com")
17
+
18
+ test_stub = stub_request(:get, "http://example.com/verify").with { |req|
19
+ req.headers['User-Agent'] =~ /Sunstone\/\S+ Ruby\/\S+ \S+/
20
+ }
21
+ connection.get('/verify')
22
+ assert_requested(test_stub)
23
+
24
+ # Custom Agent
25
+ connection = Sunstone::Connection.new(url: "http://example.com", user_agent: "MyClient/2")
26
+
27
+ test_stub = stub_request(:get, "http://example.com/verify").with { |req|
28
+ req.headers['User-Agent'] =~ /MyClient\/2 Sunstone\/\S+ Ruby\/\S+ \S+/
29
+ }
30
+ connection.get('/verify')
31
+ assert_requested(test_stub)
32
+ end
33
+
34
+ test '#send_request(#<Net::HTTPRequest>)' do
35
+ stub_request(:get, "http://testhost.com/test").to_return(body: 'get')
36
+
37
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
38
+ assert_equal('get', connection.send_request(Net::HTTP::Get.new('/test')).body)
39
+ end
40
+
41
+ test '#send_request(#<Net::HTTPRequest>, body) with string body' do
42
+ stub_request(:post, "http://testhost.com/test").with(
43
+ body: '{"key":"value"}'
44
+ ).to_return(
45
+ body: "post"
46
+ )
47
+
48
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
49
+ assert_equal('post', connection.send_request(Net::HTTP::Post.new('/test'), '{"key":"value"}').body)
50
+ end
51
+
52
+ test '#send_request(#<Net::HTTPRequest>, body) with IO body' do
53
+ stub_request(:post, "http://testhost.com/test").with { |request|
54
+ request.headers['Transfer-Encoding'] == "chunked" && request.body == '{"key":"value"}'
55
+ }.to_return(:body => "post")
56
+
57
+ rd, wr = IO.pipe
58
+ wr.write('{"key":"value"}')
59
+ wr.close
60
+
61
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
62
+ assert_equal('post', connection.send_request(Net::HTTP::Post.new('/test'), rd).body)
63
+ end
64
+
65
+ test '#send_request(#<Net::HTTPRequest>, body) with Ruby Object body' do
66
+ stub_request(:post, "http://testhost.com/test").with(body: '{"key":"value"}').to_return(body: "post")
67
+
68
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
69
+ assert_equal('post', connection.send_request(Net::HTTP::Post.new('/test'), {:key => 'value'}).body)
70
+ end
71
+
72
+ test '#send_request(#<Net::HTTPRequest>) raises Sunstone::Exceptions on non-200 responses' do
73
+ stub_request(:get, "http://testhost.com/400").to_return(status: 400)
74
+ stub_request(:get, "http://testhost.com/401").to_return(status: 401)
75
+ stub_request(:get, "http://testhost.com/403").to_return(status: 403)
76
+ stub_request(:get, "http://testhost.com/404").to_return(status: 404)
77
+ stub_request(:get, "http://testhost.com/410").to_return(status: 410)
78
+ stub_request(:get, "http://testhost.com/422").to_return(status: 422)
79
+ stub_request(:get, "http://testhost.com/450").to_return(status: 450)
80
+ stub_request(:get, "http://testhost.com/503").to_return(status: 503)
81
+ stub_request(:get, "http://testhost.com/550").to_return(status: 550)
82
+
83
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
84
+ assert_raises(Sunstone::Exception::BadRequest) { connection.send_request(Net::HTTP::Get.new('/400')) }
85
+ assert_raises(Sunstone::Exception::Unauthorized) { connection.send_request(Net::HTTP::Get.new('/401')) }
86
+ assert_raises(Sunstone::Exception::Forbidden) { connection.send_request(Net::HTTP::Get.new('/403')) }
87
+ assert_raises(Sunstone::Exception::NotFound) { connection.send_request(Net::HTTP::Get.new('/404')) }
88
+ assert_raises(Sunstone::Exception::Gone) { connection.send_request(Net::HTTP::Get.new('/410')) }
89
+ assert_raises(Sunstone::Exception::ApiVersionUnsupported) { connection.send_request(Net::HTTP::Get.new('/422')) }
90
+ assert_raises(Sunstone::Exception) { connection.send_request(Net::HTTP::Get.new('/450')) }
91
+ assert_raises(Sunstone::Exception::ServiceUnavailable) { connection.send_request(Net::HTTP::Get.new('/503')) }
92
+ assert_raises(Sunstone::Exception) { connection.send_request(Net::HTTP::Get.new('/550')) }
93
+ end
94
+
95
+ test '#send_request(#<Net::HTTPRequest>, &block) returns value returned from &block' do
96
+ stub_request(:get, "http://testhost.com/test").to_return(body: 'get')
97
+
98
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
99
+ value = connection.send_request(Net::HTTP::Get.new('/test')) do |response|
100
+ 3215
101
+ end
102
+
103
+ assert_equal 3215, value
104
+ end
105
+
106
+ test '#send_request(#<Net::HTTPRequest>, &block)' do
107
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
108
+ stub_request(:get, "http://testhost.com/test").to_return(body: 'get')
109
+
110
+ connection.send_request(Net::HTTP::Get.new('/test')) do |response|
111
+ assert_equal 'get', response.body
112
+ end
113
+
114
+ # make sure block is not called when not in valid_response_codes
115
+ stub_request(:get, "http://testhost.com/test").to_return(status: 401, body: 'get')
116
+
117
+ assert_raises(Sunstone::Exception::Unauthorized) {
118
+ connection.send_request(Net::HTTP::Get.new('/test')) do |response|
119
+ raise Sunstone::Exception, 'Should not get here'
120
+ end
121
+ }
122
+ end
123
+
124
+ test '#send_request(#<Net::HTTPRequest>, &block) with block reading chunks' do
125
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
126
+
127
+ rd, wr = IO.pipe
128
+ rd = Net::BufferedIO.new(rd)
129
+ wr.write(<<-DATA.gsub(/^ +/, '').gsub(/\n/, "\r\n"))
130
+ HTTP/1.1 200 OK
131
+ Content-Length: 5
132
+
133
+ hello
134
+ DATA
135
+
136
+ res = Net::HTTPResponse.read_new(rd)
137
+ mock_connection = mock('connection')
138
+ mock_connection.stubs(:request).yields(res)
139
+ connection.instance_variable_set(:@connection, mock_connection)
140
+
141
+ res.reading_body(rd, true) do
142
+ connection.send_request(Net::HTTP::Get.new('/test')) do |response|
143
+ response.read_body do |chunk|
144
+ assert_equal('hello', chunk)
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ # TODO: support multple depreaction-notice headers
151
+ test 'deprecation warning printed when deprecation header returned' do
152
+ connection = Sunstone::Connection.new(url: "http://testhost.com")
153
+
154
+ stub_request(:get, "http://testhost.com/test").to_return(
155
+ body: 'get',
156
+ headers: { 'Deprecation-Notice': 'my deprecation message' }
157
+ )
158
+
159
+ ActiveSupport::Deprecation.expects(:warn).with('my deprecation message')
160
+
161
+ connection.send_request(Net::HTTP::Get.new('/test'))
162
+ end
163
+
164
+ end