monkeywrench 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/monkeywrench/list.rb +5 -3
- data/test/lib/fakeweb/CHANGELOG +179 -0
- data/test/lib/fakeweb/LICENSE.txt +281 -0
- data/test/lib/fakeweb/README.rdoc +193 -0
- data/test/lib/fakeweb/Rakefile +70 -0
- data/test/lib/fakeweb/VERSION +1 -0
- data/test/lib/fakeweb/fakeweb.gemspec +88 -0
- data/test/lib/fakeweb/lib/fake_web.rb +179 -0
- data/test/lib/fakeweb/lib/fake_web/ext/net_http.rb +81 -0
- data/test/lib/fakeweb/lib/fake_web/registry.rb +120 -0
- data/test/lib/fakeweb/lib/fake_web/responder.rb +118 -0
- data/test/lib/fakeweb/lib/fake_web/response.rb +10 -0
- data/test/lib/fakeweb/lib/fake_web/stub_socket.rb +15 -0
- data/test/lib/fakeweb/lib/fake_web/utility.rb +76 -0
- data/test/lib/fakeweb/lib/fakeweb.rb +2 -0
- data/test/lib/fakeweb/test/fixtures/google_response_from_curl +12 -0
- data/test/lib/fakeweb/test/fixtures/google_response_with_transfer_encoding +17 -0
- data/test/lib/fakeweb/test/fixtures/google_response_without_transfer_encoding +11 -0
- data/test/lib/fakeweb/test/fixtures/test_example.txt +1 -0
- data/test/lib/fakeweb/test/fixtures/test_txt_file +3 -0
- data/test/lib/fakeweb/test/test_allow_net_connect.rb +85 -0
- data/test/lib/fakeweb/test/test_deprecations.rb +54 -0
- data/test/lib/fakeweb/test/test_fake_authentication.rb +92 -0
- data/test/lib/fakeweb/test/test_fake_web.rb +539 -0
- data/test/lib/fakeweb/test/test_fake_web_open_uri.rb +58 -0
- data/test/lib/fakeweb/test/test_helper.rb +76 -0
- data/test/lib/fakeweb/test/test_missing_open_uri.rb +25 -0
- data/test/lib/fakeweb/test/test_other_net_http_libraries.rb +37 -0
- data/test/lib/fakeweb/test/test_precedence.rb +79 -0
- data/test/lib/fakeweb/test/test_query_string.rb +45 -0
- data/test/lib/fakeweb/test/test_regexes.rb +161 -0
- data/test/lib/fakeweb/test/test_response_headers.rb +73 -0
- data/test/lib/fakeweb/test/test_trailing_slashes.rb +53 -0
- data/test/lib/fakeweb/test/test_utility.rb +91 -0
- metadata +70 -26
- data/test/monkey_wrench/base_test.rbc +0 -1758
- data/test/monkey_wrench/campaign_aim_test.rbc +0 -40
- data/test/monkey_wrench/campaign_stats_test.rbc +0 -40
- data/test/monkey_wrench/campaign_test.rbc +0 -40
- data/test/monkey_wrench/hash_test.rbc +0 -1636
- data/test/monkey_wrench/helper_test.rbc +0 -40
- data/test/monkey_wrench/list_test.rbc +0 -10798
- data/test/monkey_wrench/security_test.rbc +0 -40
- data/test/test_helper.rbc +0 -2045
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
= FakeWeb
|
|
2
|
+
|
|
3
|
+
FakeWeb is a helper for faking web requests in Ruby. It works at a global
|
|
4
|
+
level, without modifying code or writing extensive stubs.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
== Installation
|
|
8
|
+
|
|
9
|
+
The latest release of FakeWeb is once again available from your friendly
|
|
10
|
+
RubyForge mirror. Just install the gem:
|
|
11
|
+
|
|
12
|
+
sudo gem install fakeweb
|
|
13
|
+
|
|
14
|
+
Note: the gem was previously available as +FakeWeb+ (capital letters), but now
|
|
15
|
+
all versions are simply registered as +fakeweb+. If you have any old +FakeWeb+
|
|
16
|
+
gems lying around, remove them: <tt>sudo gem uninstall FakeWeb</tt>
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
== Help and discussion
|
|
20
|
+
|
|
21
|
+
RDocs for the current release are available at http://fakeweb.rubyforge.org.
|
|
22
|
+
|
|
23
|
+
There's a mailing list for questions and discussion at
|
|
24
|
+
http://groups.google.com/group/fakeweb-users.
|
|
25
|
+
|
|
26
|
+
The main source repository is http://github.com/chrisk/fakeweb.
|
|
27
|
+
|
|
28
|
+
== Examples
|
|
29
|
+
|
|
30
|
+
Start by requiring FakeWeb:
|
|
31
|
+
|
|
32
|
+
require 'rubygems'
|
|
33
|
+
require 'fakeweb'
|
|
34
|
+
|
|
35
|
+
=== Registering basic string responses
|
|
36
|
+
|
|
37
|
+
FakeWeb.register_uri(:get, "http://example.com/test1", :body => "Hello World!")
|
|
38
|
+
|
|
39
|
+
Net::HTTP.get(URI.parse("http://example.com/test1"))
|
|
40
|
+
=> "Hello World!"
|
|
41
|
+
|
|
42
|
+
Net::HTTP.get(URI.parse("http://example.com/test2"))
|
|
43
|
+
=> FakeWeb is bypassed and the response from a real request is returned
|
|
44
|
+
|
|
45
|
+
You can also call <tt>register_uri</tt> with a regular expression, to match
|
|
46
|
+
more than one URI.
|
|
47
|
+
|
|
48
|
+
=== Replaying a recorded response
|
|
49
|
+
|
|
50
|
+
page = `curl -is http://www.google.com/`
|
|
51
|
+
FakeWeb.register_uri(:get, "http://www.google.com/", :response => page)
|
|
52
|
+
|
|
53
|
+
Net::HTTP.get(URI.parse("http://www.google.com/"))
|
|
54
|
+
# => Full response, including headers
|
|
55
|
+
|
|
56
|
+
=== Adding a custom status to the response
|
|
57
|
+
|
|
58
|
+
FakeWeb.register_uri(:get, "http://example.com/", :body => "Nothing to be found 'round here",
|
|
59
|
+
:status => ["404", "Not Found"])
|
|
60
|
+
|
|
61
|
+
Net::HTTP.start("example.com") do |req|
|
|
62
|
+
response = req.get("/")
|
|
63
|
+
response.code # => "404"
|
|
64
|
+
response.message # => "Not Found"
|
|
65
|
+
response.body # => "Nothing to be found 'round here"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
=== Responding to any HTTP method
|
|
69
|
+
|
|
70
|
+
FakeWeb.register_uri(:any, "http://example.com", :body => "response for any HTTP method")
|
|
71
|
+
|
|
72
|
+
If you use the <tt>:any</tt> symbol, the URI you specify will be completely
|
|
73
|
+
stubbed out (regardless of the HTTP method of the request). This can be useful
|
|
74
|
+
for RPC-like services, where the HTTP method isn't significant. (Older
|
|
75
|
+
versions of FakeWeb always behaved like this, and didn't accept the first
|
|
76
|
+
+method+ argument above; this syntax is now deprecated.)
|
|
77
|
+
|
|
78
|
+
=== Rotating responses
|
|
79
|
+
|
|
80
|
+
You can optionally call FakeWeb.register_uri with an array of options hashes;
|
|
81
|
+
these are used, in order, to respond to repeated requests. Once you run out of
|
|
82
|
+
responses, further requests always receive the last response. (You can also send
|
|
83
|
+
a response more than once before rotating, by specifying a <tt>:times</tt>
|
|
84
|
+
option for that response.)
|
|
85
|
+
|
|
86
|
+
FakeWeb.register_uri(:delete, "http://example.com/posts/1",
|
|
87
|
+
[{:body => "Post 1 deleted.", :status => ["200", "OK"]},
|
|
88
|
+
{:body => "Post not found", :status => ["404", "Not Found"]}])
|
|
89
|
+
|
|
90
|
+
Net::HTTP.start("example.com") do |req|
|
|
91
|
+
req.delete("/posts/1").body # => "Post 1 deleted"
|
|
92
|
+
req.delete("/posts/1").body # => "Post not found"
|
|
93
|
+
req.delete("/posts/1").body # => "Post not found"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
=== Using HTTP basic authentication
|
|
97
|
+
|
|
98
|
+
You can stub requests that use basic authentication with +userinfo+ strings in
|
|
99
|
+
the URIs:
|
|
100
|
+
|
|
101
|
+
FakeWeb.register_uri(:get, "http://example.com/secret", :body => "Unauthorized", :status => ["401", "Unauthorized"])
|
|
102
|
+
FakeWeb.register_uri(:get, "http://user:pass@example.com/secret", :body => "Authorized")
|
|
103
|
+
|
|
104
|
+
Net::HTTP.start("example.com") do |http|
|
|
105
|
+
req = Net::HTTP::Get.new("/secret")
|
|
106
|
+
http.request(req) # => "Unauthorized"
|
|
107
|
+
req.basic_auth("user", "pass")
|
|
108
|
+
http.request(req) # => "Authorized"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
=== Clearing registered URIs
|
|
112
|
+
|
|
113
|
+
The FakeWeb registry is a singleton that lasts for the duration of your
|
|
114
|
+
program, maintaining every fake response you register. If needed, you
|
|
115
|
+
can clean out the registry and remove all registered URIs:
|
|
116
|
+
|
|
117
|
+
FakeWeb.clean_registry
|
|
118
|
+
|
|
119
|
+
=== Blocking all real requests
|
|
120
|
+
|
|
121
|
+
When you're using FakeWeb to replace _all_ of your requests, it's useful to
|
|
122
|
+
catch when requests are made for unregistered URIs (unlike the default
|
|
123
|
+
behavior, which is to pass those requests through to Net::HTTP as usual).
|
|
124
|
+
|
|
125
|
+
FakeWeb.allow_net_connect = false
|
|
126
|
+
Net::HTTP.get(URI.parse("http://example.com/"))
|
|
127
|
+
=> raises FakeWeb::NetConnectNotAllowedError
|
|
128
|
+
|
|
129
|
+
FakeWeb.allow_net_connect = true
|
|
130
|
+
Net::HTTP.get(URI.parse("http://example.com/"))
|
|
131
|
+
=> FakeWeb is bypassed and the response from a real request is returned
|
|
132
|
+
|
|
133
|
+
This is handy when you want to make sure your tests are self-contained, or you
|
|
134
|
+
want to catch the scenario when a URI is changed in implementation code
|
|
135
|
+
without a corresponding test change.
|
|
136
|
+
|
|
137
|
+
=== Specifying HTTP response headers
|
|
138
|
+
|
|
139
|
+
When you register a response using the <tt>:body</tt> option, you're only
|
|
140
|
+
setting the body of the response. If you want to add headers to these responses,
|
|
141
|
+
simply add the header as an option to +register_uri+:
|
|
142
|
+
|
|
143
|
+
FakeWeb.register_uri(:get, "http://example.com/hello.txt", :body => "Hello", :content_type => "text/plain")
|
|
144
|
+
|
|
145
|
+
This sets the "Content-Type" header in the response.
|
|
146
|
+
|
|
147
|
+
== More info
|
|
148
|
+
|
|
149
|
+
FakeWeb lets you decouple your test environment from live services without
|
|
150
|
+
modifying code or writing extensive stubs.
|
|
151
|
+
|
|
152
|
+
In addition to the conceptual advantage of having idempotent request
|
|
153
|
+
behaviour, FakeWeb makes tests run faster than if they were made to remote (or
|
|
154
|
+
even local) web servers. It also makes it possible to run tests without a
|
|
155
|
+
network connection or in situations where the server is behind a firewall or
|
|
156
|
+
has host-based access controls.
|
|
157
|
+
|
|
158
|
+
FakeWeb works with anything based on Net::HTTP--both higher-level wrappers,
|
|
159
|
+
like OpenURI, as well as a ton of libraries for popular web services.
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
== Known Issues
|
|
163
|
+
|
|
164
|
+
* Request bodies are ignored, including PUT and POST parameters. If you need
|
|
165
|
+
different responses for different request bodies, you need to request
|
|
166
|
+
different URLs, and register different responses for each. (Query strings are
|
|
167
|
+
fully supported, though.) We're currently considering how the API should
|
|
168
|
+
change to add support for request bodies in 1.3.0. Your input would be really
|
|
169
|
+
helpful: see http://groups.google.com/group/fakeweb-users/browse_thread/thread/44d190a6b12e4273
|
|
170
|
+
for a discussion of some different options. Thanks!
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
== Copyright
|
|
174
|
+
|
|
175
|
+
Copyright 2006-2007 Blaine Cook
|
|
176
|
+
|
|
177
|
+
Copyright 2008-2009 various contributors
|
|
178
|
+
|
|
179
|
+
FakeWeb is free software; you can redistribute it and/or modify it under the
|
|
180
|
+
terms of the GNU General Public License as published by the Free Software
|
|
181
|
+
Foundation; either version 2 of the License, or (at your option) any later
|
|
182
|
+
version.
|
|
183
|
+
|
|
184
|
+
FakeWeb is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
185
|
+
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
186
|
+
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
187
|
+
details.
|
|
188
|
+
|
|
189
|
+
You should have received a copy of the GNU General Public License along
|
|
190
|
+
with FakeWeb; if not, write to the Free Software Foundation, Inc., 51
|
|
191
|
+
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
192
|
+
|
|
193
|
+
See <tt>LICENSE.txt</tt> for the full terms.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# puts "Using ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'rake'
|
|
5
|
+
|
|
6
|
+
# begin
|
|
7
|
+
# require 'jeweler'
|
|
8
|
+
# Jeweler::Tasks.new do |gem|
|
|
9
|
+
# gem.name = "fakeweb"
|
|
10
|
+
# gem.rubyforge_project = "fakeweb"
|
|
11
|
+
# gem.summary = "A tool for faking responses to HTTP requests"
|
|
12
|
+
# gem.description = "FakeWeb is a helper for faking web requests in Ruby. It works at a global level, without modifying code or writing extensive stubs."
|
|
13
|
+
# gem.email = ["chris@kampers.net", "romeda@gmail.com"]
|
|
14
|
+
# gem.authors = ["Chris Kampmeier", "Blaine Cook"]
|
|
15
|
+
# gem.homepage = "http://github.com/chrisk/fakeweb"
|
|
16
|
+
# gem.add_development_dependency "mocha", ">= 0.9.5"
|
|
17
|
+
# end
|
|
18
|
+
# Jeweler::GemcutterTasks.new
|
|
19
|
+
# Jeweler::RubyforgeTasks.new do |rubyforge|
|
|
20
|
+
# rubyforge.doc_task = "rdoc"
|
|
21
|
+
# rubyforge.remote_doc_path = ""
|
|
22
|
+
# end
|
|
23
|
+
# rescue LoadError
|
|
24
|
+
# puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
|
25
|
+
# end
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
require 'rake/testtask'
|
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
|
30
|
+
test.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb", "test/vendor")
|
|
31
|
+
test.verbose = false
|
|
32
|
+
test.warning = true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
task :default => :test
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
begin
|
|
39
|
+
require 'rcov/rcovtask'
|
|
40
|
+
Rcov::RcovTask.new do |t|
|
|
41
|
+
t.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb", "test/vendor")
|
|
42
|
+
t.rcov_opts << "--sort coverage"
|
|
43
|
+
t.rcov_opts << "--exclude gems"
|
|
44
|
+
t.warning = true
|
|
45
|
+
end
|
|
46
|
+
rescue
|
|
47
|
+
print "rcov support disabled "
|
|
48
|
+
if RUBY_PLATFORM =~ /java/
|
|
49
|
+
puts "(running under JRuby)"
|
|
50
|
+
elsif RUBY_VERSION =~ /^1\.9/
|
|
51
|
+
puts "(running under Ruby 1.9)"
|
|
52
|
+
else
|
|
53
|
+
puts "(install RCov to enable the `rcov` task)"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
begin
|
|
59
|
+
require 'rdoc/task'
|
|
60
|
+
Rake::RDocTask.new do |rdoc|
|
|
61
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
|
62
|
+
rdoc.main = "README.rdoc"
|
|
63
|
+
rdoc.rdoc_files.include("README.rdoc", "CHANGELOG", "LICENSE.txt", "lib/*.rb")
|
|
64
|
+
rdoc.title = "FakeWeb #{version} API Documentation"
|
|
65
|
+
rdoc.options << '--line-numbers' << '--charset' << 'utf-8'
|
|
66
|
+
end
|
|
67
|
+
rescue LoadError
|
|
68
|
+
puts "\nIt looks like you're using an old version of RDoc, but FakeWeb requires a newer one."
|
|
69
|
+
puts "You can try upgrading with `sudo gem install rdoc`.\n\n"
|
|
70
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.2.6
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
|
4
|
+
# -*- encoding: utf-8 -*-
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |s|
|
|
7
|
+
s.name = %q{fakeweb}
|
|
8
|
+
s.version = "1.2.6"
|
|
9
|
+
|
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
+
s.authors = ["Chris Kampmeier", "Blaine Cook"]
|
|
12
|
+
s.date = %q{2009-08-31}
|
|
13
|
+
s.description = %q{FakeWeb is a helper for faking web requests in Ruby. It works at a global level, without modifying code or writing extensive stubs.}
|
|
14
|
+
s.email = ["chris@kampers.net", "romeda@gmail.com"]
|
|
15
|
+
s.extra_rdoc_files = [
|
|
16
|
+
"LICENSE.txt",
|
|
17
|
+
"README.rdoc"
|
|
18
|
+
]
|
|
19
|
+
s.files = [
|
|
20
|
+
".gitignore",
|
|
21
|
+
"CHANGELOG",
|
|
22
|
+
"LICENSE.txt",
|
|
23
|
+
"README.rdoc",
|
|
24
|
+
"Rakefile",
|
|
25
|
+
"VERSION",
|
|
26
|
+
"fakeweb.gemspec",
|
|
27
|
+
"lib/fake_web.rb",
|
|
28
|
+
"lib/fake_web/ext/net_http.rb",
|
|
29
|
+
"lib/fake_web/registry.rb",
|
|
30
|
+
"lib/fake_web/responder.rb",
|
|
31
|
+
"lib/fake_web/response.rb",
|
|
32
|
+
"lib/fake_web/stub_socket.rb",
|
|
33
|
+
"lib/fake_web/utility.rb",
|
|
34
|
+
"lib/fakeweb.rb",
|
|
35
|
+
"test/fixtures/google_response_from_curl",
|
|
36
|
+
"test/fixtures/google_response_with_transfer_encoding",
|
|
37
|
+
"test/fixtures/google_response_without_transfer_encoding",
|
|
38
|
+
"test/fixtures/test_example.txt",
|
|
39
|
+
"test/fixtures/test_txt_file",
|
|
40
|
+
"test/test_allow_net_connect.rb",
|
|
41
|
+
"test/test_deprecations.rb",
|
|
42
|
+
"test/test_fake_authentication.rb",
|
|
43
|
+
"test/test_fake_web.rb",
|
|
44
|
+
"test/test_fake_web_open_uri.rb",
|
|
45
|
+
"test/test_helper.rb",
|
|
46
|
+
"test/test_missing_open_uri.rb",
|
|
47
|
+
"test/test_precedence.rb",
|
|
48
|
+
"test/test_query_string.rb",
|
|
49
|
+
"test/test_regexes.rb",
|
|
50
|
+
"test/test_response_headers.rb",
|
|
51
|
+
"test/test_trailing_slashes.rb",
|
|
52
|
+
"test/test_utility.rb"
|
|
53
|
+
]
|
|
54
|
+
s.homepage = %q{http://github.com/chrisk/fakeweb}
|
|
55
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
56
|
+
s.require_paths = ["lib"]
|
|
57
|
+
s.rubyforge_project = %q{fakeweb}
|
|
58
|
+
s.rubygems_version = %q{1.3.5}
|
|
59
|
+
s.summary = %q{A tool for faking responses to HTTP requests}
|
|
60
|
+
s.test_files = [
|
|
61
|
+
"test/test_allow_net_connect.rb",
|
|
62
|
+
"test/test_deprecations.rb",
|
|
63
|
+
"test/test_fake_authentication.rb",
|
|
64
|
+
"test/test_fake_web.rb",
|
|
65
|
+
"test/test_fake_web_open_uri.rb",
|
|
66
|
+
"test/test_helper.rb",
|
|
67
|
+
"test/test_missing_open_uri.rb",
|
|
68
|
+
"test/test_precedence.rb",
|
|
69
|
+
"test/test_query_string.rb",
|
|
70
|
+
"test/test_regexes.rb",
|
|
71
|
+
"test/test_response_headers.rb",
|
|
72
|
+
"test/test_trailing_slashes.rb",
|
|
73
|
+
"test/test_utility.rb"
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
if s.respond_to? :specification_version then
|
|
77
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
78
|
+
s.specification_version = 3
|
|
79
|
+
|
|
80
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
81
|
+
s.add_development_dependency(%q<mocha>, [">= 0.9.5"])
|
|
82
|
+
else
|
|
83
|
+
s.add_dependency(%q<mocha>, [">= 0.9.5"])
|
|
84
|
+
end
|
|
85
|
+
else
|
|
86
|
+
s.add_dependency(%q<mocha>, [">= 0.9.5"])
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
|
|
3
|
+
require "#{File.dirname(__FILE__)}/fake_web/ext/net_http"
|
|
4
|
+
require "#{File.dirname(__FILE__)}/fake_web/registry"
|
|
5
|
+
require "#{File.dirname(__FILE__)}/fake_web/response"
|
|
6
|
+
require "#{File.dirname(__FILE__)}/fake_web/responder"
|
|
7
|
+
require "#{File.dirname(__FILE__)}/fake_web/stub_socket"
|
|
8
|
+
require "#{File.dirname(__FILE__)}/fake_web/utility"
|
|
9
|
+
|
|
10
|
+
FakeWeb::Utility.record_loaded_net_http_replacement_libs
|
|
11
|
+
FakeWeb::Utility.puts_warning_for_net_http_around_advice_libs_if_needed
|
|
12
|
+
|
|
13
|
+
module FakeWeb
|
|
14
|
+
|
|
15
|
+
# Returns the version string for the copy of FakeWeb you have loaded.
|
|
16
|
+
VERSION = File.read(File.join(File.dirname(__FILE__), "..", "VERSION")).strip
|
|
17
|
+
|
|
18
|
+
# Resets the FakeWeb Registry. This will force all subsequent web requests to
|
|
19
|
+
# behave as real requests.
|
|
20
|
+
def self.clean_registry
|
|
21
|
+
Registry.instance.clean_registry
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Enables or disables real HTTP connections for requests that don't match
|
|
25
|
+
# registered URIs.
|
|
26
|
+
#
|
|
27
|
+
# If you set <tt>FakeWeb.allow_net_connect = false</tt> and subsequently try
|
|
28
|
+
# to make a request to a URI you haven't registered with #register_uri, a
|
|
29
|
+
# NetConnectNotAllowedError will be raised. This is handy when you want to
|
|
30
|
+
# make sure your tests are self-contained, or want to catch the scenario
|
|
31
|
+
# when a URI is changed in implementation code without a corresponding test
|
|
32
|
+
# change.
|
|
33
|
+
#
|
|
34
|
+
# When <tt>FakeWeb.allow_net_connect = true</tt> (the default), requests to
|
|
35
|
+
# URIs not stubbed with FakeWeb are passed through to Net::HTTP.
|
|
36
|
+
def self.allow_net_connect=(allowed)
|
|
37
|
+
@allow_net_connect = allowed
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Enable pass-through to Net::HTTP by default.
|
|
41
|
+
self.allow_net_connect = true
|
|
42
|
+
|
|
43
|
+
# Returns +true+ if requests to URIs not registered with FakeWeb are passed
|
|
44
|
+
# through to Net::HTTP for normal processing (the default). Returns +false+
|
|
45
|
+
# if an exception is raised for these requests.
|
|
46
|
+
def self.allow_net_connect?
|
|
47
|
+
@allow_net_connect
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# This exception is raised if you set <tt>FakeWeb.allow_net_connect =
|
|
51
|
+
# false</tt> and subsequently try to make a request to a URI you haven't
|
|
52
|
+
# stubbed.
|
|
53
|
+
class NetConnectNotAllowedError < StandardError; end;
|
|
54
|
+
|
|
55
|
+
# This exception is raised if a Net::HTTP request matches more than one of
|
|
56
|
+
# the stubs you've registered. To fix the problem, remove a duplicate
|
|
57
|
+
# registration or disambiguate any regular expressions by making them more
|
|
58
|
+
# specific.
|
|
59
|
+
class MultipleMatchingURIsError < StandardError; end;
|
|
60
|
+
|
|
61
|
+
# call-seq:
|
|
62
|
+
# FakeWeb.register_uri(method, uri, options)
|
|
63
|
+
#
|
|
64
|
+
# Register requests using the HTTP method specified by the symbol +method+
|
|
65
|
+
# for +uri+ to be handled according to +options+. If you specify the method
|
|
66
|
+
# <tt>:any</tt>, the response will be reigstered for any request for +uri+.
|
|
67
|
+
# +uri+ can be a +String+, +URI+, or +Regexp+ object. +options+ must be either
|
|
68
|
+
# a +Hash+ or an +Array+ of +Hashes+ (see below), which must contain one of
|
|
69
|
+
# these two keys:
|
|
70
|
+
#
|
|
71
|
+
# <tt>:body</tt>::
|
|
72
|
+
# A string which is used as the body of the response. If the string refers
|
|
73
|
+
# to a valid filesystem path, the contents of that file will be read and used
|
|
74
|
+
# as the body of the response instead. (This used to be two options,
|
|
75
|
+
# <tt>:string</tt> and <tt>:file</tt>, respectively. These are now deprecated.)
|
|
76
|
+
# <tt>:response</tt>::
|
|
77
|
+
# Either an <tt>Net::HTTPResponse</tt>, an +IO+, or a +String+ which is used
|
|
78
|
+
# as the full response for the request.
|
|
79
|
+
#
|
|
80
|
+
# The easier way by far is to pass the <tt>:response</tt> option to
|
|
81
|
+
# +register_uri+ as a +String+ or an (open for reads) +IO+ object which
|
|
82
|
+
# will be used as the complete HTTP response, including headers and body.
|
|
83
|
+
# If the string points to a readable file, this file will be used as the
|
|
84
|
+
# content for the request.
|
|
85
|
+
#
|
|
86
|
+
# To obtain a complete response document, you can use the +curl+ command,
|
|
87
|
+
# like so:
|
|
88
|
+
#
|
|
89
|
+
# curl -i http://www.example.com/ > response_for_www.example.com
|
|
90
|
+
#
|
|
91
|
+
# which can then be used in your test environment like so:
|
|
92
|
+
#
|
|
93
|
+
# FakeWeb.register_uri(:get, 'http://www.example.com/', :response => 'response_for_www.example.com')
|
|
94
|
+
#
|
|
95
|
+
# See the <tt>Net::HTTPResponse</tt>
|
|
96
|
+
# documentation[http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html]
|
|
97
|
+
# for more information on creating custom response objects.
|
|
98
|
+
#
|
|
99
|
+
# +options+ may also be an +Array+ containing a list of the above-described
|
|
100
|
+
# +Hash+. In this case, FakeWeb will rotate through each provided response,
|
|
101
|
+
# you may optionally provide:
|
|
102
|
+
#
|
|
103
|
+
# <tt>:times</tt>::
|
|
104
|
+
# The number of times this response will be used. Decremented by one each time it's called.
|
|
105
|
+
# FakeWeb will use the final provided request indefinitely, regardless of its :times parameter.
|
|
106
|
+
#
|
|
107
|
+
# Two optional arguments are also accepted:
|
|
108
|
+
#
|
|
109
|
+
# <tt>:status</tt>::
|
|
110
|
+
# Passing <tt>:status</tt> as a two-value array will set the response code
|
|
111
|
+
# and message. The defaults are <tt>200</tt> and <tt>OK</tt>, respectively.
|
|
112
|
+
# Example:
|
|
113
|
+
# FakeWeb.register_uri("http://www.example.com/", :body => "Go away!", :status => [404, "Not Found"])
|
|
114
|
+
# <tt>:exception</tt>::
|
|
115
|
+
# The argument passed via <tt>:exception</tt> will be raised when the
|
|
116
|
+
# specified URL is requested. Any +Exception+ class is valid. Example:
|
|
117
|
+
# FakeWeb.register_uri('http://www.example.com/', :exception => Net::HTTPError)
|
|
118
|
+
#
|
|
119
|
+
# If you're using the <tt>:body</tt> response type, you can pass additional
|
|
120
|
+
# options to specify the HTTP headers to be used in the response. Example:
|
|
121
|
+
#
|
|
122
|
+
# FakeWeb.register_uri(:get, "http://example.com/index.txt", :body => "Hello", :content_type => "text/plain")
|
|
123
|
+
def self.register_uri(*args)
|
|
124
|
+
case args.length
|
|
125
|
+
when 3
|
|
126
|
+
Registry.instance.register_uri(*args)
|
|
127
|
+
when 2
|
|
128
|
+
print_missing_http_method_deprecation_warning(*args)
|
|
129
|
+
Registry.instance.register_uri(:any, *args)
|
|
130
|
+
else
|
|
131
|
+
raise ArgumentError.new("wrong number of arguments (#{args.length} for 3)")
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# call-seq:
|
|
136
|
+
# FakeWeb.response_for(method, uri)
|
|
137
|
+
#
|
|
138
|
+
# Returns the faked Net::HTTPResponse object associated with +method+ and +uri+.
|
|
139
|
+
def self.response_for(*args, &block) #:nodoc: :yields: response
|
|
140
|
+
case args.length
|
|
141
|
+
when 2
|
|
142
|
+
Registry.instance.response_for(*args, &block)
|
|
143
|
+
when 1
|
|
144
|
+
print_missing_http_method_deprecation_warning(*args)
|
|
145
|
+
Registry.instance.response_for(:any, *args, &block)
|
|
146
|
+
else
|
|
147
|
+
raise ArgumentError.new("wrong number of arguments (#{args.length} for 2)")
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# call-seq:
|
|
152
|
+
# FakeWeb.registered_uri?(method, uri)
|
|
153
|
+
#
|
|
154
|
+
# Returns true if a +method+ request for +uri+ is registered with FakeWeb.
|
|
155
|
+
# Specify a method of <tt>:any</tt> to check for against all HTTP methods.
|
|
156
|
+
def self.registered_uri?(*args)
|
|
157
|
+
case args.length
|
|
158
|
+
when 2
|
|
159
|
+
Registry.instance.registered_uri?(*args)
|
|
160
|
+
when 1
|
|
161
|
+
print_missing_http_method_deprecation_warning(*args)
|
|
162
|
+
Registry.instance.registered_uri?(:any, *args)
|
|
163
|
+
else
|
|
164
|
+
raise ArgumentError.new("wrong number of arguments (#{args.length} for 2)")
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
private
|
|
169
|
+
|
|
170
|
+
def self.print_missing_http_method_deprecation_warning(*args)
|
|
171
|
+
method = caller.first.match(/`(.*?)'/)[1]
|
|
172
|
+
new_args = args.map { |a| a.inspect }.unshift(":any")
|
|
173
|
+
new_args.last.gsub!(/^\{|\}$/, "").gsub!("=>", " => ") if args.last.is_a?(Hash)
|
|
174
|
+
$stderr.puts
|
|
175
|
+
$stderr.puts "Deprecation warning: FakeWeb requires an HTTP method argument (or use :any). Try this:"
|
|
176
|
+
$stderr.puts " FakeWeb.#{method}(#{new_args.join(', ')})"
|
|
177
|
+
$stderr.puts "Called at #{caller[1]}"
|
|
178
|
+
end
|
|
179
|
+
end
|