Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Raise an error if JSON encoding adapters are missing

  • Loading branch information...
commit f87ea6e636c08e71bce95c85d0bdfb1cc8a03b36 1 parent 264492a
@rsutphin rsutphin authored mislav committed
View
2  lib/faraday/error.rb
@@ -34,5 +34,7 @@ def inspect
class ConnectionFailed < ClientError; end
class ResourceNotFound < ClientError; end
class ParsingError < ClientError; end
+
+ class MissingDependency < StandardError; end
end
end
View
6 lib/faraday/request/json.rb
@@ -3,7 +3,11 @@ class Request::JSON < Request::UrlEncoded
self.mime_type = 'application/json'.freeze
class << self
- attr_accessor :adapter
+ attr_writer :adapter
+
+ def adapter
+ @adapter or raise Error::MissingDependency, "No JSON adapter available. Install either activesupport or yajl-ruby."
+ end
end
# loads the JSON encoder either from yajl-ruby or activesupport
View
31 test/request_middleware_test.rb
@@ -42,6 +42,37 @@ def test_json_skips_encoding_for_strings
assert_equal '{"a":"b"}', response.body
end
+ def test_json_fails_with_useful_message_when_no_json_adapter_available
+ without_json_adapter do
+ expected_msg = "No JSON adapter available. Install either activesupport or yajl-ruby."
+ # assert_raise doesn't work to check the message (at least on 1.8.7)
+ begin
+ @conn.post('/echo', { :fruit => %w[apples oranges] }, 'content-type' => 'application/json')
+ fail "Exception not raised"
+ rescue Faraday::Error::MissingDependency => e
+ assert_equal expected_msg, e.message
+ end
+ end
+ end
+
+ def test_url_encoded_does_not_fail_when_no_json_adapter_available
+ without_json_adapter do
+ assert_nothing_raised {
+ @conn.post('/echo', { :fruit => %w[apples oranges] })
+ }
+ end
+ end
+
+ def without_json_adapter
+ original_adapter = Faraday::Request::JSON.adapter
+ Faraday::Request::JSON.adapter = nil
+ begin
+ yield
+ ensure
+ Faraday::Request::JSON.adapter = original_adapter
+ end
+ end
+
def test_url_encoded_no_header
response = @conn.post('/echo', { :fruit => %w[apples oranges] })
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
Please sign in to comment.
Something went wrong with that request. Please try again.