test/format_test.rb in activeresource-2.0.2 vs test/format_test.rb in activeresource-2.0.4
- old
+ new
@@ -27,9 +27,50 @@
assert remote_programmers.select { |p| p.name == 'David' }
end
end
end
+ def test_formats_on_custom_collection_method
+ for format in [ :json, :xml ]
+ using_format(Person, format) do
+ ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {}, ActiveResource::Formats[format].encode([@david])
+ remote_programmers = Person.get(:retrieve, :name => 'David')
+ assert_equal 1, remote_programmers.size
+ assert_equal @david[:id], remote_programmers[0]['id']
+ assert_equal @david[:name], remote_programmers[0]['name']
+ end
+ end
+ end
+
+ def test_formats_on_custom_element_method
+ for format in [ :json, :xml ]
+ using_format(Person, format) do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/people/2.#{format}", {}, ActiveResource::Formats[format].encode(@david)
+ mock.get "/people/2/shallow.#{format}", {}, ActiveResource::Formats[format].encode(@david)
+ end
+ remote_programmer = Person.find(2).get(:shallow)
+ assert_equal @david[:id], remote_programmer['id']
+ assert_equal @david[:name], remote_programmer['name']
+ end
+ end
+
+ for format in [ :json, :xml ]
+ ryan = ActiveResource::Formats[format].encode({ :name => 'Ryan' })
+ using_format(Person, format) do
+ ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {}, ryan, 201, 'Location' => "/people/5.#{format}"
+ remote_ryan = Person.new(:name => 'Ryan')
+ assert_equal ActiveResource::Response.new(ryan, 201, {'Location' => "/people/5.#{format}"}), remote_ryan.post(:register)
+ end
+ end
+ end
+
+ def test_setting_format_before_site
+ resource = Class.new(ActiveResource::Base)
+ resource.format = :json
+ resource.site = 'http://37s.sunrise.i:3000'
+ assert_equal ActiveResource::Formats[:json], resource.connection.format
+ end
private
def using_format(klass, mime_type_reference)
previous_format = klass.format
klass.format = mime_type_reference
\ No newline at end of file