faraday 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +1 -1
- data/README.rdoc +48 -61
- data/Rakefile +3 -1
- data/VERSION +1 -1
- data/faraday.gemspec +31 -14
- data/lib/faraday.rb +33 -26
- data/lib/faraday/adapter/net_http.rb +17 -31
- data/lib/faraday/adapter/patron.rb +33 -0
- data/lib/faraday/adapter/test.rb +96 -0
- data/lib/faraday/adapter/typhoeus.rb +43 -59
- data/lib/faraday/builder.rb +57 -0
- data/lib/faraday/connection.rb +112 -105
- data/lib/faraday/middleware.rb +54 -0
- data/lib/faraday/request.rb +77 -0
- data/lib/faraday/request/active_support_json.rb +20 -0
- data/lib/faraday/request/yajl.rb +18 -0
- data/lib/faraday/response.rb +41 -26
- data/lib/faraday/response/active_support_json.rb +22 -0
- data/lib/faraday/response/yajl.rb +20 -0
- data/test/adapters/live_test.rb +157 -0
- data/test/adapters/test_middleware_test.rb +28 -0
- data/test/adapters/typhoeus_test.rb +28 -0
- data/test/connection_app_test.rb +49 -0
- data/test/connection_test.rb +47 -18
- data/test/env_test.rb +35 -0
- data/test/helper.rb +5 -2
- data/test/live_server.rb +2 -15
- data/test/request_middleware_test.rb +19 -0
- data/test/response_middleware_test.rb +21 -0
- metadata +46 -16
- data/lib/faraday/adapter/mock_request.rb +0 -120
- data/lib/faraday/loadable.rb +0 -13
- data/lib/faraday/request/post_request.rb +0 -30
- data/lib/faraday/request/yajl_request.rb +0 -27
- data/lib/faraday/response/yajl_response.rb +0 -35
- data/lib/faraday/test_connection.rb +0 -5
- data/test/adapter/typhoeus_test.rb +0 -26
- data/test/adapter_test.rb +0 -118
- data/test/response_test.rb +0 -34
@@ -1,120 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
module Adapter
|
3
|
-
module MockRequest
|
4
|
-
extend Faraday::Connection::Options
|
5
|
-
def self.loaded?() false end
|
6
|
-
|
7
|
-
include Faraday::Error # ConnectionFailed
|
8
|
-
|
9
|
-
class Stubs
|
10
|
-
def initialize
|
11
|
-
# {:get => [Stub, Stub]}
|
12
|
-
@stack = {}
|
13
|
-
yield self if block_given?
|
14
|
-
end
|
15
|
-
|
16
|
-
def empty?
|
17
|
-
@stack.empty?
|
18
|
-
end
|
19
|
-
|
20
|
-
def match(request_method, path, data, request_headers)
|
21
|
-
return false if !@stack.key?(request_method)
|
22
|
-
stub = @stack[request_method].detect { |stub| stub.matches?(path, data, request_headers) }
|
23
|
-
@stack[request_method].delete(stub) if stub
|
24
|
-
end
|
25
|
-
|
26
|
-
def get(path, request_headers = {}, &block)
|
27
|
-
(@stack[:get] ||= []) << new_stub(path, {}, request_headers, block)
|
28
|
-
end
|
29
|
-
|
30
|
-
def delete(path, request_headers = {}, &block)
|
31
|
-
(@stack[:delete] ||= []) << new_stub(path, {}, request_headers, block)
|
32
|
-
end
|
33
|
-
|
34
|
-
def post(path, data, request_headers = {}, &block)
|
35
|
-
(@stack[:post] ||= []) << new_stub(path, data, request_headers, block)
|
36
|
-
end
|
37
|
-
|
38
|
-
def put(path, data, request_headers = {}, &block)
|
39
|
-
(@stack[:put] ||= []) << new_stub(path, data, request_headers, block)
|
40
|
-
end
|
41
|
-
|
42
|
-
def new_stub(path, data, request_headers, block)
|
43
|
-
status, response_headers, body = block.call
|
44
|
-
Stub.new(path, request_headers, status, response_headers, body, data)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class Stub < Struct.new(:path, :request_headers, :status, :response_headers, :body, :data)
|
49
|
-
def matches?(request_path, params, headers)
|
50
|
-
return false if request_path != path
|
51
|
-
return false if params != data
|
52
|
-
return true if request_headers.empty?
|
53
|
-
request_headers.each do |key, value|
|
54
|
-
return false if headers[key] != value
|
55
|
-
end
|
56
|
-
true
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def initialize &block
|
61
|
-
super
|
62
|
-
configure(&block) if block
|
63
|
-
end
|
64
|
-
|
65
|
-
def configure
|
66
|
-
yield stubs
|
67
|
-
end
|
68
|
-
|
69
|
-
def stubs
|
70
|
-
@stubs ||= Stubs.new
|
71
|
-
end
|
72
|
-
|
73
|
-
def _get(uri, headers)
|
74
|
-
raise ConnectionFailed, "no stubbed requests" if stubs.empty?
|
75
|
-
if stub = @stubs.match(:get, uri.path, {}, headers)
|
76
|
-
response_class.new do |resp|
|
77
|
-
resp.headers = stub.response_headers
|
78
|
-
resp.process stub.body
|
79
|
-
end
|
80
|
-
else
|
81
|
-
nil
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def _delete(uri, headers)
|
86
|
-
raise ConnectionFailed, "no stubbed requests" if stubs.empty?
|
87
|
-
if stub = @stubs.match(:delete, uri.path, {}, headers)
|
88
|
-
response_class.new do |resp|
|
89
|
-
resp.headers = stub.response_headers
|
90
|
-
resp.process stub.body
|
91
|
-
end
|
92
|
-
else
|
93
|
-
nil
|
94
|
-
end
|
95
|
-
end
|
96
|
-
def _post(uri, data, headers)
|
97
|
-
raise ConnectionFailed, "no stubbed requests" if stubs.empty?
|
98
|
-
if stub = @stubs.match(:post, uri.path, data, headers)
|
99
|
-
response_class.new do |resp|
|
100
|
-
resp.headers = stub.response_headers
|
101
|
-
resp.process stub.body
|
102
|
-
end
|
103
|
-
else
|
104
|
-
nil
|
105
|
-
end
|
106
|
-
end
|
107
|
-
def _put(uri, data, headers)
|
108
|
-
raise ConnectionFailed, "no stubbed requests" if stubs.empty?
|
109
|
-
if stub = @stubs.match(:put, uri.path, data, headers)
|
110
|
-
response_class.new do |resp|
|
111
|
-
resp.headers = stub.response_headers
|
112
|
-
resp.process stub.body
|
113
|
-
end
|
114
|
-
else
|
115
|
-
nil
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
data/lib/faraday/loadable.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
module Request
|
3
|
-
class PostRequest
|
4
|
-
extend Loadable
|
5
|
-
|
6
|
-
def initialize params, headers={}
|
7
|
-
@params = params
|
8
|
-
@headers = headers
|
9
|
-
end
|
10
|
-
|
11
|
-
def headers
|
12
|
-
@headers.merge('Content-Type' => 'application/x-www-form-urlencoded')
|
13
|
-
end
|
14
|
-
|
15
|
-
def body
|
16
|
-
create_post_params @params
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
def create_post_params(params, base = "")
|
21
|
-
[].tap do |toreturn|
|
22
|
-
params.each_key do |key|
|
23
|
-
keystring = base == '' ? key : "#{base}[#{key}]"
|
24
|
-
toreturn << (params[key].kind_of?(Hash) ? create_post_params(params[key], keystring) : "#{keystring}=#{CGI.escape(params[key].to_s)}")
|
25
|
-
end
|
26
|
-
end.join('&')
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
module Request
|
3
|
-
class YajlRequest
|
4
|
-
extend Loadable
|
5
|
-
|
6
|
-
begin
|
7
|
-
require 'yajl'
|
8
|
-
|
9
|
-
def initialize params, headers={}
|
10
|
-
@params = params
|
11
|
-
@headers = headers
|
12
|
-
end
|
13
|
-
|
14
|
-
def headers
|
15
|
-
@headers.merge('Content-Type' => 'application/json')
|
16
|
-
end
|
17
|
-
|
18
|
-
# TODO streaming
|
19
|
-
def body
|
20
|
-
Yajl::Encoder.encode @params
|
21
|
-
end
|
22
|
-
rescue LoadError => e
|
23
|
-
self.load_error = e
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Response
|
3
|
-
class YajlResponse < Response
|
4
|
-
attr_reader :body
|
5
|
-
|
6
|
-
begin
|
7
|
-
require 'yajl'
|
8
|
-
|
9
|
-
def initialize(headers = nil, body = nil)
|
10
|
-
super
|
11
|
-
@parser = nil
|
12
|
-
end
|
13
|
-
|
14
|
-
def process(chunk)
|
15
|
-
if !@parser
|
16
|
-
@parser = Yajl::Parser.new
|
17
|
-
@parser.on_parse_complete = method(:object_parsed)
|
18
|
-
end
|
19
|
-
@parser << chunk
|
20
|
-
end
|
21
|
-
|
22
|
-
def processed!
|
23
|
-
@parser = nil
|
24
|
-
end
|
25
|
-
|
26
|
-
def object_parsed(obj)
|
27
|
-
@body = obj
|
28
|
-
end
|
29
|
-
|
30
|
-
rescue LoadError => e
|
31
|
-
self.load_error = e
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
|
2
|
-
|
3
|
-
if Faraday::Adapter::Typhoeus.loaded?
|
4
|
-
class TyphoeusTest < Faraday::TestCase
|
5
|
-
describe "#parse_response_headers" do
|
6
|
-
before do
|
7
|
-
@conn = Object.new.extend(Faraday::Adapter::Typhoeus)
|
8
|
-
end
|
9
|
-
|
10
|
-
it "leaves http status line out" do
|
11
|
-
headers = @conn.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
|
-
it "parses lower-cased header name and value" do
|
16
|
-
headers = @conn.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
|
-
it "parses lower-cased header name and value with colon" do
|
21
|
-
headers = @conn.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
|
-
end
|
25
|
-
end
|
26
|
-
end
|
data/test/adapter_test.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
2
|
-
|
3
|
-
class AdapterTest < Faraday::TestCase
|
4
|
-
before do
|
5
|
-
@connection = Faraday::Connection.new(LIVE_SERVER)
|
6
|
-
end
|
7
|
-
|
8
|
-
Faraday::Adapter.loaded_adapters.each do |adapter|
|
9
|
-
describe "with #{adapter} adapter" do
|
10
|
-
before do
|
11
|
-
@connection.extend adapter
|
12
|
-
end
|
13
|
-
|
14
|
-
describe "#delete" do
|
15
|
-
it "retrieves the response body with YajlResponse" do
|
16
|
-
@connection.response_class = Faraday::Response::YajlResponse
|
17
|
-
assert_equal({'deleted' => true},
|
18
|
-
@connection.delete('delete_with_json').body)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "send url-encoded params" do
|
22
|
-
assert_equal('foobar', @connection.delete('delete_with_params', 'deleted' => 'foobar').body)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
it "passes params" do
|
27
|
-
@connection.params = {:a => 1}
|
28
|
-
assert_equal "params[:a] == 1", @connection.get('params').body
|
29
|
-
end
|
30
|
-
|
31
|
-
it "passes headers" do
|
32
|
-
@connection.headers = {"X-Test" => 1}
|
33
|
-
assert_equal "env[HTTP_X_TEST] == 1", @connection.get('headers').body
|
34
|
-
end
|
35
|
-
|
36
|
-
it "retrieves the response body" do
|
37
|
-
assert_equal 'hello world', @connection.get('hello_world').body
|
38
|
-
end
|
39
|
-
|
40
|
-
describe "#put" do
|
41
|
-
it "sends params" do
|
42
|
-
assert_equal 'hello zack', @connection.put('hello', 'name' => 'zack').body
|
43
|
-
end
|
44
|
-
|
45
|
-
it "retrieves the response body with YajlResponse" do
|
46
|
-
@connection.response_class = Faraday::Response::YajlResponse
|
47
|
-
assert_equal({'name' => 'zack'},
|
48
|
-
@connection.put('echo_name', 'name' => 'zack').body)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "#post" do
|
53
|
-
it "sends params" do
|
54
|
-
assert_equal 'hello zack', @connection.post('hello', 'name' => 'zack').body
|
55
|
-
end
|
56
|
-
|
57
|
-
it "retrieves the response body with YajlResponse" do
|
58
|
-
@connection.response_class = Faraday::Response::YajlResponse
|
59
|
-
assert_equal({'name' => 'zack'},
|
60
|
-
@connection.post('echo_name', 'name' => 'zack').body)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe "#get" do
|
65
|
-
it "raises on 404" do
|
66
|
-
assert_raise(Faraday::Error::ResourceNotFound) { @connection.get('/nothing') }
|
67
|
-
end
|
68
|
-
it "retrieves the response body" do
|
69
|
-
assert_equal 'hello world', @connection.get('hello_world').body
|
70
|
-
end
|
71
|
-
|
72
|
-
it "send url-encoded params" do
|
73
|
-
assert_equal('hello zack', @connection.get('hello', 'name' => 'zack').body)
|
74
|
-
end
|
75
|
-
|
76
|
-
it "retrieves the response body with YajlResponse" do
|
77
|
-
@connection.response_class = Faraday::Response::YajlResponse
|
78
|
-
assert_equal [1,2,3], @connection.get('json').body
|
79
|
-
end
|
80
|
-
|
81
|
-
it "retrieves the response headers" do
|
82
|
-
assert_equal 'text/html', @connection.get('hello_world').headers['content-type']
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
describe "async requests" do
|
88
|
-
before do
|
89
|
-
@connection.extend adapter
|
90
|
-
end
|
91
|
-
|
92
|
-
it "clears parallel manager after running a single request" do
|
93
|
-
assert !@connection.in_parallel?
|
94
|
-
resp = @connection.get('hello_world')
|
95
|
-
assert !@connection.in_parallel?
|
96
|
-
assert_equal 'hello world', @connection.get('hello_world').body
|
97
|
-
end
|
98
|
-
|
99
|
-
it "uses parallel manager to run multiple json requests" do
|
100
|
-
resp1, resp2 = nil, nil
|
101
|
-
|
102
|
-
@connection.response_class = Faraday::Response::YajlResponse
|
103
|
-
@connection.in_parallel do
|
104
|
-
resp1 = @connection.get('json')
|
105
|
-
resp2 = @connection.get('json')
|
106
|
-
assert @connection.in_parallel?
|
107
|
-
if adapter.supports_async?
|
108
|
-
assert_nil resp1.body
|
109
|
-
assert_nil resp2.body
|
110
|
-
end
|
111
|
-
end
|
112
|
-
assert !@connection.in_parallel?
|
113
|
-
assert_equal [1,2,3], resp1.body
|
114
|
-
assert_equal [1,2,3], resp2.body
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
data/test/response_test.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
2
|
-
|
3
|
-
class ResponseTest < Faraday::TestCase
|
4
|
-
describe "unloaded response class" do
|
5
|
-
it "is not allowed to be set" do
|
6
|
-
resp_class = Object.new
|
7
|
-
def resp_class.loaded?() false end
|
8
|
-
conn = Faraday::Connection.new
|
9
|
-
assert_raises ArgumentError do
|
10
|
-
conn.response_class = resp_class
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "TestConnection#get with default Faraday::Response class" do
|
16
|
-
it "returns Faraday::Response" do
|
17
|
-
conn = Faraday::TestConnection.new do |stub|
|
18
|
-
stub.get('/hello') { [200, {}, 'hello world']}
|
19
|
-
end
|
20
|
-
resp = conn.get('/hello')
|
21
|
-
assert_equal 'hello world', resp.body
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "TestConnection#get with Faraday::YajlResponse class" do
|
26
|
-
it "returns string body" do
|
27
|
-
conn = Faraday::TestConnection.new do |stub|
|
28
|
-
stub.get('/hello') { [200, {}, '[1,2,3]']}
|
29
|
-
end
|
30
|
-
conn.response_class = Faraday::Response::YajlResponse
|
31
|
-
assert_equal [1,2,3], conn.get('/hello').body
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|