diff options
Diffstat (limited to 'activeresource')
-rw-r--r-- | activeresource/lib/active_resource.rb | 1 | ||||
-rw-r--r-- | activeresource/lib/active_resource/http_mock.rb | 56 | ||||
-rw-r--r-- | activeresource/lib/active_resource/validations.rb | 2 | ||||
-rw-r--r-- | activeresource/test/cases/format_test.rb | 2 | ||||
-rw-r--r-- | activeresource/test/cases/http_mock_test.rb | 36 |
5 files changed, 72 insertions, 25 deletions
diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb index 3e4a1dd4a1..186865f811 100644 --- a/activeresource/lib/active_resource.rb +++ b/activeresource/lib/active_resource.rb @@ -29,6 +29,7 @@ $:.unshift(activemodel_path) if File.directory?(activemodel_path) && !$:.include require 'active_support' require 'active_model' +require 'active_resource/version' module ActiveResource extend ActiveSupport::Autoload diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index ddd3fb1f5d..9aefde7c30 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -100,11 +100,11 @@ module ActiveResource # Accepts a block which declares a set of requests and responses for the HttpMock to respond to in # the following format: - # + # # mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {}) - # + # # === Example - # + # # @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person") # ActiveResource::HttpMock.respond_to do |mock| # mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml" @@ -112,65 +112,76 @@ module ActiveResource # mock.put "/people/1.xml", {}, nil, 204 # mock.delete "/people/1.xml", {}, nil, 200 # end - # + # # Alternatively, accepts a hash of <tt>{Request => Response}</tt> pairs allowing you to generate # these the following format: - # + # # ActiveResource::Request.new(method, path, body, request_headers) # ActiveResource::Response.new(body, status, response_headers) - # + # # === Example - # + # # Request.new(:#{method}, path, nil, request_headers) - # + # # @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person") # # create_matz = ActiveResource::Request.new(:post, '/people.xml', @matz, {}) # created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"}) # get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil) # ok_response = ActiveResource::Response.new("", 200, {}) - # + # # pairs = {create_matz => created_response, get_matz => ok_response} - # + # # ActiveResource::HttpMock.respond_to(pairs) # # Note, by default, every time you call +respond_to+, any previous request and response pairs stored # in HttpMock will be deleted giving you a clean slate to work on. - # + # # If you want to override this behaviour, pass in +false+ as the last argument to +respond_to+ - # + # # === Example - # + # # ActiveResource::HttpMock.respond_to do |mock| # mock.send(:get, "/people/1", {}, "XML1") # end # ActiveResource::HttpMock.responses.length #=> 1 - # + # # ActiveResource::HttpMock.respond_to(false) do |mock| # mock.send(:get, "/people/2", {}, "XML2") # end # ActiveResource::HttpMock.responses.length #=> 2 - # + # # This also works with passing in generated pairs of requests and responses, again, just pass in false # as the last argument: - # + # # === Example - # + # # ActiveResource::HttpMock.respond_to do |mock| # mock.send(:get, "/people/1", {}, "XML1") # end # ActiveResource::HttpMock.responses.length #=> 1 - # + # # get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil) # ok_response = ActiveResource::Response.new("", 200, {}) - # + # # pairs = {get_matz => ok_response} # # ActiveResource::HttpMock.respond_to(pairs, false) # ActiveResource::HttpMock.responses.length #=> 2 + # + # # If you add a response with an existing request, it will be replaced + # + # fail_response = ActiveResource::Response.new("", 404, {}) + # pairs = {get_matz => fail_response} + # + # ActiveResource::HttpMock.respond_to(pairs, false) + # ActiveResource::HttpMock.responses.length #=> 2 + # def respond_to(*args) #:yields: mock pairs = args.first || {} reset! if args.last.class != FalseClass + + delete_responses_to_replace pairs.to_a responses.concat pairs.to_a if block_given? yield Responder.new(responses) @@ -179,6 +190,13 @@ module ActiveResource end end + def delete_responses_to_replace(new_responses) + new_responses.each{|nr| + request_to_remove = nr[0] + @@responses = responses.delete_if{|r| r[0] == request_to_remove} + } + end + # Deletes all logged requests and responses. def reset! requests.clear diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index d3b19ee560..a373e53f11 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -13,7 +13,7 @@ module ActiveResource # or not (by passing true) def from_array(messages, save_cache = false) clear unless save_cache - humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) } + humanized_attributes = Hash[@base.attributes.keys.map { |attr_name| [attr_name.humanize, attr_name] }] messages.each do |message| attr_message = humanized_attributes.keys.detect do |attr_name| if message[0, attr_name.size + 1] == "#{attr_name} " diff --git a/activeresource/test/cases/format_test.rb b/activeresource/test/cases/format_test.rb index c3733e13d8..fc1a7b8c6f 100644 --- a/activeresource/test/cases/format_test.rb +++ b/activeresource/test/cases/format_test.rb @@ -33,7 +33,7 @@ class FormatTest < Test::Unit::TestCase ActiveResource::HttpMock.respond_to.get "/people.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@programmers) remote_programmers = Person.find(:all) assert_equal 2, remote_programmers.size - assert remote_programmers.select { |p| p.name == 'David' } + assert remote_programmers.find { |p| p.name == 'David' } end end end diff --git a/activeresource/test/cases/http_mock_test.rb b/activeresource/test/cases/http_mock_test.rb index d90d1e01b8..82b5e60c77 100644 --- a/activeresource/test/cases/http_mock_test.rb +++ b/activeresource/test/cases/http_mock_test.rb @@ -69,19 +69,19 @@ class HttpMockTest < ActiveSupport::TestCase request(method, "/people/1", FORMAT_HEADER[method] => "application/json") end end - + end test "allows you to send in pairs directly to the respond_to method" do matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person") - + create_matz = ActiveResource::Request.new(:post, '/people.xml', matz, {}) created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"}) get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil) ok_response = ActiveResource::Response.new(matz, 200, {}) - + pairs = {create_matz => created_response, get_matz => ok_response} - + ActiveResource::HttpMock.respond_to(pairs) assert_equal 2, ActiveResource::HttpMock.responses.length assert_equal "", ActiveResource::HttpMock.responses.assoc(create_matz)[1].body @@ -140,6 +140,34 @@ class HttpMockTest < ActiveSupport::TestCase assert_equal 2, ActiveResource::HttpMock.responses.length end + test "allows you to replace the existing reponse with the same request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(:get, "/people/1", {}, "XML1") + end + assert_equal 1, ActiveResource::HttpMock.responses.length + + matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person") + get_matz = ActiveResource::Request.new(:get, '/people/1', nil) + ok_response = ActiveResource::Response.new(matz, 200, {}) + + ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false) + + assert_equal 1, ActiveResource::HttpMock.responses.length + end + + test "do not replace the response with the same path but different method" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(:get, "/people/1", {}, "XML1") + end + assert_equal 1, ActiveResource::HttpMock.responses.length + + put_matz = ActiveResource::Request.new(:put, '/people/1', nil) + ok_response = ActiveResource::Response.new("", 200, {}) + + ActiveResource::HttpMock.respond_to({put_matz => ok_response}, false) + assert_equal 2, ActiveResource::HttpMock.responses.length + end + def request(method, path, headers = {}, body = nil) if [:put, :post].include? method @http.send(method, path, body, headers) |