excon 0.62.0 → 0.85.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE.md +1 -1
  3. data/README.md +6 -5
  4. data/data/cacert.pem +939 -1720
  5. data/excon.gemspec +17 -2
  6. data/lib/excon.rb +25 -17
  7. data/lib/excon/connection.rb +206 -139
  8. data/lib/excon/constants.rb +38 -13
  9. data/lib/excon/error.rb +15 -0
  10. data/lib/excon/headers.rb +4 -3
  11. data/lib/excon/instrumentors/logging_instrumentor.rb +4 -15
  12. data/lib/excon/instrumentors/standard_instrumentor.rb +2 -9
  13. data/lib/excon/middlewares/base.rb +6 -0
  14. data/lib/excon/middlewares/capture_cookies.rb +1 -1
  15. data/lib/excon/middlewares/decompress.rb +2 -2
  16. data/lib/excon/middlewares/expects.rb +7 -1
  17. data/lib/excon/middlewares/idempotent.rb +20 -3
  18. data/lib/excon/middlewares/instrumentor.rb +8 -0
  19. data/lib/excon/middlewares/mock.rb +12 -3
  20. data/lib/excon/middlewares/redirect_follower.rb +25 -3
  21. data/lib/excon/middlewares/response_parser.rb +3 -0
  22. data/lib/excon/pretty_printer.rb +1 -8
  23. data/lib/excon/response.rb +12 -9
  24. data/lib/excon/socket.rb +51 -33
  25. data/lib/excon/ssl_socket.rb +33 -13
  26. data/lib/excon/test/plugin/server/exec.rb +2 -2
  27. data/lib/excon/test/server.rb +1 -1
  28. data/lib/excon/unix_socket.rb +1 -0
  29. data/lib/excon/utils.rb +58 -5
  30. data/lib/excon/version.rb +1 -1
  31. metadata +27 -98
  32. data/.document +0 -5
  33. data/.gitignore +0 -13
  34. data/.rspec +0 -3
  35. data/.travis.yml +0 -29
  36. data/Gemfile +0 -19
  37. data/Rakefile +0 -41
  38. data/benchmarks/class_vs_lambda.rb +0 -50
  39. data/benchmarks/concat_vs_insert.rb +0 -21
  40. data/benchmarks/concat_vs_interpolate.rb +0 -22
  41. data/benchmarks/cr_lf.rb +0 -21
  42. data/benchmarks/downcase-eq-eq_vs_casecmp.rb +0 -169
  43. data/benchmarks/excon.rb +0 -69
  44. data/benchmarks/excon_vs.rb +0 -165
  45. data/benchmarks/for_vs_array_each.rb +0 -27
  46. data/benchmarks/for_vs_hash_each.rb +0 -27
  47. data/benchmarks/has_key-vs-lookup.rb +0 -177
  48. data/benchmarks/headers_case_sensitivity.rb +0 -83
  49. data/benchmarks/headers_split_vs_match.rb +0 -34
  50. data/benchmarks/implicit_block-vs-explicit_block.rb +0 -98
  51. data/benchmarks/merging.rb +0 -21
  52. data/benchmarks/single_vs_double_quotes.rb +0 -21
  53. data/benchmarks/string_ranged_index.rb +0 -87
  54. data/benchmarks/strip_newline.rb +0 -115
  55. data/benchmarks/vs_stdlib.rb +0 -82
  56. data/changelog.txt +0 -1083
  57. data/spec/excon/error_spec.rb +0 -139
  58. data/spec/excon/test/server_spec.rb +0 -28
  59. data/spec/excon_spec.rb +0 -7
  60. data/spec/helpers/file_path_helpers.rb +0 -22
  61. data/spec/requests/basic_spec.rb +0 -40
  62. data/spec/requests/eof_requests_spec.rb +0 -36
  63. data/spec/requests/unix_socket_spec.rb +0 -46
  64. data/spec/spec_helper.rb +0 -24
  65. data/spec/support/shared_contexts/test_server_context.rb +0 -83
  66. data/spec/support/shared_examples/shared_example_for_clients.rb +0 -218
  67. data/spec/support/shared_examples/shared_example_for_streaming_clients.rb +0 -20
  68. data/spec/support/shared_examples/shared_example_for_test_servers.rb +0 -16
  69. data/tests/authorization_header_tests.rb +0 -29
  70. data/tests/bad_tests.rb +0 -47
  71. data/tests/basic_tests.rb +0 -351
  72. data/tests/batch_requests.rb +0 -133
  73. data/tests/complete_responses.rb +0 -31
  74. data/tests/data/127.0.0.1.cert.crt +0 -20
  75. data/tests/data/127.0.0.1.cert.key +0 -27
  76. data/tests/data/excon.cert.crt +0 -20
  77. data/tests/data/excon.cert.key +0 -27
  78. data/tests/data/xs +0 -1
  79. data/tests/error_tests.rb +0 -145
  80. data/tests/header_tests.rb +0 -119
  81. data/tests/middlewares/canned_response_tests.rb +0 -34
  82. data/tests/middlewares/capture_cookies_tests.rb +0 -34
  83. data/tests/middlewares/decompress_tests.rb +0 -157
  84. data/tests/middlewares/escape_path_tests.rb +0 -36
  85. data/tests/middlewares/idempotent_tests.rb +0 -206
  86. data/tests/middlewares/instrumentation_tests.rb +0 -315
  87. data/tests/middlewares/mock_tests.rb +0 -304
  88. data/tests/middlewares/redirect_follower_tests.rb +0 -112
  89. data/tests/pipeline_tests.rb +0 -40
  90. data/tests/proxy_tests.rb +0 -306
  91. data/tests/query_string_tests.rb +0 -87
  92. data/tests/rackups/basic.rb +0 -41
  93. data/tests/rackups/basic.ru +0 -3
  94. data/tests/rackups/basic_auth.ru +0 -14
  95. data/tests/rackups/deflater.ru +0 -4
  96. data/tests/rackups/proxy.ru +0 -18
  97. data/tests/rackups/query_string.ru +0 -13
  98. data/tests/rackups/redirecting.ru +0 -23
  99. data/tests/rackups/redirecting_with_cookie.ru +0 -40
  100. data/tests/rackups/request_headers.ru +0 -15
  101. data/tests/rackups/request_methods.ru +0 -21
  102. data/tests/rackups/response_header.ru +0 -18
  103. data/tests/rackups/ssl.ru +0 -16
  104. data/tests/rackups/ssl_mismatched_cn.ru +0 -15
  105. data/tests/rackups/ssl_verify_peer.ru +0 -16
  106. data/tests/rackups/streaming.ru +0 -30
  107. data/tests/rackups/thread_safety.ru +0 -17
  108. data/tests/rackups/timeout.ru +0 -14
  109. data/tests/rackups/webrick_patch.rb +0 -34
  110. data/tests/request_headers_tests.rb +0 -21
  111. data/tests/request_method_tests.rb +0 -47
  112. data/tests/request_tests.rb +0 -59
  113. data/tests/response_tests.rb +0 -197
  114. data/tests/servers/bad.rb +0 -20
  115. data/tests/servers/eof.rb +0 -17
  116. data/tests/servers/error.rb +0 -20
  117. data/tests/servers/good.rb +0 -350
  118. data/tests/test_helper.rb +0 -306
  119. data/tests/thread_safety_tests.rb +0 -39
  120. data/tests/timeout_tests.rb +0 -12
  121. data/tests/utils_tests.rb +0 -81
@@ -1,315 +0,0 @@
1
- require 'active_support/notifications'
2
- require 'securerandom'
3
-
4
- class SimpleInstrumentor
5
- class << self
6
- attr_accessor :events, :blocks
7
-
8
- def instrument(name, params = {}, &block)
9
- @events << name
10
- @blocks << name if block_given?
11
-
12
- yield if block_given?
13
- end
14
-
15
- def reset!
16
- @events = []
17
- @blocks = []
18
- end
19
- end
20
- end
21
-
22
- Shindo.tests('Excon instrumentation') do
23
-
24
- after do
25
- ActiveSupport::Notifications.unsubscribe("excon")
26
- ActiveSupport::Notifications.unsubscribe("excon.request")
27
- ActiveSupport::Notifications.unsubscribe("excon.response")
28
- ActiveSupport::Notifications.unsubscribe("excon.retry")
29
- ActiveSupport::Notifications.unsubscribe("excon.error")
30
- ActiveSupport::Notifications.unsubscribe("gug")
31
- Delorean.back_to_the_present
32
- Excon.stubs.clear
33
- end
34
-
35
- before do
36
- SimpleInstrumentor.reset!
37
- end
38
-
39
- def subscribe(match)
40
- @events = []
41
- ActiveSupport::Notifications.subscribe(match) do |*args|
42
- @events << ActiveSupport::Notifications::Event.new(*args)
43
- end
44
- end
45
-
46
- def make_request(idempotent = false, params = {})
47
- connection = Excon.new(
48
- 'http://127.0.0.1:9292',
49
- :instrumentor => ActiveSupport::Notifications,
50
- :mock => true
51
- )
52
- if idempotent
53
- params[:idempotent] = :true
54
- end
55
- connection.get(params)
56
- end
57
-
58
- REQUEST_DELAY_SECONDS = 30
59
- def stub_success
60
- Excon.stub({:method => :get}) { |params|
61
- Delorean.jump REQUEST_DELAY_SECONDS
62
- {:body => params[:body], :headers => params[:headers], :status => 200}
63
- }
64
- end
65
-
66
- def stub_retries
67
- run_count = 0
68
- Excon.stub({:method => :get}) { |params|
69
- run_count += 1
70
- if run_count <= 3 # First 3 calls fail.
71
- raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
72
- else
73
- {:body => params[:body], :headers => params[:headers], :status => 200}
74
- end
75
- }
76
- end
77
-
78
- def stub_failure
79
- Excon.stub({:method => :get}) { |params|
80
- raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
81
- }
82
- end
83
-
84
- tests('basic notification').returns(['excon.request', 'excon.response']) do
85
- subscribe(/excon/)
86
- stub_success
87
- make_request
88
- @events.map(&:name)
89
- end
90
-
91
- tests('captures scheme, host, port, and path').returns([:host, :path, :port, :scheme]) do
92
- subscribe(/excon/)
93
- stub_success
94
- make_request
95
- [:host, :path, :port, :scheme].select {|k| @events.first.payload.has_key? k}
96
- end
97
-
98
- tests('params in request overwrite those in constructor').returns('/cheezburger') do
99
- subscribe(/excon/)
100
- stub_success
101
- make_request(false, :path => '/cheezburger')
102
- @events.first.payload[:path]
103
- end
104
-
105
- tests('notify on retry').returns(3) do
106
- subscribe(/excon/)
107
- stub_retries
108
- make_request(true)
109
- @events.count{|e| e.name =~ /retry/}
110
- end
111
-
112
- tests('notify on error').returns(true) do
113
- subscribe(/excon/)
114
- stub_failure
115
- raises(Excon::Errors::SocketError) do
116
- make_request
117
- end
118
-
119
- @events.any?{|e| e.name =~ /error/}
120
- end
121
-
122
- tests('filtering').returns(['excon.request', 'excon.error']) do
123
- subscribe(/excon.request/)
124
- subscribe(/excon.error/)
125
- stub_failure
126
- raises(Excon::Errors::SocketError) do
127
- make_request(true)
128
- end
129
-
130
- @events.map(&:name)
131
- end
132
-
133
- tests('more filtering').returns(['excon.retry', 'excon.retry', 'excon.retry']) do
134
- subscribe(/excon.retry/)
135
- stub_failure
136
- raises(Excon::Errors::SocketError) do
137
- make_request(true)
138
- end
139
-
140
- @events.map(&:name)
141
- end
142
-
143
- tests('indicates duration').returns(true) do
144
- subscribe(/excon/)
145
- stub_success
146
- make_request
147
- (@events.first.duration/1000 - REQUEST_DELAY_SECONDS).abs < 1
148
- end
149
-
150
- tests('standard instrumentor') do
151
-
152
- tests('success').returns(
153
- ['excon.request', 'excon.retry', 'excon.retry', 'excon.retry', 'excon.error']) do
154
-
155
- begin
156
- original_stderr = $stderr
157
- $stderr = captured_stderr = StringIO.new
158
- stub_failure
159
- connection = Excon.new(
160
- 'http://127.0.0.1:9292',
161
- :instrumentor => Excon::StandardInstrumentor,
162
- :mock => true
163
- )
164
- raises(Excon::Errors::SocketError) do
165
- connection.get(:idempotent => true)
166
- end
167
-
168
- captured_stderr.string.split("\n").reject {|line| line =~ %r{^ }}.map {|event| event.split(' ').first}
169
- ensure
170
- $stderr = original_stderr
171
- end
172
- end
173
-
174
- tests('authorization header REDACT') do
175
-
176
- @auth_header = 'Basic dXNlcjpwYXNz'
177
-
178
- begin
179
- original_stderr = $stderr
180
- $stderr = @captured_stderr = StringIO.new
181
- stub_failure
182
- raises(Excon::Errors::SocketError) do
183
- @connection = Excon.new(
184
- 'http://user:pass@127.0.0.1:9292',
185
- :headers => {
186
- 'Authorization' => @auth_header
187
- },
188
- :instrumentor => Excon::StandardInstrumentor,
189
- :mock => true
190
- )
191
- @connection.get(:idempotent => true)
192
- end
193
- ensure
194
- $stderr = original_stderr
195
- end
196
-
197
- test('does not appear in response') do
198
- !@captured_stderr.string.include?(@auth_header)
199
- end
200
-
201
- test('does not mutate Authorization value') do
202
- @connection.data[:headers]['Authorization'] == @auth_header
203
- end
204
-
205
- end
206
-
207
- tests('password REDACT') do
208
-
209
- begin
210
- original_stderr = $stderr
211
- $stderr = @captured_stderr = StringIO.new
212
- stub_failure
213
- raises(Excon::Errors::SocketError) do
214
- @connection = Excon.new(
215
- 'http://user:pass@127.0.0.1:9292',
216
- :instrumentor => Excon::StandardInstrumentor,
217
- :mock => true
218
- )
219
- @connection.get(:idempotent => true)
220
- end
221
- ensure
222
- $stderr = original_stderr
223
- end
224
-
225
- @password_param = '"pass"'
226
-
227
- test('does not appear in response') do
228
- !@captured_stderr.string.include?(@password_param)
229
- end
230
-
231
- test('does not mutate password value') do
232
- @connection.data[:password] == "pass"
233
- end
234
-
235
- end
236
-
237
- end
238
-
239
- tests('use our own instrumentor').returns(
240
- ['excon.request', 'excon.retry', 'excon.retry', 'excon.retry', 'excon.error']) do
241
- stub_failure
242
- connection = Excon.new(
243
- 'http://127.0.0.1:9292',
244
- :instrumentor => SimpleInstrumentor,
245
- :mock => true
246
- )
247
- raises(Excon::Errors::SocketError) do
248
- connection.get(:idempotent => true)
249
- end
250
-
251
- SimpleInstrumentor.events
252
- end
253
-
254
- tests('always passes the block').returns(
255
- ['excon.request', 'excon.response']) do
256
- stub_success
257
- connection = Excon.new(
258
- 'http://127.0.0.1:9292',
259
- :instrumentor => SimpleInstrumentor,
260
- :mock => true
261
- )
262
- connection.get(:idempotent => true)
263
-
264
- SimpleInstrumentor.blocks
265
- end
266
-
267
- tests('does not generate events when not provided').returns(0) do
268
- subscribe(/excon/)
269
- stub_success
270
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
271
- connection.get(:idempotent => true)
272
- @events.count
273
- end
274
-
275
- tests('allows setting the prefix').returns(
276
- ['gug.request', 'gug.retry', 'gug.retry','gug.retry', 'gug.error']) do
277
- subscribe(/gug/)
278
- stub_failure
279
- connection = Excon.new(
280
- 'http://127.0.0.1:9292',
281
- :instrumentor => ActiveSupport::Notifications,
282
- :instrumentor_name => 'gug',
283
- :mock => true
284
- )
285
- raises(Excon::Errors::SocketError) do
286
- connection.get(:idempotent => true)
287
- end
288
- @events.map(&:name)
289
- end
290
-
291
- tests('allows setting the prefix when not idempotent', 'foo').returns(
292
- ['gug.request', 'gug.error']) do
293
- subscribe(/gug/)
294
- stub_failure
295
- connection = Excon.new(
296
- 'http://127.0.0.1:9292',
297
- :instrumentor => ActiveSupport::Notifications,
298
- :instrumentor_name => 'gug',
299
- :mock => true
300
- )
301
- raises(Excon::Errors::SocketError) do
302
- connection.get()
303
- end
304
- @events.map(&:name)
305
- end
306
-
307
- with_rackup('basic.ru') do
308
- tests('works unmocked').returns(['excon.request', 'excon.response']) do
309
- subscribe(/excon/)
310
- make_request(false, :mock => false)
311
- @events.map(&:name)
312
- end
313
- end
314
- end
315
-
@@ -1,304 +0,0 @@
1
- Shindo.tests('Excon stubs') do
2
- env_init
3
-
4
- tests("missing stub").raises(Excon::Errors::StubNotFound) do
5
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
6
- connection.request(:method => :get, :path => '/content-length/100')
7
- end
8
-
9
- tests("stub({})").raises(ArgumentError) do
10
- Excon.stub({})
11
- end
12
-
13
- tests("stub({}, {}) {}").raises(ArgumentError) do
14
- Excon.stub({}, {}) {}
15
- end
16
-
17
- tests("stub({:method => :get}, {:body => 'body', :status => 200})") do
18
- connection = nil
19
- response = nil
20
-
21
- tests('response.body').returns('body') do
22
- Excon.stub({:method => :get}, {:body => 'body', :status => 200})
23
-
24
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
25
- response = connection.request(:method => :get, :path => '/content-length/100')
26
-
27
- response.body
28
- end
29
-
30
- tests('response.headers').returns({}) do
31
- response.headers
32
- end
33
-
34
- tests('response.status').returns(200) do
35
- response.status
36
- end
37
-
38
- tests('response_block yields body').returns('body') do
39
- body = ''
40
- response_block = lambda do |chunk, remaining_bytes, total_bytes|
41
- body << chunk
42
- end
43
- connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block)
44
- body
45
- end
46
-
47
- tests('response.body empty with response_block').returns('') do
48
- response_block = lambda { |_, _, _| }
49
- connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block).body
50
- end
51
-
52
- Excon.stubs.clear
53
-
54
- end
55
-
56
- tests("stub({:path => %r{/tests/(\S+)}}, {:body => $1, :status => 200})") do
57
- connection = nil
58
- response = nil
59
-
60
- tests('response.body').returns('test') do
61
- Excon.stub({:path => %r{/tests/(\S+)}}) do |params|
62
- {
63
- :body => params[:captures][:path].first,
64
- :status => 200
65
- }
66
- end
67
-
68
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
69
- response = connection.request(:method => :get, :path => '/tests/test')
70
-
71
- response.body
72
- end
73
-
74
- tests('response.headers').returns({}) do
75
- response.headers
76
- end
77
-
78
- tests('response.status').returns(200) do
79
- response.status
80
- end
81
-
82
- Excon.stubs.clear
83
-
84
- end
85
-
86
- tests("stub({:body => 'body', :method => :get}) {|params| {:body => params[:body], :headers => params[:headers], :status => 200}}") do
87
- connection = nil
88
- response = nil
89
-
90
- tests('response.body').returns('body') do
91
- Excon.stub({:body => 'body', :method => :get}) {|params| {:body => params[:body], :headers => params[:headers], :status => 200}}
92
-
93
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
94
- response = connection.request(:body => 'body', :method => :get, :path => '/content-length/100')
95
-
96
- response.body
97
- end
98
-
99
- tests('response.headers').returns({'Host' => '127.0.0.1:9292', 'User-Agent' => "excon/#{Excon::VERSION}"}) do
100
- response.headers
101
- end
102
-
103
- tests('response.status').returns(200) do
104
- response.status
105
- end
106
-
107
- tests('response_block yields body').returns('body') do
108
- body = ''
109
- response_block = lambda do |chunk, remaining_bytes, total_bytes|
110
- body << chunk
111
- end
112
- connection.request(:body => 'body', :method => :get, :path => '/content-length/100', :response_block => response_block)
113
- body
114
- end
115
-
116
- tests('response.body empty with response_block').returns('') do
117
- response_block = lambda { |_, _, _| }
118
- connection.request(:body => 'body', :method => :get, :path => '/content-length/100', :response_block => response_block).body
119
- end
120
-
121
- Excon.stubs.clear
122
-
123
- end
124
-
125
- tests("stub({:body => File.open(...), :method => :get}, { :status => 200 })") do
126
-
127
- tests('response.status').returns(200) do
128
- file_path = File.join(File.dirname(__FILE__), '..', 'data', 'xs')
129
-
130
- Excon.stub(
131
- { :body => File.read(file_path), :method => :get },
132
- { :status => 200 }
133
- )
134
-
135
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
136
- response = connection.request(:body => File.open(file_path), :method => :get, :path => '/')
137
-
138
- response.status
139
- end
140
-
141
- Excon.stubs.clear
142
-
143
- end
144
-
145
- tests("invalid stub response").raises(Excon::Errors::InvalidStub) do
146
- Excon.stub({:body => 42, :method => :get}, {:status => 200})
147
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
148
- connection.request(:body => 42, :method => :get, :path => '/').status
149
- end
150
-
151
- tests("mismatched stub").raises(Excon::Errors::StubNotFound) do
152
- Excon.stub({:method => :post}, {:body => 'body'})
153
- Excon.get('http://127.0.0.1:9292/', :mock => true)
154
- end
155
-
156
- with_server('good') do
157
- tests('allow mismatched stub').returns(200) do
158
- Excon.stub({:path => '/echo/request_count'}, {:body => 'body'})
159
- Excon.get(
160
- 'http://127.0.0.1:9292/echo/request',
161
- :mock => true,
162
- :allow_unstubbed_requests => true
163
- ).status
164
- end
165
- end
166
-
167
- Excon.stubs.clear
168
-
169
- tests("stub({}, {:body => 'x' * (Excon::DEFAULT_CHUNK_SIZE + 1)})") do
170
-
171
- test("response_block yields body") do
172
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
173
- Excon.stub({}, {:body => 'x' * (Excon::DEFAULT_CHUNK_SIZE + 1)})
174
-
175
- chunks = []
176
- response_block = lambda do |chunk, remaining_bytes, total_bytes|
177
- chunks << chunk
178
- end
179
- connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block)
180
- chunks == ['x' * Excon::DEFAULT_CHUNK_SIZE, 'x']
181
- end
182
-
183
- tests("response.body empty with response_block").returns('') do
184
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
185
- Excon.stub({}, {:body => 'x' * (Excon::DEFAULT_CHUNK_SIZE + 1)})
186
- response_block = lambda { |_, _, _| }
187
- connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block).body
188
- end
189
-
190
- end
191
-
192
- Excon.stubs.clear
193
-
194
- tests("stub({:url => 'https://user:pass@foo.bar.com:9999/baz?quux=true'}, {:status => 200})") do
195
- test("get(:expects => 200)") do
196
- Excon.stub({:url => 'https://user:pass@foo.bar.com:9999/baz?quux=true'}, {:status => 200})
197
- Excon.new("https://user:pass@foo.bar.com:9999/baz?quux=true", :mock => true).get(:expects => 200)
198
- true
199
- end
200
- end
201
-
202
- Excon.stubs.clear
203
-
204
- tests("stub({}, {:status => 404, :body => 'Not Found'}") do
205
- connection = nil
206
-
207
- tests("request(:expects => 200, :method => :get, :path => '/')").raises(Excon::Errors::NotFound) do
208
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
209
- Excon.stub({}, {:status => 404, :body => 'Not Found'})
210
-
211
- connection.request(:expects => 200, :method => :get, :path => '/')
212
- end
213
-
214
- tests("Expects exception should contain response object").returns(Excon::Response) do
215
- begin
216
- connection.request(:expects => 200, :method => :get, :path => '/')
217
- rescue Excon::Errors::NotFound => e
218
- e.response.class
219
- end
220
- end
221
-
222
- test("request(:expects => 200, :method => :get, :path => '/') with block does not invoke the block since it raises an error") do
223
- block_called = false
224
- begin
225
- response_block = lambda do |_,_,_|
226
- block_called = true
227
- end
228
- connection.request(:expects => 200, :method => :get, :path => '/', :response_block => response_block)
229
- rescue Excon::Errors::NotFound
230
- end
231
- !block_called
232
- end
233
-
234
- Excon.stubs.clear
235
-
236
- end
237
-
238
- tests("stub_for({})") do
239
- tests("stub_for({})").returns([{}, {}]) do
240
- Excon.new('http://127.0.0.1:9292', :mock => true)
241
- Excon.stub({}, {})
242
-
243
- Excon.stub_for({})
244
- end
245
-
246
- Excon.stubs.clear
247
- end
248
-
249
- tests("unstub({})") do
250
- connection = nil
251
-
252
- tests("unstub({})").returns([{}, {}]) do
253
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
254
- Excon.stub({}, {})
255
-
256
- Excon.unstub({})
257
- end
258
-
259
- tests("request(:method => :get)").raises(Excon::Errors::StubNotFound) do
260
- connection.request(:method => :get)
261
- end
262
-
263
- Excon.stubs.clear
264
- end
265
-
266
- tests("global stubs") do
267
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
268
- Excon.stub({}, {:body => '1'})
269
- t = Thread.new do
270
- Excon.stub({}, {:body => '2'})
271
- connection.request(:method => :get).body
272
- end
273
- tests("get on a different thread").returns('2') do
274
- t.join.value
275
- end
276
- tests("get on main thread").returns('2') do
277
- connection.request(:method => :get).body
278
- end
279
- Excon.stubs.clear
280
- end
281
-
282
- tests("thread-local stubs") do
283
- original_stubs_value = Excon.defaults[:stubs]
284
- Excon.defaults[:stubs] = :local
285
-
286
- connection = Excon.new('http://127.0.0.1:9292', :mock => true)
287
- Excon.stub({}, {:body => '1'})
288
- t = Thread.new do
289
- Excon.stub({}, {:body => '2'})
290
- connection.request(:method => :get).body
291
- end
292
- tests("get on a different thread").returns('2') do
293
- t.join.value
294
- end
295
- tests("get on main thread").returns('1') do
296
- connection.request(:method => :get).body
297
- end
298
- Excon.stubs.clear
299
-
300
- Excon.defaults[:stubs] = original_stubs_value
301
- end
302
-
303
- env_restore
304
- end