~nchohan/appscale/GAE1.4.0-namespaces

« back to all changes in this revision

Viewing changes to AppLoadBalancer/vendor/rails/activeresource/test/format_test.rb

  • Committer: Chris Bunch
  • Date: 2009-11-16 18:37:46 UTC
  • mto: This revision was merged to the branch mainline in revision 303.
  • Revision ID: chris@magna-carta-20091116183746-1sqxle515itjf6si
added fix for create_user on load balancer

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'abstract_unit'
 
2
require "fixtures/person"
 
3
require "fixtures/street_address"
 
4
 
 
5
class FormatTest < Test::Unit::TestCase
 
6
  def setup
 
7
    @matz  = { :id => 1, :name => 'Matz' }
 
8
    @david = { :id => 2, :name => 'David' }
 
9
 
 
10
    @programmers = [ @matz, @david ]
 
11
  end
 
12
 
 
13
  def test_http_format_header_name
 
14
    header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:get]
 
15
    assert_equal 'Accept', header_name
 
16
 
 
17
    headers_names = [ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:post]]
 
18
    headers_names.each{ |name| assert_equal 'Content-Type', name }
 
19
  end
 
20
 
 
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
 
26
      end
 
27
    end
 
28
  end
 
29
 
 
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' }
 
37
      end
 
38
    end
 
39
  end
 
40
 
 
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']
 
49
      end
 
50
    end
 
51
  end
 
52
 
 
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)
 
59
        end
 
60
        remote_programmer = Person.find(2).get(:shallow)
 
61
        assert_equal @david[:id], remote_programmer['id']
 
62
        assert_equal @david[:name], remote_programmer['name']
 
63
      end
 
64
    end
 
65
 
 
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}"}
 
71
        remote_ryan.save
 
72
 
 
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)
 
76
      end
 
77
    end
 
78
  end
 
79
 
 
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
 
85
  end
 
86
 
 
87
  def test_serialization_of_nested_resource
 
88
    address  = { :street => '12345 Street' }
 
89
    person  = { :name=> 'Rus', :address => address}
 
90
 
 
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}"}
 
98
        remote_person.save
 
99
      end
 
100
    end
 
101
  end
 
102
 
 
103
  private
 
104
    def using_format(klass, mime_type_reference)
 
105
      previous_format = klass.format
 
106
      klass.format = mime_type_reference
 
107
 
 
108
      yield
 
109
    ensure
 
110
      klass.format = previous_format
 
111
    end
 
112
end