avdi-faraday 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +27 -0
- data/LICENSE.md +20 -0
- data/README.md +250 -0
- data/Rakefile +87 -0
- data/config.ru +6 -0
- data/faraday.gemspec +86 -0
- data/lib/faraday.rb +276 -0
- data/lib/faraday/adapter.rb +71 -0
- data/lib/faraday/adapter/em_http.rb +217 -0
- data/lib/faraday/adapter/em_synchrony.rb +89 -0
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +66 -0
- data/lib/faraday/adapter/excon.rb +59 -0
- data/lib/faraday/adapter/httpclient.rb +92 -0
- data/lib/faraday/adapter/net_http.rb +116 -0
- data/lib/faraday/adapter/net_http_persistent.rb +37 -0
- data/lib/faraday/adapter/patron.rb +65 -0
- data/lib/faraday/adapter/rack.rb +57 -0
- data/lib/faraday/adapter/test.rb +162 -0
- data/lib/faraday/adapter/typhoeus.rb +107 -0
- data/lib/faraday/builder.rb +184 -0
- data/lib/faraday/connection.rb +468 -0
- data/lib/faraday/error.rb +40 -0
- data/lib/faraday/middleware.rb +41 -0
- data/lib/faraday/request.rb +101 -0
- data/lib/faraday/request/authorization.rb +40 -0
- data/lib/faraday/request/basic_authentication.rb +13 -0
- data/lib/faraday/request/multipart.rb +62 -0
- data/lib/faraday/request/retry.rb +67 -0
- data/lib/faraday/request/token_authentication.rb +15 -0
- data/lib/faraday/request/url_encoded.rb +35 -0
- data/lib/faraday/response.rb +99 -0
- data/lib/faraday/response/logger.rb +34 -0
- data/lib/faraday/response/raise_error.rb +16 -0
- data/lib/faraday/upload_io.rb +23 -0
- data/lib/faraday/utils.rb +274 -0
- data/script/test +91 -0
- data/test/adapters/default_test.rb +14 -0
- data/test/adapters/em_http_test.rb +19 -0
- data/test/adapters/em_synchrony_test.rb +20 -0
- data/test/adapters/excon_test.rb +15 -0
- data/test/adapters/httpclient_test.rb +16 -0
- data/test/adapters/integration.rb +193 -0
- data/test/adapters/logger_test.rb +37 -0
- data/test/adapters/net_http_persistent_test.rb +11 -0
- data/test/adapters/net_http_test.rb +49 -0
- data/test/adapters/patron_test.rb +17 -0
- data/test/adapters/rack_test.rb +26 -0
- data/test/adapters/test_middleware_test.rb +70 -0
- data/test/adapters/typhoeus_test.rb +20 -0
- data/test/authentication_middleware_test.rb +65 -0
- data/test/connection_test.rb +375 -0
- data/test/env_test.rb +183 -0
- data/test/helper.rb +75 -0
- data/test/live_server.rb +57 -0
- data/test/middleware/retry_test.rb +62 -0
- data/test/middleware_stack_test.rb +203 -0
- data/test/middleware_test.rb +12 -0
- data/test/request_middleware_test.rb +108 -0
- data/test/response_middleware_test.rb +74 -0
- metadata +182 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
2
|
+
|
3
|
+
class TestConnection < Faraday::TestCase
|
4
|
+
|
5
|
+
def test_adapters_know_they_are_adapters
|
6
|
+
assert Faraday::Adapter.adapter?
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_middleware_is_not_adapter_by_default
|
10
|
+
assert !Faraday::Middleware.adapter?
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
3
|
+
|
4
|
+
Faraday::CompositeReadIO.class_eval { attr_reader :ios }
|
5
|
+
|
6
|
+
class RequestMiddlewareTest < Faraday::TestCase
|
7
|
+
def setup
|
8
|
+
@conn = Faraday.new do |b|
|
9
|
+
b.request :multipart
|
10
|
+
b.request :url_encoded
|
11
|
+
b.adapter :test do |stub|
|
12
|
+
stub.post('/echo') do |env|
|
13
|
+
posted_as = env[:request_headers]['Content-Type']
|
14
|
+
[200, {'Content-Type' => posted_as}, env[:body]]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_does_nothing_without_payload
|
21
|
+
response = @conn.post('/echo')
|
22
|
+
assert_nil response.headers['Content-Type']
|
23
|
+
assert response.body.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_ignores_custom_content_type
|
27
|
+
response = @conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo')
|
28
|
+
assert_equal 'application/x-foo', response.headers['Content-Type']
|
29
|
+
assert_equal({ :some => 'data' }, response.body)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_url_encoded_no_header
|
33
|
+
response = @conn.post('/echo', { :fruit => %w[apples oranges] })
|
34
|
+
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
35
|
+
assert_equal 'fruit%5B%5D=apples&fruit%5B%5D=oranges', response.body
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_url_encoded_with_header
|
39
|
+
response = @conn.post('/echo', {'a'=>123}, 'content-type' => 'application/x-www-form-urlencoded')
|
40
|
+
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
41
|
+
assert_equal 'a=123', response.body
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_url_encoded_nested
|
45
|
+
response = @conn.post('/echo', { :user => {:name => 'Mislav', :web => 'mislav.net'} })
|
46
|
+
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
47
|
+
expected = { 'user' => {'name' => 'Mislav', 'web' => 'mislav.net'} }
|
48
|
+
assert_equal expected, Faraday::Utils.parse_nested_query(response.body)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_url_encoded_unicode
|
52
|
+
err = capture_warnings {
|
53
|
+
response = @conn.post('/echo', {:str => "eé cç aã aâ"})
|
54
|
+
assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
|
55
|
+
}
|
56
|
+
assert err.empty?
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_url_encoded_nested_keys
|
60
|
+
response = @conn.post('/echo', {'a'=>{'b'=>{'c'=>['d']}}})
|
61
|
+
assert_equal "a%5Bb%5D%5Bc%5D%5B%5D=d", response.body
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_multipart
|
65
|
+
# assume params are out of order
|
66
|
+
regexes = [
|
67
|
+
/name\=\"a\"/,
|
68
|
+
/name=\"b\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
|
69
|
+
/name=\"b\[d\]\"/]
|
70
|
+
|
71
|
+
payload = {:a => 1, :b => {:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}}
|
72
|
+
response = @conn.post('/echo', payload)
|
73
|
+
|
74
|
+
assert_kind_of Faraday::CompositeReadIO, response.body
|
75
|
+
assert_equal "multipart/form-data;boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
|
76
|
+
response.headers['Content-Type']
|
77
|
+
|
78
|
+
response.body.send(:ios).map{|io| io.read}.each do |io|
|
79
|
+
if re = regexes.detect { |r| io =~ r }
|
80
|
+
regexes.delete re
|
81
|
+
end
|
82
|
+
end
|
83
|
+
assert_equal [], regexes
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_multipart_with_arrays
|
87
|
+
# assume params are out of order
|
88
|
+
regexes = [
|
89
|
+
/name\=\"a\"/,
|
90
|
+
/name=\"b\[\]\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
|
91
|
+
/name=\"b\[\]\[d\]\"/]
|
92
|
+
|
93
|
+
payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
|
94
|
+
response = @conn.post('/echo', payload)
|
95
|
+
|
96
|
+
assert_kind_of Faraday::CompositeReadIO, response.body
|
97
|
+
assert_equal "multipart/form-data;boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
|
98
|
+
response.headers['Content-Type']
|
99
|
+
|
100
|
+
response.body.send(:ios).map{|io| io.read}.each do |io|
|
101
|
+
if re = regexes.detect { |r| io =~ r }
|
102
|
+
regexes.delete re
|
103
|
+
end
|
104
|
+
end
|
105
|
+
assert_equal [], regexes
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
2
|
+
|
3
|
+
class ResponseMiddlewareTest < Faraday::TestCase
|
4
|
+
def setup
|
5
|
+
@conn = Faraday.new do |b|
|
6
|
+
b.response :raise_error
|
7
|
+
b.adapter :test do |stub|
|
8
|
+
stub.get('ok') { [200, {'Content-Type' => 'text/html'}, '<body></body>'] }
|
9
|
+
stub.get('not-found') { [404, {'X-Reason' => 'because'}, 'keep looking'] }
|
10
|
+
stub.get('error') { [500, {'X-Error' => 'bailout'}, 'fail'] }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ResponseUpcaser < Faraday::Response::Middleware
|
16
|
+
def parse(body)
|
17
|
+
body.upcase
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_success
|
22
|
+
assert_nothing_raised do
|
23
|
+
@conn.get('ok')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_raises_not_found
|
28
|
+
error = assert_raises Faraday::Error::ResourceNotFound do
|
29
|
+
@conn.get('not-found')
|
30
|
+
end
|
31
|
+
assert_equal 'the server responded with status 404', error.message
|
32
|
+
assert_equal 'because', error.response[:headers]['X-Reason']
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_raises_error
|
36
|
+
error = assert_raises Faraday::Error::ClientError do
|
37
|
+
@conn.get('error')
|
38
|
+
end
|
39
|
+
assert_equal 'the server responded with status 500', error.message
|
40
|
+
assert_equal 'bailout', error.response[:headers]['X-Error']
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_upcase
|
44
|
+
@conn.builder.insert(0, ResponseUpcaser)
|
45
|
+
assert_equal '<BODY></BODY>', @conn.get('ok').body
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class ResponseNoBodyMiddleWareTest < Faraday::TestCase
|
50
|
+
def setup
|
51
|
+
@conn = Faraday.new do |b|
|
52
|
+
b.response :raise_error
|
53
|
+
b.adapter :test do |stub|
|
54
|
+
stub.get('not_modified') { [304, nil, nil] }
|
55
|
+
stub.get('no_content') { [204, nil, nil] }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
@conn.builder.insert(0, NotCalled)
|
59
|
+
end
|
60
|
+
|
61
|
+
class NotCalled < Faraday::Response::Middleware
|
62
|
+
def parse(body)
|
63
|
+
raise "this should not be called"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_204
|
68
|
+
assert_equal nil, @conn.get('no_content').body
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_304
|
72
|
+
assert_equal nil, @conn.get('not_modified').body
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: avdi-faraday
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rick Olson
|
9
|
+
- Avdi Grimm
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-06-21 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: multipart-post
|
17
|
+
requirement: &26448080 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '1.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *26448080
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &26447700 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *26447700
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: simplecov
|
39
|
+
requirement: &26447240 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *26447240
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: test-unit
|
50
|
+
requirement: &26446820 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *26446820
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: webmock
|
61
|
+
requirement: &26446400 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *26446400
|
70
|
+
description: Just a fork so I can satisfy gem dependencies in Leadlight.
|
71
|
+
email: avdi@avdi.org
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE.md
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- config.ru
|
81
|
+
- faraday.gemspec
|
82
|
+
- lib/faraday.rb
|
83
|
+
- lib/faraday/adapter.rb
|
84
|
+
- lib/faraday/adapter/em_http.rb
|
85
|
+
- lib/faraday/adapter/em_synchrony.rb
|
86
|
+
- lib/faraday/adapter/em_synchrony/parallel_manager.rb
|
87
|
+
- lib/faraday/adapter/excon.rb
|
88
|
+
- lib/faraday/adapter/httpclient.rb
|
89
|
+
- lib/faraday/adapter/net_http.rb
|
90
|
+
- lib/faraday/adapter/net_http_persistent.rb
|
91
|
+
- lib/faraday/adapter/patron.rb
|
92
|
+
- lib/faraday/adapter/rack.rb
|
93
|
+
- lib/faraday/adapter/test.rb
|
94
|
+
- lib/faraday/adapter/typhoeus.rb
|
95
|
+
- lib/faraday/builder.rb
|
96
|
+
- lib/faraday/connection.rb
|
97
|
+
- lib/faraday/error.rb
|
98
|
+
- lib/faraday/middleware.rb
|
99
|
+
- lib/faraday/request.rb
|
100
|
+
- lib/faraday/request/authorization.rb
|
101
|
+
- lib/faraday/request/basic_authentication.rb
|
102
|
+
- lib/faraday/request/multipart.rb
|
103
|
+
- lib/faraday/request/retry.rb
|
104
|
+
- lib/faraday/request/token_authentication.rb
|
105
|
+
- lib/faraday/request/url_encoded.rb
|
106
|
+
- lib/faraday/response.rb
|
107
|
+
- lib/faraday/response/logger.rb
|
108
|
+
- lib/faraday/response/raise_error.rb
|
109
|
+
- lib/faraday/upload_io.rb
|
110
|
+
- lib/faraday/utils.rb
|
111
|
+
- script/test
|
112
|
+
- test/adapters/default_test.rb
|
113
|
+
- test/adapters/em_http_test.rb
|
114
|
+
- test/adapters/em_synchrony_test.rb
|
115
|
+
- test/adapters/excon_test.rb
|
116
|
+
- test/adapters/httpclient_test.rb
|
117
|
+
- test/adapters/integration.rb
|
118
|
+
- test/adapters/logger_test.rb
|
119
|
+
- test/adapters/net_http_persistent_test.rb
|
120
|
+
- test/adapters/net_http_test.rb
|
121
|
+
- test/adapters/patron_test.rb
|
122
|
+
- test/adapters/rack_test.rb
|
123
|
+
- test/adapters/test_middleware_test.rb
|
124
|
+
- test/adapters/typhoeus_test.rb
|
125
|
+
- test/authentication_middleware_test.rb
|
126
|
+
- test/connection_test.rb
|
127
|
+
- test/env_test.rb
|
128
|
+
- test/helper.rb
|
129
|
+
- test/live_server.rb
|
130
|
+
- test/middleware/retry_test.rb
|
131
|
+
- test/middleware_stack_test.rb
|
132
|
+
- test/middleware_test.rb
|
133
|
+
- test/request_middleware_test.rb
|
134
|
+
- test/response_middleware_test.rb
|
135
|
+
homepage: https://github.com/avdi/faraday
|
136
|
+
licenses: []
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ! '>='
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 1.3.5
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 1.8.10
|
156
|
+
signing_key:
|
157
|
+
specification_version: 2
|
158
|
+
summary: Avdi's personal fork of Faraday. Nothing to see here. Move along.
|
159
|
+
test_files:
|
160
|
+
- test/adapters/default_test.rb
|
161
|
+
- test/adapters/em_http_test.rb
|
162
|
+
- test/adapters/em_synchrony_test.rb
|
163
|
+
- test/adapters/excon_test.rb
|
164
|
+
- test/adapters/httpclient_test.rb
|
165
|
+
- test/adapters/integration.rb
|
166
|
+
- test/adapters/logger_test.rb
|
167
|
+
- test/adapters/net_http_persistent_test.rb
|
168
|
+
- test/adapters/net_http_test.rb
|
169
|
+
- test/adapters/patron_test.rb
|
170
|
+
- test/adapters/rack_test.rb
|
171
|
+
- test/adapters/test_middleware_test.rb
|
172
|
+
- test/adapters/typhoeus_test.rb
|
173
|
+
- test/authentication_middleware_test.rb
|
174
|
+
- test/connection_test.rb
|
175
|
+
- test/env_test.rb
|
176
|
+
- test/helper.rb
|
177
|
+
- test/live_server.rb
|
178
|
+
- test/middleware/retry_test.rb
|
179
|
+
- test/middleware_stack_test.rb
|
180
|
+
- test/middleware_test.rb
|
181
|
+
- test/request_middleware_test.rb
|
182
|
+
- test/response_middleware_test.rb
|