aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-06-28 00:12:15 +0200
committerXavier Noria <fxn@hashref.com>2010-06-28 00:12:15 +0200
commit4329f8133fee8e4f3e558787f67de59f0c4a4dd1 (patch)
tree346ef7340d8348e50d119ca749a16c1654c20a08 /activeresource/test
parentc37f7d66e49ffe5ac2115cc30e5529fd1c2924a8 (diff)
parentebee77a28a7267d5f23a28ba23c1eb88a2d7d527 (diff)
downloadrails-4329f8133fee8e4f3e558787f67de59f0c4a4dd1.tar.gz
rails-4329f8133fee8e4f3e558787f67de59f0c4a4dd1.tar.bz2
rails-4329f8133fee8e4f3e558787f67de59f0c4a4dd1.zip
Merge remote branch 'rails/master'
Diffstat (limited to 'activeresource/test')
-rw-r--r--activeresource/test/cases/base_errors_test.rb13
-rw-r--r--activeresource/test/cases/http_mock_test.rb71
-rw-r--r--activeresource/test/cases/log_subscriber_test.rb9
3 files changed, 87 insertions, 6 deletions
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)