faraday 0.5.7 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -8
- data/README.md +99 -20
- data/faraday.gemspec +9 -11
- data/lib/faraday.rb +1 -1
- data/lib/faraday/adapter.rb +11 -86
- data/lib/faraday/adapter/action_dispatch.rb +3 -12
- data/lib/faraday/adapter/em_synchrony.rb +9 -24
- data/lib/faraday/adapter/excon.rb +7 -19
- data/lib/faraday/adapter/net_http.rb +37 -35
- data/lib/faraday/adapter/patron.rb +16 -23
- data/lib/faraday/adapter/test.rb +4 -10
- data/lib/faraday/adapter/typhoeus.rb +11 -39
- data/lib/faraday/builder.rb +82 -33
- data/lib/faraday/connection.rb +3 -10
- data/lib/faraday/error.rb +20 -15
- data/lib/faraday/middleware.rb +7 -2
- data/lib/faraday/request.rb +13 -10
- data/lib/faraday/request/json.rb +31 -0
- data/lib/faraday/request/multipart.rb +63 -0
- data/lib/faraday/request/url_encoded.rb +37 -0
- data/lib/faraday/response.rb +72 -30
- data/lib/faraday/response/logger.rb +34 -0
- data/lib/faraday/response/raise_error.rb +16 -0
- data/lib/faraday/upload_io.rb +14 -6
- data/lib/faraday/utils.rb +54 -17
- data/test/adapters/live_test.rb +36 -14
- data/test/adapters/logger_test.rb +1 -1
- data/test/adapters/net_http_test.rb +33 -0
- data/test/connection_test.rb +0 -39
- data/test/env_test.rb +84 -6
- data/test/helper.rb +17 -8
- data/test/live_server.rb +19 -17
- data/test/middleware_stack_test.rb +91 -0
- data/test/request_middleware_test.rb +75 -21
- data/test/response_middleware_test.rb +34 -31
- metadata +21 -17
- data/lib/faraday/adapter/logger.rb +0 -32
- data/lib/faraday/request/active_support_json.rb +0 -21
- data/lib/faraday/request/yajl.rb +0 -18
- data/lib/faraday/response/active_support_json.rb +0 -30
- data/lib/faraday/response/yajl.rb +0 -26
- data/test/adapters/typhoeus_test.rb +0 -31
- data/test/connection_app_test.rb +0 -60
- data/test/form_post_test.rb +0 -58
- data/test/multipart_test.rb +0 -48
data/lib/faraday/request/yajl.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Request::Yajl < Faraday::Middleware
|
3
|
-
begin
|
4
|
-
require 'yajl'
|
5
|
-
|
6
|
-
rescue LoadError, NameError => e
|
7
|
-
self.load_error = e
|
8
|
-
end
|
9
|
-
|
10
|
-
def call(env)
|
11
|
-
env[:request_headers]['Content-Type'] = 'application/json'
|
12
|
-
if env[:body] && !env[:body].respond_to?(:to_str)
|
13
|
-
env[:body] = Yajl::Encoder.encode(env[:body])
|
14
|
-
end
|
15
|
-
@app.call env
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Response::ActiveSupportJson < Response::Middleware
|
3
|
-
begin
|
4
|
-
if !defined?(ActiveSupport::JSON)
|
5
|
-
require 'active_support'
|
6
|
-
ActiveSupport::JSON
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.register_on_complete(env)
|
10
|
-
env[:response].on_complete do |finished_env|
|
11
|
-
finished_env[:body] = parse(finished_env[:body])
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
15
|
-
rescue LoadError, NameError => e
|
16
|
-
self.load_error = e
|
17
|
-
end
|
18
|
-
|
19
|
-
def initialize(app)
|
20
|
-
super
|
21
|
-
@parser = nil
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.parse(body)
|
25
|
-
ActiveSupport::JSON.decode(body)
|
26
|
-
rescue Object
|
27
|
-
raise Faraday::Error::ParsingError, $!
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Response::Yajl < Response::Middleware
|
3
|
-
begin
|
4
|
-
require 'yajl'
|
5
|
-
|
6
|
-
def self.register_on_complete(env)
|
7
|
-
env[:response].on_complete do |finished_env|
|
8
|
-
finished_env[:body] = parse(finished_env[:body])
|
9
|
-
end
|
10
|
-
end
|
11
|
-
rescue LoadError, NameError => e
|
12
|
-
self.load_error = e
|
13
|
-
end
|
14
|
-
|
15
|
-
def initialize(app)
|
16
|
-
super
|
17
|
-
@parser = nil
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.parse(body)
|
21
|
-
Yajl::Parser.parse(body)
|
22
|
-
rescue Object
|
23
|
-
raise Faraday::Error::ParsingError, $!
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
|
2
|
-
|
3
|
-
if Faraday::Adapter::Typhoeus.loaded?
|
4
|
-
module Adapters
|
5
|
-
class TestTyphoeus < Faraday::TestCase
|
6
|
-
def setup
|
7
|
-
@adapter = Faraday::Adapter::Typhoeus.new
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_parse_response_headers_leaves_http_status_line_out
|
11
|
-
headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
|
12
|
-
assert_equal %w(content-type), headers.keys
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_parse_response_headers_parses_lower_cased_header_name_and_value
|
16
|
-
headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
|
17
|
-
assert_equal 'text/html', headers['content-type']
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_parse_response_headers_parses_lower_cased_header_name_and_value_with_colon
|
21
|
-
headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://sushi.com/\r\n\r\n")
|
22
|
-
assert_equal 'http://sushi.com/', headers['location']
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_parse_response_headers_parses_blank_lines
|
26
|
-
headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\n\r\nContent-Type: text/html\r\n\r\n")
|
27
|
-
assert_equal 'text/html', headers['content-type']
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/test/connection_app_test.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
2
|
-
|
3
|
-
class TestConnectionApps < Faraday::TestCase
|
4
|
-
class TestAdapter
|
5
|
-
def initialize(app)
|
6
|
-
@app = app
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(env)
|
10
|
-
[200, {}, env[:test]]
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class TestMiddleWare
|
15
|
-
def initialize(app)
|
16
|
-
@app = app
|
17
|
-
end
|
18
|
-
|
19
|
-
def call(env)
|
20
|
-
env[:test] = 'hi'
|
21
|
-
@app.call(env)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def setup
|
26
|
-
@conn = Faraday.new do |b|
|
27
|
-
b.use TestMiddleWare
|
28
|
-
b.use TestAdapter
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_builder_is_built_from_faraday_connection
|
33
|
-
assert_kind_of Faraday::Builder, @conn.builder
|
34
|
-
assert_equal 3, @conn.builder.handlers.size
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_builder_adds_middleware_to_builder_stack
|
38
|
-
assert_kind_of TestMiddleWare, @conn.builder[0].call(nil)
|
39
|
-
assert_kind_of TestAdapter, @conn.builder[1].call(nil)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_to_app_returns_rack_object
|
43
|
-
assert @conn.to_app.respond_to?(:call)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_builder_is_passed_to_new_faraday_connection
|
47
|
-
new_conn = Faraday::Connection.new :builder => @conn.builder
|
48
|
-
assert_equal @conn.builder, new_conn.builder
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_builder_is_built_on_new_faraday_connection
|
52
|
-
new_conn = Faraday::Connection.new
|
53
|
-
new_conn.build do |b|
|
54
|
-
b.run @conn.builder[0]
|
55
|
-
b.run @conn.builder[1]
|
56
|
-
end
|
57
|
-
assert_kind_of TestMiddleWare, new_conn.builder[0].call(nil)
|
58
|
-
assert_kind_of TestAdapter, new_conn.builder[1].call(nil)
|
59
|
-
end
|
60
|
-
end
|
data/test/form_post_test.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
2
|
-
|
3
|
-
class FormPostTest < Faraday::TestCase
|
4
|
-
def setup
|
5
|
-
@app = Faraday::Adapter.new nil
|
6
|
-
@env = {:request_headers => {}}
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_processes_nested_body
|
10
|
-
@env[:body] = {:a => 1, :b => {:c => 2}}
|
11
|
-
@app.process_body_for_request @env
|
12
|
-
assert_match /^|\&a=1/, @env[:body]
|
13
|
-
assert_match /^|\&b\[c\]=2/, @env[:body]
|
14
|
-
assert_equal Faraday::Adapter::FORM_TYPE, @env[:request_headers]['Content-Type']
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_processes_with_custom_type
|
18
|
-
@env[:body] = {:a => 1}
|
19
|
-
@env[:request_headers]['Content-Type'] = 'test/type'
|
20
|
-
@app.process_body_for_request @env
|
21
|
-
assert_equal 'a=1', @env[:body]
|
22
|
-
assert_equal 'test/type', @env[:request_headers]['Content-Type']
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_processes_nil_body
|
26
|
-
@env[:body] = nil
|
27
|
-
@app.process_body_for_request @env
|
28
|
-
assert_nil @env[:body]
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_processes_empty_body
|
32
|
-
@env[:body] = ''
|
33
|
-
@app.process_body_for_request @env
|
34
|
-
assert_equal '', @env[:body]
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_processes_string_body
|
38
|
-
@env[:body] = 'abc'
|
39
|
-
@app.process_body_for_request @env
|
40
|
-
assert_equal 'abc', @env[:body]
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_processes_array_values
|
44
|
-
@env[:body] = {:a => [:b, 1]}
|
45
|
-
@app.process_body_for_request @env
|
46
|
-
assert_equal 'a[]=b&a[]=1', @env[:body]
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_processes_nested_array_values
|
50
|
-
@env[:body] = {:a => [:b, {:c => :d}, [:e]]}
|
51
|
-
@app.process_body_for_request @env
|
52
|
-
|
53
|
-
# a[]=b&a[][c]=d&a[][]=e
|
54
|
-
assert_match /a\[\]=b/, @env[:body]
|
55
|
-
assert_match /a\[\]\[c\]=d/, @env[:body]
|
56
|
-
assert_match /a\[\]\[\]=e/, @env[:body]
|
57
|
-
end
|
58
|
-
end
|
data/test/multipart_test.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
2
|
-
|
3
|
-
Faraday::CompositeReadIO.send :attr_reader, :ios
|
4
|
-
|
5
|
-
class MultipartTest < Faraday::TestCase
|
6
|
-
def setup
|
7
|
-
@app = Faraday::Adapter.new nil
|
8
|
-
@env = {:request_headers => {}}
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_processes_nested_body
|
12
|
-
# assume params are out of order
|
13
|
-
regexes = [
|
14
|
-
/name\=\"a\"/,
|
15
|
-
/name=\"b\[c\]\"\; filename\=\"multipart_test\.rb\"/,
|
16
|
-
/name=\"b\[d\]\"/]
|
17
|
-
@env[:body] = {:a => 1, :b => {:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}}
|
18
|
-
@app.process_body_for_request @env
|
19
|
-
@env[:body].send(:ios).map(&:read).each do |io|
|
20
|
-
if re = regexes.detect { |r| io =~ r }
|
21
|
-
regexes.delete re
|
22
|
-
end
|
23
|
-
end
|
24
|
-
assert_equal [], regexes
|
25
|
-
assert_kind_of CompositeReadIO, @env[:body]
|
26
|
-
assert_equal "%s;boundary=%s" %
|
27
|
-
[Faraday::Adapter::MULTIPART_TYPE, Faraday::Adapter::DEFAULT_BOUNDARY],
|
28
|
-
@env[:request_headers]['Content-Type']
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_processes_nil_body
|
32
|
-
@env[:body] = nil
|
33
|
-
@app.process_body_for_request @env
|
34
|
-
assert_nil @env[:body]
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_processes_empty_body
|
38
|
-
@env[:body] = ''
|
39
|
-
@app.process_body_for_request @env
|
40
|
-
assert_equal '', @env[:body]
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_processes_string_body
|
44
|
-
@env[:body] = 'abc'
|
45
|
-
@app.process_body_for_request @env
|
46
|
-
assert_equal 'abc', @env[:body]
|
47
|
-
end
|
48
|
-
end
|