faraday 0.7.6 → 0.8.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,7 @@ if ENV['LEFTRIGHT']
9
9
  end
10
10
  end
11
11
 
12
- require 'faraday'
12
+ require File.expand_path('../../lib/faraday', __FILE__)
13
13
 
14
14
  begin
15
15
  require 'ruby-debug'
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "helper"))
2
+
3
+ module Middleware
4
+ class RetryTest < Faraday::TestCase
5
+ def setup
6
+ @stubs = Faraday::Adapter::Test::Stubs.new
7
+ @conn = Faraday.new do |b|
8
+ b.request :retry, 2
9
+ b.adapter :test, @stubs
10
+ end
11
+ end
12
+
13
+ def test_retries
14
+ times_called = 0
15
+
16
+ @stubs.post("/echo") do
17
+ times_called += 1
18
+ raise "Error occurred"
19
+ end
20
+
21
+ @conn.post("/echo") rescue nil
22
+ assert_equal times_called, 3
23
+ end
24
+ end
25
+ end
@@ -12,14 +12,18 @@ class MiddlewareStackTest < Faraday::TestCase
12
12
  class Orange < Handler; end
13
13
  class Banana < Handler; end
14
14
 
15
+ class Broken < Faraday::Middleware
16
+ dependency 'zomg/i_dont/exist'
17
+ end
18
+
15
19
  def setup
16
20
  @conn = Faraday::Connection.new
17
21
  @builder = @conn.builder
18
22
  end
19
23
 
20
24
  def test_sets_default_adapter_if_none_set
21
- default_middleware = Faraday::Request.lookup_module :url_encoded
22
- default_adapter_klass = Faraday::Adapter.lookup_module Faraday.default_adapter
25
+ default_middleware = Faraday::Request.lookup_middleware :url_encoded
26
+ default_adapter_klass = Faraday::Adapter.lookup_middleware Faraday.default_adapter
23
27
  assert @builder[0] == default_middleware
24
28
  assert @builder[1] == default_adapter_klass
25
29
  end
@@ -94,12 +98,57 @@ class MiddlewareStackTest < Faraday::TestCase
94
98
  assert_equal @builder.handlers.first, Faraday::Builder::Handler.new(Apple)
95
99
  end
96
100
 
101
+ def test_unregistered_symbol
102
+ err = assert_raise(RuntimeError) { build_stack :apple }
103
+ assert_equal ":apple is not registered on Faraday::Middleware", err.message
104
+ end
105
+
106
+ def test_registered_symbol
107
+ Faraday.register_middleware :apple => Apple
108
+ begin
109
+ build_stack :apple
110
+ assert_handlers %w[Apple]
111
+ ensure
112
+ unregister_middleware Faraday::Middleware, :apple
113
+ end
114
+ end
115
+
116
+ def test_registered_symbol_with_proc
117
+ Faraday.register_middleware :apple => lambda { Apple }
118
+ begin
119
+ build_stack :apple
120
+ assert_handlers %w[Apple]
121
+ ensure
122
+ unregister_middleware Faraday::Middleware, :apple
123
+ end
124
+ end
125
+
126
+ def test_registered_symbol_with_type
127
+ Faraday.register_middleware :request, :orange => Orange
128
+ begin
129
+ build_stack {|b| b.request :orange }
130
+ assert_handlers %w[Orange]
131
+ ensure
132
+ unregister_middleware Faraday::Request, :orange
133
+ end
134
+ end
135
+
136
+ def test_missing_dependencies
137
+ build_stack Broken
138
+ err = assert_raises RuntimeError do
139
+ @conn.get('/')
140
+ end
141
+ assert_match "missing dependency for MiddlewareStackTest::Broken: ", err.message
142
+ assert_match "zomg/i_dont/exist", err.message
143
+ end
144
+
97
145
  private
98
146
 
99
147
  # make a stack with test adapter that reflects the order of middleware
100
148
  def build_stack(*handlers)
101
149
  @builder.build do |b|
102
150
  handlers.each { |handler| b.use(*handler) }
151
+ yield b if block_given?
103
152
 
104
153
  b.adapter :test do |stub|
105
154
  stub.get '/' do |env|
@@ -115,4 +164,9 @@ class MiddlewareStackTest < Faraday::TestCase
115
164
  echoed_list.shift if echoed_list.first == ''
116
165
  assert_equal list, echoed_list
117
166
  end
167
+
168
+ def unregister_middleware(component, key)
169
+ # TODO: unregister API?
170
+ component.instance_variable_get('@registered_middleware').delete(key)
171
+ end
118
172
  end
@@ -1,6 +1,5 @@
1
1
  # encoding: utf-8
2
2
  require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
3
- require 'rack/utils'
4
3
 
5
4
  Faraday::CompositeReadIO.class_eval { attr_reader :ios }
6
5
 
@@ -9,7 +8,6 @@ class RequestMiddlewareTest < Faraday::TestCase
9
8
  @conn = Faraday.new do |b|
10
9
  b.request :multipart
11
10
  b.request :url_encoded
12
- b.request :json
13
11
  b.adapter :test do |stub|
14
12
  stub.post('/echo') do |env|
15
13
  posted_as = env[:request_headers]['Content-Type']
@@ -31,49 +29,6 @@ class RequestMiddlewareTest < Faraday::TestCase
31
29
  assert_equal({ :some => 'data' }, response.body)
32
30
  end
33
31
 
34
- def test_json_encodes_hash
35
- response = @conn.post('/echo', { :fruit => %w[apples oranges] }, 'content-type' => 'application/json')
36
- assert_equal 'application/json', response.headers['Content-Type']
37
- assert_equal '{"fruit":["apples","oranges"]}', response.body
38
- end
39
-
40
- def test_json_skips_encoding_for_strings
41
- response = @conn.post('/echo', '{"a":"b"}', 'content-type' => 'application/json')
42
- assert_equal 'application/json', response.headers['Content-Type']
43
- assert_equal '{"a":"b"}', response.body
44
- end
45
-
46
- def test_json_fails_with_useful_message_when_no_json_adapter_available
47
- without_json_adapter do
48
- expected_msg = "No JSON adapter available. Install either activesupport or yajl-ruby."
49
- # assert_raise doesn't work to check the message (at least on 1.8.7)
50
- begin
51
- @conn.post('/echo', { :fruit => %w[apples oranges] }, 'content-type' => 'application/json')
52
- fail "Exception not raised"
53
- rescue Faraday::Error::MissingDependency => e
54
- assert_equal expected_msg, e.message
55
- end
56
- end
57
- end
58
-
59
- def test_url_encoded_does_not_fail_when_no_json_adapter_available
60
- without_json_adapter do
61
- assert_nothing_raised {
62
- @conn.post('/echo', { :fruit => %w[apples oranges] })
63
- }
64
- end
65
- end
66
-
67
- def without_json_adapter
68
- original_adapter = Faraday::Request::JSON.adapter
69
- Faraday::Request::JSON.adapter = nil
70
- begin
71
- yield
72
- ensure
73
- Faraday::Request::JSON.adapter = original_adapter
74
- end
75
- end
76
-
77
32
  def test_url_encoded_no_header
78
33
  response = @conn.post('/echo', { :fruit => %w[apples oranges] })
79
34
  assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
@@ -90,13 +45,13 @@ class RequestMiddlewareTest < Faraday::TestCase
90
45
  response = @conn.post('/echo', { :user => {:name => 'Mislav', :web => 'mislav.net'} })
91
46
  assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
92
47
  expected = { 'user' => {'name' => 'Mislav', 'web' => 'mislav.net'} }
93
- assert_equal expected, Rack::Utils.parse_nested_query(response.body)
48
+ assert_equal expected, Faraday::Utils.parse_nested_query(response.body)
94
49
  end
95
50
 
96
51
  def test_url_encoded_unicode
97
52
  err = capture_warnings {
98
53
  response = @conn.post('/echo', {:str => "eé cç aã aâ"})
99
- assert_equal "str=e%C3%A9%20c%C3%A7%20a%C3%A3%20a%C3%A2", response.body
54
+ assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
100
55
  }
101
56
  assert err.empty?
102
57
  end
@@ -51,8 +51,8 @@ class ResponseNoBodyMiddleWareTest < Faraday::TestCase
51
51
  @conn = Faraday.new do |b|
52
52
  b.response :raise_error
53
53
  b.adapter :test do |stub|
54
- stub.get('not modified') { [304, nil, nil] }
55
- stub.get('no content') { [204, nil, nil] }
54
+ stub.get('not_modified') { [304, nil, nil] }
55
+ stub.get('no_content') { [204, nil, nil] }
56
56
  end
57
57
  end
58
58
  @conn.builder.insert(0, NotCalled)
@@ -65,10 +65,10 @@ class ResponseNoBodyMiddleWareTest < Faraday::TestCase
65
65
  end
66
66
 
67
67
  def test_204
68
- assert_equal nil, @conn.get('no content').body
68
+ assert_equal nil, @conn.get('no_content').body
69
69
  end
70
70
 
71
71
  def test_304
72
- assert_equal nil, @conn.get('not modified').body
72
+ assert_equal nil, @conn.get('not_modified').body
73
73
  end
74
74
  end
metadata CHANGED
@@ -1,41 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.6
5
- prerelease:
4
+ version: 0.8.0.rc2
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rick Olson
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-21 00:00:00.000000000 Z
12
+ date: 2012-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: addressable
16
- requirement: &70207313872560 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '2.2'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *70207313872560
25
14
  - !ruby/object:Gem::Dependency
26
15
  name: multipart-post
27
- requirement: &70207313884640 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ~>
31
- - !ruby/object:Gem::Version
32
- version: '1.1'
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: *70207313884640
36
- - !ruby/object:Gem::Dependency
37
- name: rack
38
- requirement: &70207313889320 !ruby/object:Gem::Requirement
16
+ requirement: &70137915346940 !ruby/object:Gem::Requirement
39
17
  none: false
40
18
  requirements:
41
19
  - - ~>
@@ -43,10 +21,10 @@ dependencies:
43
21
  version: '1.1'
44
22
  type: :runtime
45
23
  prerelease: false
46
- version_requirements: *70207313889320
24
+ version_requirements: *70137915346940
47
25
  - !ruby/object:Gem::Dependency
48
26
  name: rake
49
- requirement: &70207313910400 !ruby/object:Gem::Requirement
27
+ requirement: &70137915345760 !ruby/object:Gem::Requirement
50
28
  none: false
51
29
  requirements:
52
30
  - - ! '>='
@@ -54,10 +32,10 @@ dependencies:
54
32
  version: '0'
55
33
  type: :development
56
34
  prerelease: false
57
- version_requirements: *70207313910400
35
+ version_requirements: *70137915345760
58
36
  - !ruby/object:Gem::Dependency
59
37
  name: test-unit
60
- requirement: &70207313926400 !ruby/object:Gem::Requirement
38
+ requirement: &70137915343760 !ruby/object:Gem::Requirement
61
39
  none: false
62
40
  requirements:
63
41
  - - ! '>='
@@ -65,10 +43,10 @@ dependencies:
65
43
  version: '0'
66
44
  type: :development
67
45
  prerelease: false
68
- version_requirements: *70207313926400
46
+ version_requirements: *70137915343760
69
47
  - !ruby/object:Gem::Dependency
70
48
  name: webmock
71
- requirement: &70207313936720 !ruby/object:Gem::Requirement
49
+ requirement: &70137915292660 !ruby/object:Gem::Requirement
72
50
  none: false
73
51
  requirements:
74
52
  - - ! '>='
@@ -76,8 +54,8 @@ dependencies:
76
54
  version: '0'
77
55
  type: :development
78
56
  prerelease: false
79
- version_requirements: *70207313936720
80
- description: HTTP/REST API client library.
57
+ version_requirements: *70137915292660
58
+ description:
81
59
  email: technoweenie@gmail.com
82
60
  executables: []
83
61
  extensions: []
@@ -92,7 +70,9 @@ files:
92
70
  - lib/faraday.rb
93
71
  - lib/faraday/adapter.rb
94
72
  - lib/faraday/adapter/action_dispatch.rb
73
+ - lib/faraday/adapter/em_http.rb
95
74
  - lib/faraday/adapter/em_synchrony.rb
75
+ - lib/faraday/adapter/em_synchrony/parallel_manager.rb
96
76
  - lib/faraday/adapter/excon.rb
97
77
  - lib/faraday/adapter/net_http.rb
98
78
  - lib/faraday/adapter/patron.rb
@@ -103,8 +83,10 @@ files:
103
83
  - lib/faraday/error.rb
104
84
  - lib/faraday/middleware.rb
105
85
  - lib/faraday/request.rb
106
- - lib/faraday/request/json.rb
86
+ - lib/faraday/request/basic_authentication.rb
107
87
  - lib/faraday/request/multipart.rb
88
+ - lib/faraday/request/retry.rb
89
+ - lib/faraday/request/token_authentication.rb
108
90
  - lib/faraday/request/url_encoded.rb
109
91
  - lib/faraday/response.rb
110
92
  - lib/faraday/response/logger.rb
@@ -116,14 +98,16 @@ files:
116
98
  - test/adapters/net_http_test.rb
117
99
  - test/adapters/test_middleware_test.rb
118
100
  - test/adapters/typhoeus_test.rb
101
+ - test/authentication_middleware_test.rb
119
102
  - test/connection_test.rb
120
103
  - test/env_test.rb
121
104
  - test/helper.rb
122
105
  - test/live_server.rb
106
+ - test/middleware/retry_test.rb
123
107
  - test/middleware_stack_test.rb
124
108
  - test/request_middleware_test.rb
125
109
  - test/response_middleware_test.rb
126
- homepage: http://github.com/technoweenie/faraday
110
+ homepage: https://github.com/technoweenie/faraday
127
111
  licenses: []
128
112
  post_install_message:
129
113
  rdoc_options: []
@@ -138,11 +122,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
122
  required_rubygems_version: !ruby/object:Gem::Requirement
139
123
  none: false
140
124
  requirements:
141
- - - ! '>='
125
+ - - ! '>'
142
126
  - !ruby/object:Gem::Version
143
- version: 1.3.5
127
+ version: 1.3.1
144
128
  requirements: []
145
- rubyforge_project: faraday
129
+ rubyforge_project:
146
130
  rubygems_version: 1.8.12
147
131
  signing_key:
148
132
  specification_version: 2
@@ -153,10 +137,12 @@ test_files:
153
137
  - test/adapters/net_http_test.rb
154
138
  - test/adapters/test_middleware_test.rb
155
139
  - test/adapters/typhoeus_test.rb
140
+ - test/authentication_middleware_test.rb
156
141
  - test/connection_test.rb
157
142
  - test/env_test.rb
158
143
  - test/helper.rb
159
144
  - test/live_server.rb
145
+ - test/middleware/retry_test.rb
160
146
  - test/middleware_stack_test.rb
161
147
  - test/request_middleware_test.rb
162
148
  - test/response_middleware_test.rb
@@ -1,35 +0,0 @@
1
- module Faraday
2
- class Request::JSON < Request::UrlEncoded
3
- self.mime_type = 'application/json'.freeze
4
-
5
- class << self
6
- attr_writer :adapter
7
-
8
- def adapter
9
- @adapter or raise Error::MissingDependency, "No JSON adapter available. Install either activesupport or yajl-ruby."
10
- end
11
- end
12
-
13
- # loads the JSON encoder either from yajl-ruby or activesupport
14
- dependency do
15
- begin
16
- require 'yajl'
17
- self.adapter = Yajl::Encoder
18
- rescue LoadError, NameError
19
- require 'active_support/core_ext/module/attribute_accessors' # AS 2.3.11
20
- require 'active_support/core_ext/kernel/reporting' # AS 2.3.11
21
- require 'active_support/json/encoding'
22
- require 'active_support/ordered_hash' # AS 3.0.4
23
- self.adapter = ActiveSupport::JSON
24
- end
25
- end
26
-
27
- def call(env)
28
- match_content_type(env) do |data|
29
- # encode with the first successfully loaded adapter
30
- env[:body] = self.class.adapter.encode data
31
- end
32
- @app.call env
33
- end
34
- end
35
- end