webmock-disabler 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README.markdown +43 -0
- data/Rakefile +7 -0
- data/lib/webmock/disabler/disabler.rb +31 -0
- data/lib/webmock/disabler/version.rb +5 -0
- data/lib/webmock/disabler/webmock_extensions.rb +15 -0
- data/lib/webmock/disabler.rb +3 -0
- data/spec/webmock_spec.rb +42 -0
- data/webmock-disabler.gemspec +22 -0
- metadata +89 -0
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.2-p0@webmock-disabler --create
|
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#webmock-disabler
|
2
|
+
webmock-disabler is a gem that allows users of [WebMock](https://github.com/bblimke/webmock) to disable and re-enable WebMock on-demand. This is useful in testing scenarios where the mere presence of WebMock causes some tests to fail. A common case is that the presence of WebMock is known to cause failures in Selenium-based integration tests, such as RSpec's request specs and Cucumber features.
|
3
|
+
|
4
|
+
#Installation
|
5
|
+
gem install webmock-disabler
|
6
|
+
or, if you use Bundler
|
7
|
+
|
8
|
+
gem 'webmock-disabler'
|
9
|
+
|
10
|
+
#Requirements
|
11
|
+
webmock-disabler works by monkey patching WebMock, so, its implementation is understandably fragile. It was written against version 1.6.4 of WebMock, and it might work with higher 1.x versions. Then again, it might not.
|
12
|
+
|
13
|
+
If future versions of WebMock break webmock-disabler, file an issue about it, or, better yet, fork, fix, and send a pull request.
|
14
|
+
|
15
|
+
#Limitations
|
16
|
+
While WebMock can mock out lots of different HTTP libraries (Net::HTTP, HTTPClient, EventMachine::HttpRequest, and more), webmock-disabler only supports Net::HTTP. So if you're using any HTTP library other than Net::HTTP, don't expect webmock-disabler to do squat.
|
17
|
+
|
18
|
+
I have no plans to implement support for other HTTP libraries, but I do have plans to accept pull requests- so have at it.
|
19
|
+
|
20
|
+
#Usage
|
21
|
+
require 'webmock'
|
22
|
+
require 'webmock-disabler'
|
23
|
+
|
24
|
+
Net::HTTP.get(URI.parse('http://www.google.com')) => raises WebMock::NetConnectNotAllowedError
|
25
|
+
|
26
|
+
WebMock.disable!
|
27
|
+
|
28
|
+
Net::HTTP.get(URI.parse('http://www.google.com')) => the HTML at www.google.com
|
29
|
+
|
30
|
+
WebMock.enable!
|
31
|
+
|
32
|
+
Net::HTTP.get(URI.parse('http://www.google.com')) => raises WebMock::NetConnectNotAllowedError
|
33
|
+
|
34
|
+
#Usage with RSpec
|
35
|
+
In your spec_helper.rb:
|
36
|
+
|
37
|
+
before(:each), :type => :request do
|
38
|
+
WebMock.disable!
|
39
|
+
end
|
40
|
+
|
41
|
+
after(:each), :type => :request do
|
42
|
+
WebMock.enable!
|
43
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Net #:nodoc: all
|
2
|
+
class BufferedIO
|
3
|
+
def initialize_with_disabling(*args)
|
4
|
+
WebMock.enabled? ? initialize_with_webmock(*args) : initialize_without_webmock(*args)
|
5
|
+
end
|
6
|
+
|
7
|
+
alias_method :initialize, :initialize_with_disabling
|
8
|
+
end
|
9
|
+
|
10
|
+
class HTTP
|
11
|
+
class << self
|
12
|
+
def socket_type_with_disabling
|
13
|
+
WebMock.enabled? ? socket_type_with_webmock : socket_type_without_webmock
|
14
|
+
end
|
15
|
+
|
16
|
+
alias_method :socket_type, :socket_type_with_disabling
|
17
|
+
end
|
18
|
+
|
19
|
+
def request_with_disabling(*args, &block)
|
20
|
+
WebMock.enabled? ? request_with_webmock(*args, &block) : request_without_webmock(*args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method :request, :request_with_disabling
|
24
|
+
|
25
|
+
def connect_with_disabling
|
26
|
+
WebMock.enabled? ? connect_with_webmock : connect_without_webmock
|
27
|
+
end
|
28
|
+
|
29
|
+
alias_method :connect, :connect_with_disabling
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'webmock/disabler'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
describe WebMock do
|
5
|
+
shared_examples_for "WebMock enabled" do
|
6
|
+
it "does not allow outbound connections" do
|
7
|
+
Net::HTTP.any_instance.should_not_receive(:request)
|
8
|
+
lambda {
|
9
|
+
Net::HTTP.get(URI.parse("http://www.google.com"))
|
10
|
+
}.should raise_error(WebMock::NetConnectNotAllowedError)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "default behavior" do
|
15
|
+
it_should_behave_like "WebMock enabled"
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".disable!" do
|
19
|
+
before(:each) do
|
20
|
+
WebMock.disable!
|
21
|
+
end
|
22
|
+
|
23
|
+
after(:each) do
|
24
|
+
WebMock.enable!
|
25
|
+
end
|
26
|
+
|
27
|
+
it "allows outbound connections" do
|
28
|
+
Net::HTTP.any_instance.should_receive(:request).and_raise("good enough")
|
29
|
+
lambda {
|
30
|
+
Net::HTTP.get(URI.parse("http://www.google.com"))
|
31
|
+
}.should raise_error("good enough")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".enable!" do
|
36
|
+
before(:each) do
|
37
|
+
WebMock.enable!
|
38
|
+
end
|
39
|
+
|
40
|
+
it_should_behave_like "WebMock enabled"
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "webmock/disabler/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "webmock-disabler"
|
7
|
+
s.version = WebMock::Disabler::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Danny Burkes"]
|
10
|
+
s.email = ["dburkes@netable.com"]
|
11
|
+
s.homepage = %q{http://github.com/dburkes/webmock-disabler}
|
12
|
+
s.summary = %q{Disable and re-enable WebMock with ease}
|
13
|
+
s.description = %q{Disable and re-enable WebMock with ease}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_runtime_dependency(%q{webmock}, [">= 1.6.4", "< 2.0.0"])
|
21
|
+
s.add_development_dependency(%q{rspec})
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webmock-disabler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Danny Burkes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-11 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: webmock
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.6.4
|
24
|
+
- - <
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.0
|
27
|
+
type: :runtime
|
28
|
+
version_requirements: *id001
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rspec
|
31
|
+
prerelease: false
|
32
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: "0"
|
38
|
+
type: :development
|
39
|
+
version_requirements: *id002
|
40
|
+
description: Disable and re-enable WebMock with ease
|
41
|
+
email:
|
42
|
+
- dburkes@netable.com
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions: []
|
46
|
+
|
47
|
+
extra_rdoc_files: []
|
48
|
+
|
49
|
+
files:
|
50
|
+
- .gitignore
|
51
|
+
- .rvmrc
|
52
|
+
- Gemfile
|
53
|
+
- README.markdown
|
54
|
+
- Rakefile
|
55
|
+
- lib/webmock/disabler.rb
|
56
|
+
- lib/webmock/disabler/disabler.rb
|
57
|
+
- lib/webmock/disabler/version.rb
|
58
|
+
- lib/webmock/disabler/webmock_extensions.rb
|
59
|
+
- spec/webmock_spec.rb
|
60
|
+
- webmock-disabler.gemspec
|
61
|
+
homepage: http://github.com/dburkes/webmock-disabler
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.4
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Disable and re-enable WebMock with ease
|
88
|
+
test_files:
|
89
|
+
- spec/webmock_spec.rb
|