sunstone 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::ModelTest < Minitest::Test
4
+
5
+ def setup
6
+ Sunstone.site = "http://test_api_key@testhost.com"
7
+ end
8
+
9
+ #TODO: test initializer
10
+
11
+ end
@@ -0,0 +1,124 @@
1
+ require 'test_helper'
2
+
3
+ class TestModel < Sunstone::Model
4
+
5
+ belongs_to :test_model
6
+
7
+ has_many :test_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(TestModel, '{"red": true}').red
23
+ end
24
+
25
+ test '::parse(model_instance, string)' do
26
+ model = TestModel.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(TestModel, response)
37
+ end
38
+
39
+ assert_equal true, model.red
40
+ end
41
+
42
+ test "parse boolean attributes" do
43
+ parser = Sunstone::Parser.new(TestModel)
44
+ assert_equal true, parser.parse('{"red": true}').red
45
+
46
+ parser = Sunstone::Parser.new(TestModel)
47
+ assert_equal false, parser.parse('{"red": false}').red
48
+ end
49
+
50
+ test "parse date attributes" do
51
+ parser = Sunstone::Parser.new(TestModel)
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(TestModel)
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(TestModel)
62
+ assert_equal 123654, parser.parse('{"testid": 123654}').testid
63
+ end
64
+
65
+ test "parse string attributes" do
66
+ parser = Sunstone::Parser.new(TestModel)
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(TestModel)
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(TestModel, '{"other_key": "name 2"}')
78
+ Sunstone::Parser.parse(TestModel, '{"other_key": ["name 1", "name 2"]}')
79
+ end
80
+ end
81
+
82
+ test "parse belong_to association" do
83
+ parser = Sunstone::Parser.new(TestModel)
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_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_model.instance_variable_get(:@attributes))
97
+ end
98
+
99
+ test "parse has_many association" do
100
+ parser = Sunstone::Parser.new(TestModel)
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_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_models.map{|m| m.instance_variable_get(:@attributes)})
122
+ end
123
+
124
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::SchemaTest < Minitest::Test
4
+
5
+ test "type methods" do
6
+ schema = Sunstone::Schema.new
7
+
8
+ schema.boolean(:boolean)
9
+ schema.datetime(:datetime)
10
+ schema.decimal(:decimal)
11
+ schema.integer(:integer)
12
+ schema.string(:string)
13
+
14
+ {
15
+ :boolean => Sunstone::Type::Boolean,
16
+ :datetime => Sunstone::Type::DateTime,
17
+ :decimal => Sunstone::Type::Decimal,
18
+ :integer => Sunstone::Type::Integer,
19
+ :string => Sunstone::Type::String
20
+ }.each do |name, klass|
21
+ assert_kind_of klass, schema[name]
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Type::BooleanTest < Minitest::Test
4
+
5
+ test "type casting" do
6
+ type = Sunstone::Type::Boolean.new
7
+
8
+ assert_equal true, type.type_cast_from_user(true)
9
+ assert_equal true, type.type_cast_from_user('true')
10
+ assert_equal true, type.type_cast_from_user('TRUE')
11
+
12
+ assert_equal false, type.type_cast_from_user(false)
13
+ assert_equal false, type.type_cast_from_user('false')
14
+ assert_equal false, type.type_cast_from_user('FALSE')
15
+ end
16
+
17
+ test "#type_cast_for_json" do
18
+ type = Sunstone::Type::Boolean.new
19
+
20
+ assert_equal true, type.type_cast_for_json(true)
21
+ assert_equal false, type.type_cast_for_json(false)
22
+ end
23
+
24
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Type::DateTimeTest < Minitest::Test
4
+
5
+ test "#type_cast_from_user" do
6
+ type = Sunstone::Type::DateTime.new
7
+
8
+ assert_equal DateTime.new(2001, 2, 3, 4, 5, 6, '+7'), type.type_cast_from_user('2001-02-03T04:05:06+07:00')
9
+ assert_equal DateTime.new(2001, 2, 3, 4, 5, 6, '+7'), type.type_cast_from_user('20010203T040506+0700')
10
+ assert_equal DateTime.new(2001, 2, 3, 4, 5, 6, '+7'), type.type_cast_from_user('2001-W05-6T04:05:06+07:00')
11
+ assert_equal DateTime.new(2014, 7, 14, 16, 44, 15, '-7'), type.type_cast_from_user("2014-07-14T16:44:15-07:00")
12
+ assert_equal Rational(123,1000), type.type_cast_from_user('2001-02-03T04:05:06.123+07:00').sec_fraction
13
+ end
14
+
15
+ test "#type_cast_from_json" do
16
+ type = Sunstone::Type::DateTime.new
17
+
18
+ assert_equal DateTime.new(2001, 2, 3, 4, 5, 6, '+7'), type.type_cast_from_json('2001-02-03T04:05:06+07:00')
19
+ assert_equal DateTime.new(2001, 2, 3, 4, 5, 6, '+7'), type.type_cast_from_json('20010203T040506+0700')
20
+ assert_equal DateTime.new(2001, 2, 3, 4, 5, 6, '+7'), type.type_cast_from_json('2001-W05-6T04:05:06+07:00')
21
+ assert_equal DateTime.new(2014, 7, 14, 16, 44, 15, '-7'), type.type_cast_from_json("2014-07-14T16:44:15-07:00")
22
+ assert_equal Rational(123,1000), type.type_cast_from_json('2001-02-03T04:05:06.123+07:00').sec_fraction
23
+ end
24
+
25
+ test "#type_cast_for_json" do
26
+ type = Sunstone::Type::DateTime.new
27
+
28
+ assert_equal '2001-02-03T04:05:06.000+07:00', type.type_cast_for_json(DateTime.new(2001, 2, 3, 4, 5, 6, '+7'))
29
+ end
30
+
31
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Type::DecimalTest < Minitest::Test
4
+
5
+ test "#type_cast_from_user" do
6
+ type = Sunstone::Type::Decimal.new
7
+
8
+ assert_equal BigDecimal.new("10.50"), type.type_cast_from_user(BigDecimal.new("10.50"))
9
+ assert_equal BigDecimal.new("10.50"), type.type_cast_from_user(10.50)
10
+ assert_equal BigDecimal.new("10.50"), type.type_cast_from_user("10.50")
11
+ end
12
+
13
+ test "#type_cast_from_json" do
14
+ type = Sunstone::Type::Decimal.new
15
+
16
+ assert_equal BigDecimal.new("10.50"), type.type_cast_from_json(BigDecimal.new("10.50"))
17
+ assert_equal BigDecimal.new("10.50"), type.type_cast_from_json(10.50)
18
+ assert_equal BigDecimal.new("10.50"), type.type_cast_from_json("10.50")
19
+ end
20
+
21
+ test "#type_cast_for_json" do
22
+ type = Sunstone::Type::Decimal.new
23
+
24
+ assert_equal BigDecimal.new("10.50"), type.type_cast_for_json(BigDecimal.new("10.50"))
25
+ end
26
+
27
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Type::IntegerTest < Minitest::Test
4
+
5
+ test "#type_cast_from_user" do
6
+ type = Sunstone::Type::Integer.new
7
+
8
+ assert_equal 10, type.type_cast_from_user(10)
9
+ assert_equal 10, type.type_cast_from_user("10")
10
+ assert_equal 1, type.type_cast_from_user(true)
11
+ assert_equal 0, type.type_cast_from_user(false)
12
+ end
13
+
14
+ test "#type_cast_from_json" do
15
+ type = Sunstone::Type::Integer.new
16
+
17
+ assert_equal 10, type.type_cast_from_json(10)
18
+ assert_equal 10, type.type_cast_from_json("10")
19
+ assert_equal 1, type.type_cast_from_json(true)
20
+ assert_equal 0, type.type_cast_from_json(false)
21
+ end
22
+
23
+ test "#type_cast_for_json" do
24
+ type = Sunstone::Type::Integer.new
25
+
26
+ assert_equal 10, type.type_cast_for_json(10)
27
+ end
28
+
29
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Type::StringTest < Minitest::Test
4
+
5
+ test "#type_cast_from_user" do
6
+ type = Sunstone::Type::String.new
7
+
8
+ assert_equal "1", type.type_cast_from_user(true)
9
+ assert_equal "0", type.type_cast_from_user(false)
10
+ assert_equal "123", type.type_cast_from_user(123)
11
+ assert_equal "string", type.type_cast_from_user("string")
12
+ end
13
+
14
+ test "#type_cast_from_json" do
15
+ type = Sunstone::Type::String.new
16
+
17
+ assert_equal "1", type.type_cast_from_json(true)
18
+ assert_equal "0", type.type_cast_from_json(false)
19
+ assert_equal "123", type.type_cast_from_json(123)
20
+ assert_equal "string", type.type_cast_from_json("string")
21
+ end
22
+
23
+ test "#type_cast_for_json" do
24
+ type = Sunstone::Type::String.new
25
+
26
+ assert_equal "10", type.type_cast_for_json("10")
27
+ end
28
+
29
+ test "values are duped coming out" do
30
+ s = "foo"
31
+ type = Sunstone::Type::String.new
32
+
33
+ assert_not_same s, type.type_cast_from_user(s)
34
+ assert_not_same s, type.type_cast_from_json(s)
35
+ end
36
+
37
+ test "string mutations are detected" # do
38
+ # klass = Class.new(Model)
39
+ # klass.table_name = 'authors'
40
+ #
41
+ # author = klass.create!(name: 'Sean')
42
+ # assert_not author.changed?
43
+ #
44
+ # author.name << ' Griffin'
45
+ # assert author.name_changed?
46
+ #
47
+ # author.save!
48
+ # author.reload
49
+ #
50
+ # assert_equal 'Sean Griffin', author.name
51
+ # assert_not author.changed?
52
+ # end
53
+
54
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class Sunstone::Type::ValueTest < Minitest::Test
4
+
5
+ test "#readonly?" do
6
+ type = Sunstone::Type::Value.new(:readonly => true)
7
+ assert_equal true, type.readonly?
8
+ end
9
+
10
+ test "array support" do
11
+ type = Sunstone::Type::Value.new(:array => true)
12
+
13
+ type.expects(:_type_cast).with(1).returns(1).twice
14
+ type.expects(:_type_cast).with('2').returns('2').twice
15
+ type.expects(:_type_cast).with(:a).returns(:a).twice
16
+
17
+ assert_equal([1, '2', :a], type.type_cast_from_user([1, '2', :a]))
18
+ assert_equal([1, '2', :a], type.type_cast_from_json([1, '2', :a]))
19
+ end
20
+
21
+ test "#type_cast_for_json" do
22
+ type = Sunstone::Type::Value.new(:array => true)
23
+
24
+ assert_equal [1, '2', :a], type.type_cast_for_json([1, '2', :a])
25
+ end
26
+
27
+ end
@@ -0,0 +1,302 @@
1
+ require 'test_helper'
2
+
3
+ class SunstoneTest < Minitest::Test
4
+
5
+ def setup
6
+ Sunstone.site = "http://test_api_key@testhost.com"
7
+ end
8
+
9
+ # Sunstone.site= ============================================================
10
+
11
+ test "setting the site sets the api_key" do
12
+ Sunstone.site = 'https://my_api_key@localhost'
13
+ assert_equal('my_api_key', Sunstone.api_key)
14
+ end
15
+
16
+ test "setting the site sets the host" do
17
+ Sunstone.site = 'https://my_api_key@example.com'
18
+ assert_equal('example.com', Sunstone.host)
19
+ end
20
+
21
+ test "setting the site sets the port" do
22
+ Sunstone.site = 'http://my_api_key@example.com'
23
+ assert_equal(80, Sunstone.port)
24
+
25
+ Sunstone.site = 'https://my_api_key@example.com'
26
+ assert_equal(443, Sunstone.port)
27
+
28
+ Sunstone.site = 'https://my_api_key@example.com:4321'
29
+ assert_equal(4321, Sunstone.port)
30
+ end
31
+
32
+ test "setting the site sets the use_ssl option" do
33
+ Sunstone.site = 'http://my_api_key@example.com'
34
+ assert_equal(false, Sunstone.use_ssl)
35
+
36
+ Sunstone.site = 'https://my_api_key@example.com'
37
+ assert_equal(true, Sunstone.use_ssl)
38
+ end
39
+
40
+ # Sunstone.user_agent= ======================================================
41
+ test "setting the user_agent appends it to the User-Agent" do
42
+ assert_equal("Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", Sunstone.user_agent)
43
+
44
+ Sunstone.user_agent = "MyGem/3.14"
45
+ assert_equal("MyGem/3.14 Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", Sunstone.user_agent)
46
+ Sunstone.user_agent = nil
47
+ end
48
+
49
+ # Sunstone.with_cookie_store ================================================
50
+
51
+ test '#with_cookie_store(store, &block) sets the cookie-store' do
52
+ assert_nil Thread.current[:sunstone_cookie_store]
53
+ Sunstone.with_cookie_store('my_store') do
54
+ assert_equal 'my_store', Thread.current[:sunstone_cookie_store]
55
+ end
56
+ assert_nil Thread.current[:sunstone_cookie_store]
57
+ end
58
+
59
+ # Sunstone.send_request =====================================================
60
+
61
+ test '#send_request(#<Net::HTTPRequest>)' do
62
+ stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
63
+
64
+ assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
65
+ end
66
+
67
+ test '#send_request(#<Net::HTTPRequest>, body) with string body' do
68
+ stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
69
+
70
+ assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), '{"key":"value"}').body)
71
+ end
72
+
73
+ test '#send_request(#<Net::HTTPRequest>, body) with IO body' do
74
+ stub_request(:post, "http://testhost.com/test").with { |request|
75
+ request.headers['Transfer-Encoding'] == "chunked" && request.body == '{"key":"value"}'
76
+ }.to_return(:body => "post")
77
+
78
+ rd, wr = IO.pipe
79
+ wr.write('{"key":"value"}')
80
+ wr.close
81
+
82
+ assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), rd).body)
83
+ end
84
+
85
+ test '#send_request(#<Net::HTTPRequest>, body) with Ruby Object body' do
86
+ stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
87
+
88
+ assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), {:key => 'value'}).body)
89
+ end
90
+
91
+ test '#send_request(#<Net::HTTPRequest>) raises Sunstone::Exceptions on non-200 responses' do
92
+ stub_request(:get, "http://testhost.com/400").to_return(:status => 400)
93
+ stub_request(:get, "http://testhost.com/401").to_return(:status => 401)
94
+ stub_request(:get, "http://testhost.com/404").to_return(:status => 404)
95
+ stub_request(:get, "http://testhost.com/410").to_return(:status => 410)
96
+ stub_request(:get, "http://testhost.com/422").to_return(:status => 422)
97
+ stub_request(:get, "http://testhost.com/450").to_return(:status => 450)
98
+ stub_request(:get, "http://testhost.com/503").to_return(:status => 503)
99
+ stub_request(:get, "http://testhost.com/550").to_return(:status => 550)
100
+
101
+ assert_raises(Sunstone::Exception::BadRequest) { Sunstone.send_request(Net::HTTP::Get.new('/400')) }
102
+ assert_raises(Sunstone::Exception::Unauthorized) { Sunstone.send_request(Net::HTTP::Get.new('/401')) }
103
+ assert_raises(Sunstone::Exception::NotFound) { Sunstone.send_request(Net::HTTP::Get.new('/404')) }
104
+ assert_raises(Sunstone::Exception::Gone) { Sunstone.send_request(Net::HTTP::Get.new('/410')) }
105
+ assert_raises(Sunstone::Exception::ApiVersionUnsupported) { Sunstone.send_request(Net::HTTP::Get.new('/422')) }
106
+ assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/450')) }
107
+ assert_raises(Sunstone::Exception::ServiceUnavailable) { Sunstone.send_request(Net::HTTP::Get.new('/503')) }
108
+ assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/550')) }
109
+ end
110
+
111
+ test '#send_request(#<Net::HTTPRequest>, &block) returns value returned from &block' do
112
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
113
+
114
+ value = Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
115
+ 3215
116
+ end
117
+
118
+ assert_equal 3215, value
119
+ end
120
+
121
+ test '#send_request(#<Net::HTTPRequest>, &block)' do
122
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
123
+
124
+ Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
125
+ assert_equal 'get', response.body
126
+ end
127
+
128
+ # make sure block is not called when not in valid_response_codes
129
+ stub_request(:get, "http://testhost.com/test").to_return(:status => 401, :body => 'get')
130
+
131
+ assert_raises(Sunstone::Exception::Unauthorized) {
132
+ Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
133
+ raise Sunstone::Exception, 'Should not get here'
134
+ end
135
+ }
136
+ end
137
+
138
+ test '#send_request(#<Net::HTTPRequest>, &block) with block reading chunks' do
139
+ rd, wr = IO.pipe
140
+ rd = Net::BufferedIO.new(rd)
141
+ wr.write(<<-DATA.gsub(/^ +/, '').gsub(/\n/, "\r\n"))
142
+ HTTP/1.1 200 OK
143
+ Content-Length: 5
144
+
145
+ hello
146
+ DATA
147
+
148
+ res = Net::HTTPResponse.read_new(rd)
149
+ connection = mock('connection')
150
+ connection.stubs(:request).yields(res)
151
+ Sunstone.stubs(:with_connection).yields(connection)
152
+
153
+ res.reading_body(rd, true) do
154
+ Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
155
+ response.read_body do |chunk|
156
+ assert_equal('hello', chunk)
157
+ end
158
+ end
159
+ end
160
+ end
161
+
162
+ test '#send_request(#<Net::HTTPRequest) adds cookies to the cookie store if present' do
163
+ store = CookieStore::HashStore.new
164
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get', :headers => {'Set-Cookie' => 'foo=bar; Max-Age=3600'})
165
+
166
+ Sunstone.with_cookie_store(store) do
167
+ Sunstone.send_request(Net::HTTP::Get.new('/test'))
168
+ end
169
+
170
+ assert_equal 1, store.instance_variable_get(:@domains).size
171
+ assert_equal 1, store.instance_variable_get(:@domains)['testhost.com'].size
172
+ assert_equal 1, store.instance_variable_get(:@domains)['testhost.com']['/test'].size
173
+ assert_equal 'bar', store.instance_variable_get(:@domains)['testhost.com']['/test']['foo'].value
174
+ end
175
+
176
+ test '#send_request(#<Net::HTTPRequest>) includes the headers' do
177
+ stub_request(:get, "http://testhost.com/test").with(:headers => {
178
+ 'Api-Key' => 'test_api_key',
179
+ 'Content-Type' => 'application/json',
180
+ 'User-Agent' => Sunstone.user_agent
181
+ }).to_return(:body => "get")
182
+
183
+ assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
184
+
185
+ # Test without api key
186
+ Sunstone.site = "http://testhost.com"
187
+ stub_request(:get, "http://testhost.com/test").with(:headers => {
188
+ 'Content-Type'=>'application/json',
189
+ 'User-Agent'=>'Sunstone/0.1 Ruby/2.1.1-p76 x86_64-darwin13.0'
190
+ }).to_return(:body => "get")
191
+
192
+ assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
193
+ end
194
+
195
+ # Sunstone.get ==============================================================
196
+
197
+ test '#get(path)' do
198
+ stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
199
+
200
+ assert_equal('get', Sunstone.get('/test').body)
201
+ end
202
+
203
+ test '#get(path, params) with params as string' do
204
+ stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
205
+
206
+ assert_equal 'get', Sunstone.get('/test', 'key=value').body
207
+ end
208
+
209
+ test '#get(path, params) with params as hash' do
210
+ stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
211
+
212
+ assert_equal 'get', Sunstone.get('/test', {:key => 'value'}).body
213
+ end
214
+
215
+ test '#get(path, &block)' do
216
+ stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
217
+
218
+ Sunstone.get('/test') do |response|
219
+ assert_equal 'get', response.body
220
+ end
221
+ end
222
+
223
+ # Sunstone.post =============================================================
224
+
225
+ test '#post(path)' do
226
+ stub_request(:post, "http://testhost.com/test").to_return(:body => "post")
227
+
228
+ assert_equal('post', Sunstone.post('/test').body)
229
+ end
230
+
231
+ test '#post(path, body)' do
232
+ stub_request(:post, "http://testhost.com/test").with(:body => 'body').to_return(:body => "post")
233
+
234
+ assert_equal('post', Sunstone.post('/test', 'body').body)
235
+ end
236
+
237
+ test '#post(path, &block)' do
238
+ stub_request(:post, "http://testhost.com/test").to_return(:body => 'post')
239
+
240
+ Sunstone.post('/test') do |response|
241
+ assert_equal 'post', response.body
242
+ end
243
+ end
244
+
245
+ # Sunstone.put ==============================================================
246
+
247
+ test '#put(path)' do
248
+ stub_request(:put, "http://testhost.com/test").to_return(:body => "put")
249
+
250
+ assert_equal('put', Sunstone.put('/test').body)
251
+ end
252
+
253
+ test '#put(path, body)' do
254
+ stub_request(:put, "http://testhost.com/test").with(:body => 'body').to_return(:body => "put")
255
+
256
+ assert_equal('put', Sunstone.put('/test', 'body').body)
257
+ end
258
+
259
+ test '#put(path, &block)' do
260
+ stub_request(:put, "http://testhost.com/test").to_return(:body => 'put')
261
+
262
+ Sunstone.put('/test') do |response|
263
+ assert_equal 'put', response.body
264
+ end
265
+ end
266
+
267
+ # Sunstone.delete ===========================================================
268
+
269
+ test '#delete' do
270
+ stub_request(:delete, "http://testhost.com/test").to_return(:body => "delete")
271
+
272
+ assert_equal('delete', Sunstone.delete('/test').body)
273
+ end
274
+
275
+ test '#delete(path, &block)' do
276
+ stub_request(:delete, "http://testhost.com/test").to_return(:body => 'delete')
277
+
278
+ Sunstone.delete('/test') do |response|
279
+ assert_equal 'delete', response.body
280
+ end
281
+ end
282
+
283
+ # Sunstone.ping =============================================================
284
+
285
+ test '#ping' do
286
+ stub_request(:get, "http://testhost.com/ping").to_return(:body => 'pong')
287
+
288
+ assert_equal( 'pong', Sunstone.ping )
289
+ end
290
+
291
+ # Sunstone.config ===========================================================
292
+
293
+ test '#config' do
294
+ stub_request(:get, "http://testhost.com/config").to_return(:body => '{"server": "configs"}')
295
+
296
+ assert_equal( {:server => "configs"}, Sunstone.config )
297
+ end
298
+
299
+
300
+
301
+
302
+ end