mirror of
https://github.com/lostisland/faraday.git
synced 2025-12-06 00:01:23 -05:00
"JSON" middleware will use either Yajl or ActiveSupport::JSON, whichever happens to load successfully (with a preference for Yajl).
34 lines
1021 B
Ruby
34 lines
1021 B
Ruby
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
|
|
|
class RequestMiddlewareTest < Faraday::TestCase
|
|
def setup
|
|
@conn = Faraday.new do |b|
|
|
b.request :json
|
|
b.adapter :test do |stub|
|
|
stub.post('/echo') do |env|
|
|
posted_as = env[:request_headers]['Content-Type']
|
|
[200, {'Content-Type' => posted_as}, env[:body]]
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_does_nothing_without_payload
|
|
response = @conn.post('/echo')
|
|
assert_nil response.headers['Content-Type']
|
|
assert response.body.empty?
|
|
end
|
|
|
|
def test_encodes_hash
|
|
response = @conn.post('/echo', { :fruit => %w[apples oranges] })
|
|
assert_equal 'application/json', response.headers['Content-Type']
|
|
assert_equal '{"fruit":["apples","oranges"]}', response.body
|
|
end
|
|
|
|
def test_skips_encoding_for_strings
|
|
response = @conn.post('/echo', '{"a":"b"}')
|
|
assert_equal 'application/json', response.headers['Content-Type']
|
|
assert_equal '{"a":"b"}', response.body
|
|
end
|
|
end
|