2008-01-05 08:32:38 -05:00
|
|
|
require 'abstract_unit'
|
2007-09-20 19:18:05 -04:00
|
|
|
require "fixtures/person"
|
2008-10-07 12:05:41 -04:00
|
|
|
require "fixtures/street_address"
|
2007-09-20 19:18:05 -04:00
|
|
|
|
|
|
|
class FormatTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@matz = { :id => 1, :name => 'Matz' }
|
|
|
|
@david = { :id => 2, :name => 'David' }
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2007-09-20 19:18:05 -04:00
|
|
|
@programmers = [ @matz, @david ]
|
|
|
|
end
|
2008-08-29 21:19:18 -04:00
|
|
|
|
|
|
|
def test_http_format_header_name
|
|
|
|
header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:get]
|
|
|
|
assert_equal 'Accept', header_name
|
|
|
|
|
|
|
|
headers_names = [ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:post]]
|
2008-11-22 21:40:08 -05:00
|
|
|
headers_names.each{ |name| assert_equal 'Content-Type', name }
|
2008-08-29 21:19:18 -04:00
|
|
|
end
|
|
|
|
|
2007-09-20 19:18:05 -04:00
|
|
|
def test_formats_on_single_element
|
|
|
|
for format in [ :json, :xml ]
|
|
|
|
using_format(Person, format) do
|
2008-08-29 21:19:18 -04:00
|
|
|
ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
|
2007-09-20 19:18:05 -04:00
|
|
|
assert_equal @david[:name], Person.find(1).name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_formats_on_collection
|
|
|
|
for format in [ :json, :xml ]
|
|
|
|
using_format(Person, format) do
|
2008-08-29 21:19:18 -04:00
|
|
|
ActiveResource::HttpMock.respond_to.get "/people.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@programmers)
|
2007-09-20 19:18:05 -04:00
|
|
|
remote_programmers = Person.find(:all)
|
|
|
|
assert_equal 2, remote_programmers.size
|
2010-09-30 14:00:14 -04:00
|
|
|
assert remote_programmers.find { |p| p.name == 'David' }
|
2007-09-20 19:18:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-28 12:03:58 -05:00
|
|
|
def test_formats_on_custom_collection_method
|
|
|
|
for format in [ :json, :xml ]
|
|
|
|
using_format(Person, format) do
|
2008-08-29 21:19:18 -04:00
|
|
|
ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode([@david])
|
2007-12-28 12:03:58 -05:00
|
|
|
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
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2007-12-28 12:03:58 -05:00
|
|
|
def test_formats_on_custom_element_method
|
2011-05-17 19:30:43 -04:00
|
|
|
[:json, :xml].each do |format|
|
2007-12-28 12:03:58 -05:00
|
|
|
using_format(Person, format) do
|
2011-05-17 19:30:43 -04:00
|
|
|
david = (format == :json ? { :person => @david } : @david)
|
2007-12-28 12:03:58 -05:00
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:30:43 -04:00
|
|
|
mock.get "/people/2.#{format}", { 'Accept' => ActiveResource::Formats[format].mime_type }, ActiveResource::Formats[format].encode(david)
|
|
|
|
mock.get "/people/2/shallow.#{format}", { 'Accept' => ActiveResource::Formats[format].mime_type }, ActiveResource::Formats[format].encode(david)
|
2007-12-28 12:03:58 -05:00
|
|
|
end
|
2011-05-17 19:30:43 -04:00
|
|
|
|
2007-12-28 12:03:58 -05:00
|
|
|
remote_programmer = Person.find(2).get(:shallow)
|
|
|
|
assert_equal @david[:id], remote_programmer['id']
|
|
|
|
assert_equal @david[:name], remote_programmer['name']
|
|
|
|
end
|
|
|
|
|
2011-05-17 19:30:43 -04:00
|
|
|
ryan_hash = { :name => 'Ryan' }
|
|
|
|
ryan_hash = (format == :json ? { :person => ryan_hash } : ryan_hash)
|
|
|
|
ryan = ActiveResource::Formats[format].encode(ryan_hash)
|
2007-12-28 12:03:58 -05:00
|
|
|
using_format(Person, format) do
|
|
|
|
remote_ryan = Person.new(:name => 'Ryan')
|
2011-05-17 19:30:43 -04:00
|
|
|
ActiveResource::HttpMock.respond_to.post "/people.#{format}", { 'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, { 'Location' => "/people/5.#{format}" }
|
2008-08-29 21:19:18 -04:00
|
|
|
remote_ryan.save
|
|
|
|
|
|
|
|
remote_ryan = Person.new(:name => 'Ryan')
|
2011-05-17 19:30:43 -04:00
|
|
|
ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", { 'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, { 'Location' => "/people/5.#{format}" }
|
|
|
|
assert_equal ActiveResource::Response.new(ryan, 201, { 'Location' => "/people/5.#{format}" }), remote_ryan.post(:register)
|
2007-12-28 12:03:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2007-12-28 12:12:35 -05:00
|
|
|
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
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2008-10-07 12:05:41 -04:00
|
|
|
def test_serialization_of_nested_resource
|
2008-11-22 21:39:24 -05:00
|
|
|
address = { :street => '12345 Street' }
|
2011-05-10 19:58:36 -04:00
|
|
|
person = { :name => 'Rus', :address => address}
|
2008-10-07 12:05:41 -04:00
|
|
|
|
2008-11-22 21:39:24 -05:00
|
|
|
[:json, :xml].each do |format|
|
|
|
|
encoded_person = ActiveResource::Formats[format].encode(person)
|
|
|
|
assert_match(/12345 Street/, encoded_person)
|
|
|
|
remote_person = Person.new(person.update({:address => StreetAddress.new(address)}))
|
|
|
|
assert_kind_of StreetAddress, remote_person.address
|
|
|
|
using_format(Person, format) do
|
|
|
|
ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, encoded_person, 201, {'Location' => "/people/5.#{format}"}
|
|
|
|
remote_person.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-10-07 12:05:41 -04:00
|
|
|
|
2007-09-20 19:18:05 -04:00
|
|
|
private
|
|
|
|
def using_format(klass, mime_type_reference)
|
|
|
|
previous_format = klass.format
|
|
|
|
klass.format = mime_type_reference
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
klass.format = previous_format
|
|
|
|
end
|
2008-01-05 08:32:38 -05:00
|
|
|
end
|