Permalink
Browse files

updated all the tests in ARes to work with json

  • Loading branch information...
1 parent a6c4f40 commit 2c90ddb4b2f20bb387774851fbf1aeb2eabfb26c @joshk joshk committed May 17, 2011
@@ -498,9 +498,9 @@ def format=(mime_type_reference_or_format)
connection.format = format if site
end
- # Returns the current format, default is ActiveResource::Formats::XmlFormat.
+ # Returns the current format, default is ActiveResource::Formats::JsonFormat.
def format
- self._format || ActiveResource::Formats::XmlFormat
+ self._format || ActiveResource::Formats::JsonFormat
end
# Sets the number of seconds after which requests to the REST API should time out.
@@ -1232,9 +1232,16 @@ def reload
# your_supplier = Supplier.new
# your_supplier.load(my_attrs)
# your_supplier.save
- def load(attributes)
+ def load(attributes, remove_root = false)
raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
@prefix_options, attributes = split_options(attributes)
+
+ if attributes.keys.size == 1
+ remove_root = self.class.element_name == attributes.keys.first.to_s
+ end
+
+ attributes = Formats.remove_root(attributes) if remove_root
+
attributes.each do |key, value|
@attributes[key.to_s] =
case value
@@ -1285,7 +1292,7 @@ def update_attribute(name, value)
# resource's attributes, the full body of the request will still be sent
# in the save request to the remote service.
def update_attributes(attributes)
- load(attributes) && save
+ load(attributes, false) && save
end
# For checking <tt>respond_to?</tt> without searching the attributes (which is faster).
@@ -1339,7 +1346,7 @@ def create
def load_attributes_from_response(response)
if !response['Content-Length'].blank? && response['Content-Length'] != "0" && !response.body.nil? && response.body.strip.size > 0
- load(self.class.format.decode(response.body))
+ load(self.class.format.decode(response.body), true)
@gtd
gtd Dec 12, 2012

I'm migrating a 3.0.17 app to a branch from the latest compatible code in the rails/activeresource repo and I got some breakages around root removal, mostly because we have unusual semantics in that we return {success: true} for a PUT request (yes I realize this is unconventional, but I don't want to write code to reproduce the resource structure and send it back over the wire because the data only flows one way).

Anyway I'm trying to understand the intention of this commit. Was it just to make the tests pass after switching to JSON as the default? Because it breaks existing code, and some things like this line in particular are nonsensical. Passing true here is redundant because self.class.format.decode is already removing the root, so its entirely redundant, and in fact could lead to even weirder problems for single-attribute objects.

@persisted = true
end
end
@@ -30,7 +30,7 @@ def requests
# The +site+ parameter is required and will set the +site+
# attribute to the URI for the remote resource service.
- def initialize(site, format = ActiveResource::Formats::XmlFormat)
+ def initialize(site, format = ActiveResource::Formats::JsonFormat)
raise ArgumentError, 'Missing site URI' unless site
@user = @password = nil
self.site = site
@@ -54,7 +54,9 @@ class << self
#
# Person.find(:all, :from => :active)
def get(custom_method_name, options = {})
- format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body)
+ hashified = format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body)
+ derooted = Formats.remove_root(hashified)
+ derooted.is_a?(Array) ? derooted.map { |e| Formats.remove_root(e) } : derooted
end
def post(custom_method_name, options = {}, body = '')
@@ -10,5 +10,13 @@ module Formats
def self.[](mime_type_reference)
ActiveResource::Formats.const_get(ActiveSupport::Inflector.camelize(mime_type_reference.to_s) + "Format")
end
+
+ def self.remove_root(data)
+ if data.is_a?(Hash) && data.keys.size == 1
+ data.values.first
+ else
+ data
+ end
+ end
end
end
@@ -18,7 +18,7 @@ def encode(hash, options = nil)
end
def decode(json)
- ActiveSupport::JSON.decode(json)
+ Formats.remove_root(ActiveSupport::JSON.decode(json))
end
end
end
@@ -18,19 +18,8 @@ def encode(hash, options={})
end
def decode(xml)
- from_xml_data(Hash.from_xml(xml))
+ Formats.remove_root(Hash.from_xml(xml))
end
-
- private
- # Manipulate from_xml Hash, because xml_simple is not exactly what we
- # want for Active Resource.
- def from_xml_data(data)
- if data.is_a?(Hash) && data.keys.size == 1
- data.values.first
- else
- data
- end
- end
end
end
end
View
@@ -20,16 +20,19 @@
end
def setup_response
- @default_request_headers = { 'Content-Type' => 'application/xml' }
- @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
- @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
- @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person')
- @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address')
- @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person")
- @joe = { 'person' => { :id => 6, :name => 'Joe' }}.to_json
- @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people')
- @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people')
- @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses')
+ matz_hash = { 'person' => { :id => 1, :name => 'Matz' } }
+
+ @default_request_headers = { 'Content-Type' => 'application/json' }
+ @matz = matz_hash.to_json
+ @matz_xml = matz_hash.to_xml
+ @david = { :person => { :id => 2, :name => 'David' } }.to_json
+ @greg = { :person => { :id => 3, :name => 'Greg' } }.to_json
+ @addy = { :address => { :id => 1, :street => '12345 Street', :country => 'Australia' } }.to_json
+ @rick = { :person => { :name => "Rick", :age => 25 } }.to_json
+ @joe = { :person => { :id => 6, :name => 'Joe', :likes_hats => true }}.to_json
+ @people = { :people => [ { :person => { :id => 1, :name => 'Matz' } }, { :person => { :id => 2, :name => 'David' } }] }.to_json
+ @people_david = { :people => [ { :person => { :id => 2, :name => 'David' } }] }.to_json
+ @addresses = { :addresses => [{ :address => { :id => 1, :street => '12345 Street', :country => 'Australia' } }] }.to_json
# - deep nested resource -
# - Luis (Customer)
@@ -48,19 +51,38 @@ def setup_response
# - Natacha (Customer::Friend::Brother::Child)
# - Milena (Customer::Friend::Brother)
#
- @luis = {:id => 1, :name => 'Luis',
- :friends => [{:name => 'JK',
- :brothers => [{:name => 'Mateo',
- :children => [{:name => 'Edith'},{:name => 'Martha'}]},
- {:name => 'Felipe',
- :children => [{:name => 'Bryan'},{:name => 'Luke'}]}]},
- {:name => 'Eduardo',
- :brothers => [{:name => 'Sebas',
- :children => [{:name => 'Andres'},{:name => 'Jorge'}]},
- {:name => 'Elsa',
- :children => [{:name => 'Natacha'}]},
- {:name => 'Milena',
- :children => []}]}]}.to_xml(:root => 'customer')
+ @luis = {
+ :customer => {
+ :id => 1,
+ :name => 'Luis',
+ :friends => [{
+ :name => 'JK',
+ :brothers => [
+ {
+ :name => 'Mateo',
+ :children => [{ :name => 'Edith' },{ :name => 'Martha' }]
+ }, {
+ :name => 'Felipe',
+ :children => [{ :name => 'Bryan' },{ :name => 'Luke' }]
+ }
+ ]
+ }, {
+ :name => 'Eduardo',
+ :brothers => [
+ {
+ :name => 'Sebas',
+ :children => [{ :name => 'Andres' },{ :name => 'Jorge' }]
+ }, {
+ :name => 'Elsa',
+ :children => [{ :name => 'Natacha' }]
+ }, {
+ :name => 'Milena',
+ :children => []
+ }
+ ]
+ }]
+ }
+ }.to_json
# - resource with yaml array of strings; for ARs using serialize :bar, Array
@marty = <<-eof.strip
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
@@ -75,49 +97,52 @@ def setup_response
</person>
eof
- @startup_sound = {
- :name => "Mac Startup Sound", :author => { :name => "Jim Reekes" }
- }.to_xml(:root => 'sound')
+ @startup_sound = {
+ :sound => {
+ :name => "Mac Startup Sound", :author => { :name => "Jim Reekes" }
+ }
+ }.to_json
ActiveResource::HttpMock.respond_to do |mock|
- mock.get "/people/1.xml", {}, @matz
+ mock.get "/people/1.json", {}, @matz
+ mock.get "/people/1.xml", {}, @matz_xml
mock.get "/people/2.xml", {}, @david
mock.get "/people/5.xml", {}, @marty
- mock.get "/people/Greg.xml", {}, @greg
+ mock.get "/people/Greg.json", {}, @greg
mock.get "/people/6.json", {}, @joe
- mock.get "/people/4.xml", {'key' => 'value'}, nil, 404
- mock.put "/people/1.xml", {}, nil, 204
- mock.delete "/people/1.xml", {}, nil, 200
+ mock.get "/people/4.json", { 'key' => 'value' }, nil, 404
+ mock.put "/people/1.json", {}, nil, 204
+ mock.delete "/people/1.json", {}, nil, 200
mock.delete "/people/2.xml", {}, nil, 400
- mock.get "/people/99.xml", {}, nil, 404
- mock.post "/people.xml", {}, @rick, 201, 'Location' => '/people/5.xml'
- mock.get "/people.xml", {}, @people
- mock.get "/people/1/addresses.xml", {}, @addresses
- mock.get "/people/1/addresses/1.xml", {}, @addy
+ mock.get "/people/99.json", {}, nil, 404
+ mock.post "/people.json", {}, @rick, 201, 'Location' => '/people/5.xml'
+ mock.get "/people.json", {}, @people
+ mock.get "/people/1/addresses.json", {}, @addresses
+ mock.get "/people/1/addresses/1.json", {}, @addy
mock.get "/people/1/addresses/2.xml", {}, nil, 404
- mock.get "/people/2/addresses.xml", {}, nil, 404
+ mock.get "/people/2/addresses.json", {}, nil, 404
mock.get "/people/2/addresses/1.xml", {}, nil, 404
- mock.get "/people/Greg/addresses/1.xml", {}, @addy
- mock.put "/people/1/addresses/1.xml", {}, nil, 204
- mock.delete "/people/1/addresses/1.xml", {}, nil, 200
- mock.post "/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5'
- mock.get "/people/1/addresses/99.xml", {}, nil, 404
+ mock.get "/people/Greg/addresses/1.json", {}, @addy
+ mock.put "/people/1/addresses/1.json", {}, nil, 204
+ mock.delete "/people/1/addresses/1.json", {}, nil, 200
+ mock.post "/people/1/addresses.json", {}, nil, 201, 'Location' => '/people/1/addresses/5'
+ mock.get "/people/1/addresses/99.json", {}, nil, 404
mock.get "/people//addresses.xml", {}, nil, 404
mock.get "/people//addresses/1.xml", {}, nil, 404
mock.put "/people//addresses/1.xml", {}, nil, 404
mock.delete "/people//addresses/1.xml", {}, nil, 404
mock.post "/people//addresses.xml", {}, nil, 404
- mock.head "/people/1.xml", {}, nil, 200
- mock.head "/people/Greg.xml", {}, nil, 200
- mock.head "/people/99.xml", {}, nil, 404
- mock.head "/people/1/addresses/1.xml", {}, nil, 200
- mock.head "/people/1/addresses/2.xml", {}, nil, 404
- mock.head "/people/2/addresses/1.xml", {}, nil, 404
- mock.head "/people/Greg/addresses/1.xml", {}, nil, 200
+ mock.head "/people/1.json", {}, nil, 200
+ mock.head "/people/Greg.json", {}, nil, 200
+ mock.head "/people/99.json", {}, nil, 404
+ mock.head "/people/1/addresses/1.json", {}, nil, 200
+ mock.head "/people/1/addresses/2.json", {}, nil, 404
+ mock.head "/people/2/addresses/1.json", {}, nil, 404
+ mock.head "/people/Greg/addresses/1.json", {}, nil, 200
# customer
- mock.get "/customers/1.xml", {}, @luis
+ mock.get "/customers/1.json", {}, @luis
# sound
- mock.get "/sounds/1.xml", {}, @startup_sound
+ mock.get "/sounds/1.json", {}, @startup_sound
end
Person.user = nil
Oops, something went wrong.

0 comments on commit 2c90ddb

Please sign in to comment.