faraday 0.8.0.rc2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,29 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class ActionDispatch < Faraday::Adapter
4
- attr_reader :session
5
-
6
- # Initializes a new middleware instance for each request. Instead of
7
- # initiating an HTTP request with a web server, this adapter calls
8
- # a Rails 3 app using integration tests.
9
- #
10
- # app - The current Faraday request.
11
- # session - An ActionDispatch::Integration::Session instance.
12
- #
13
- # Returns nothing.
14
- def initialize(app, session)
15
- super(app)
16
- @session = session
17
- @session.reset!
18
- end
19
-
20
- def call(env)
21
- super
22
- @session.__send__(env[:method], env[:url].request_uri, env[:body], env[:request_headers])
23
- resp = @session.response
24
- save_response(env, resp.status, resp.body, resp.headers)
25
- @app.call env
26
- end
27
- end
28
- end
29
- end
@@ -1,232 +0,0 @@
1
- require File.expand_path("../../helper", __FILE__)
2
-
3
- if !Faraday::TestCase::LIVE_SERVER
4
- warn "warning: test server not specified; skipping live server tests"
5
- else
6
- module Adapters
7
- class LiveTest < Faraday::TestCase
8
- adapters = if ENV['ADAPTER']
9
- ENV['ADAPTER'].split(':').map { |name| Faraday::Adapter.lookup_middleware name.to_sym }
10
- else
11
- loaded_adapters = Faraday::Adapter.all_loaded_constants
12
- loaded_adapters -= [Faraday::Adapter::Test, Faraday::Adapter::ActionDispatch]
13
- # https://github.com/geemus/excon/issues/98
14
- loaded_adapters -= [Faraday::Adapter::Excon] if defined? RUBY_ENGINE and "rbx" == RUBY_ENGINE
15
- loaded_adapters << :default
16
- end
17
-
18
- adapters.each do |adapter|
19
- define_method "test_#{adapter}_GET_retrieves_the_response_body" do
20
- assert_equal 'hello world', create_connection(adapter).get('hello_world').body
21
- end
22
-
23
- define_method "test_#{adapter}_GET_send_url_encoded_params" do
24
- resp = create_connection(adapter).get do |req|
25
- req.url 'hello', 'name' => 'zack'
26
- end
27
- assert_equal('hello zack', resp.body)
28
- end
29
-
30
- define_method "test_#{adapter}_GET_retrieves_the_response_headers" do
31
- response = create_connection(adapter).get('hello_world')
32
- assert_match(/text\/html/, response.headers['Content-Type'], 'original case fail')
33
- assert_match(/text\/html/, response.headers['content-type'], 'lowercase fail')
34
- end
35
-
36
- # https://github.com/geemus/excon/issues/10
37
- unless %[Faraday::Adapter::Excon] == adapter.to_s
38
- define_method "test_#{adapter}_GET_handles_headers_with_multiple_values" do
39
- response = create_connection(adapter).get('multi')
40
- assert_equal 'one, two', response.headers['set-cookie']
41
- end
42
- end
43
-
44
- # https://github.com/dbalatero/typhoeus/issues/75
45
- # https://github.com/toland/patron/issues/52
46
- unless %[Faraday::Adapter::Typhoeus Faraday::Adapter::Patron].include? adapter.to_s
47
- define_method "test_#{adapter}_GET_with_body" do
48
- response = create_connection(adapter).get('echo') do |req|
49
- req.body = {'bodyrock' => true}
50
- end
51
- assert_equal %(get {"bodyrock"=>"true"}), response.body
52
- end
53
- end
54
-
55
- define_method "test_#{adapter}_POST_send_url_encoded_params" do
56
- resp = create_connection(adapter).post do |req|
57
- req.url 'echo_name'
58
- req.body = {'name' => 'zack'}
59
- end
60
- assert_equal %("zack"), resp.body
61
- end
62
-
63
- define_method "test_#{adapter}_POST_send_url_encoded_nested_params" do
64
- resp = create_connection(adapter).post do |req|
65
- req.url 'echo_name'
66
- req.body = {'name' => {'first' => 'zack'}}
67
- end
68
- assert_equal %({"first"=>"zack"}), resp.body
69
- end
70
-
71
- define_method "test_#{adapter}_POST_retrieves_the_response_headers" do
72
- assert_match(/text\/html/, create_connection(adapter).post('echo_name').headers['content-type'])
73
- end
74
-
75
- define_method "test_#{adapter}_POST_sends_files" do
76
- resp = create_connection(adapter).post do |req|
77
- req.url 'file'
78
- req.body = {'uploaded_file' => Faraday::UploadIO.new(__FILE__, 'text/x-ruby')}
79
- end
80
- assert_equal "file live_test.rb text/x-ruby", resp.body
81
- end unless :default == adapter # isn't configured for multipart
82
-
83
- # https://github.com/toland/patron/issues/9
84
- if ENV['FORCE'] || %[Faraday::Adapter::Patron] != adapter.to_s
85
- define_method "test_#{adapter}_PUT_send_url_encoded_params" do
86
- resp = create_connection(adapter).put do |req|
87
- req.url 'echo_name'
88
- req.body = {'name' => 'zack'}
89
- end
90
- assert_equal %("zack"), resp.body
91
- end
92
-
93
- define_method "test_#{adapter}_PUT_send_url_encoded_nested_params" do
94
- resp = create_connection(adapter).put do |req|
95
- req.url 'echo_name'
96
- req.body = {'name' => {'first' => 'zack'}}
97
- end
98
- assert_equal %({"first"=>"zack"}), resp.body
99
- end
100
- end
101
-
102
- # https://github.com/toland/patron/issues/9
103
- # https://github.com/dbalatero/typhoeus/issues/84
104
- if ENV['FORCE'] || !%w[Faraday::Adapter::Patron Faraday::Adapter::Typhoeus].include?(adapter.to_s)
105
- define_method "test_#{adapter}_PUT_retrieves_the_response_headers" do
106
- assert_match(/text\/html/, create_connection(adapter).put('echo_name').headers['content-type'])
107
- end
108
- end
109
-
110
- # https://github.com/toland/patron/issues/34
111
- unless %w[Faraday::Adapter::Patron].include? adapter.to_s
112
- define_method "test_#{adapter}_PATCH_send_url_encoded_params" do
113
- resp = create_connection(adapter).patch('echo_name', 'name' => 'zack')
114
- assert_equal %("zack"), resp.body
115
- end
116
- end
117
-
118
- define_method "test_#{adapter}_OPTIONS" do
119
- resp = create_connection(adapter).run_request(:options, '/options', nil, {})
120
- assert_equal "hi", resp.body
121
- end
122
-
123
- define_method "test_#{adapter}_HEAD_send_url_encoded_params" do
124
- resp = create_connection(adapter).head do |req|
125
- req.url 'hello', 'name' => 'zack'
126
- end
127
- assert_match(/text\/html/, resp.headers['content-type'])
128
- end
129
-
130
- define_method "test_#{adapter}_HEAD_retrieves_no_response_body" do
131
- assert_equal '', create_connection(adapter).head('hello_world').body.to_s
132
- end
133
-
134
- define_method "test_#{adapter}_HEAD_retrieves_the_response_headers" do
135
- assert_match(/text\/html/, create_connection(adapter).head('hello_world').headers['content-type'])
136
- end
137
-
138
- define_method "test_#{adapter}_DELETE_retrieves_the_response_headers" do
139
- assert_match(/text\/html/, create_connection(adapter).delete('delete_with_json').headers['content-type'])
140
- end
141
-
142
- define_method "test_#{adapter}_DELETE_retrieves_the_body" do
143
- assert_match(/deleted/, create_connection(adapter).delete('delete_with_json').body)
144
- end
145
-
146
- if :default != adapter and adapter.supports_parallel?
147
- define_method "test_#{adapter}_in_parallel" do
148
- resp1, resp2 = nil, nil
149
-
150
- connection = create_connection(adapter)
151
- klass = real_adapter_for(adapter)
152
-
153
- connection.in_parallel do
154
- resp1 = connection.get('echo?a=1')
155
- resp2 = connection.get('echo?b=2')
156
- assert connection.in_parallel?
157
- assert_nil resp1.body
158
- assert_nil resp2.body
159
- end
160
- assert !connection.in_parallel?
161
- assert_equal 'get ?{"a"=>"1"}', resp1.body
162
- assert_equal 'get ?{"b"=>"2"}', resp2.body
163
- end
164
- else
165
- define_method "test_#{adapter}_no_parallel_support" do
166
- connection = create_connection(adapter)
167
- response = nil
168
-
169
- err = capture_warnings do
170
- connection.in_parallel do
171
- response = connection.get('echo').body
172
- end
173
- end
174
- assert response
175
- assert_match "no parallel-capable adapter on Faraday stack", err
176
- assert_match __FILE__, err
177
- end
178
- end
179
-
180
- if adapter.to_s == "Faraday::Adapter::EMSynchrony"
181
- instance_methods.grep(%r{Faraday::Adapter::EMSynchrony}).each do |method|
182
- em = method.to_s.sub %r{^test_}, "test_under_em_"
183
- define_method em do
184
- EM.run do
185
- Fiber.new do
186
- self.send method
187
- EM.stop
188
- end.resume
189
- end
190
- end
191
- end
192
- end
193
-
194
- # https://github.com/eventmachine/eventmachine/pull/289
195
- unless %w[Faraday::Adapter::EMHttp Faraday::Adapter::EMSynchrony Faraday::Adapter::Excon].include?(adapter.to_s)
196
- define_method "test_#{adapter}_timeout" do
197
- conn = create_connection(adapter, :request => {:timeout => 1, :open_timeout => 1})
198
- assert_raise Faraday::Error::TimeoutError do
199
- conn.get '/slow'
200
- end
201
- end
202
- end
203
- end
204
-
205
- def create_connection(adapter, options = {})
206
- if adapter == :default
207
- builder_block = nil
208
- else
209
- builder_block = Proc.new do |b|
210
- b.request :multipart
211
- b.request :url_encoded
212
- b.use adapter
213
- end
214
- end
215
-
216
- Faraday::Connection.new(LIVE_SERVER, options, &builder_block).tap do |conn|
217
- conn.headers['X-Faraday-Adapter'] = adapter.to_s
218
- adapter_handler = conn.builder.handlers.last
219
- conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
220
- end
221
- end
222
-
223
- def real_adapter_for(adapter)
224
- if adapter == :default
225
- Faraday::Adapter.lookup_middleware(Faraday.default_adapter)
226
- else
227
- adapter
228
- end
229
- end
230
- end
231
- end
232
- end