Permalink
Browse files

test for non JSON response using multi_json

  • Loading branch information...
1 parent e5d86b7 commit 515f6791c020c8a1b16859c5dd96b491effc9c6f @hone committed Aug 14, 2010
Showing with 32 additions and 12 deletions.
  1. +32 −12 test/multi_json_test.rb
View
@@ -4,21 +4,41 @@ class Multi_Json_Test < Test::Unit::TestCase
context 'when used' do
setup do
@stubs = Faraday::Adapter::Test::Stubs.new
- @stubs.get('/me') { [200, {}, "{\"name\":\"Wynn Netherland\",\"username\":\"pengwynn\"}"] }
- end
-
- should 'should parse the body as JSON' do
- conn = Faraday::Connection.new do |builder|
+ @conn = Faraday::Connection.new do |builder|
builder.adapter :test, @stubs
builder.use Faraday::Response::MultiJson
end
-
- me = conn.get("/me").body
- assert me.is_a?(Hash)
- me['name'].should == 'Wynn Netherland'
- me['username'].should == 'pengwynn'
-
+ end
+
+ context "when there is a JSON body" do
+ setup do
+ @stubs.get('/me') { [200, {}, "{\"name\":\"Wynn Netherland\",\"username\":\"pengwynn\"}"] }
+ end
+
+ should 'should parse the body as JSON' do
+ me = @conn.get("/me").body
+ assert me.is_a?(Hash)
+ me['name'].should == 'Wynn Netherland'
+ me['username'].should == 'pengwynn'
+
+ end
+ end
+
+ context "when the JSON body is empty" do
+ setup do
+ @stubs.get('/me') { [200, {}, ""] }
+ end
+
+ should 'still have the status code' do
+ response = @conn.get("/me")
+ response.status.should == 200
+ end
+
+ should 'set body to nil' do
+ response = @conn.get("/me")
+ response.body.should be_nil
+ end
end
end
-end
+end

0 comments on commit 515f679

Please sign in to comment.