module FakeWeb
def self.allow_net_connect=(allowed)
allow access to a local server for integration testing, while still
will be allowed if they match that value. This is useful when you want to
If you assign a +String+, +URI+, or +Regexp+ object, unstubbed requests
URIs not stubbed with FakeWeb are passed through to Net::HTTP.
When FakeWeb.allow_net_connect = true (the default), requests to
change.
when a URI is changed in implementation code without a corresponding test
make sure your tests are self-contained, or want to catch the scenario
NetConnectNotAllowedError will be raised. This is handy when you want to
to make a request to a URI you haven't registered with #register_uri, a
If you set FakeWeb.allow_net_connect = false and subsequently try
registered URIs.
Enables or disables real HTTP connections for requests that don't match
def self.allow_net_connect=(allowed) case allowed when String, URI, Regexp @allow_all_connections = false Registry.instance.register_passthrough_uri(allowed) else @allow_all_connections = allowed Registry.instance.remove_passthrough_uri end end
def self.allow_net_connect?(uri = nil)
FakeWeb.allow_net_connect=, you must supply a URI to check
If you've assigned a +String+, +URI+, or +Regexp+ to
if an exception is raised for these requests.
through to Net::HTTP for normal processing (the default). Returns +false+
Returns +true+ if requests to URIs not registered with FakeWeb are passed
def self.allow_net_connect?(uri = nil) if Registry.instance.passthrough_uri_map.any? raise ArgumentError, "You must supply a URI to test" if uri.nil? Registry.instance.passthrough_uri_matches?(uri) else @allow_all_connections end end
def self.clean_registry
Resets the FakeWeb Registry. This will force all subsequent web requests to
def self.clean_registry Registry.instance.clean_registry end
def self.last_request
def self.last_request @last_request end
def self.last_request=(request) #:nodoc:
def self.last_request=(request) #:nodoc: @last_request = request end
def self.print_missing_http_method_deprecation_warning(*args)
def self.print_missing_http_method_deprecation_warning(*args) method = caller.first.match(/`(.*?)'/)[1] new_args = args.map { |a| a.inspect }.unshift(":any") new_args.last.gsub!(/^\{|\}$/, "").gsub!("=>", " => ") if args.last.is_a?(Hash) $stderr.puts $stderr.puts "Deprecation warning: FakeWeb requires an HTTP method argument (or use :any). Try this:" $stderr.puts " FakeWeb.#{method}(#{new_args.join(', ')})" $stderr.puts "Called at #{caller[1]}" end
def self.register_uri(*args)
response more than once:
You can also pass an array of header values to include a header in the
FakeWeb.register_uri(:get, "http://example.com/index.txt", :body => "Hello", :content_type => "text/plain")
options to specify the HTTP headers to be used in the response. Example:
If you're using the :body response type, you can pass additional
FakeWeb.register_uri(:get, "http://example.com", :exception => Net::HTTPError)
specified URL is requested. Any +Exception+ class is valid. Example:
The argument passed via :exception will be raised when the
:exception::
FakeWeb.register_uri(:get, "http://example.com", :body => "Go away!", :status => [404, "Not Found"])
Example:
and message. The defaults are 200 and OK, respectively.
Passing :status as a two-value array will set the response code
:status::
Two optional arguments are also accepted:
its :times parameter.
next one. The last response will be repeated indefinitely, regardless of
The number of times this response will be used before moving on to the
:times::
optionally repeat a response more than once before rotating:
+Hash+. In this case, FakeWeb will rotate through each response. You can
+options+ may also be an +Array+ containing a list of the above-described
for more information on creating custom response objects.
documentation[http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html]
See the Net::HTTPResponse
FakeWeb.register_uri(:get, "http://example.com", :response => "response_from_example.com")
which can then be used in your test environment like so:
curl -i http://example.com > response_from_example.com
like so:
To obtain a complete response document, you can use the +curl+ command,
content for the request.
If the string points to a readable file, this file will be used as the
will be used as the complete HTTP response, including headers and body.
+register_uri+ as a +String+ or an (open for reads) +IO+ object which
The easier way by far is to pass the :response option to
as the full response for the request.
Either a Net::HTTPResponse, an +IO+, or a +String+ which is used
:response::
:string and :file, respectively. These are now deprecated.)
as the body of the response instead. (This used to be two options,
to a valid filesystem path, the contents of that file will be read and used
A string which is used as the body of the response. If the string refers
:body::
these two keys:
a +Hash+ or an +Array+ of +Hashes+ (see below), which must contain one of
+uri+ can be a +String+, +URI+, or +Regexp+ object. +options+ must be either
:any, the response will be reigstered for any request for +uri+.
for +uri+ to be handled according to +options+. If you specify the method
Register requests using the HTTP method specified by the symbol +method+
FakeWeb.register_uri(method, uri, options)
call-seq:
def self.register_uri(*args) case args.length when 3 Registry.instance.register_uri(*args) when 2 print_missing_http_method_deprecation_warning(*args) Registry.instance.register_uri(:any, *args) else raise ArgumentError.new("wrong number of arguments (#{args.length} for 3)") end end
def self.registered_uri?(*args)
Returns true if a +method+ request for +uri+ is registered with FakeWeb.
FakeWeb.registered_uri?(method, uri)
call-seq:
def self.registered_uri?(*args) case args.length when 2 Registry.instance.registered_uri?(*args) when 1 print_missing_http_method_deprecation_warning(*args) Registry.instance.registered_uri?(:any, *args) else raise ArgumentError.new("wrong number of arguments (#{args.length} for 2)") end end
def self.response_for(*args, &block) #:nodoc: :yields: response
Returns the faked Net::HTTPResponse object associated with +method+ and +uri+.
FakeWeb.response_for(method, uri)
call-seq:
def self.response_for(*args, &block) #:nodoc: :yields: response case args.length when 2 Registry.instance.response_for(*args, &block) when 1 print_missing_http_method_deprecation_warning(*args) Registry.instance.response_for(:any, *args, &block) else raise ArgumentError.new("wrong number of arguments (#{args.length} for 2)") end end