faraday 0.9.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +360 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +28 -195
  5. data/Rakefile +4 -68
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +158 -58
  9. data/lib/faraday/adapter/typhoeus.rb +7 -115
  10. data/lib/faraday/adapter.rb +79 -20
  11. data/lib/faraday/adapter_registry.rb +30 -0
  12. data/lib/faraday/autoload.rb +39 -36
  13. data/lib/faraday/connection.rb +378 -168
  14. data/lib/faraday/dependency_loader.rb +37 -0
  15. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  16. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  17. data/lib/faraday/error.rb +128 -29
  18. data/lib/faraday/file_part.rb +128 -0
  19. data/lib/faraday/logging/formatter.rb +105 -0
  20. data/lib/faraday/methods.rb +6 -0
  21. data/lib/faraday/middleware.rb +19 -25
  22. data/lib/faraday/middleware_registry.rb +129 -0
  23. data/lib/faraday/options/connection_options.rb +22 -0
  24. data/lib/faraday/options/env.rb +181 -0
  25. data/lib/faraday/options/proxy_options.rb +32 -0
  26. data/lib/faraday/options/request_options.rb +22 -0
  27. data/lib/faraday/options/ssl_options.rb +59 -0
  28. data/lib/faraday/options.rb +61 -193
  29. data/lib/faraday/param_part.rb +53 -0
  30. data/lib/faraday/parameters.rb +4 -180
  31. data/lib/faraday/rack_builder.rb +84 -47
  32. data/lib/faraday/request/authorization.rb +51 -31
  33. data/lib/faraday/request/basic_authentication.rb +14 -7
  34. data/lib/faraday/request/instrumentation.rb +45 -27
  35. data/lib/faraday/request/multipart.rb +88 -45
  36. data/lib/faraday/request/retry.rb +212 -121
  37. data/lib/faraday/request/token_authentication.rb +15 -10
  38. data/lib/faraday/request/url_encoded.rb +43 -23
  39. data/lib/faraday/request.rb +96 -32
  40. data/lib/faraday/response/logger.rb +22 -48
  41. data/lib/faraday/response/raise_error.rb +49 -14
  42. data/lib/faraday/response.rb +33 -25
  43. data/lib/faraday/utils/headers.rb +139 -0
  44. data/lib/faraday/utils/params_hash.rb +61 -0
  45. data/lib/faraday/utils.rb +38 -218
  46. data/lib/faraday/version.rb +5 -0
  47. data/lib/faraday.rb +130 -213
  48. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  49. data/spec/faraday/adapter/em_http_spec.rb +49 -0
  50. data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
  51. data/spec/faraday/adapter/excon_spec.rb +49 -0
  52. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  53. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  54. data/spec/faraday/adapter/patron_spec.rb +18 -0
  55. data/spec/faraday/adapter/rack_spec.rb +8 -0
  56. data/spec/faraday/adapter/test_spec.rb +377 -0
  57. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  58. data/spec/faraday/adapter_registry_spec.rb +28 -0
  59. data/spec/faraday/adapter_spec.rb +55 -0
  60. data/spec/faraday/composite_read_io_spec.rb +80 -0
  61. data/spec/faraday/connection_spec.rb +736 -0
  62. data/spec/faraday/error_spec.rb +60 -0
  63. data/spec/faraday/middleware_spec.rb +52 -0
  64. data/spec/faraday/options/env_spec.rb +70 -0
  65. data/spec/faraday/options/options_spec.rb +297 -0
  66. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  67. data/spec/faraday/options/request_options_spec.rb +19 -0
  68. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  69. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  70. data/spec/faraday/rack_builder_spec.rb +345 -0
  71. data/spec/faraday/request/authorization_spec.rb +96 -0
  72. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  73. data/spec/faraday/request/multipart_spec.rb +302 -0
  74. data/spec/faraday/request/retry_spec.rb +242 -0
  75. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  76. data/spec/faraday/request_spec.rb +120 -0
  77. data/spec/faraday/response/logger_spec.rb +220 -0
  78. data/spec/faraday/response/middleware_spec.rb +68 -0
  79. data/spec/faraday/response/raise_error_spec.rb +169 -0
  80. data/spec/faraday/response_spec.rb +75 -0
  81. data/spec/faraday/utils/headers_spec.rb +82 -0
  82. data/spec/faraday/utils_spec.rb +56 -0
  83. data/spec/faraday_spec.rb +37 -0
  84. data/spec/spec_helper.rb +132 -0
  85. data/spec/support/disabling_stub.rb +14 -0
  86. data/spec/support/fake_safe_buffer.rb +15 -0
  87. data/spec/support/helper_methods.rb +133 -0
  88. data/spec/support/shared_examples/adapter.rb +105 -0
  89. data/spec/support/shared_examples/params_encoder.rb +18 -0
  90. data/spec/support/shared_examples/request_method.rb +262 -0
  91. data/spec/support/streaming_response_checker.rb +35 -0
  92. data/spec/support/webmock_rack_app.rb +68 -0
  93. metadata +199 -98
  94. data/.document +0 -6
  95. data/CONTRIBUTING.md +0 -36
  96. data/Gemfile +0 -25
  97. data/faraday.gemspec +0 -34
  98. data/lib/faraday/adapter/em_http.rb +0 -237
  99. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  100. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  101. data/lib/faraday/adapter/em_synchrony.rb +0 -92
  102. data/lib/faraday/adapter/excon.rb +0 -80
  103. data/lib/faraday/adapter/httpclient.rb +0 -106
  104. data/lib/faraday/adapter/net_http.rb +0 -130
  105. data/lib/faraday/adapter/net_http_persistent.rb +0 -48
  106. data/lib/faraday/adapter/patron.rb +0 -72
  107. data/lib/faraday/adapter/rack.rb +0 -58
  108. data/lib/faraday/upload_io.rb +0 -67
  109. data/script/cached-bundle +0 -46
  110. data/script/console +0 -7
  111. data/script/generate_certs +0 -42
  112. data/script/package +0 -7
  113. data/script/proxy-server +0 -42
  114. data/script/release +0 -17
  115. data/script/s3-put +0 -71
  116. data/script/server +0 -36
  117. data/script/test +0 -172
  118. data/test/adapters/default_test.rb +0 -14
  119. data/test/adapters/em_http_test.rb +0 -20
  120. data/test/adapters/em_synchrony_test.rb +0 -20
  121. data/test/adapters/excon_test.rb +0 -20
  122. data/test/adapters/httpclient_test.rb +0 -21
  123. data/test/adapters/integration.rb +0 -254
  124. data/test/adapters/logger_test.rb +0 -82
  125. data/test/adapters/net_http_persistent_test.rb +0 -20
  126. data/test/adapters/net_http_test.rb +0 -14
  127. data/test/adapters/patron_test.rb +0 -20
  128. data/test/adapters/rack_test.rb +0 -31
  129. data/test/adapters/test_middleware_test.rb +0 -114
  130. data/test/adapters/typhoeus_test.rb +0 -28
  131. data/test/authentication_middleware_test.rb +0 -65
  132. data/test/composite_read_io_test.rb +0 -111
  133. data/test/connection_test.rb +0 -522
  134. data/test/env_test.rb +0 -218
  135. data/test/helper.rb +0 -81
  136. data/test/live_server.rb +0 -67
  137. data/test/middleware/instrumentation_test.rb +0 -88
  138. data/test/middleware/retry_test.rb +0 -177
  139. data/test/middleware_stack_test.rb +0 -173
  140. data/test/multibyte.txt +0 -1
  141. data/test/options_test.rb +0 -252
  142. data/test/parameters_test.rb +0 -64
  143. data/test/request_middleware_test.rb +0 -142
  144. data/test/response_middleware_test.rb +0 -72
  145. data/test/strawberry.rb +0 -2
  146. data/test/utils_test.rb +0 -58
@@ -1,173 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class MiddlewareStackTest < Faraday::TestCase
4
- # mock handler classes
5
- class Handler < Struct.new(:app)
6
- def call(env)
7
- (env[:request_headers]['X-Middleware'] ||= '') << ":#{self.class.name.split('::').last}"
8
- app.call(env)
9
- end
10
- end
11
- class Apple < Handler; end
12
- class Orange < Handler; end
13
- class Banana < Handler; end
14
-
15
- class Broken < Faraday::Middleware
16
- dependency 'zomg/i_dont/exist'
17
- end
18
-
19
- def setup
20
- @conn = Faraday::Connection.new
21
- @builder = @conn.builder
22
- end
23
-
24
- def test_sets_default_adapter_if_none_set
25
- default_middleware = Faraday::Request.lookup_middleware :url_encoded
26
- default_adapter_klass = Faraday::Adapter.lookup_middleware Faraday.default_adapter
27
- assert @builder[0] == default_middleware
28
- assert @builder[1] == default_adapter_klass
29
- end
30
-
31
- def test_allows_rebuilding
32
- build_stack Apple
33
- build_stack Orange
34
- assert_handlers %w[Orange]
35
- end
36
-
37
- def test_allows_extending
38
- build_stack Apple
39
- @conn.use Orange
40
- assert_handlers %w[Apple Orange]
41
- end
42
-
43
- def test_builder_is_passed_to_new_faraday_connection
44
- new_conn = Faraday::Connection.new :builder => @builder
45
- assert_equal @builder, new_conn.builder
46
- end
47
-
48
- def test_insert_before
49
- build_stack Apple, Orange
50
- @builder.insert_before Apple, Banana
51
- assert_handlers %w[Banana Apple Orange]
52
- end
53
-
54
- def test_insert_after
55
- build_stack Apple, Orange
56
- @builder.insert_after Apple, Banana
57
- assert_handlers %w[Apple Banana Orange]
58
- end
59
-
60
- def test_swap_handlers
61
- build_stack Apple, Orange
62
- @builder.swap Apple, Banana
63
- assert_handlers %w[Banana Orange]
64
- end
65
-
66
- def test_delete_handler
67
- build_stack Apple, Orange
68
- @builder.delete Apple
69
- assert_handlers %w[Orange]
70
- end
71
-
72
- def test_stack_is_locked_after_making_requests
73
- build_stack Apple
74
- assert !@builder.locked?
75
- @conn.get('/')
76
- assert @builder.locked?
77
-
78
- assert_raises Faraday::RackBuilder::StackLocked do
79
- @conn.use Orange
80
- end
81
- end
82
-
83
- def test_duped_stack_is_unlocked
84
- build_stack Apple
85
- assert !@builder.locked?
86
- @builder.lock!
87
- assert @builder.locked?
88
-
89
- duped_connection = @conn.dup
90
- assert_equal @builder, duped_connection.builder
91
- assert !duped_connection.builder.locked?
92
- end
93
-
94
- def test_handler_comparison
95
- build_stack Apple
96
- assert_equal @builder.handlers.first, Apple
97
- assert_equal @builder.handlers[0,1], [Apple]
98
- assert_equal @builder.handlers.first, Faraday::RackBuilder::Handler.new(Apple)
99
- end
100
-
101
- def test_unregistered_symbol
102
- err = assert_raises(Faraday::Error){ 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::Middleware.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::Middleware.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_array
127
- Faraday::Middleware.register_middleware File.expand_path("..", __FILE__),
128
- :strawberry => [lambda { Strawberry }, 'strawberry']
129
- begin
130
- build_stack :strawberry
131
- assert_handlers %w[Strawberry]
132
- ensure
133
- unregister_middleware Faraday::Middleware, :strawberry
134
- end
135
- end
136
-
137
- def test_missing_dependencies
138
- build_stack Broken
139
- err = assert_raises RuntimeError do
140
- @conn.get('/')
141
- end
142
- assert_match "missing dependency for MiddlewareStackTest::Broken: ", err.message
143
- assert_match "zomg/i_dont/exist", err.message
144
- end
145
-
146
- private
147
-
148
- # make a stack with test adapter that reflects the order of middleware
149
- def build_stack(*handlers)
150
- @builder.build do |b|
151
- handlers.each { |handler| b.use(*handler) }
152
- yield(b) if block_given?
153
-
154
- b.adapter :test do |stub|
155
- stub.get '/' do |env|
156
- # echo the "X-Middleware" request header in the body
157
- [200, {}, env[:request_headers]['X-Middleware'].to_s]
158
- end
159
- end
160
- end
161
- end
162
-
163
- def assert_handlers(list)
164
- echoed_list = @conn.get('/').body.to_s.split(':')
165
- echoed_list.shift if echoed_list.first == ''
166
- assert_equal list, echoed_list
167
- end
168
-
169
- def unregister_middleware(component, key)
170
- # TODO: unregister API?
171
- component.instance_variable_get('@registered_middleware').delete(key)
172
- end
173
- end
data/test/multibyte.txt DELETED
@@ -1 +0,0 @@
1
- ファイル
data/test/options_test.rb DELETED
@@ -1,252 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class OptionsTest < Faraday::TestCase
4
- class SubOptions < Faraday::Options.new(:sub); end
5
- class ParentOptions < Faraday::Options.new(:a, :b, :c)
6
- options :c => SubOptions
7
- end
8
-
9
- def test_clear
10
- options = SubOptions.new(1)
11
- assert !options.empty?
12
- assert options.clear
13
- assert options.empty?
14
- end
15
-
16
- def test_empty
17
- options = SubOptions.new
18
- assert options.empty?
19
- options.sub = 1
20
- assert !options.empty?
21
- options.delete(:sub)
22
- assert options.empty?
23
- end
24
-
25
- def test_each_key
26
- options = ParentOptions.new(1, 2, 3)
27
- enum = options.each_key
28
- assert_equal enum.next.to_sym, :a
29
- assert_equal enum.next.to_sym, :b
30
- assert_equal enum.next.to_sym, :c
31
- end
32
-
33
- def test_key?
34
- options = SubOptions.new
35
- assert !options.key?(:sub)
36
- options.sub = 1
37
- if RUBY_VERSION >= '1.9'
38
- assert options.key?(:sub)
39
- else
40
- assert options.key?("sub")
41
- end
42
- end
43
-
44
- def test_each_value
45
- options = ParentOptions.new(1, 2, 3)
46
- enum = options.each_value
47
- assert_equal enum.next, 1
48
- assert_equal enum.next, 2
49
- assert_equal enum.next, 3
50
- end
51
-
52
- def test_value?
53
- options = SubOptions.new
54
- assert !options.value?(1)
55
- options.sub = 1
56
- assert options.value?(1)
57
- end
58
-
59
- def test_request_proxy_setter
60
- options = Faraday::RequestOptions.new
61
- assert_nil options.proxy
62
-
63
- assert_raises NoMethodError do
64
- options[:proxy] = {:booya => 1}
65
- end
66
-
67
- options[:proxy] = {:user => 'user'}
68
- assert_kind_of Faraday::ProxyOptions, options.proxy
69
- assert_equal 'user', options.proxy.user
70
-
71
- options.proxy = nil
72
- assert_nil options.proxy
73
- end
74
-
75
- def test_proxy_options_from_string
76
- options = Faraday::ProxyOptions.from 'http://user:pass@example.org'
77
- assert_equal 'user', options.user
78
- assert_equal 'pass', options.password
79
- assert_kind_of URI, options.uri
80
- assert_equal '', options.path
81
- assert_equal 80, options.port
82
- assert_equal 'example.org', options.host
83
- assert_equal 'http', options.scheme
84
- end
85
-
86
- def test_proxy_options_hash_access
87
- proxy = Faraday::ProxyOptions.from 'http://a%40b:pw%20d@example.org'
88
- assert_equal 'a@b', proxy[:user]
89
- assert_equal 'a@b', proxy.user
90
- assert_equal 'pw d', proxy[:password]
91
- assert_equal 'pw d', proxy.password
92
- end
93
-
94
- def test_proxy_options_no_auth
95
- proxy = Faraday::ProxyOptions.from 'http://example.org'
96
- assert_nil proxy.user
97
- assert_nil proxy.password
98
- end
99
-
100
- def test_from_options
101
- options = ParentOptions.new(1)
102
-
103
- value = ParentOptions.from(options)
104
- assert_equal 1, value.a
105
- assert_nil value.b
106
- end
107
-
108
- def test_from_options_with_sub_object
109
- sub = SubOptions.new(1)
110
- options = ParentOptions.from :a => 1, :c => sub
111
- assert_kind_of ParentOptions, options
112
- assert_equal 1, options.a
113
- assert_nil options.b
114
- assert_kind_of SubOptions, options.c
115
- assert_equal 1, options.c.sub
116
- end
117
-
118
- def test_from_hash
119
- options = ParentOptions.from :a => 1
120
- assert_kind_of ParentOptions, options
121
- assert_equal 1, options.a
122
- assert_nil options.b
123
- end
124
-
125
- def test_from_hash_with_sub_object
126
- options = ParentOptions.from :a => 1, :c => {:sub => 1}
127
- assert_kind_of ParentOptions, options
128
- assert_equal 1, options.a
129
- assert_nil options.b
130
- assert_kind_of SubOptions, options.c
131
- assert_equal 1, options.c.sub
132
- end
133
-
134
- def test_inheritance
135
- subclass = Class.new(ParentOptions)
136
- options = subclass.from(:c => {:sub => 'hello'})
137
- assert_kind_of SubOptions, options.c
138
- assert_equal 'hello', options.c.sub
139
- end
140
-
141
- def test_from_deep_hash
142
- hash = {:b => 1}
143
- options = ParentOptions.from :a => hash
144
- assert_equal 1, options.a[:b]
145
-
146
- hash[:b] = 2
147
- assert_equal 1, options.a[:b]
148
-
149
- options.a[:b] = 3
150
- assert_equal 2, hash[:b]
151
- assert_equal 3, options.a[:b]
152
- end
153
-
154
- def test_from_nil
155
- options = ParentOptions.from(nil)
156
- assert_kind_of ParentOptions, options
157
- assert_nil options.a
158
- assert_nil options.b
159
- end
160
-
161
- def test_invalid_key
162
- assert_raises NoMethodError do
163
- ParentOptions.from :invalid => 1
164
- end
165
- end
166
-
167
- def test_update
168
- options = ParentOptions.new(1)
169
- assert_equal 1, options.a
170
- assert_nil options.b
171
-
172
- updated = options.update :a => 2, :b => 3
173
- assert_equal 2, options.a
174
- assert_equal 3, options.b
175
- assert_equal options, updated
176
- end
177
-
178
- def test_delete
179
- options = ParentOptions.new(1)
180
- assert_equal 1, options.a
181
- assert_equal 1, options.delete(:a)
182
- assert_nil options.a
183
- end
184
-
185
- def test_merge
186
- options = ParentOptions.new(1)
187
- assert_equal 1, options.a
188
- assert_nil options.b
189
-
190
- dup = options.merge :a => 2, :b => 3
191
- assert_equal 2, dup.a
192
- assert_equal 3, dup.b
193
- assert_equal 1, options.a
194
- assert_nil options.b
195
- end
196
-
197
- def test_env_access_member
198
- e = Faraday::Env.new
199
- assert_nil e.method
200
- e.method = :get
201
- assert_equal :get, e.method
202
- end
203
-
204
- def test_env_access_symbol_non_member
205
- e = Faraday::Env.new
206
- assert_nil e[:custom]
207
- e[:custom] = :boom
208
- assert_equal :boom, e[:custom]
209
- end
210
-
211
- def test_env_access_string_non_member
212
- e = Faraday::Env.new
213
- assert_nil e["custom"]
214
- e["custom"] = :boom
215
- assert_equal :boom, e["custom"]
216
- end
217
-
218
- def test_env_fetch_ignores_false
219
- ssl = Faraday::SSLOptions.new
220
- ssl.verify = false
221
- assert !ssl.fetch(:verify, true)
222
- end
223
-
224
- def test_fetch_grabs_value
225
- opt = Faraday::SSLOptions.new
226
- opt.verify = 1
227
- assert_equal 1, opt.fetch(:verify, false) { |k| :blah }
228
- end
229
-
230
- def test_fetch_uses_falsey_default
231
- opt = Faraday::SSLOptions.new
232
- assert_equal false, opt.fetch(:verify, false) { |k| :blah }
233
- end
234
-
235
- def test_fetch_accepts_block
236
- opt = Faraday::SSLOptions.new
237
- assert_equal "yo :verify", opt.fetch(:verify) { |k| "yo #{k.inspect}"}
238
- end
239
-
240
- def test_fetch_needs_a_default_if_key_is_missing
241
- opt = Faraday::SSLOptions.new
242
- assert_raises Faraday::Options.fetch_error_class do
243
- opt.fetch :verify
244
- end
245
- end
246
-
247
- def test_fetch_works_with_key
248
- opt = Faraday::SSLOptions.new
249
- opt.verify = 1
250
- assert_equal 1, opt.fetch(:verify)
251
- end
252
- end
@@ -1,64 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class TestParameters < Faraday::TestCase
4
- # emulates ActiveSupport::SafeBuffer#gsub
5
- FakeSafeBuffer = Struct.new(:string) do
6
- def to_s() self end
7
- def gsub(regex)
8
- string.gsub(regex) {
9
- match, = $&, '' =~ /a/
10
- yield(match)
11
- }
12
- end
13
- end
14
-
15
- def test_escaping_safe_buffer_nested
16
- monies = FakeSafeBuffer.new("$32,000.00")
17
- assert_equal "a=%2432%2C000.00", Faraday::NestedParamsEncoder.encode("a" => monies)
18
- end
19
-
20
- def test_escaping_safe_buffer_flat
21
- monies = FakeSafeBuffer.new("$32,000.00")
22
- assert_equal "a=%2432%2C000.00", Faraday::FlatParamsEncoder.encode("a" => monies)
23
- end
24
-
25
- def test_raises_typeerror_nested
26
- error = assert_raises TypeError do
27
- Faraday::NestedParamsEncoder.encode("")
28
- end
29
- assert_equal "Can't convert String into Hash.", error.message
30
- end
31
-
32
- def test_raises_typeerror_flat
33
- error = assert_raises TypeError do
34
- Faraday::FlatParamsEncoder.encode("")
35
- end
36
- assert_equal "Can't convert String into Hash.", error.message
37
- end
38
-
39
- def test_decode_array_nested
40
- query = "a[1]=one&a[2]=two&a[3]=three"
41
- expected = {"a" => ["one", "two", "three"]}
42
- assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
43
- end
44
-
45
- def test_decode_array_flat
46
- query = "a=one&a=two&a=three"
47
- expected = {"a" => ["one", "two", "three"]}
48
- assert_equal expected, Faraday::FlatParamsEncoder.decode(query)
49
- end
50
-
51
- def test_nested_decode_hash
52
- query = "a[b1]=one&a[b2]=two&a[b][c]=foo"
53
- expected = {"a" => {"b1" => "one", "b2" => "two", "b" => {"c" => "foo"}}}
54
- assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
55
- end
56
-
57
- def test_encode_nil_nested
58
- assert_equal "a=", Faraday::NestedParamsEncoder.encode("a" => nil)
59
- end
60
-
61
- def test_encode_nil_flat
62
- assert_equal "a", Faraday::FlatParamsEncoder.encode("a" => nil)
63
- end
64
- end
@@ -1,142 +0,0 @@
1
- # encoding: utf-8
2
- require File.expand_path('../helper', __FILE__)
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 with_utf8
21
- if defined?(RUBY_VERSION) && RUBY_VERSION.match(/1.8.\d/)
22
- begin
23
- previous_kcode = $KCODE
24
- $KCODE = "UTF8"
25
- yield
26
- ensure
27
- $KCODE = previous_kcode
28
- end
29
- else
30
- yield
31
- end
32
- end
33
-
34
- def test_does_nothing_without_payload
35
- response = @conn.post('/echo')
36
- assert_nil response.headers['Content-Type']
37
- assert response.body.empty?
38
- end
39
-
40
- def test_ignores_custom_content_type
41
- response = @conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo')
42
- assert_equal 'application/x-foo', response.headers['Content-Type']
43
- assert_equal({ :some => 'data' }, response.body)
44
- end
45
-
46
- def test_url_encoded_no_header
47
- response = @conn.post('/echo', { :fruit => %w[apples oranges] })
48
- assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
49
- assert_equal 'fruit%5B%5D=apples&fruit%5B%5D=oranges', response.body
50
- end
51
-
52
- def test_url_encoded_with_header
53
- response = @conn.post('/echo', {'a'=>123}, 'content-type' => 'application/x-www-form-urlencoded')
54
- assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
55
- assert_equal 'a=123', response.body
56
- end
57
-
58
- def test_url_encoded_nested
59
- response = @conn.post('/echo', { :user => {:name => 'Mislav', :web => 'mislav.net'} })
60
- assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
61
- expected = { 'user' => {'name' => 'Mislav', 'web' => 'mislav.net'} }
62
- assert_equal expected, Faraday::Utils.parse_nested_query(response.body)
63
- end
64
-
65
- def test_url_encoded_non_nested
66
- response = @conn.post('/echo', { :dimensions => ['date', 'location']}) do |req|
67
- req.options.params_encoder = Faraday::FlatParamsEncoder
68
- end
69
- assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
70
- expected = { 'dimensions' => ['date', 'location'] }
71
- assert_equal expected, Faraday::Utils.parse_query(response.body)
72
- assert_equal 'dimensions=date&dimensions=location', response.body
73
- end
74
-
75
- def test_url_encoded_unicode
76
- err = capture_warnings {
77
- response = @conn.post('/echo', {:str => "eé cç aã aâ"})
78
- assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
79
- }
80
- assert err.empty?, "stderr did include: #{err}"
81
- end
82
-
83
- def test_url_encoded_unicode_with_kcode_set
84
- with_utf8 do
85
- err = capture_warnings {
86
- response = @conn.post('/echo', {:str => "eé cç aã aâ"})
87
- assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
88
- }
89
- assert err.empty?, "stderr did include: #{err}"
90
- end
91
- end
92
-
93
- def test_url_encoded_nested_keys
94
- response = @conn.post('/echo', {'a'=>{'b'=>{'c'=>['d']}}})
95
- assert_equal "a%5Bb%5D%5Bc%5D%5B%5D=d", response.body
96
- end
97
-
98
- def test_multipart
99
- # assume params are out of order
100
- regexes = [
101
- /name\=\"a\"/,
102
- /name=\"b\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
103
- /name=\"b\[d\]\"/]
104
-
105
- payload = {:a => 1, :b => {:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}}
106
- response = @conn.post('/echo', payload)
107
-
108
- assert_kind_of Faraday::CompositeReadIO, response.body
109
- assert_equal "multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
110
- response.headers['Content-Type']
111
-
112
- response.body.send(:ios).map{|io| io.read}.each do |io|
113
- if re = regexes.detect { |r| io =~ r }
114
- regexes.delete re
115
- end
116
- end
117
- assert_equal [], regexes
118
- end
119
-
120
- def test_multipart_with_arrays
121
- # assume params are out of order
122
- regexes = [
123
- /name\=\"a\"/,
124
- /name=\"b\[\]\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
125
- /name=\"b\[\]\[d\]\"/]
126
-
127
- payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
128
- response = @conn.post('/echo', payload)
129
-
130
- assert_kind_of Faraday::CompositeReadIO, response.body
131
- assert_equal "multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
132
- response.headers['Content-Type']
133
-
134
- response.body.send(:ios).map{|io| io.read}.each do |io|
135
- if re = regexes.detect { |r| io =~ r }
136
- regexes.delete re
137
- end
138
- end
139
- assert_equal [], regexes
140
- end
141
-
142
- end
@@ -1,72 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
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 @conn.get('ok')
23
- end
24
-
25
- def test_raises_not_found
26
- error = assert_raises Faraday::Error::ResourceNotFound do
27
- @conn.get('not-found')
28
- end
29
- assert_equal 'the server responded with status 404', error.message
30
- assert_equal 'because', error.response[:headers]['X-Reason']
31
- end
32
-
33
- def test_raises_error
34
- error = assert_raises Faraday::Error::ClientError do
35
- @conn.get('error')
36
- end
37
- assert_equal 'the server responded with status 500', error.message
38
- assert_equal 'bailout', error.response[:headers]['X-Error']
39
- end
40
-
41
- def test_upcase
42
- @conn.builder.insert(0, ResponseUpcaser)
43
- assert_equal '<BODY></BODY>', @conn.get('ok').body
44
- end
45
- end
46
-
47
- class ResponseNoBodyMiddleWareTest < Faraday::TestCase
48
- def setup
49
- @conn = Faraday.new do |b|
50
- b.response :raise_error
51
- b.adapter :test do |stub|
52
- stub.get('not_modified') { [304, nil, nil] }
53
- stub.get('no_content') { [204, nil, nil] }
54
- end
55
- end
56
- @conn.builder.insert(0, NotCalled)
57
- end
58
-
59
- class NotCalled < Faraday::Response::Middleware
60
- def parse(body)
61
- raise "this should not be called"
62
- end
63
- end
64
-
65
- def test_204
66
- assert_equal nil, @conn.get('no_content').body
67
- end
68
-
69
- def test_304
70
- assert_equal nil, @conn.get('not_modified').body
71
- end
72
- end
data/test/strawberry.rb DELETED
@@ -1,2 +0,0 @@
1
- class MiddlewareStackTest::Strawberry < MiddlewareStackTest::Handler
2
- end