Roman2K-web-service 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
1
+ require 'test_helper'
2
+
3
+ class WebService::RemoteCollectionTest < Test::Unit::TestCase
4
+ def setup
5
+ @resource_details = Object.new
6
+ @collection = WebService::RemoteCollection.new(@resource_details)
7
+ end
8
+
9
+ def res
10
+ @resource_details
11
+ end
12
+
13
+ # private
14
+
15
+ def test_build_url_for
16
+ build = lambda { |*args| @collection.ieval { build_url_for(*args) } }
17
+
18
+ def res.site; WebService::Site.new("http://example.com") end
19
+ def res.singleton; false end
20
+ def res.element_name; "foo" end
21
+
22
+ # Collection
23
+ assert_equal "/foos", build[nil, nil].path
24
+
25
+ # Member
26
+ assert_equal "/foos/1", build[1, nil].path
27
+
28
+ # Member (escaped)
29
+ assert_equal "/foos/example%2ecom", build["example.com", nil].path
30
+
31
+ # Member action
32
+ assert_equal "/foos/1/action", build[1, :action].path
33
+
34
+ # Singleton
35
+ def res.singleton; true end
36
+ assert_equal "/foo", build[nil, nil].path
37
+
38
+ # Non-singleton
39
+ assert_raise ArgumentError, "singleton resources do not require an ID parameter" do
40
+ build[1, nil]
41
+ end
42
+ def res.singleton; false end
43
+
44
+ # Nested collection
45
+ url = @collection.with_nesting([["bar", 1]]).ieval { build_url_for(nil, nil) }
46
+ assert_equal "/bars/1/foos", url.path
47
+
48
+ # Nested collection missing the ID for the association
49
+ assert_raise RuntimeError, "attribute `bar_id' is missing" do
50
+ @collection.with_nesting([["bar", nil]]).ieval { build_url_for(nil, nil) }
51
+ end
52
+ end
53
+
54
+ # private # utilities
55
+
56
+ def test_recognize
57
+ # Single pattern
58
+ hash, string, symbol = @collection.instance_eval { recognize([Hash, /^a/, Symbol], "abc", {:options => true}) }
59
+ assert_equal({:options => true}, hash)
60
+ assert_equal("abc", string)
61
+ assert_equal(nil, symbol)
62
+
63
+ # Multiple patterns
64
+ symbol, integer = @collection.instance_eval { recognize([Symbol, [/^\d+$/, Integer]], 3, :other) }
65
+ assert_equal 3, integer
66
+ assert_equal :other, symbol
67
+
68
+ symbol, integer = @collection.instance_eval { recognize([Symbol, [/^\d+$/, Integer]], "3", :other) }
69
+ assert_equal "3", integer
70
+ assert_equal :other, symbol
71
+ end
72
+ end
73
+
74
+ class WebService::RemoteCollection::ResponseDataUnserializationTest < Test::Unit::TestCase
75
+ class Response < Struct.new(:content_type, :body, :parse_count)
76
+ include WebService::RemoteCollection::ResponseDataUnserialization
77
+
78
+ def parse_data
79
+ self.parse_count ||= 0
80
+ self.parse_count += 1
81
+ super
82
+ end
83
+ end
84
+
85
+ def test_data
86
+ #################
87
+ # Blank => nil
88
+ #################
89
+ response = Response.new
90
+ response.body = " "
91
+ assert_equal(nil, response.data)
92
+
93
+ # Cache even nil
94
+ response.data
95
+ response.data
96
+ assert_equal(1, response.parse_count)
97
+
98
+ #########
99
+ # JSON
100
+ #########
101
+ response = Response.new
102
+ response.content_type = "application/json"
103
+ response.body = "{}"
104
+ assert_equal({}, response.data)
105
+
106
+ #########
107
+ # XML
108
+ #########
109
+ response = Response.new
110
+ response.content_type = "application/xml"
111
+ response.body = "<entries type='array'></entries>"
112
+ assert_equal({'entries' => []}, response.data)
113
+ end
114
+ end
@@ -0,0 +1,200 @@
1
+ require 'test_helper'
2
+
3
+ class WebService::ResourceTest < Test::Unit::TestCase
4
+ @@anonymous = Class.new(WebService::Resource)
5
+ @@anonymous_with_named_parent = Class.new(Foo)
6
+
7
+ # class
8
+
9
+ def test_element_name
10
+ # Anonymous class
11
+ assert_raise NameError do
12
+ @@anonymous.instance_eval { element_name }
13
+ end
14
+
15
+ # Named class
16
+ assert_equal "foo", Foo.instance_eval { element_name }
17
+
18
+ # Anonymous class with named parent
19
+ assert_equal "foo", @@anonymous_with_named_parent.instance_eval { element_name }
20
+
21
+ # Anonymous class with explicit element name
22
+ anonymous = Class.new(WebService::Resource)
23
+ anonymous.element_name = "bar"
24
+ assert_equal "bar", anonymous.element_name
25
+ end
26
+
27
+ def test_belongs_to
28
+ # Saving
29
+ bar = Bar.new("foo" => Foo.new("id" => 1), "a" => "b")
30
+
31
+ expect_request bar,
32
+ :post, "/foos/1/bars", :body => {"bar" => {"foo_id" => 1, "a" => "b"}},
33
+ :return => {:status => "201", :body => {"bar" => {"c" => "d"}}}
34
+
35
+ bar.save
36
+ assert_equal({"c" => "d"}, bar.attributes)
37
+
38
+ # Fetching
39
+ expect_request Bar, :get, "/bars", :return => {:status => "200", :body => []}
40
+ Bar.all
41
+ end
42
+
43
+ def test_has_many
44
+ foo = Foo.new("id" => 1)
45
+
46
+ expect_creation_on = lambda do |collection|
47
+ expect_request collection,
48
+ :post, "/foos/1/bars", :body => {"bar" => {"foo_id" => 1, "a" => "b"}},
49
+ :return => {:status => "201", :body => {"bar" => {"c" => "d"}}}
50
+ end
51
+
52
+ # Instantiation
53
+ bar = foo.bars.build("a" => "b")
54
+ assert_equal({"foo_id" => 1, "a" => "b"}, bar.attributes)
55
+
56
+ # Fetching
57
+ expect_request foo.bars,
58
+ :get, "/foos/1/bars",
59
+ :return => {:status => "200", :body => []}
60
+ foo.bars.all
61
+
62
+ # Creation (bars.create)
63
+ expect_creation_on[foo.bars]
64
+ foo.bars.create("a" => "b")
65
+
66
+ # Creation (bars.build.save)
67
+ bar = foo.bars.build "a" => "b"
68
+ expect_creation_on[bar]
69
+ bar.save
70
+
71
+ # Arbitrary actions
72
+ expect_request foo.bars,
73
+ :delete, "/foos/1/bars/99",
74
+ :return => {:status => "200"}
75
+ foo.bars.delete(99)
76
+ end
77
+
78
+ def test_has_many=
79
+ foo = Foo.new("id" => 1)
80
+ foo.bars.expects(:cache=).with("collection")
81
+ foo.bars = "collection"
82
+ end
83
+
84
+ def test_has_one
85
+ # Fetching
86
+ foo = Foo.new("id" => 1)
87
+ expect_request foo.instance_eval { association_collection_from_name(:bar, :singleton => true) },
88
+ :get, "/foos/1/bar",
89
+ :return => {:status => "200", :body => {"bar" => {"a" => "b"}}}
90
+ assert_equal Bar.new("a" => "b"), foo.bar
91
+
92
+ # Building + saving = creating
93
+ foo = Foo.new("id" => 1)
94
+ bar = foo.build_bar("a" => "b")
95
+ expect_request bar,
96
+ :post, "/foos/1/bar", :body => {"bar" => {"foo_id" => 1, "a" => "b"}},
97
+ :return => {:status => "201", :body => {"bar" => {"c" => "d"}}}
98
+ assert_equal Bar.new("c" => "d"), bar.save
99
+
100
+ # Plural-form resource class name
101
+ foo = Foo.new("id" => 1)
102
+ expect_request foo.instance_eval { association_collection_from_name(:details, :singleton => true) },
103
+ :get, "/foos/1/details",
104
+ :return => {:status => "200", :body => {"details" => {"a" => "b"}}}
105
+ assert_equal Details.new("a" => "b"), foo.details
106
+
107
+ # Constant name resolution
108
+ assert_raise NameError, /uninitialized constant Things\b/ do
109
+ Class.new(Foo) { has_one :things }.new.things
110
+ end
111
+ end
112
+
113
+ # public
114
+
115
+ def test_to_hash
116
+ res = WebService::Resource.new(:foo => :bar)
117
+ assert_equal res.attributes, res.to_hash
118
+ end
119
+
120
+ def test_to_s
121
+ assert_equal "Foo(new)", Foo.new.to_s
122
+ assert_equal "Foo[1]", Foo.new('id' => 1).to_s
123
+ end
124
+
125
+ def test_inspect
126
+ assert_equal "#<Foo(new)>", Foo.new.inspect
127
+ assert_equal "#<Foo[1]>", Foo.new('id' => 1).inspect
128
+ assert_equal "#<Foo[1] bar=2.0 baz=\"abcdefghijklmnopqrstuv...\">", Foo.new('id' => 1, 'bar' => 2.0, 'baz' => Array('a'..'z').join).inspect
129
+
130
+ # with custom to_s
131
+ foo = Foo.new
132
+ def foo.to_s; "custom" end
133
+ assert_equal "#<Foo(new)>", foo.inspect
134
+ end
135
+
136
+ def test_save
137
+ #############
138
+ # Creation
139
+ #############
140
+ foo = Foo.new("a" => "b")
141
+
142
+ expect_request foo,
143
+ :post, "/foos", :body => {"foo" => {"a" => "b"}},
144
+ :return => {:status => '201', :body => {"foo" => {"c" => "d"}}}
145
+
146
+ foo.save
147
+ assert_equal({"c" => "d"}, foo.attributes)
148
+
149
+ # Accepted
150
+ foo = Foo.new("a" => "b")
151
+ expect_request foo,
152
+ :post, "/foos", :body => {"foo" => {"a" => "b"}},
153
+ :return => {:status => '202', :body => " "}
154
+ foo.save
155
+ assert_equal({"a" => "b"}, foo.attributes)
156
+
157
+ ###########
158
+ # Update
159
+ ###########
160
+ foo = Foo.new("id" => 1, "a" => "b")
161
+
162
+ expect_request foo,
163
+ :put, "/foos/1", :body => {"foo" => {"id" => 1, "a" => "b"}},
164
+ :return => {:status => "200", :body => {"foo" => {"c" => "d"}}}
165
+
166
+ foo.save
167
+ assert_equal({"c" => "d"}, foo.attributes)
168
+
169
+ ##############
170
+ # Singleton
171
+ ##############
172
+ type_foo = Class.new(Foo) { self.singleton = true }
173
+
174
+ # Creation
175
+ foo = type_foo.new
176
+ expect_request foo,
177
+ :post, "/foo", :body => {"foo" => {}},
178
+ :return => {:status => "201", :body => {"foo" => {}}}
179
+ foo.save
180
+
181
+ # Update
182
+ foo = type_foo.new("id" => 1)
183
+ expect_request foo,
184
+ :put, "/foo", :body => {"foo" => {"id" => 1}},
185
+ :return => {:status => "200", :body => {"foo" => {}}}
186
+ foo.save
187
+ end
188
+
189
+ def test_reload
190
+ foo = Foo.new("id" => 1, "a" => "b")
191
+ expect_request foo,
192
+ :get, "/foos/1",
193
+ :return => {:status => 200, :body => {"foo" => {"id" => 1, "b" => "c"}}}
194
+
195
+ result = foo.reload
196
+
197
+ assert_equal Foo.new("id" => 1, "b" => "c"), foo
198
+ assert_equal foo.object_id, result.object_id
199
+ end
200
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ class WebService::ResponseHandlingTest < Test::Unit::TestCase
4
+ M = WebService::ResponseHandling
5
+
6
+ def test_connection_error_message
7
+ error_type = M::ConnectionError
8
+
9
+ # Can't use stub since stub(:method => :result) doesn't respond_to?(:method)
10
+ response = Object.new
11
+ def response.code; 400 end
12
+
13
+ # Nil response / No custom message
14
+ err = error_type.new(nil)
15
+ assert_equal "Failed", err.to_s
16
+
17
+ # Nil response + Custom message
18
+ err = error_type.new(nil, "message")
19
+ assert_equal "Failed: message", err.to_s
20
+
21
+ # No response message / No custom message
22
+ err = error_type.new(response)
23
+ assert_equal "Failed with 400", err.to_s
24
+
25
+ # Nil response message / No custom message
26
+ def response.message; nil end
27
+ err = error_type.new(response)
28
+ assert_equal "Failed with 400", err.to_s
29
+
30
+ # Blank response message / No custom message
31
+ def response.message; "" end
32
+ err = error_type.new(response)
33
+ assert_equal "Failed with 400", err.to_s
34
+
35
+ # Response message / No custom message
36
+ def response.message; "Bad Request" end
37
+ err = error_type.new(response)
38
+ assert_equal "Failed with 400 (Bad Request)", err.to_s
39
+
40
+ # Response message + Custom message
41
+ err = error_type.new(response, "message")
42
+ assert_equal "Failed with 400 (Bad Request): message", err.to_s
43
+ end
44
+
45
+ # protected
46
+
47
+ def test_handle_response
48
+ handler = Object.new.extend M
49
+
50
+ assert_raise_with_code = lambda do |code, exc|
51
+ response = stub(:code => code)
52
+ assert_raise(exc) do
53
+ handler.instance_eval { handle_response(response) }
54
+ end
55
+ end
56
+
57
+ assert_raise_with_code[406, M::NotAcceptable]
58
+ assert_raise_with_code[502, M::BadGateway]
59
+ assert_raise_with_code[503, M::ServiceUnavailable]
60
+ assert_raise_with_code[504, M::GatewayTimeout]
61
+
62
+ response = stub(:code => 200)
63
+ result = handler.instance_eval { handle_response(response) }
64
+ assert_equal response, response
65
+ end
66
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class WebService::SiteTest < Test::Unit::TestCase
4
+ include WebService
5
+
6
+ def setup
7
+ @site = Site.new("http://example")
8
+ end
9
+
10
+ def test_url
11
+ assert_equal "http://example", @site.url().to_s
12
+ assert_equal "http://example", @site.url(:prefix => true).to_s
13
+ end
14
+
15
+ def test_credentials
16
+ site = Site.new("http://example")
17
+ assert_equal nil, site.credentials
18
+
19
+ site = Site.new("http://foo@example")
20
+ assert_equal ["foo", nil], site.credentials
21
+
22
+ site = Site.new("http://foo:bar@example")
23
+ assert_equal ["foo", "bar"], site.credentials
24
+ end
25
+
26
+ def test_url_for
27
+ assert_equal "http://example/foo", @site.url_for("/foo").to_s
28
+
29
+ @site.expects(:url).with(:public => true).returns URI("http://public")
30
+ assert_equal "http://public/foo", @site.url_for("/foo", :public => true).to_s
31
+ end
32
+
33
+ def test_root
34
+ assert_equal "http://example/", @site.root.to_s
35
+ end
36
+ end
37
+
38
+ module Rails
39
+ def self.env
40
+ @env ||= Object.new
41
+ end
42
+ end
43
+
44
+ class WebService::Site::SwitchTest < Test::Unit::TestCase
45
+ include WebService
46
+
47
+ def setup
48
+ @site = Site::Switch.new("http://prod", "http://dev")
49
+ end
50
+
51
+ def test_url
52
+ env = Rails.env
53
+
54
+ def env.production?; true end
55
+ assert_equal "http://prod", @site.url.to_s
56
+
57
+ def env.production?; false end
58
+ assert_equal "http://dev", @site.url.to_s
59
+ assert_equal "http://prod", @site.url(:public => true).to_s
60
+ assert_equal "http://dev", @site.url.to_s
61
+ end
62
+ end
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{web-service}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Roman Le N\303\251grate"]
9
+ s.date = %q{2009-03-17}
10
+ s.description = %q{REST client; an alternative to ActiveResource}
11
+ s.email = %q{roman.lenegrate@gmail.com}
12
+ s.extra_rdoc_files = ["lib/web_service/attribute_accessors.rb", "lib/web_service/core_ext.rb", "lib/web_service/crud_operations.rb", "lib/web_service/named_request_methods.rb", "lib/web_service/remote_collection.rb", "lib/web_service/resource.rb", "lib/web_service/response_handling.rb", "lib/web_service/site.rb", "lib/web_service.rb", "LICENSE", "README.mdown"]
13
+ s.files = ["lib/web_service/attribute_accessors.rb", "lib/web_service/core_ext.rb", "lib/web_service/crud_operations.rb", "lib/web_service/named_request_methods.rb", "lib/web_service/remote_collection.rb", "lib/web_service/resource.rb", "lib/web_service/response_handling.rb", "lib/web_service/site.rb", "lib/web_service.rb", "LICENSE", "Manifest", "Rakefile", "README.mdown", "test/test_helper.rb", "test/web_service/attribute_accessors_test.rb", "test/web_service/core_ext_test.rb", "test/web_service/crud_operations_test.rb", "test/web_service/named_request_methods_test.rb", "test/web_service/remote_collection_test.rb", "test/web_service/resource_test.rb", "test/web_service/response_handling_test.rb", "test/web_service/site_test.rb", "web-service.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{https://github.com/Roman2K/web-service}
16
+ s.rdoc_options = ["--main", "README.mdown", "--inline-source", "--line-numbers", "--charset", "UTF-8"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{web-service}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{REST client; an alternative to ActiveResource}
21
+ s.test_files = ["test/test_helper.rb", "test/web_service/attribute_accessors_test.rb", "test/web_service/core_ext_test.rb", "test/web_service/crud_operations_test.rb", "test/web_service/named_request_methods_test.rb", "test/web_service/remote_collection_test.rb", "test/web_service/resource_test.rb", "test/web_service/response_handling_test.rb", "test/web_service/site_test.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.2.2"])
29
+ s.add_runtime_dependency(%q<class-inheritable-attributes>, [">= 0"])
30
+ s.add_development_dependency(%q<mocha>, [">= 0"])
31
+ s.add_development_dependency(%q<test-unit-ext>, [">= 0"])
32
+ else
33
+ s.add_dependency(%q<activesupport>, [">= 2.2.2"])
34
+ s.add_dependency(%q<class-inheritable-attributes>, [">= 0"])
35
+ s.add_dependency(%q<mocha>, [">= 0"])
36
+ s.add_dependency(%q<test-unit-ext>, [">= 0"])
37
+ end
38
+ else
39
+ s.add_dependency(%q<activesupport>, [">= 2.2.2"])
40
+ s.add_dependency(%q<class-inheritable-attributes>, [">= 0"])
41
+ s.add_dependency(%q<mocha>, [">= 0"])
42
+ s.add_dependency(%q<test-unit-ext>, [">= 0"])
43
+ end
44
+ end