webmock 1.6.4 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.gitignore +3 -1
- data/.travis.yml +6 -0
- data/CHANGELOG.md +211 -118
- data/Gemfile +16 -1
- data/Guardfile +24 -0
- data/LICENSE +1 -1
- data/README.md +64 -15
- data/Rakefile +19 -6
- data/lib/webmock.rb +9 -4
- data/lib/webmock/api.rb +5 -4
- data/lib/webmock/assertion_failure.rb +1 -1
- data/lib/webmock/callback_registry.rb +1 -1
- data/lib/webmock/config.rb +2 -2
- data/lib/webmock/cucumber.rb +1 -1
- data/lib/webmock/errors.rb +17 -5
- data/lib/webmock/http_lib_adapters/{curb.rb → curb_adapter.rb} +79 -49
- data/lib/webmock/http_lib_adapters/{em_http_request.rb → em_http_request/em_http_request_0_x.rb} +20 -15
- data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +201 -0
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +11 -0
- data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +7 -0
- data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +19 -0
- data/lib/webmock/http_lib_adapters/{httpclient.rb → httpclient_adapter.rb} +35 -8
- data/lib/webmock/http_lib_adapters/net_http.rb +84 -25
- data/lib/webmock/http_lib_adapters/net_http_response.rb +17 -17
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +124 -0
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +166 -0
- data/lib/webmock/minitest.rb +15 -0
- data/lib/webmock/rack_response.rb +52 -0
- data/lib/webmock/request_pattern.rb +4 -2
- data/lib/webmock/request_signature.rb +4 -0
- data/lib/webmock/request_stub.rb +35 -2
- data/lib/webmock/responses_sequence.rb +2 -2
- data/lib/webmock/rspec.rb +2 -2
- data/lib/webmock/rspec/matchers.rb +9 -4
- data/lib/webmock/rspec/matchers/webmock_matcher.rb +1 -1
- data/lib/webmock/stub_registry.rb +1 -1
- data/lib/webmock/stub_request_snippet.rb +14 -11
- data/lib/webmock/util/hash_keys_stringifier.rb +4 -4
- data/lib/webmock/util/headers.rb +3 -3
- data/lib/webmock/util/json.rb +54 -0
- data/lib/webmock/util/uri.rb +1 -1
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +20 -3
- data/minitest/test_helper.rb +29 -0
- data/minitest/test_webmock.rb +6 -0
- data/minitest/webmock_spec.rb +30 -0
- data/spec/curb_spec.rb +26 -8
- data/spec/curb_spec_helper.rb +6 -6
- data/spec/em_http_request_spec.rb +95 -1
- data/spec/em_http_request_spec_helper.rb +16 -16
- data/spec/errors_spec.rb +19 -4
- data/spec/example_curl_output.txt +22 -22
- data/spec/http_lib_adapters/http_lib_adapter_registry_spec.rb +17 -0
- data/spec/http_lib_adapters/http_lib_adapter_spec.rb +12 -0
- data/spec/httpclient_spec.rb +1 -1
- data/spec/httpclient_spec_helper.rb +3 -38
- data/spec/my_rack_app.rb +18 -0
- data/spec/net_http_shared.rb +125 -0
- data/spec/net_http_spec.rb +27 -31
- data/spec/net_http_spec_helper.rb +4 -34
- data/spec/network_connection.rb +1 -1
- data/spec/patron_spec_helper.rb +4 -7
- data/spec/quality_spec.rb +60 -0
- data/spec/rack_response_spec.rb +33 -0
- data/spec/real_net_http_spec.rb +20 -0
- data/spec/request_execution_verifier_spec.rb +8 -8
- data/spec/request_pattern_spec.rb +3 -3
- data/spec/request_stub_spec.rb +19 -19
- data/spec/response_spec.rb +8 -8
- data/spec/spec_helper.rb +14 -11
- data/spec/stub_request_snippet_spec.rb +85 -37
- data/spec/support/webmock_server.rb +62 -0
- data/spec/typhoeus_hydra_spec.rb +53 -0
- data/spec/typhoeus_hydra_spec_helper.rb +50 -0
- data/spec/util/headers_spec.rb +5 -5
- data/spec/util/json_spec.rb +7 -0
- data/spec/util/uri_spec.rb +1 -1
- data/spec/vendor/addressable/lib/uri.rb +1 -0
- data/spec/vendor/crack/lib/crack.rb +1 -0
- data/spec/vendor/right_http_connection-1.2.4/History.txt +4 -4
- data/spec/vendor/right_http_connection-1.2.4/README.txt +4 -4
- data/spec/vendor/right_http_connection-1.2.4/Rakefile +3 -3
- data/spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb +4 -4
- data/spec/vendor/right_http_connection-1.2.4/setup.rb +4 -4
- data/spec/webmock_shared.rb +375 -143
- data/spec/webmock_spec.rb +7 -0
- data/test/http_request.rb +24 -0
- data/test/shared_test.rb +47 -0
- data/test/test_helper.rb +6 -3
- data/test/test_webmock.rb +2 -67
- data/webmock.gemspec +8 -7
- metadata +153 -88
- data/lib/webmock/http_lib_adapters/patron.rb +0 -100
- data/spec/other_net_http_libs_spec.rb +0 -30
data/Gemfile
CHANGED
@@ -1,2 +1,17 @@
|
|
1
|
-
source
|
1
|
+
source 'http://rubygems.org/'
|
2
|
+
|
2
3
|
gemspec
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
gem 'rake'
|
7
|
+
gem 'guard-rspec'
|
8
|
+
gem 'rb-fsevent'
|
9
|
+
end
|
10
|
+
|
11
|
+
group :test do
|
12
|
+
gem 'rack'
|
13
|
+
end
|
14
|
+
|
15
|
+
platforms :jruby do
|
16
|
+
gem 'jruby-openssl', '~> 0.7'
|
17
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
# rubies = %w[
|
5
|
+
# 1.8.6
|
6
|
+
# 1.8.7
|
7
|
+
# 1.9.2
|
8
|
+
# ree
|
9
|
+
# jruby
|
10
|
+
# ].map { |ruby| "#{ruby}@webmock" }
|
11
|
+
|
12
|
+
rspec_options = {
|
13
|
+
# :rvm => rubies,
|
14
|
+
:all_on_start => false,
|
15
|
+
:notification => false,
|
16
|
+
:cli => '--color',
|
17
|
+
:version => 2
|
18
|
+
}
|
19
|
+
|
20
|
+
guard 'rspec', rspec_options do
|
21
|
+
watch(%r{^spec/.+_spec\.rb})
|
22
|
+
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
23
|
+
watch('spec/spec_helper.rb') { "spec" }
|
24
|
+
end
|
data/LICENSE
CHANGED
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,14 +1,8 @@
|
|
1
|
-
WebMock
|
1
|
+
WebMock [![Build Status](https://secure.travis-ci.org/bblimke/webmock.png)](http://travis-ci.org/bblimke/webmock)
|
2
2
|
=======
|
3
3
|
|
4
4
|
Library for stubbing and setting expectations on HTTP requests in Ruby.
|
5
5
|
|
6
|
-
WebMock is looking for a maintainer
|
7
|
-
===================================
|
8
|
-
|
9
|
-
I'm not able to maintain WebMock until end of June 2011.
|
10
|
-
If anyone is interested in maintaining it in the meantime (at least handling pull requests and creating patch releases), please get in touch.
|
11
|
-
|
12
6
|
Features
|
13
7
|
--------
|
14
8
|
|
@@ -19,6 +13,7 @@ Features
|
|
19
13
|
* Smart matching of the same headers in different representations.
|
20
14
|
* Support for Test::Unit
|
21
15
|
* Support for RSpec 1.x and RSpec 2.x
|
16
|
+
* Support for MiniTest
|
22
17
|
|
23
18
|
Supported HTTP libraries
|
24
19
|
------------------------
|
@@ -28,6 +23,18 @@ Supported HTTP libraries
|
|
28
23
|
* Patron
|
29
24
|
* EM-HTTP-Request
|
30
25
|
* Curb (currently only Curb::Easy)
|
26
|
+
* Typhoeus (currently only Typhoeus::Hydra)
|
27
|
+
|
28
|
+
Supported Ruby Interpreters
|
29
|
+
---------------------------
|
30
|
+
|
31
|
+
* MRI 1.8.6
|
32
|
+
* MRI 1.8.7
|
33
|
+
* MRI 1.9.1
|
34
|
+
* MRI 1.9.2
|
35
|
+
* MRI 1.9.3-preview1
|
36
|
+
* REE 1.8.7
|
37
|
+
* JRuby
|
31
38
|
|
32
39
|
##Installation
|
33
40
|
|
@@ -51,6 +58,12 @@ Add the following code to `spec/spec_helper`:
|
|
51
58
|
|
52
59
|
require 'webmock/rspec'
|
53
60
|
|
61
|
+
### MiniTest
|
62
|
+
|
63
|
+
Add the following code to `test/test_helper`:
|
64
|
+
|
65
|
+
require 'webmock/minitest'
|
66
|
+
|
54
67
|
### Cucumber
|
55
68
|
|
56
69
|
Add the following code to `features/support/env.rb`
|
@@ -219,6 +232,18 @@ You can also use WebMock outside a test framework:
|
|
219
232
|
|
220
233
|
RestClient.post('www.example.net', 'abc') # ===> "abc\n"
|
221
234
|
|
235
|
+
### Rack responses
|
236
|
+
|
237
|
+
class MyRackApp
|
238
|
+
def self.call(env)
|
239
|
+
[200, {}, ["Hello"]]
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
stub_request(:get, "www.example.com").to_rack(MyRackApp)
|
244
|
+
|
245
|
+
RestClient.post('www.example.com') # ===> "Hello"
|
246
|
+
|
222
247
|
### Raising errors
|
223
248
|
|
224
249
|
#### Exception declared by class
|
@@ -294,13 +319,15 @@ You can also use WebMock outside a test framework:
|
|
294
319
|
|
295
320
|
Net::HTTP.get('localhost:9887', '/') # ===> Allowed. Perhaps to Selenium?
|
296
321
|
|
297
|
-
### External requests can be disabled while allowing any hostname
|
322
|
+
### External requests can be disabled while allowing any hostname or port
|
298
323
|
|
299
|
-
WebMock.disable_net_connect!(:allow => "www.example.org")
|
324
|
+
WebMock.disable_net_connect!(:allow => "www.example.org:8080")
|
300
325
|
|
301
|
-
|
326
|
+
RestClient.get('www.something.com', '/') # ===> Failure
|
327
|
+
|
328
|
+
RestClient.get('www.example.org', '/') # ===> Failure.
|
302
329
|
|
303
|
-
|
330
|
+
RestClient.get('www.example.org:8080', '/') # ===> Allowed
|
304
331
|
|
305
332
|
## Connecting on Net::HTTP.start
|
306
333
|
|
@@ -351,7 +378,7 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
|
|
351
378
|
assert_requested :get, "http://www.example.com" # ===> Success
|
352
379
|
|
353
380
|
|
354
|
-
### Setting expectations in RSpec
|
381
|
+
### Setting expectations in RSpec on `WebMock` module
|
355
382
|
This style is borrowed from [fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher)
|
356
383
|
|
357
384
|
require 'webmock/rspec'
|
@@ -365,9 +392,9 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
|
|
365
392
|
WebMock.should have_requested(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
|
366
393
|
|
367
394
|
WebMock.should have_requested(:get, "www.example.com").
|
368
|
-
with(:body => {"a" => ["b", "c"]}, :headers => 'Content-Type' => 'application/json')
|
395
|
+
with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'})
|
369
396
|
|
370
|
-
###
|
397
|
+
### Setting expectations in RSpec with `a_request`
|
371
398
|
|
372
399
|
a_request(:post, "www.example.com").with(:body => "abc", :headers => {'Content-Length' => 3}).should have_been_made.once
|
373
400
|
|
@@ -380,7 +407,13 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
|
|
380
407
|
a_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
|
381
408
|
|
382
409
|
a_request(:post, "www.example.com").
|
383
|
-
with(:body => {"a" => ["b", "c"]}, :headers => 'Content-Type' => 'application/json').should have_been_made
|
410
|
+
with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'}).should have_been_made
|
411
|
+
|
412
|
+
### Setting expectations in RSpec on the stub
|
413
|
+
|
414
|
+
stub = stub_request(:get, "www.example.com")
|
415
|
+
# ... make requests ...
|
416
|
+
stub.should have_been_requested
|
384
417
|
|
385
418
|
## Clearing stubs and request history
|
386
419
|
|
@@ -396,6 +429,13 @@ If you want to reset all current stubs and history of requests use `WebMock.rese
|
|
396
429
|
|
397
430
|
assert_not_requested :get, "www.example.com" # ===> Success
|
398
431
|
|
432
|
+
## Disabling and enabling WebMock or only some http client adapters
|
433
|
+
|
434
|
+
WebMock.disable! #disable WebMock (all adapters)
|
435
|
+
WebMock.disable!(:except => [:net_http]) #disable WebMock for all libs except Net::HTTP
|
436
|
+
WebMock.enable! #enable WebMock (all adapters)
|
437
|
+
WebMock.enable!(:except => [:patron]) #enable WebMock for all libs except Patron
|
438
|
+
|
399
439
|
|
400
440
|
## Matching requests
|
401
441
|
|
@@ -597,6 +637,15 @@ People who submitted patches and new features or suggested improvements. Many th
|
|
597
637
|
* Alastair Brunton
|
598
638
|
* Sam Stokes
|
599
639
|
* Eugene Bolshakov
|
640
|
+
* James Conroy-Finn
|
641
|
+
* Salvador Fuentes Jr
|
642
|
+
* Alex Rothenberg
|
643
|
+
* Aidan Feldman
|
644
|
+
* Steve Hull
|
645
|
+
* Jay Adkisson
|
646
|
+
* Zach Dennis
|
647
|
+
* Nikita Fedyashev
|
648
|
+
* Lin Jen-Shin
|
600
649
|
|
601
650
|
For a full list of contributors you can visit the
|
602
651
|
[contributors](https://github.com/bblimke/webmock/contributors) page.
|
data/Rakefile
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
1
3
|
require 'bundler'
|
2
4
|
Bundler::GemHelper.install_tasks
|
3
5
|
|
4
|
-
namespace :
|
6
|
+
namespace :rvm do
|
5
7
|
desc 'Run specs against 1.8.6, REE, 1.8.7, 1.9.2 and jRuby'
|
6
|
-
task :
|
8
|
+
task :specs do
|
7
9
|
# JCF: I'd love to be able to use RVM's `rvm {rubies} specs` command but
|
8
10
|
# the require tests in spec/other_net_http_libs_spec.rb break when doing
|
9
11
|
# so.
|
@@ -13,11 +15,16 @@ namespace :spec do
|
|
13
15
|
end
|
14
16
|
|
15
17
|
require "rspec/core/rake_task"
|
16
|
-
RSpec::Core::RakeTask.new do |t|
|
18
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
17
19
|
t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
|
18
20
|
t.pattern = 'spec/**/*_spec.rb'
|
19
21
|
end
|
20
22
|
|
23
|
+
RSpec::Core::RakeTask.new(:spec_http_without_webmock) do |t|
|
24
|
+
t.rspec_opts = ["-c", "-f progress", "-r ./spec/real_net_http_spec.rb"]
|
25
|
+
t.pattern = 'spec/real_net_http_spec.rb'
|
26
|
+
end
|
27
|
+
|
21
28
|
require 'rake/testtask'
|
22
29
|
Rake::TestTask.new(:test) do |test|
|
23
30
|
test.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb")
|
@@ -25,10 +32,16 @@ Rake::TestTask.new(:test) do |test|
|
|
25
32
|
test.warning = false
|
26
33
|
end
|
27
34
|
|
28
|
-
|
35
|
+
Rake::TestTask.new(:minitest) do |test|
|
36
|
+
test.test_files = FileList["minitest/**/*.rb"].exclude("test/test_helper.rb")
|
37
|
+
test.verbose = false
|
38
|
+
test.warning = false
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => [:spec, :spec_http_without_webmock, :test, :minitest]
|
29
42
|
|
30
|
-
require '
|
31
|
-
|
43
|
+
require 'rdoc/task'
|
44
|
+
RDoc::Task.new do |rdoc|
|
32
45
|
$:.push File.expand_path('../lib', __FILE__)
|
33
46
|
require 'webmock/version'
|
34
47
|
|
data/lib/webmock.rb
CHANGED
@@ -6,11 +6,14 @@ require 'crack'
|
|
6
6
|
require 'webmock/deprecation'
|
7
7
|
require 'webmock/version'
|
8
8
|
|
9
|
+
require 'webmock/http_lib_adapters/http_lib_adapter_registry'
|
10
|
+
require 'webmock/http_lib_adapters/http_lib_adapter'
|
9
11
|
require 'webmock/http_lib_adapters/net_http'
|
10
|
-
require 'webmock/http_lib_adapters/
|
11
|
-
require 'webmock/http_lib_adapters/
|
12
|
-
require 'webmock/http_lib_adapters/
|
13
|
-
require 'webmock/http_lib_adapters/
|
12
|
+
require 'webmock/http_lib_adapters/httpclient_adapter'
|
13
|
+
require 'webmock/http_lib_adapters/patron_adapter'
|
14
|
+
require 'webmock/http_lib_adapters/curb_adapter'
|
15
|
+
require 'webmock/http_lib_adapters/em_http_request_adapter'
|
16
|
+
require 'webmock/http_lib_adapters/typhoeus_hydra_adapter'
|
14
17
|
|
15
18
|
require 'webmock/errors'
|
16
19
|
|
@@ -18,12 +21,14 @@ require 'webmock/util/uri'
|
|
18
21
|
require 'webmock/util/headers'
|
19
22
|
require 'webmock/util/hash_counter'
|
20
23
|
require 'webmock/util/hash_keys_stringifier'
|
24
|
+
require 'webmock/util/json'
|
21
25
|
|
22
26
|
require 'webmock/request_pattern'
|
23
27
|
require 'webmock/request_signature'
|
24
28
|
require 'webmock/responses_sequence'
|
25
29
|
require 'webmock/request_stub'
|
26
30
|
require 'webmock/response'
|
31
|
+
require 'webmock/rack_response'
|
27
32
|
|
28
33
|
require 'webmock/stub_request_snippet'
|
29
34
|
|
data/lib/webmock/api.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module WebMock
|
2
2
|
module API
|
3
3
|
extend self
|
4
|
-
|
4
|
+
|
5
5
|
def stub_request(method, uri)
|
6
|
-
WebMock::StubRegistry.instance.
|
6
|
+
WebMock::StubRegistry.instance.
|
7
|
+
register_request_stub(WebMock::RequestStub.new(method, uri))
|
7
8
|
end
|
8
9
|
|
9
10
|
alias_method :stub_http_request, :stub_request
|
@@ -11,7 +12,7 @@ module WebMock
|
|
11
12
|
def a_request(method, uri)
|
12
13
|
WebMock::RequestPattern.new(method, uri)
|
13
14
|
end
|
14
|
-
|
15
|
+
|
15
16
|
class << self
|
16
17
|
alias :request :a_request
|
17
18
|
end
|
@@ -29,4 +30,4 @@ module WebMock
|
|
29
30
|
WebMock::AssertionFailure.failure(verifier.negative_failure_message) unless verifier.does_not_match?
|
30
31
|
end
|
31
32
|
end
|
32
|
-
end
|
33
|
+
end
|
data/lib/webmock/config.rb
CHANGED
data/lib/webmock/cucumber.rb
CHANGED
data/lib/webmock/errors.rb
CHANGED
@@ -1,21 +1,33 @@
|
|
1
1
|
module WebMock
|
2
|
-
|
2
|
+
|
3
3
|
class NetConnectNotAllowedError < StandardError
|
4
4
|
def initialize(request_signature)
|
5
5
|
text = "Real HTTP connections are disabled. Unregistered request: #{request_signature}"
|
6
|
+
text << "\n\n"
|
6
7
|
text << stubbing_instructions(request_signature)
|
8
|
+
text << request_stubs
|
9
|
+
text << "\n\n" + "="*60
|
7
10
|
super(text)
|
8
11
|
end
|
9
12
|
|
10
13
|
private
|
11
14
|
|
15
|
+
def request_stubs
|
16
|
+
return "" if WebMock::StubRegistry.instance.request_stubs.empty?
|
17
|
+
text = "\n\nregistered request stubs:\n"
|
18
|
+
WebMock::StubRegistry.instance.request_stubs.each do |stub|
|
19
|
+
text << "\n#{WebMock::StubRequestSnippet.new(stub).to_s(false)}"
|
20
|
+
end
|
21
|
+
text
|
22
|
+
end
|
23
|
+
|
12
24
|
def stubbing_instructions(request_signature)
|
13
|
-
text = "
|
25
|
+
text = ""
|
26
|
+
request_stub = RequestStub.from_request_signature(request_signature)
|
14
27
|
text << "You can stub this request with the following snippet:\n\n"
|
15
|
-
text << WebMock::StubRequestSnippet.new(
|
16
|
-
text << "\n\n" + "="*60
|
28
|
+
text << WebMock::StubRequestSnippet.new(request_stub).to_s
|
17
29
|
text
|
18
30
|
end
|
19
31
|
end
|
20
32
|
|
21
|
-
end
|
33
|
+
end
|
@@ -1,7 +1,57 @@
|
|
1
|
+
begin
|
2
|
+
require 'curb'
|
3
|
+
rescue LoadError
|
4
|
+
# curb not found
|
5
|
+
end
|
6
|
+
|
1
7
|
if defined?(Curl)
|
8
|
+
module WebMock
|
9
|
+
module HttpLibAdapters
|
10
|
+
class CurbAdapter < HttpLibAdapter
|
11
|
+
adapter_for :curb
|
12
|
+
|
13
|
+
OriginalCurlEasy = Curl::Easy unless const_defined?(:OriginalCurlEasy)
|
14
|
+
|
15
|
+
def self.enable!
|
16
|
+
Curl.send(:remove_const, :Easy)
|
17
|
+
Curl.send(:const_set, :Easy, Curl::WebMockCurlEasy)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.disable!
|
21
|
+
Curl.send(:remove_const, :Easy)
|
22
|
+
Curl.send(:const_set, :Easy, OriginalCurlEasy)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Borrowed from Patron:
|
26
|
+
# http://github.com/toland/patron/blob/master/lib/patron/response.rb
|
27
|
+
def self.parse_header_string(header_string)
|
28
|
+
status, headers = nil, {}
|
29
|
+
|
30
|
+
header_string.split(/\r\n/).each do |header|
|
31
|
+
if header =~ %r|^HTTP/1.[01] \d\d\d (.*)|
|
32
|
+
status = $1
|
33
|
+
else
|
34
|
+
parts = header.split(':', 2)
|
35
|
+
unless parts.empty?
|
36
|
+
parts[1].strip! unless parts[1].nil?
|
37
|
+
if headers.has_key?(parts[0])
|
38
|
+
headers[parts[0]] = [headers[parts[0]]] unless headers[parts[0]].kind_of? Array
|
39
|
+
headers[parts[0]] << parts[1]
|
40
|
+
else
|
41
|
+
headers[parts[0]] = parts[1]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
return status, headers
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
2
52
|
|
3
53
|
module Curl
|
4
|
-
class Easy
|
54
|
+
class WebMockCurlEasy < Curl::Easy
|
5
55
|
def curb_or_webmock
|
6
56
|
request_signature = build_request_signature
|
7
57
|
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
|
@@ -19,7 +69,7 @@ if defined?(Curl)
|
|
19
69
|
webmock_response = build_webmock_response
|
20
70
|
WebMock::CallbackRegistry.invoke_callbacks(
|
21
71
|
{:lib => :curb, :real_request => true}, request_signature,
|
22
|
-
webmock_response)
|
72
|
+
webmock_response)
|
23
73
|
end
|
24
74
|
res
|
25
75
|
else
|
@@ -54,15 +104,15 @@ if defined?(Curl)
|
|
54
104
|
end
|
55
105
|
|
56
106
|
def build_curb_response(webmock_response)
|
57
|
-
raise Curl::Err::TimeoutError if webmock_response.should_timeout
|
107
|
+
raise Curl::Err::TimeoutError if webmock_response.should_timeout
|
58
108
|
webmock_response.raise_error_if_any
|
59
|
-
|
109
|
+
|
60
110
|
@body_str = webmock_response.body
|
61
111
|
@response_code = webmock_response.status[0]
|
62
112
|
|
63
113
|
@header_str = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n"
|
64
114
|
if webmock_response.headers
|
65
|
-
@header_str << webmock_response.headers.map do |k,v|
|
115
|
+
@header_str << webmock_response.headers.map do |k,v|
|
66
116
|
"#{k}: #{v.is_a?(Array) ? v.join(", ") : v}"
|
67
117
|
end.join("\r\n")
|
68
118
|
|
@@ -104,7 +154,8 @@ if defined?(Curl)
|
|
104
154
|
end
|
105
155
|
|
106
156
|
def build_webmock_response
|
107
|
-
status, headers =
|
157
|
+
status, headers =
|
158
|
+
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(self.header_str)
|
108
159
|
|
109
160
|
webmock_response = WebMock::Response.new
|
110
161
|
webmock_response.status = [self.response_code, status]
|
@@ -115,7 +166,7 @@ if defined?(Curl)
|
|
115
166
|
|
116
167
|
###
|
117
168
|
### Mocks of Curl::Easy methods below here.
|
118
|
-
###
|
169
|
+
###
|
119
170
|
|
120
171
|
def http_with_webmock(method)
|
121
172
|
@webmock_method = method
|
@@ -148,11 +199,11 @@ if defined?(Curl)
|
|
148
199
|
alias_method :http_put_without_webmock, :http_put
|
149
200
|
alias_method :http_put, :http_put_with_webmock
|
150
201
|
|
151
|
-
def http_post_with_webmock data
|
202
|
+
def http_post_with_webmock *data
|
152
203
|
@webmock_method = :post
|
153
|
-
@post_body = data if data
|
204
|
+
@post_body = data.join('&') if data && !data.empty?
|
154
205
|
curb_or_webmock do
|
155
|
-
http_post_without_webmock(data)
|
206
|
+
http_post_without_webmock(*data)
|
156
207
|
end
|
157
208
|
end
|
158
209
|
alias_method :http_post_without_webmock, :http_post
|
@@ -163,11 +214,11 @@ if defined?(Curl)
|
|
163
214
|
@webmock_method ||= :get
|
164
215
|
curb_or_webmock do
|
165
216
|
perform_without_webmock
|
166
|
-
end
|
217
|
+
end
|
167
218
|
end
|
168
219
|
alias :perform_without_webmock :perform
|
169
220
|
alias :perform :perform_with_webmock
|
170
|
-
|
221
|
+
|
171
222
|
def put_data_with_webmock= data
|
172
223
|
@webmock_method = :put
|
173
224
|
@put_data = data
|
@@ -175,7 +226,7 @@ if defined?(Curl)
|
|
175
226
|
end
|
176
227
|
alias_method :put_data_without_webmock=, :put_data=
|
177
228
|
alias_method :put_data=, :put_data_with_webmock=
|
178
|
-
|
229
|
+
|
179
230
|
def post_body_with_webmock= data
|
180
231
|
@webmock_method = :post
|
181
232
|
self.post_body_without_webmock = data
|
@@ -250,44 +301,23 @@ if defined?(Curl)
|
|
250
301
|
METHOD
|
251
302
|
end
|
252
303
|
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
end
|
262
|
-
METHOD
|
263
|
-
end
|
264
|
-
|
265
|
-
module WebmockHelper
|
266
|
-
# Borrowed from Patron:
|
267
|
-
# http://github.com/toland/patron/blob/master/lib/patron/response.rb
|
268
|
-
def self.parse_header_string(header_string)
|
269
|
-
status, headers = nil, {}
|
270
|
-
|
271
|
-
header_string.split(/\r\n/).each do |header|
|
272
|
-
if header =~ %r|^HTTP/1.[01] \d\d\d (.*)|
|
273
|
-
status = $1
|
274
|
-
else
|
275
|
-
parts = header.split(':', 2)
|
276
|
-
unless parts.empty?
|
277
|
-
parts[1].strip! unless parts[1].nil?
|
278
|
-
if headers.has_key?(parts[0])
|
279
|
-
headers[parts[0]] = [headers[parts[0]]] unless headers[parts[0]].kind_of? Array
|
280
|
-
headers[parts[0]] << parts[1]
|
281
|
-
else
|
282
|
-
headers[parts[0]] = parts[1]
|
283
|
-
end
|
284
|
-
end
|
285
|
-
end
|
286
|
-
end
|
304
|
+
class_eval <<-METHOD, __FILE__, __LINE__
|
305
|
+
def self.http_put(url, data, &block)
|
306
|
+
c = new
|
307
|
+
c.url = url
|
308
|
+
block.call(c) if block
|
309
|
+
c.send(:http_put, data)
|
310
|
+
c
|
311
|
+
end
|
287
312
|
|
288
|
-
|
313
|
+
def self.http_post(url, *data, &block)
|
314
|
+
c = new
|
315
|
+
c.url = url
|
316
|
+
block.call(c) if block
|
317
|
+
c.send(:http_post, *data)
|
318
|
+
c
|
289
319
|
end
|
290
|
-
|
320
|
+
METHOD
|
291
321
|
end
|
292
322
|
end
|
293
323
|
end
|