PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Tests/Libraries/Rails-3.0.0/activeresource/test/cases/format_test.rb

http://github.com/IronLanguages/main
Ruby | 112 lines | 97 code | 15 blank | 0 comment | 6 complexity | 6e3b50672fe171b01d9502fe2e059763 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require 'abstract_unit'
  2. require "fixtures/person"
  3. require "fixtures/street_address"
  4. class FormatTest < Test::Unit::TestCase
  5. def setup
  6. @matz = { :id => 1, :name => 'Matz' }
  7. @david = { :id => 2, :name => 'David' }
  8. @programmers = [ @matz, @david ]
  9. end
  10. def test_http_format_header_name
  11. header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:get]
  12. assert_equal 'Accept', header_name
  13. headers_names = [ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:post]]
  14. headers_names.each{ |name| assert_equal 'Content-Type', name }
  15. end
  16. def test_formats_on_single_element
  17. for format in [ :json, :xml ]
  18. using_format(Person, format) do
  19. ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
  20. assert_equal @david[:name], Person.find(1).name
  21. end
  22. end
  23. end
  24. def test_formats_on_collection
  25. for format in [ :json, :xml ]
  26. using_format(Person, format) do
  27. ActiveResource::HttpMock.respond_to.get "/people.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@programmers)
  28. remote_programmers = Person.find(:all)
  29. assert_equal 2, remote_programmers.size
  30. assert remote_programmers.select { |p| p.name == 'David' }
  31. end
  32. end
  33. end
  34. def test_formats_on_custom_collection_method
  35. for format in [ :json, :xml ]
  36. using_format(Person, format) do
  37. ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode([@david])
  38. remote_programmers = Person.get(:retrieve, :name => 'David')
  39. assert_equal 1, remote_programmers.size
  40. assert_equal @david[:id], remote_programmers[0]['id']
  41. assert_equal @david[:name], remote_programmers[0]['name']
  42. end
  43. end
  44. end
  45. def test_formats_on_custom_element_method
  46. for format in [ :json, :xml ]
  47. using_format(Person, format) do
  48. ActiveResource::HttpMock.respond_to do |mock|
  49. mock.get "/people/2.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
  50. mock.get "/people/2/shallow.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david)
  51. end
  52. remote_programmer = Person.find(2).get(:shallow)
  53. assert_equal @david[:id], remote_programmer['id']
  54. assert_equal @david[:name], remote_programmer['name']
  55. end
  56. end
  57. for format in [ :json, :xml ]
  58. ryan = ActiveResource::Formats[format].encode({ :name => 'Ryan' })
  59. using_format(Person, format) do
  60. remote_ryan = Person.new(:name => 'Ryan')
  61. ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' => "/people/5.#{format}"}
  62. remote_ryan.save
  63. remote_ryan = Person.new(:name => 'Ryan')
  64. ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' => "/people/5.#{format}"}
  65. assert_equal ActiveResource::Response.new(ryan, 201, {'Location' => "/people/5.#{format}"}), remote_ryan.post(:register)
  66. end
  67. end
  68. end
  69. def test_setting_format_before_site
  70. resource = Class.new(ActiveResource::Base)
  71. resource.format = :json
  72. resource.site = 'http://37s.sunrise.i:3000'
  73. assert_equal ActiveResource::Formats[:json], resource.connection.format
  74. end
  75. def test_serialization_of_nested_resource
  76. address = { :street => '12345 Street' }
  77. person = { :name=> 'Rus', :address => address}
  78. [:json, :xml].each do |format|
  79. encoded_person = ActiveResource::Formats[format].encode(person)
  80. assert_match(/12345 Street/, encoded_person)
  81. remote_person = Person.new(person.update({:address => StreetAddress.new(address)}))
  82. assert_kind_of StreetAddress, remote_person.address
  83. using_format(Person, format) do
  84. ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, encoded_person, 201, {'Location' => "/people/5.#{format}"}
  85. remote_person.save
  86. end
  87. end
  88. end
  89. private
  90. def using_format(klass, mime_type_reference)
  91. previous_format = klass.format
  92. klass.format = mime_type_reference
  93. yield
  94. ensure
  95. klass.format = previous_format
  96. end
  97. end