corntrace-fakeweb 1.2.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.gitignore +9 -0
  2. data/CHANGELOG +197 -0
  3. data/LICENSE.txt +281 -0
  4. data/README.rdoc +222 -0
  5. data/Rakefile +70 -0
  6. data/fakeweb.gemspec +123 -0
  7. data/lib/fake_web.rb +179 -0
  8. data/lib/fake_web/VERSION +1 -0
  9. data/lib/fake_web/ext/net_http.rb +81 -0
  10. data/lib/fake_web/registry.rb +114 -0
  11. data/lib/fake_web/responder.rb +120 -0
  12. data/lib/fake_web/response.rb +10 -0
  13. data/lib/fake_web/stub_socket.rb +15 -0
  14. data/lib/fake_web/utility.rb +65 -0
  15. data/lib/fakeweb.rb +2 -0
  16. data/test/fixtures/google_response_from_curl +12 -0
  17. data/test/fixtures/google_response_with_transfer_encoding +17 -0
  18. data/test/fixtures/google_response_without_transfer_encoding +11 -0
  19. data/test/fixtures/test_example.txt +1 -0
  20. data/test/fixtures/test_txt_file +3 -0
  21. data/test/test_allow_net_connect.rb +85 -0
  22. data/test/test_deprecations.rb +54 -0
  23. data/test/test_fake_authentication.rb +92 -0
  24. data/test/test_fake_web.rb +553 -0
  25. data/test/test_fake_web_open_uri.rb +58 -0
  26. data/test/test_helper.rb +87 -0
  27. data/test/test_missing_open_uri.rb +25 -0
  28. data/test/test_missing_pathname.rb +37 -0
  29. data/test/test_other_net_http_libraries.rb +36 -0
  30. data/test/test_precedence.rb +79 -0
  31. data/test/test_query_string.rb +45 -0
  32. data/test/test_regexes.rb +157 -0
  33. data/test/test_response_headers.rb +73 -0
  34. data/test/test_trailing_slashes.rb +53 -0
  35. data/test/test_utility.rb +76 -0
  36. data/test/vendor/right_http_connection-1.2.4/History.txt +59 -0
  37. data/test/vendor/right_http_connection-1.2.4/Manifest.txt +7 -0
  38. data/test/vendor/right_http_connection-1.2.4/README.txt +54 -0
  39. data/test/vendor/right_http_connection-1.2.4/Rakefile +103 -0
  40. data/test/vendor/right_http_connection-1.2.4/lib/net_fix.rb +160 -0
  41. data/test/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb +435 -0
  42. data/test/vendor/right_http_connection-1.2.4/setup.rb +1585 -0
  43. data/test/vendor/samuel-0.2.1/.document +5 -0
  44. data/test/vendor/samuel-0.2.1/.gitignore +5 -0
  45. data/test/vendor/samuel-0.2.1/LICENSE +20 -0
  46. data/test/vendor/samuel-0.2.1/README.rdoc +70 -0
  47. data/test/vendor/samuel-0.2.1/Rakefile +62 -0
  48. data/test/vendor/samuel-0.2.1/VERSION +1 -0
  49. data/test/vendor/samuel-0.2.1/lib/samuel.rb +52 -0
  50. data/test/vendor/samuel-0.2.1/lib/samuel/net_http.rb +10 -0
  51. data/test/vendor/samuel-0.2.1/lib/samuel/request.rb +96 -0
  52. data/test/vendor/samuel-0.2.1/samuel.gemspec +69 -0
  53. data/test/vendor/samuel-0.2.1/test/request_test.rb +193 -0
  54. data/test/vendor/samuel-0.2.1/test/samuel_test.rb +42 -0
  55. data/test/vendor/samuel-0.2.1/test/test_helper.rb +66 -0
  56. data/test/vendor/samuel-0.2.1/test/thread_test.rb +32 -0
  57. metadata +167 -0
@@ -0,0 +1,222 @@
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
+ gem install fakeweb
10
+
11
+ Note: the gem was previously available as +FakeWeb+ (capital letters), but now
12
+ all versions are simply registered as +fakeweb+. If you have any old +FakeWeb+
13
+ gems lying around, remove them: <tt>gem uninstall FakeWeb</tt>
14
+
15
+
16
+ == Help and discussion
17
+
18
+ RDocs for the current release are available at http://fakeweb.rubyforge.org.
19
+
20
+ There's a mailing list for questions and discussion at
21
+ http://groups.google.com/group/fakeweb-users.
22
+
23
+ The main source repository is http://github.com/chrisk/fakeweb.
24
+
25
+ == Examples
26
+
27
+ Start by requiring FakeWeb:
28
+
29
+ require 'fakeweb'
30
+
31
+ === Registering basic string responses
32
+
33
+ FakeWeb.register_uri(:get, "http://example.com/test1", :body => "Hello World!")
34
+
35
+ Net::HTTP.get(URI.parse("http://example.com/test1"))
36
+ => "Hello World!"
37
+
38
+ Net::HTTP.get(URI.parse("http://example.com/test2"))
39
+ => FakeWeb is bypassed and the response from a real request is returned
40
+
41
+ === Registering erb template responses
42
+
43
+ @somebody = "Kevin"
44
+ FakeWeb.register_uri(:get, "http://example.com/test1", :body => "Hello <%= @somebody%>!")
45
+
46
+ Net::HTTP.get(URI.parse("http://example.com/test1"))
47
+ => "Hello Kevin!"
48
+
49
+ You can also call <tt>register_uri</tt> with a regular expression, to match
50
+ more than one URI.
51
+
52
+ FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "Hello World!")
53
+
54
+ Net::HTTP.get(URI.parse("http://example.com/test3"))
55
+ => "Hello World!"
56
+
57
+ And the match captures could be used in erb template.
58
+
59
+ FakeWeb.register_uri(:get, %r|http://example\.com/users/(\d+)\.json|, :body => %Q|{"id":<%=$1%>, "username":"user<%=$!%>"}|)
60
+
61
+ Net::HTTP.get(URI.parse("http://example.com/users/3.json"))
62
+ => '{"id":3, "username":"user3"}'
63
+
64
+ The :body's value could be a path of a file which stores the body content.
65
+
66
+ /path/to/body/content.text.erb:
67
+ Lorem ipsum dolor sit amet for user <%= $1%>
68
+
69
+ FakeWeb.register_uri(:get, %r|http://example\.com/users/(\d+)\.json|, :body => "/path/to/body/content.text.erb")
70
+
71
+ Net::HTTP.get(URI.parse("http://example.com/users/3.json"))
72
+ => "Lorem ipsum dolor sit amet for user 3"
73
+
74
+ === Replaying a recorded response
75
+
76
+ page = `curl -is http://www.google.com/`
77
+ FakeWeb.register_uri(:get, "http://www.google.com/", :response => page)
78
+
79
+ Net::HTTP.get(URI.parse("http://www.google.com/"))
80
+ # => Full response, including headers
81
+
82
+ The :response's value could also be a path of file which stores the entire response from the server. ERB template and
83
+ regular expression matching capture are supported too.
84
+
85
+ === Adding a custom status to the response
86
+
87
+ FakeWeb.register_uri(:get, "http://example.com/", :body => "Nothing to be found 'round here",
88
+ :status => ["404", "Not Found"])
89
+
90
+ Net::HTTP.start("example.com") do |req|
91
+ response = req.get("/")
92
+ response.code # => "404"
93
+ response.message # => "Not Found"
94
+ response.body # => "Nothing to be found 'round here"
95
+ end
96
+
97
+ === Responding to any HTTP method
98
+
99
+ FakeWeb.register_uri(:any, "http://example.com", :body => "response for any HTTP method")
100
+
101
+ If you use the <tt>:any</tt> symbol, the URI you specify will be completely
102
+ stubbed out (regardless of the HTTP method of the request). This can be useful
103
+ for RPC-style services, where the HTTP method isn't significant. (Older
104
+ versions of FakeWeb always behaved like this, and didn't accept the first
105
+ +method+ argument above; this syntax is now deprecated.)
106
+
107
+ === Rotating responses
108
+
109
+ You can optionally call <tt>FakeWeb.register_uri</tt> with an array of options
110
+ hashes; these are used, in order, to respond to repeated requests. Once you run
111
+ out of responses, further requests always receive the last response. (You can
112
+ also send a response more than once before rotating, by specifying a
113
+ <tt>:times</tt> option for that response.)
114
+
115
+ FakeWeb.register_uri(:delete, "http://example.com/posts/1",
116
+ [{:body => "Post 1 deleted.", :status => ["200", "OK"]},
117
+ {:body => "Post not found", :status => ["404", "Not Found"]}])
118
+
119
+ Net::HTTP.start("example.com") do |req|
120
+ req.delete("/posts/1").body # => "Post 1 deleted"
121
+ req.delete("/posts/1").body # => "Post not found"
122
+ req.delete("/posts/1").body # => "Post not found"
123
+ end
124
+
125
+ === Using HTTP basic authentication
126
+
127
+ You can fake requests that use basic authentication by adding +userinfo+ strings
128
+ to your URIs:
129
+
130
+ FakeWeb.register_uri(:get, "http://example.com/secret", :body => "Unauthorized", :status => ["401", "Unauthorized"])
131
+ FakeWeb.register_uri(:get, "http://user:pass@example.com/secret", :body => "Authorized")
132
+
133
+ Net::HTTP.start("example.com") do |http|
134
+ req = Net::HTTP::Get.new("/secret")
135
+ http.request(req) # => "Unauthorized"
136
+ req.basic_auth("user", "pass")
137
+ http.request(req) # => "Authorized"
138
+ end
139
+
140
+ === Clearing registered URIs
141
+
142
+ The FakeWeb registry is a singleton that lasts for the duration of your program,
143
+ maintaining every fake response you register. If needed, you can clean out the
144
+ registry and remove all registered URIs:
145
+
146
+ FakeWeb.clean_registry
147
+
148
+ === Blocking all real requests
149
+
150
+ When you're using FakeWeb to replace _all_ of your requests, it's useful to
151
+ catch when requests are made for unregistered URIs (unlike the default
152
+ behavior, which is to pass those requests through to Net::HTTP as usual).
153
+
154
+ FakeWeb.allow_net_connect = false
155
+ Net::HTTP.get(URI.parse("http://example.com/"))
156
+ => raises FakeWeb::NetConnectNotAllowedError
157
+
158
+ FakeWeb.allow_net_connect = true
159
+ Net::HTTP.get(URI.parse("http://example.com/"))
160
+ => FakeWeb is bypassed and the response from a real request is returned
161
+
162
+ This is handy when you want to make sure your tests are self-contained, or you
163
+ want to catch the scenario when a URI is changed in implementation code
164
+ without a corresponding test change.
165
+
166
+ === Specifying HTTP response headers
167
+
168
+ When you register a response using the <tt>:body</tt> option, you're only
169
+ setting the body of the response. If you want to add headers to these responses,
170
+ simply add the header as an option to +register_uri+:
171
+
172
+ FakeWeb.register_uri(:get, "http://example.com/hello.txt", :body => "Hello", :content_type => "text/plain")
173
+
174
+ This sets the "Content-Type" header in the response.
175
+
176
+ == More info
177
+
178
+ FakeWeb lets you decouple your test environment from live services without
179
+ modifying code or writing extensive stubs.
180
+
181
+ In addition to the conceptual advantage of having idempotent request
182
+ behaviour, FakeWeb makes tests run faster than if they were made to remote (or
183
+ even local) web servers. It also makes it possible to run tests without a
184
+ network connection or in situations where the server is behind a firewall or
185
+ has host-based access controls.
186
+
187
+ FakeWeb works with anything based on Net::HTTP--both higher-level wrappers,
188
+ like OpenURI, as well as a ton of libraries for popular web services.
189
+
190
+
191
+ == Known Issues
192
+
193
+ * Request bodies are ignored, including PUT and POST parameters. If you need
194
+ different responses for different request bodies, you need to request
195
+ different URLs, and register different responses for each. (Query strings are
196
+ fully supported, though.) We're currently considering how the API should
197
+ change to add support for request bodies in 1.3.0. Your input would be really
198
+ helpful: see http://groups.google.com/group/fakeweb-users/browse_thread/thread/44d190a6b12e4273
199
+ for a discussion of some different options. Thanks!
200
+
201
+
202
+ == Copyright
203
+
204
+ Copyright 2006-2007 Blaine Cook
205
+
206
+ Copyright 2008-2009 various contributors
207
+
208
+ FakeWeb is free software; you can redistribute it and/or modify it under the
209
+ terms of the GNU General Public License as published by the Free Software
210
+ Foundation; either version 2 of the License, or (at your option) any later
211
+ version.
212
+
213
+ FakeWeb is distributed in the hope that it will be useful, but WITHOUT ANY
214
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
215
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
216
+ details.
217
+
218
+ You should have received a copy of the GNU General Public License along
219
+ with FakeWeb; if not, write to the Free Software Foundation, Inc., 51
220
+ Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
221
+
222
+ See <tt>LICENSE.txt</tt> for the full terms.
@@ -0,0 +1,70 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ version = File.read("lib/fake_web/VERSION").strip
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "fakeweb"
10
+ gem.rubyforge_project = "fakeweb"
11
+ gem.version = version
12
+ gem.summary = "A tool for faking responses to HTTP requests"
13
+ 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."
14
+ gem.email = ["chris@kampers.net", "romeda@gmail.com"]
15
+ gem.authors = ["Chris Kampmeier", "Blaine Cook"]
16
+ gem.homepage = "http://github.com/chrisk/fakeweb"
17
+ gem.add_development_dependency "mocha", ">= 0.9.5"
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ Jeweler::RubyforgeTasks.new do |rubyforge|
21
+ rubyforge.doc_task = "rdoc"
22
+ rubyforge.remote_doc_path = ""
23
+ end
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
+ end
27
+
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb", "test/vendor")
32
+ test.libs << "test"
33
+ test.verbose = false
34
+ test.warning = true
35
+ end
36
+
37
+ task :default => [:check_dependencies, :test]
38
+
39
+
40
+ begin
41
+ require 'rcov/rcovtask'
42
+ Rcov::RcovTask.new do |t|
43
+ t.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb", "test/vendor")
44
+ t.libs << "test"
45
+ t.rcov_opts << "--sort coverage"
46
+ t.rcov_opts << "--exclude gems"
47
+ t.warning = true
48
+ end
49
+ rescue LoadError
50
+ print "rcov support disabled "
51
+ if RUBY_PLATFORM =~ /java/
52
+ puts "(running under JRuby)"
53
+ else
54
+ puts "(install RCov to enable the `rcov` task)"
55
+ end
56
+ end
57
+
58
+
59
+ begin
60
+ require 'rdoc/task'
61
+ Rake::RDocTask.new do |rdoc|
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 `gem install rdoc`.\n\n"
70
+ end
@@ -0,0 +1,123 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{corntrace-fakeweb}
8
+ s.version = "1.2.9"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kevin Fu", "Chris Kampmeier", "Blaine Cook"]
12
+ s.date = %q{2010-07-13}
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.\nI added ERB template feature and regular expression matching capture feature for :body and :response options}
14
+ s.email = ["corntrace@gmail.com", "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
+ "fakeweb.gemspec",
26
+ "lib/fake_web.rb",
27
+ "lib/fake_web/VERSION",
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_missing_pathname.rb",
48
+ "test/test_other_net_http_libraries.rb",
49
+ "test/test_precedence.rb",
50
+ "test/test_query_string.rb",
51
+ "test/test_regexes.rb",
52
+ "test/test_response_headers.rb",
53
+ "test/test_trailing_slashes.rb",
54
+ "test/test_utility.rb",
55
+ "test/vendor/right_http_connection-1.2.4/History.txt",
56
+ "test/vendor/right_http_connection-1.2.4/Manifest.txt",
57
+ "test/vendor/right_http_connection-1.2.4/README.txt",
58
+ "test/vendor/right_http_connection-1.2.4/Rakefile",
59
+ "test/vendor/right_http_connection-1.2.4/lib/net_fix.rb",
60
+ "test/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb",
61
+ "test/vendor/right_http_connection-1.2.4/setup.rb",
62
+ "test/vendor/samuel-0.2.1/.document",
63
+ "test/vendor/samuel-0.2.1/.gitignore",
64
+ "test/vendor/samuel-0.2.1/LICENSE",
65
+ "test/vendor/samuel-0.2.1/README.rdoc",
66
+ "test/vendor/samuel-0.2.1/Rakefile",
67
+ "test/vendor/samuel-0.2.1/VERSION",
68
+ "test/vendor/samuel-0.2.1/lib/samuel.rb",
69
+ "test/vendor/samuel-0.2.1/lib/samuel/net_http.rb",
70
+ "test/vendor/samuel-0.2.1/lib/samuel/request.rb",
71
+ "test/vendor/samuel-0.2.1/samuel.gemspec",
72
+ "test/vendor/samuel-0.2.1/test/request_test.rb",
73
+ "test/vendor/samuel-0.2.1/test/samuel_test.rb",
74
+ "test/vendor/samuel-0.2.1/test/test_helper.rb",
75
+ "test/vendor/samuel-0.2.1/test/thread_test.rb"
76
+ ]
77
+ s.homepage = %q{http://github.com/corntrace/fakeweb}
78
+ s.rdoc_options = ["--charset=UTF-8"]
79
+ s.require_paths = ["lib"]
80
+ s.rubygems_version = %q{1.3.5}
81
+ s.summary = %q{A tool for faking responses to HTTP requests}
82
+ s.test_files = [
83
+ "test/test_allow_net_connect.rb",
84
+ "test/test_deprecations.rb",
85
+ "test/test_fake_authentication.rb",
86
+ "test/test_fake_web.rb",
87
+ "test/test_fake_web_open_uri.rb",
88
+ "test/test_helper.rb",
89
+ "test/test_missing_open_uri.rb",
90
+ "test/test_missing_pathname.rb",
91
+ "test/test_other_net_http_libraries.rb",
92
+ "test/test_precedence.rb",
93
+ "test/test_query_string.rb",
94
+ "test/test_regexes.rb",
95
+ "test/test_response_headers.rb",
96
+ "test/test_trailing_slashes.rb",
97
+ "test/test_utility.rb",
98
+ "test/vendor/right_http_connection-1.2.4/lib/net_fix.rb",
99
+ "test/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb",
100
+ "test/vendor/right_http_connection-1.2.4/setup.rb",
101
+ "test/vendor/samuel-0.2.1/lib/samuel/net_http.rb",
102
+ "test/vendor/samuel-0.2.1/lib/samuel/request.rb",
103
+ "test/vendor/samuel-0.2.1/lib/samuel.rb",
104
+ "test/vendor/samuel-0.2.1/test/request_test.rb",
105
+ "test/vendor/samuel-0.2.1/test/samuel_test.rb",
106
+ "test/vendor/samuel-0.2.1/test/test_helper.rb",
107
+ "test/vendor/samuel-0.2.1/test/thread_test.rb"
108
+ ]
109
+
110
+ if s.respond_to? :specification_version then
111
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
112
+ s.specification_version = 3
113
+
114
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
115
+ s.add_development_dependency(%q<mocha>, [">= 0.9.5"])
116
+ else
117
+ s.add_dependency(%q<mocha>, [">= 0.9.5"])
118
+ end
119
+ else
120
+ s.add_dependency(%q<mocha>, [">= 0.9.5"])
121
+ end
122
+ end
123
+
@@ -0,0 +1,179 @@
1
+ require 'singleton'
2
+
3
+ require 'fake_web/ext/net_http'
4
+ require 'fake_web/registry'
5
+ require 'fake_web/response'
6
+ require 'fake_web/responder'
7
+ require 'fake_web/stub_socket'
8
+ require '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.dirname(__FILE__)+"/fake_web/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