diff options
Diffstat (limited to 'activeresource')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 5 | ||||
-rw-r--r-- | activeresource/lib/active_resource/http_mock.rb | 16 | ||||
-rw-r--r-- | activeresource/lib/active_resource/log_subscriber.rb | 15 | ||||
-rw-r--r-- | activeresource/lib/active_resource/railtie.rb | 3 | ||||
-rw-r--r-- | activeresource/lib/active_resource/railties/log_subscriber.rb | 15 | ||||
-rw-r--r-- | activeresource/lib/active_resource/validations.rb | 2 | ||||
-rw-r--r-- | activeresource/test/cases/base_errors_test.rb | 13 | ||||
-rw-r--r-- | activeresource/test/cases/http_mock_test.rb | 71 | ||||
-rw-r--r-- | activeresource/test/cases/log_subscriber_test.rb | 9 |
9 files changed, 120 insertions, 29 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index c1ec8559f5..6c494a8bcc 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -17,6 +17,7 @@ require 'active_resource/exceptions' require 'active_resource/connection' require 'active_resource/formats' require 'active_resource/schema' +require 'active_resource/log_subscriber' module ActiveResource # ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application. @@ -763,7 +764,7 @@ module ActiveResource # With any other scope, find returns nil when no data is returned. # # Person.find(1) - # # => raises ResourcenotFound + # # => raises ResourceNotFound # # Person.find(:all) # Person.find(:first) @@ -941,7 +942,7 @@ module ActiveResource end # This is a list of known attributes for this resource. Either - # gathered fromthe provided <tt>schema</tt>, or from the attributes + # gathered from the provided <tt>schema</tt>, or from the attributes # set on this instance after it has been fetched from the remote system. def known_attributes self.class.known_attributes + self.attributes.keys.map(&:to_s) diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index 1ed3804017..f192c53b4f 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -148,16 +148,28 @@ module ActiveResource attr_accessor :path, :method, :body, :headers def initialize(method, path, body = nil, headers = {}) - @method, @path, @body, @headers = method, path, body, headers.merge(ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method] => 'application/xml') + @method, @path, @body, @headers = method, path, body, headers end def ==(req) - path == req.path && method == req.method && headers == req.headers + path == req.path && method == req.method && headers_match?(req) end def to_s "<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>" end + + private + + def headers_match?(req) + # Ignore format header on equality if it's not defined + format_header = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method] + if headers[format_header].present? || req.headers[format_header].blank? + headers == req.headers + else + headers.dup.merge(format_header => req.headers[format_header]) == req.headers + end + end end class Response diff --git a/activeresource/lib/active_resource/log_subscriber.rb b/activeresource/lib/active_resource/log_subscriber.rb new file mode 100644 index 0000000000..9e52baf36d --- /dev/null +++ b/activeresource/lib/active_resource/log_subscriber.rb @@ -0,0 +1,15 @@ +module ActiveResource + class LogSubscriber < ActiveSupport::LogSubscriber + def request(event) + result = event.payload[:result] + info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}" + info "--> %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration] + end + + def logger + ActiveResource::Base.logger + end + end +end + +ActiveResource::LogSubscriber.attach_to :active_resource
\ No newline at end of file diff --git a/activeresource/lib/active_resource/railtie.rb b/activeresource/lib/active_resource/railtie.rb index aa878c7212..60f6f88311 100644 --- a/activeresource/lib/active_resource/railtie.rb +++ b/activeresource/lib/active_resource/railtie.rb @@ -5,9 +5,6 @@ module ActiveResource class Railtie < Rails::Railtie config.active_resource = ActiveSupport::OrderedOptions.new - require "active_resource/railties/log_subscriber" - log_subscriber :active_resource, ActiveResource::Railties::LogSubscriber.new - initializer "active_resource.set_configs" do |app| app.config.active_resource.each do |k,v| ActiveResource::Base.send "#{k}=", v diff --git a/activeresource/lib/active_resource/railties/log_subscriber.rb b/activeresource/lib/active_resource/railties/log_subscriber.rb deleted file mode 100644 index 86806a93d0..0000000000 --- a/activeresource/lib/active_resource/railties/log_subscriber.rb +++ /dev/null @@ -1,15 +0,0 @@ -module ActiveResource - module Railties - class LogSubscriber < Rails::LogSubscriber - def request(event) - result = event.payload[:result] - info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}" - info "--> %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration] - end - - def logger - ActiveResource::Base.logger - end - end - end -end
\ No newline at end of file diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index a19e0d0ac9..026d81e44a 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -27,7 +27,7 @@ module ActiveResource # Grabs errors from a json response. def from_json(json, save_cache = false) - array = ActiveSupport::JSON.decode(json)['errors'] rescue [] + array = Array.wrap(ActiveSupport::JSON.decode(json)['errors']) rescue [] from_array array, save_cache end diff --git a/activeresource/test/cases/base_errors_test.rb b/activeresource/test/cases/base_errors_test.rb index b4fd75fba3..5063916d10 100644 --- a/activeresource/test/cases/base_errors_test.rb +++ b/activeresource/test/cases/base_errors_test.rb @@ -17,7 +17,7 @@ class BaseErrorsTest < Test::Unit::TestCase end end - def test_should_parse_xml_errors + def test_should_parse_json_and_xml_errors [ :json, :xml ].each do |format| invalid_user_using_format(format) do assert_kind_of ActiveResource::Errors, @person.errors @@ -26,6 +26,17 @@ class BaseErrorsTest < Test::Unit::TestCase end end + def test_should_parse_json_errors_when_no_errors_key + ActiveResource::HttpMock.respond_to do |mock| + mock.post "/people.json", {}, '{}', 422, {'Content-Type' => 'application/json; charset=utf-8'} + end + + invalid_user_using_format(:json) do + assert_kind_of ActiveResource::Errors, @person.errors + assert_equal 0, @person.errors.size + end + end + def test_should_parse_errors_to_individual_attributes [ :json, :xml ].each do |format| invalid_user_using_format(format) do diff --git a/activeresource/test/cases/http_mock_test.rb b/activeresource/test/cases/http_mock_test.rb new file mode 100644 index 0000000000..5e032d03f1 --- /dev/null +++ b/activeresource/test/cases/http_mock_test.rb @@ -0,0 +1,71 @@ +require 'abstract_unit' + +class HttpMockTest < ActiveSupport::TestCase + setup do + @http = ActiveResource::HttpMock.new("http://example.com") + end + + FORMAT_HEADER = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES + + [:post, :put, :get, :delete, :head].each do |method| + test "responds to simple #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "Response") + end + + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body + end + + test "adds format header by default to #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {}, "Response") + end + + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body + end + + test "respond only when headers match header by default to #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {"X-Header" => "X"}, "Response") + end + + assert_equal "Response", request(method, "/people/1", "X-Header" => "X").body + assert_raise(ActiveResource::InvalidRequestError) { request(method, "/people/1") } + end + + test "does not overwrite format header to #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Response") + end + + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body + end + + test "ignores format header when there is only one response to same url in a #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {}, "Response") + end + + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body + end + + test "responds correctly when format header is given to #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML") + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Json") + end + + assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body + assert_equal "Json", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body + end + end + + def request(method, path, headers = {}, body = nil) + if [:put, :post].include? method + @http.send(method, path, body, headers) + else + @http.send(method, path, headers) + end + end +end diff --git a/activeresource/test/cases/log_subscriber_test.rb b/activeresource/test/cases/log_subscriber_test.rb index f0330e8f51..3cd96007db 100644 --- a/activeresource/test/cases/log_subscriber_test.rb +++ b/activeresource/test/cases/log_subscriber_test.rb @@ -1,12 +1,11 @@ require "abstract_unit" require "fixtures/person" -require "rails/log_subscriber/test_helper" -require "active_resource/railties/log_subscriber" +require "active_support/log_subscriber/test_helper" +require "active_resource/log_subscriber" require "active_support/core_ext/hash/conversions" -# TODO: This test should be part of Railties class LogSubscriberTest < ActiveSupport::TestCase - include Rails::LogSubscriber::TestHelper + include ActiveSupport::LogSubscriber::TestHelper def setup super @@ -16,7 +15,7 @@ class LogSubscriberTest < ActiveSupport::TestCase mock.get "/people/1.xml", {}, @matz end - Rails::LogSubscriber.add(:active_resource, ActiveResource::Railties::LogSubscriber.new) + ActiveResource::LogSubscriber.attach_to :active_resource end def set_logger(logger) |