add Faraday::Connection#dup

This commit is contained in:
rick 2010-04-22 11:16:28 -04:00
parent 415b82c89b
commit b7eabb95f7
3 changed files with 33 additions and 4 deletions

View File

@ -20,12 +20,12 @@ module Faraday
Builder.new(&block).tap { |builder| builder.run(inner) }
end
def initialize
@handlers = []
yield self
def initialize(handlers = [])
@handlers = handlers
yield self if block_given?
end
def run app
def run(app)
@handlers.unshift app
end
@ -53,5 +53,13 @@ module Faraday
def use_symbol(mod, key, *args, &block)
use mod.lookup_module(key), *args, &block
end
def ==(other)
other.is_a?(self.class) && @handlers == other.handlers
end
def dup
self.class.new @handlers.dup
end
end
end

View File

@ -34,6 +34,8 @@ module Faraday
merge_headers @headers, options[:headers] if options[:headers]
if block
@builder = Builder.create_with_inner_app(&block)
else
@builder = options[:builder] || Builder.new
end
end
@ -141,6 +143,10 @@ module Faraday
uri
end
def dup
self.class.new(build_url(''), :headers => headers.dup, :params => params.dup, :builder => builder.dup)
end
def replace_query(uri, params)
url_params = @params.dup
if uri.query && !uri.query.empty?

View File

@ -158,4 +158,19 @@ class TestConnection < Faraday::TestCase
end
assert_equal "a%5Bb%5D=1%20%2B%202", conn.build_query('a[b]' => '1 + 2')
end
def test_dups_connection_object
conn = Faraday::Connection.new 'http://sushi.com/foo' do |b|
b.adapter :net_http
end
conn.headers['content-type'] = 'text/plain'
conn.params['a'] = '1'
duped = conn.dup
assert_equal conn.build_url(''), duped.build_url('')
[:headers, :params, :builder].each do |attr|
assert_equal conn.send(attr), duped.send(attr)
assert_not_equal conn.send(attr).object_id, duped.send(attr).object_id
end
end
end