Enables or disables real HTTP connections for requests that don’t match registered URIs.
If you set FakeWeb.allow_net_connect = false and subsequently try to make a request to a URI you haven’t registered with #, a NetConnectNotAllowedError will be raised. This is handy when you want to make sure your tests are self-contained, or want to catch the scenario when a URI is changed in implementation code without a corresponding test change.
When FakeWeb.allow_net_connect = true (the default), requests to URIs not stubbed with FakeWeb are passed through to Net::HTTP.
# File lib/fake_web.rb, line 36 36: def self.allow_net_connect=(allowed) 37: @allow_net_connect = allowed 38: end
Returns true if requests to URIs not registered with FakeWeb are passed through to Net::HTTP for normal processing (the default). Returns false if an exception is raised for these requests.
# File lib/fake_web.rb, line 46 46: def self.allow_net_connect? 47: @allow_net_connect 48: end
Resets the FakeWeb Registry. This will force all subsequent web requests to behave as real requests.
# File lib/fake_web.rb, line 20 20: def self.clean_registry 21: Registry.instance.clean_registry 22: end
Register requests using the HTTP method specified by the symbol method for uri to be handled according to options. If you specify the method :any, the response will be reigstered for any request for uri. uri can be a String, URI, or Regexp object. options must be either a Hash or an Array of Hashes (see below), which must contain one of these two keys:
:body | A string which is used as the body of the response. If the string refers to a valid filesystem path, the contents of that file will be read and used as the body of the response instead. (This used to be two options, :string and :file, respectively. These are now deprecated.) |
:response | Either an Net::HTTPResponse, an IO, or a String which is used as the full response for the request. The easier way by far is to pass the :response option to register_uri as a String or an (open for reads) IO object which will be used as the complete HTTP response, including headers and body. If the string points to a readable file, this file will be used as the content for the request. To obtain a complete response document, you can use the curl command, like so: |
curl -i http://www.example.com/ > response_for_www.example.com which can then be used in your test environment like so: FakeWeb.register_uri(:get, 'http://www.example.com/', :response => 'response_for_www.example.com') See the <tt>Net::HTTPResponse</tt> documentation[http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html] for more information on creating custom response objects.
options may also be an Array containing a list of the above-described Hash. In this case, FakeWeb will rotate through each provided response, you may optionally provide:
:times | The number of times this response will be used. Decremented by one each time it’s called. FakeWeb will use the final provided request indefinitely, regardless of its :times parameter. |
Two optional arguments are also accepted:
:status | Passing :status as a two-value array will set the response code and message. The defaults are 200 and OK, respectively. Example: FakeWeb.register_uri("http://www.example.com/", :body => "Go away!", :status => [404, "Not Found"]) |
:exception | The argument passed via :exception will be raised when the specified URL is requested. Any Exception class is valid. Example: FakeWeb.register_uri('http://www.example.com/', :exception => Net::HTTPError) |
If you’re using the :body response type, you can pass additional options to specify the HTTP headers to be used in the response. Example:
FakeWeb.register_uri(:get, "http://example.com/index.txt", :body => "Hello", :content_type => "text/plain")
# File lib/fake_web.rb, line 123 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
Returns true if a method request for uri is registered with FakeWeb. Specify a method of :any to check for against all HTTP methods.
# File lib/fake_web.rb, line 156 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
# File lib/fake_web.rb, line 170 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
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.