activeresource 2.3.18 → 3.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activeresource might be problematic. Click here for more details.

@@ -1,238 +0,0 @@
1
- require 'abstract_unit'
2
-
3
- class ConnectionTest < Test::Unit::TestCase
4
- ResponseCodeStub = Struct.new(:code)
5
-
6
- def setup
7
- @conn = ActiveResource::Connection.new('http://localhost')
8
- @matz = { :id => 1, :name => 'Matz' }
9
- @david = { :id => 2, :name => 'David' }
10
- @people = [ @matz, @david ].to_xml(:root => 'people')
11
- @people_single = [ @matz ].to_xml(:root => 'people-single-elements')
12
- @people_empty = [ ].to_xml(:root => 'people-empty-elements')
13
- @matz = @matz.to_xml(:root => 'person')
14
- @david = @david.to_xml(:root => 'person')
15
- @header = {'key' => 'value'}.freeze
16
-
17
- @default_request_headers = { 'Content-Type' => 'application/xml' }
18
- ActiveResource::HttpMock.respond_to do |mock|
19
- mock.get "/people/2.xml", @header, @david
20
- mock.get "/people.xml", {}, @people
21
- mock.get "/people_single_elements.xml", {}, @people_single
22
- mock.get "/people_empty_elements.xml", {}, @people_empty
23
- mock.get "/people/1.xml", {}, @matz
24
- mock.put "/people/1.xml", {}, nil, 204
25
- mock.put "/people/2.xml", {}, @header, 204
26
- mock.delete "/people/1.xml", {}, nil, 200
27
- mock.delete "/people/2.xml", @header, nil, 200
28
- mock.post "/people.xml", {}, nil, 201, 'Location' => '/people/5.xml'
29
- mock.post "/members.xml", {}, @header, 201, 'Location' => '/people/6.xml'
30
- mock.head "/people/1.xml", {}, nil, 200
31
- end
32
- end
33
-
34
- def test_handle_response
35
- # 2xx and 3xx are valid responses.
36
- [200, 299, 300, 399].each do |code|
37
- expected = ResponseCodeStub.new(code)
38
- assert_equal expected, handle_response(expected)
39
- end
40
-
41
- # 400 is a bad request (e.g. malformed URI or missing request parameter)
42
- assert_response_raises ActiveResource::BadRequest, 400
43
-
44
- # 401 is an unauthorized request
45
- assert_response_raises ActiveResource::UnauthorizedAccess, 401
46
-
47
- # 403 is a forbidden requst (and authorizing will not help)
48
- assert_response_raises ActiveResource::ForbiddenAccess, 403
49
-
50
- # 404 is a missing resource.
51
- assert_response_raises ActiveResource::ResourceNotFound, 404
52
-
53
- # 405 is a missing not allowed error
54
- assert_response_raises ActiveResource::MethodNotAllowed, 405
55
-
56
- # 409 is an optimistic locking error
57
- assert_response_raises ActiveResource::ResourceConflict, 409
58
-
59
- # 410 is a removed resource
60
- assert_response_raises ActiveResource::ResourceGone, 410
61
-
62
- # 422 is a validation error
63
- assert_response_raises ActiveResource::ResourceInvalid, 422
64
-
65
- # 4xx are client errors.
66
- [402, 499].each do |code|
67
- assert_response_raises ActiveResource::ClientError, code
68
- end
69
-
70
- # 5xx are server errors.
71
- [500, 599].each do |code|
72
- assert_response_raises ActiveResource::ServerError, code
73
- end
74
-
75
- # Others are unknown.
76
- [199, 600].each do |code|
77
- assert_response_raises ActiveResource::ConnectionError, code
78
- end
79
- end
80
-
81
- ResponseHeaderStub = Struct.new(:code, :message, 'Allow')
82
- def test_should_return_allowed_methods_for_method_no_allowed_exception
83
- begin
84
- handle_response ResponseHeaderStub.new(405, "HTTP Failed...", "GET, POST")
85
- rescue ActiveResource::MethodNotAllowed => e
86
- assert_equal "Failed with 405 HTTP Failed...", e.message
87
- assert_equal [:get, :post], e.allowed_methods
88
- end
89
- end
90
-
91
- def test_initialize_raises_argument_error_on_missing_site
92
- assert_raise(ArgumentError) { ActiveResource::Connection.new(nil) }
93
- end
94
-
95
- def test_site_accessor_accepts_uri_or_string_argument
96
- site = URI.parse("http://localhost")
97
-
98
- assert_raise(URI::InvalidURIError) { @conn.site = nil }
99
-
100
- assert_nothing_raised { @conn.site = "http://localhost" }
101
- assert_equal site, @conn.site
102
-
103
- assert_nothing_raised { @conn.site = site }
104
- assert_equal site, @conn.site
105
- end
106
-
107
- def test_proxy_accessor_accepts_uri_or_string_argument
108
- proxy = URI.parse("http://proxy_user:proxy_password@proxy.local:4242")
109
-
110
- assert_nothing_raised { @conn.proxy = "http://proxy_user:proxy_password@proxy.local:4242" }
111
- assert_equal proxy, @conn.proxy
112
-
113
- assert_nothing_raised { @conn.proxy = proxy }
114
- assert_equal proxy, @conn.proxy
115
- end
116
-
117
- def test_timeout_accessor
118
- @conn.timeout = 5
119
- assert_equal 5, @conn.timeout
120
- end
121
-
122
- def test_get
123
- matz = @conn.get("/people/1.xml")
124
- assert_equal "Matz", matz["name"]
125
- end
126
-
127
- def test_head
128
- response = @conn.head("/people/1.xml")
129
- assert response.body.blank?
130
- assert_equal 200, response.code
131
- end
132
-
133
- def test_get_with_header
134
- david = @conn.get("/people/2.xml", @header)
135
- assert_equal "David", david["name"]
136
- end
137
-
138
- def test_get_collection
139
- people = @conn.get("/people.xml")
140
- assert_equal "Matz", people[0]["name"]
141
- assert_equal "David", people[1]["name"]
142
- end
143
-
144
- def test_get_collection_single
145
- people = @conn.get("/people_single_elements.xml")
146
- assert_equal "Matz", people[0]["name"]
147
- end
148
-
149
- def test_get_collection_empty
150
- people = @conn.get("/people_empty_elements.xml")
151
- assert_equal [], people
152
- end
153
-
154
- def test_post
155
- response = @conn.post("/people.xml")
156
- assert_equal "/people/5.xml", response["Location"]
157
- end
158
-
159
- def test_post_with_header
160
- response = @conn.post("/members.xml", @header)
161
- assert_equal "/people/6.xml", response["Location"]
162
- end
163
-
164
- def test_put
165
- response = @conn.put("/people/1.xml")
166
- assert_equal 204, response.code
167
- end
168
-
169
- def test_put_with_header
170
- response = @conn.put("/people/2.xml", @header)
171
- assert_equal 204, response.code
172
- end
173
-
174
- def test_delete
175
- response = @conn.delete("/people/1.xml")
176
- assert_equal 200, response.code
177
- end
178
-
179
- def test_delete_with_header
180
- response = @conn.delete("/people/2.xml", @header)
181
- assert_equal 200, response.code
182
- end
183
-
184
- def test_timeout
185
- @http = mock('new Net::HTTP')
186
- @conn.expects(:http).returns(@http)
187
- @http.expects(:get).raises(Timeout::Error, 'execution expired')
188
- assert_raise(ActiveResource::TimeoutError) { @conn.get('/people_timeout.xml') }
189
- end
190
-
191
- def test_setting_timeout
192
- http = Net::HTTP.new('')
193
-
194
- [10, 20].each do |timeout|
195
- @conn.timeout = timeout
196
- @conn.send(:configure_http, http)
197
- assert_equal timeout, http.open_timeout
198
- assert_equal timeout, http.read_timeout
199
- end
200
- end
201
-
202
- def test_accept_http_header
203
- @http = mock('new Net::HTTP')
204
- @conn.expects(:http).returns(@http)
205
- path = '/people/1.xml'
206
- @http.expects(:get).with(path, {'Accept' => 'application/xhtml+xml'}).returns(ActiveResource::Response.new(@matz, 200, {'Content-Type' => 'text/xhtml'}))
207
- assert_nothing_raised(Mocha::ExpectationError) { @conn.get(path, {'Accept' => 'application/xhtml+xml'}) }
208
- end
209
-
210
- def test_ssl_options_get_applied_to_http
211
- http = Net::HTTP.new('')
212
- @conn.site="https://secure"
213
- @conn.ssl_options={:verify_mode => OpenSSL::SSL::VERIFY_PEER}
214
- @conn.timeout = 10 # prevent warning about uninitialized.
215
- @conn.send(:configure_http, http)
216
-
217
- assert http.use_ssl?
218
- assert_equal http.verify_mode, OpenSSL::SSL::VERIFY_PEER
219
- end
220
-
221
- def test_ssl_error
222
- http = Net::HTTP.new('')
223
- @conn.expects(:http).returns(http)
224
- http.expects(:get).raises(OpenSSL::SSL::SSLError, 'Expired certificate')
225
- assert_raise(ActiveResource::SSLError) { @conn.get('/people/1.xml') }
226
- end
227
-
228
- protected
229
- def assert_response_raises(klass, code)
230
- assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
231
- handle_response ResponseCodeStub.new(code)
232
- end
233
- end
234
-
235
- def handle_response(response)
236
- @conn.__send__(:handle_response, response)
237
- end
238
- end
@@ -1,14 +0,0 @@
1
- class BeastResource < ActiveResource::Base
2
- self.site = 'http://beast.caboo.se'
3
- site.user = 'foo'
4
- site.password = 'bar'
5
- end
6
-
7
- class Forum < BeastResource
8
- # taken from BeastResource
9
- # self.site = 'http://beast.caboo.se'
10
- end
11
-
12
- class Topic < BeastResource
13
- self.site += '/forums/:forum_id'
14
- end
@@ -1,3 +0,0 @@
1
- class Customer < ActiveResource::Base
2
- self.site = "http://37s.sunrise.i:3000"
3
- end
@@ -1,3 +0,0 @@
1
- class Person < ActiveResource::Base
2
- self.site = "http://37s.sunrise.i:3000"
3
- end
@@ -1,4 +0,0 @@
1
- class ProxyResource < ActiveResource::Base
2
- self.site = "http://localhost"
3
- self.proxy = "http://user:password@proxy.local:3000"
4
- end
@@ -1,4 +0,0 @@
1
- class StreetAddress < ActiveResource::Base
2
- self.site = "http://37s.sunrise.i:3000/people/:person_id/"
3
- self.element_name = 'address'
4
- end
data/test/format_test.rb DELETED
@@ -1,112 +0,0 @@
1
- require 'abstract_unit'
2
- require "fixtures/person"
3
- require "fixtures/street_address"
4
-
5
- class FormatTest < Test::Unit::TestCase
6
- def setup
7
- @matz = { :id => 1, :name => 'Matz' }
8
- @david = { :id => 2, :name => 'David' }
9
-
10
- @programmers = [ @matz, @david ]
11
- end
12
-
13
- def test_http_format_header_name
14
- header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:get]
15
- assert_equal 'Accept', header_name
16
-
17
- headers_names = [ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:post]]
18
- headers_names.each{ |name| assert_equal 'Content-Type', name }
19
- end
20
-
21
- def test_formats_on_single_element
22
- for format in [ :json, :xml ]
23
- using_format(Person, format) do
24
- ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
25
- assert_equal @david[:name], Person.find(1).name
26
- end
27
- end
28
- end
29
-
30
- def test_formats_on_collection
31
- for format in [ :json, :xml ]
32
- using_format(Person, format) do
33
- ActiveResource::HttpMock.respond_to.get "/people.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@programmers)
34
- remote_programmers = Person.find(:all)
35
- assert_equal 2, remote_programmers.size
36
- assert remote_programmers.select { |p| p.name == 'David' }
37
- end
38
- end
39
- end
40
-
41
- def test_formats_on_custom_collection_method
42
- for format in [ :json, :xml ]
43
- using_format(Person, format) do
44
- ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode([@david])
45
- remote_programmers = Person.get(:retrieve, :name => 'David')
46
- assert_equal 1, remote_programmers.size
47
- assert_equal @david[:id], remote_programmers[0]['id']
48
- assert_equal @david[:name], remote_programmers[0]['name']
49
- end
50
- end
51
- end
52
-
53
- def test_formats_on_custom_element_method
54
- for format in [ :json, :xml ]
55
- using_format(Person, format) do
56
- ActiveResource::HttpMock.respond_to do |mock|
57
- mock.get "/people/2.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
58
- mock.get "/people/2/shallow.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
59
- end
60
- remote_programmer = Person.find(2).get(:shallow)
61
- assert_equal @david[:id], remote_programmer['id']
62
- assert_equal @david[:name], remote_programmer['name']
63
- end
64
- end
65
-
66
- for format in [ :json, :xml ]
67
- ryan = ActiveResource::Formats[format].encode({ :name => 'Ryan' })
68
- using_format(Person, format) do
69
- remote_ryan = Person.new(:name => 'Ryan')
70
- ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' => "/people/5.#{format}"}
71
- remote_ryan.save
72
-
73
- remote_ryan = Person.new(:name => 'Ryan')
74
- ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' => "/people/5.#{format}"}
75
- assert_equal ActiveResource::Response.new(ryan, 201, {'Location' => "/people/5.#{format}"}), remote_ryan.post(:register)
76
- end
77
- end
78
- end
79
-
80
- def test_setting_format_before_site
81
- resource = Class.new(ActiveResource::Base)
82
- resource.format = :json
83
- resource.site = 'http://37s.sunrise.i:3000'
84
- assert_equal ActiveResource::Formats[:json], resource.connection.format
85
- end
86
-
87
- def test_serialization_of_nested_resource
88
- address = { :street => '12345 Street' }
89
- person = { :name=> 'Rus', :address => address}
90
-
91
- [:json, :xml].each do |format|
92
- encoded_person = ActiveResource::Formats[format].encode(person)
93
- assert_match(/12345 Street/, encoded_person)
94
- remote_person = Person.new(person.update({:address => StreetAddress.new(address)}))
95
- assert_kind_of StreetAddress, remote_person.address
96
- using_format(Person, format) do
97
- ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, encoded_person, 201, {'Location' => "/people/5.#{format}"}
98
- remote_person.save
99
- end
100
- end
101
- end
102
-
103
- private
104
- def using_format(klass, mime_type_reference)
105
- previous_format = klass.format
106
- klass.format = mime_type_reference
107
-
108
- yield
109
- ensure
110
- klass.format = previous_format
111
- end
112
- end
@@ -1,155 +0,0 @@
1
- require 'abstract_unit'
2
-
3
- class HttpMockTest < ActiveSupport::TestCase
4
- def setup
5
- @http = ActiveResource::HttpMock.new("http://example.com")
6
- end
7
-
8
- FORMAT_HEADER = { :get => 'Accept',
9
- :put => 'Content-Type',
10
- :post => 'Content-Type',
11
- :delete => 'Accept',
12
- :head => 'Accept'
13
- }
14
-
15
- [:post, :put, :get, :delete, :head].each do |method|
16
- test "responds to simple #{method} request" do
17
- ActiveResource::HttpMock.respond_to do |mock|
18
- mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "Response")
19
- end
20
-
21
- assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
22
- end
23
-
24
- test "adds format header by default to #{method} request" do
25
- ActiveResource::HttpMock.respond_to do |mock|
26
- mock.send(method, "/people/1", {}, "Response")
27
- end
28
-
29
- assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
30
- end
31
-
32
- test "respond only when headers match header by default to #{method} request" do
33
- ActiveResource::HttpMock.respond_to do |mock|
34
- mock.send(method, "/people/1", {"X-Header" => "X"}, "Response")
35
- end
36
-
37
- assert_equal "Response", request(method, "/people/1", "X-Header" => "X").body
38
- assert_raise(ActiveResource::InvalidRequestError) { request(method, "/people/1") }
39
- end
40
-
41
- test "does not overwrite format header to #{method} request" do
42
- ActiveResource::HttpMock.respond_to do |mock|
43
- mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Response")
44
- end
45
-
46
- assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
47
- end
48
-
49
- test "ignores format header when there is only one response to same url in a #{method} request" do
50
- ActiveResource::HttpMock.respond_to do |mock|
51
- mock.send(method, "/people/1", {}, "Response")
52
- end
53
-
54
- assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
55
- assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
56
- end
57
-
58
- test "responds correctly when format header is given to #{method} request" do
59
- ActiveResource::HttpMock.respond_to do |mock|
60
- mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML")
61
- mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Json")
62
- end
63
-
64
- assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
65
- assert_equal "Json", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
66
- end
67
-
68
- test "raises InvalidRequestError if no response found for the #{method} request" do
69
- ActiveResource::HttpMock.respond_to do |mock|
70
- mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML")
71
- end
72
-
73
- assert_raise(::ActiveResource::InvalidRequestError) do
74
- request(method, "/people/1", FORMAT_HEADER[method] => "application/json")
75
- end
76
- end
77
-
78
- end
79
-
80
- test "allows you to send in pairs directly to the respond_to method" do
81
- matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
82
-
83
- create_matz = ActiveResource::Request.new(:post, '/people.xml', matz, {})
84
- created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"})
85
- get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
86
- ok_response = ActiveResource::Response.new(matz, 200, {})
87
-
88
- pairs = {create_matz => created_response, get_matz => ok_response}
89
-
90
- ActiveResource::HttpMock.respond_to(pairs)
91
- assert_equal 2, ActiveResource::HttpMock.responses.length
92
- assert_equal "", ActiveResource::HttpMock.responses.assoc(create_matz)[1].body
93
- assert_equal matz, ActiveResource::HttpMock.responses.assoc(get_matz)[1].body
94
- end
95
-
96
- test "resets all mocked responses on each call to respond_to with a block by default" do
97
- ActiveResource::HttpMock.respond_to do |mock|
98
- mock.send(:get, "/people/1", {}, "XML1")
99
- end
100
- assert_equal 1, ActiveResource::HttpMock.responses.length
101
-
102
- ActiveResource::HttpMock.respond_to do |mock|
103
- mock.send(:get, "/people/2", {}, "XML2")
104
- end
105
- assert_equal 1, ActiveResource::HttpMock.responses.length
106
- end
107
-
108
- test "resets all mocked responses on each call to respond_to by passing pairs by default" do
109
- ActiveResource::HttpMock.respond_to do |mock|
110
- mock.send(:get, "/people/1", {}, "XML1")
111
- end
112
- assert_equal 1, ActiveResource::HttpMock.responses.length
113
-
114
- matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
115
- get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
116
- ok_response = ActiveResource::Response.new(matz, 200, {})
117
- ActiveResource::HttpMock.respond_to({get_matz => ok_response})
118
-
119
- assert_equal 1, ActiveResource::HttpMock.responses.length
120
- end
121
-
122
- test "allows you to add new responses to the existing responses by calling a block" do
123
- ActiveResource::HttpMock.respond_to do |mock|
124
- mock.send(:get, "/people/1", {}, "XML1")
125
- end
126
- assert_equal 1, ActiveResource::HttpMock.responses.length
127
-
128
- ActiveResource::HttpMock.respond_to(false) do |mock|
129
- mock.send(:get, "/people/2", {}, "XML2")
130
- end
131
- assert_equal 2, ActiveResource::HttpMock.responses.length
132
- end
133
-
134
- test "allows you to add new responses to the existing responses by passing pairs" do
135
- ActiveResource::HttpMock.respond_to do |mock|
136
- mock.send(:get, "/people/1", {}, "XML1")
137
- end
138
- assert_equal 1, ActiveResource::HttpMock.responses.length
139
-
140
- matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
141
- get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
142
- ok_response = ActiveResource::Response.new(matz, 200, {})
143
- ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
144
-
145
- assert_equal 2, ActiveResource::HttpMock.responses.length
146
- end
147
-
148
- def request(method, path, headers = {}, body = nil)
149
- if [:put, :post].include? method
150
- @http.send(method, path, body, headers)
151
- else
152
- @http.send(method, path, headers)
153
- end
154
- end
155
- end