1 require 'abstract_unit'
2 require "fixtures/person"
3 require "fixtures/street_address"
5 class FormatTest
< Test
::Unit::TestCase
7 @matz = { :id => 1, :name => 'Matz' }
8 @david = { :id => 2, :name => 'David' }
10 @programmers = [ @matz, @david ]
13 def test_http_format_header_name
14 header_name
= ActiveResource
::Connection::HTTP_FORMAT_HEADER_NAMES[:get]
15 assert_equal
'Accept', header_name
17 headers_names
= [ActiveResource
::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource
::Connection::HTTP_FORMAT_HEADER_NAMES[:post]]
18 headers_names
.each
{|header_name
| assert_equal
'Content-Type', header_name
}
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
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' }
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']
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)
60 remote_programmer
= Person
.find(2).get(:shallow)
61 assert_equal
@david[:id], remote_programmer
['id']
62 assert_equal
@david[:name], remote_programmer
['name']
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}"}
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)
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
87 def test_serialization_of_nested_resource
88 address
= { :street => '12345 Street' }
89 person
= { :name=> 'Rus', :address => address
}
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}"}
104 def using_format(klass
, mime_type_reference
)
105 previous_format
= klass
.format
106 klass
.format
= mime_type_reference
110 klass
.format
= previous_format