Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activeresource / test / connection_test.rb
blob09e80f95c1544d55fe748454722cbf15af185d51
1 require "#{File.dirname(__FILE__)}/abstract_unit"
2 require 'base64'
4 class ConnectionTest < Test::Unit::TestCase
5   ResponseCodeStub = Struct.new(:code)
7   def setup
8     @conn = ActiveResource::Connection.new('http://localhost')
9     @matz  = { :id => 1, :name => 'Matz' }
10     @david = { :id => 2, :name => 'David' }
11     @people = [ @matz, @david ].to_xml(:root => 'people')
12     @people_single = [ @matz ].to_xml(:root => 'people-single-elements')
13     @people_empty = [ ].to_xml(:root => 'people-empty-elements')
14     @matz = @matz.to_xml(:root => 'person')
15     @david = @david.to_xml(:root => 'person')
16     @header = {'key' => 'value'}.freeze
18     @default_request_headers = { 'Content-Type' => 'application/xml' }
19     ActiveResource::HttpMock.respond_to do |mock|
20       mock.get    "/people/2.xml", @header, @david
21       mock.get    "/people.xml", {}, @people
22       mock.get    "/people_single_elements.xml", {}, @people_single
23       mock.get    "/people_empty_elements.xml", {}, @people_empty
24       mock.get    "/people/1.xml", {}, @matz
25       mock.put    "/people/1.xml", {}, nil, 204
26       mock.put    "/people/2.xml", {}, @header, 204
27       mock.delete "/people/1.xml", {}, nil, 200
28       mock.delete "/people/2.xml", @header, nil, 200
29       mock.post   "/people.xml",   {}, nil, 201, 'Location' => '/people/5.xml'
30       mock.post   "/members.xml",  {}, @header, 201, 'Location' => '/people/6.xml'
31     end
32   end
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
41     # 404 is a missing resource.
42     assert_response_raises ActiveResource::ResourceNotFound, 404
44     # 405 is a missing not allowed error
45     assert_response_raises ActiveResource::MethodNotAllowed, 405
47     # 409 is an optimistic locking error
48     assert_response_raises ActiveResource::ResourceConflict, 409
50     # 422 is a validation error
51     assert_response_raises ActiveResource::ResourceInvalid, 422
53     # 4xx are client errors.
54     [401, 499].each do |code|
55       assert_response_raises ActiveResource::ClientError, code
56     end
58     # 5xx are server errors.
59     [500, 599].each do |code|
60       assert_response_raises ActiveResource::ServerError, code
61     end
63     # Others are unknown.
64     [199, 600].each do |code|
65       assert_response_raises ActiveResource::ConnectionError, code
66     end
67   end
69   ResponseHeaderStub = Struct.new(:code, :message, 'Allow')
70   def test_should_return_allowed_methods_for_method_no_allowed_exception
71     begin
72       handle_response ResponseHeaderStub.new(405, "HTTP Failed...", "GET, POST")
73     rescue ActiveResource::MethodNotAllowed => e
74       assert_equal "Failed with 405 HTTP Failed...", e.message
75       assert_equal [:get, :post], e.allowed_methods
76     end
77   end
79   def test_initialize_raises_argument_error_on_missing_site
80     assert_raise(ArgumentError) { ActiveResource::Connection.new(nil) }
81   end
83   def test_site_accessor_accepts_uri_or_string_argument
84     site = URI.parse("http://localhost")
86     assert_raise(URI::InvalidURIError) { @conn.site = nil }
88     assert_nothing_raised { @conn.site = "http://localhost" }
89     assert_equal site, @conn.site
91     assert_nothing_raised { @conn.site = site }
92     assert_equal site, @conn.site
93   end
95   def test_get
96     matz = @conn.get("/people/1.xml")
97     assert_equal "Matz", matz["name"]
98   end
100   def test_get_with_header
101     david = @conn.get("/people/2.xml", @header)
102     assert_equal "David", david["name"]
103   end
105   def test_get_collection
106     people = @conn.get("/people.xml")
107     assert_equal "Matz", people[0]["name"]
108     assert_equal "David", people[1]["name"]
109   end
110   
111   def test_get_collection_single
112     people = @conn.get("/people_single_elements.xml")
113     assert_equal "Matz", people[0]["name"]
114   end
115   
116   def test_get_collection_empty
117     people = @conn.get("/people_empty_elements.xml")
118     assert_equal [], people
119   end
121   def test_post
122     response = @conn.post("/people.xml")
123     assert_equal "/people/5.xml", response["Location"]
124   end
126   def test_post_with_header
127     response = @conn.post("/members.xml", @header)
128     assert_equal "/people/6.xml", response["Location"]
129   end
131   def test_put
132     response = @conn.put("/people/1.xml")
133     assert_equal 204, response.code
134   end
136   def test_put_with_header
137     response = @conn.put("/people/2.xml", @header)
138     assert_equal 204, response.code
139   end
141   def test_delete
142     response = @conn.delete("/people/1.xml")
143     assert_equal 200, response.code
144   end
146   def test_delete_with_header
147     response = @conn.delete("/people/2.xml", @header)
148     assert_equal 200, response.code
149   end
151   protected
152     def assert_response_raises(klass, code)
153       assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
154         handle_response ResponseCodeStub.new(code)
155       end
156     end
158     def handle_response(response)
159       @conn.send!(:handle_response, response)
160     end