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
@@ -1,48 +0,0 @@
1
- module ActiveRecord::Associations::Builder
2
- class HasAndBelongsToMany # :nodoc:
3
-
4
- def through_model
5
- habtm = JoinTableResolver.build lhs_model, association_name, options
6
-
7
- join_model = Class.new(lhs_model.base_class.superclass) {
8
- class << self;
9
- attr_accessor :class_resolver
10
- attr_accessor :name
11
- attr_accessor :table_name_resolver
12
- attr_accessor :left_reflection
13
- attr_accessor :right_reflection
14
- end
15
-
16
- def self.table_name
17
- table_name_resolver.join_table
18
- end
19
-
20
- def self.compute_type(class_name)
21
- class_resolver.compute_type class_name
22
- end
23
-
24
- def self.add_left_association(name, options)
25
- belongs_to name, options
26
- self.left_reflection = _reflect_on_association(name)
27
- end
28
-
29
- def self.add_right_association(name, options)
30
- rhs_name = name.to_s.singularize.to_sym
31
- belongs_to rhs_name, options
32
- self.right_reflection = _reflect_on_association(rhs_name)
33
- end
34
-
35
- }
36
-
37
- join_model.name = "HABTM_#{association_name.to_s.camelize}"
38
- join_model.table_name_resolver = habtm
39
- join_model.class_resolver = lhs_model
40
- join_model.primary_key = nil
41
-
42
- join_model.add_left_association :left_side, anonymous_class: lhs_model
43
- join_model.add_right_association association_name, belongs_to_options(options)
44
- join_model
45
- end
46
-
47
- end
48
- end
@@ -1,32 +0,0 @@
1
- module ActiveRecord
2
- module Calculations
3
- def pluck(*column_names)
4
-
5
- # column_names.map! do |column_name|
6
- # if column_name.is_a?(Symbol) && attribute_alias?(column_name)
7
- # attribute_alias(column_name)
8
- # else
9
- # column_name.to_s
10
- # end
11
- # end
12
-
13
- if loaded? && (column_names - @klass.column_names).empty?
14
- return @records.pluck(*column_names)
15
- end
16
-
17
- if has_include?(column_names.first)
18
- construct_relation_for_association_calculations.pluck(*column_names)
19
- else
20
- relation = spawn
21
- relation.select_values = column_names.map { |cn|
22
- columns_hash.key?(cn) ? arel_table[cn] : cn
23
- }
24
-
25
- result = klass.connection.exec_query(relation.arel, nil, relation.arel.bind_values + bind_values)
26
- result.cast_values(klass.column_types)
27
- end
28
- end
29
-
30
- end
31
- end
32
-
@@ -1,30 +0,0 @@
1
- module ActiveRecord
2
- module QueryMethods
3
-
4
- def reverse_sql_order(order_query)
5
- order_query = [arel_table[primary_key].asc] if order_query.empty?
6
-
7
- order_query.flat_map do |o|
8
- case o
9
- when Arel::Nodes::Ordering
10
- o.reverse
11
- when String
12
- o.to_s.split(',').map! do |s|
13
- s.strip!
14
- s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC')
15
- end
16
- else
17
- o
18
- end
19
- end
20
- end
21
-
22
- def joins(*args)
23
- # If a joins happen with sunstone messes up visitor / collector
24
- return self if klass.connection.class.name == "ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter"
25
- check_if_method_has_arguments!(:joins, args)
26
- spawn.joins!(*args)
27
- end
28
-
29
- end
30
- end
@@ -1,23 +0,0 @@
1
- module ActiveRecord
2
- class PredicateBuilder # :nodoc:
3
-
4
- def self.expand(klass, table, column, value)
5
- queries = []
6
-
7
- # In standard Rails where takes :table => { columns }, but in sunstone we
8
- # can can do nested tables eg: where(:properties => { :regions => {:id => 1}})
9
- if klass && reflection = klass._reflect_on_association(column)
10
- if reflection.polymorphic? && base_class = polymorphic_base_class_from_value(value)
11
- queries << build(table[reflection.foreign_type], base_class)
12
- end
13
-
14
- # column = reflection.foreign_key
15
- column # Don't need Rails to assume we are referencing a table
16
- end
17
-
18
- queries << build(table[column], value)
19
- queries
20
- end
21
-
22
- end
23
- end
@@ -1,14 +0,0 @@
1
- class Fleet < ActiveRecord::Base
2
- has_many :ships
3
- end
4
-
5
- class Ship < ActiveRecord::Base
6
- belongs_to :fleet
7
-
8
- has_and_belongs_to_many :sailors
9
- end
10
-
11
- class Sailor < ActiveRecord::Base
12
- has_and_belongs_to_many :sailors
13
- end
14
-
@@ -1,134 +0,0 @@
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 column is nil' do
62
- stub_request(:get, URI::escape('malomalo.io/ships?where[leased_at]')).to_return(body: [].to_json)
63
-
64
- assert_equal [], Ship.where(:leased_at => nil).to_a
65
- end
66
-
67
- test '::where on belongs_to relation' do
68
- stub_request(:get, URI::escape('malomalo.io/ships?where[fleet][id]=1')).to_return(body: [].to_json)
69
-
70
- assert_equal [], Ship.where(:fleet => {id: 1}).to_a
71
- end
72
-
73
- test '::where on has_many relation' do
74
- stub_request(:get, URI::escape('malomalo.io/fleets?where[ships][id]=1')).to_return(body: [].to_json)
75
-
76
- assert_equal [], Fleet.where(:ships => {id: 1}).to_a
77
- end
78
-
79
- test '::where on has_and_belongs_to_many relation' do
80
- stub_request(:get, URI::escape('malomalo.io/ships?where[sailors][id]=1')).to_return(body: [].to_json)
81
-
82
- assert_equal [], Ship.where(:sailors => {id: 1}).to_a
83
- end
84
-
85
- test '::count' do
86
- stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][count]=*')).to_return(body: '[10]')
87
-
88
- assert_equal 10, Ship.count
89
- end
90
-
91
- test '::count(:column)' do
92
- stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][count]=id')).to_return(body: '[10]')
93
-
94
- assert_equal 10, Ship.count(:id)
95
- end
96
-
97
- test '::sum(:column)' do
98
- stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][sum]=weight')).to_return(body: '[10]')
99
-
100
- assert_equal 10, Ship.sum(:weight)
101
- end
102
-
103
- # Relation test
104
-
105
- test '#to_sql' do
106
- stub_request(:get, URI::escape('malomalo.io/ships?where[id]=10')).to_return(body: [].to_json)
107
-
108
- assert_equal "SELECT ships.* FROM ships WHERE ships.id = 10", Ship.where(:id => 10).to_sql
109
- end
110
-
111
- # Preload test
112
-
113
- test '#preload' do
114
- stub_request(:get, URI::escape('malomalo.io/fleets')).to_return(body: [{id: 1}].to_json)
115
- stub_request(:get, URI::escape('malomalo.io/ships?where[fleet_id][0]=1')).to_return(body: [{id: 1, fleet_id: 1}].to_json)
116
-
117
- fleets = Fleet.preload(:ships)
118
- assert_equal [1], fleets.map(&:id)
119
- assert_equal [1], fleets.first.ships.map(&:id)
120
- end
121
-
122
- # Eagerload test
123
-
124
- test '#eager_load' do
125
- stub_request(:get, URI::escape('malomalo.io/fleets?include[]=ships')).to_return(body: [{
126
- id: 1, ships: [{id: 1, fleet_id: 1}]
127
- }].to_json)
128
-
129
- fleets = Fleet.eager_load(:ships)
130
- assert_equal [1], fleets.map(&:id)
131
- assert_equal [1], fleets.first.ships.map(&:id)
132
- end
133
-
134
- end
@@ -1,124 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestParserModel < Sunstone::Model
4
-
5
- belongs_to :test_parser_model
6
-
7
- has_many :test_parser_models
8
-
9
- define_schema do
10
- integer :testid
11
- boolean :red
12
- datetime :created_at
13
- decimal :rate
14
- string :name
15
- string :nicknames, :array => true
16
- end
17
- end
18
-
19
- class Sunstone::ParserTest < Minitest::Test
20
-
21
- test '::parse(klass, string)' do
22
- assert_equal true, Sunstone::Parser.parse(TestParserModel, '{"red": true}').red
23
- end
24
-
25
- test '::parse(model_instance, string)' do
26
- model = TestParserModel.new
27
- Sunstone::Parser.parse(model, '{"red": true}')
28
- assert_equal true, model.red
29
- end
30
-
31
- test '::parse(klass, response)' do
32
- Sunstone.site = "http://test_api_key@testhost.com"
33
- stub_request(:get, "http://testhost.com/test").to_return(:body => '{"red": true}')
34
-
35
- model = Sunstone.get('/test') do |response|
36
- Sunstone::Parser.parse(TestParserModel, response)
37
- end
38
-
39
- assert_equal true, model.red
40
- end
41
-
42
- test "parse boolean attributes" do
43
- parser = Sunstone::Parser.new(TestParserModel)
44
- assert_equal true, parser.parse('{"red": true}').red
45
-
46
- parser = Sunstone::Parser.new(TestParserModel)
47
- assert_equal false, parser.parse('{"red": false}').red
48
- end
49
-
50
- test "parse date attributes" do
51
- parser = Sunstone::Parser.new(TestParserModel)
52
- assert_equal DateTime.new(2014, 7, 14, 16, 44, 15, '-7'), parser.parse('{"created_at": "2014-07-14T16:44:15-07:00"}').created_at
53
- end
54
-
55
- test "parse decimal attributes" do
56
- parser = Sunstone::Parser.new(TestParserModel)
57
- assert_equal 10.254, parser.parse('{"rate": 10.254}').rate
58
- end
59
-
60
- test "parse integer attributes" do
61
- parser = Sunstone::Parser.new(TestParserModel)
62
- assert_equal 123654, parser.parse('{"testid": 123654}').testid
63
- end
64
-
65
- test "parse string attributes" do
66
- parser = Sunstone::Parser.new(TestParserModel)
67
- assert_equal "my name", parser.parse('{"name": "my name"}').name
68
- end
69
-
70
- test "parse array attribute" do
71
- parser = Sunstone::Parser.new(TestParserModel)
72
- assert_equal ["name 1", "name 2"], parser.parse('{"nicknames": ["name 1", "name 2"]}').nicknames
73
- end
74
-
75
- test "parse skips over unkown key" do
76
- assert_nothing_raised do
77
- Sunstone::Parser.parse(TestParserModel, '{"other_key": "name 2"}')
78
- Sunstone::Parser.parse(TestParserModel, '{"other_key": ["name 1", "name 2"]}')
79
- end
80
- end
81
-
82
- test "parse belong_to association" do
83
- parser = Sunstone::Parser.new(TestParserModel)
84
- assert_equal({
85
- :rate => BigDecimal.new("10.254"),
86
- :created_at => DateTime.new(2014, 7, 14, 16, 44, 15, '-7'),
87
- :testid => 123654,
88
- :name => "my name",
89
- :nicknames => ["name 1", "name 2"]
90
- }, parser.parse('{"test_parser_model": {
91
- "rate": 10.254,
92
- "created_at": "2014-07-14T16:44:15-07:00",
93
- "testid": 123654,
94
- "name": "my name",
95
- "nicknames": ["name 1", "name 2"]
96
- }}').test_parser_model.instance_variable_get(:@attributes))
97
- end
98
-
99
- test "parse has_many association" do
100
- parser = Sunstone::Parser.new(TestParserModel)
101
- attrs = {
102
- :rate => BigDecimal.new("10.254"),
103
- :created_at => DateTime.new(2014, 7, 14, 16, 44, 15, '-7'),
104
- :testid => 123654,
105
- :name => "my name",
106
- :nicknames => ["name 1", "name 2"]
107
- }
108
-
109
- assert_equal([attrs, attrs], parser.parse('{"test_parser_models": [{
110
- "rate": 10.254,
111
- "created_at": "2014-07-14T16:44:15-07:00",
112
- "testid": 123654,
113
- "name": "my name",
114
- "nicknames": ["name 1", "name 2"]
115
- }, {
116
- "rate": 10.254,
117
- "created_at": "2014-07-14T16:44:15-07:00",
118
- "testid": 123654,
119
- "name": "my name",
120
- "nicknames": ["name 1", "name 2"]
121
- }]}').test_parser_models.map{|m| m.instance_variable_get(:@attributes)})
122
- end
123
-
124
- end
@@ -1,303 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SunstoneTest < Minitest::Test
4
-
5
- def setup
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
302
-
303
- end