Module: FakeWeb
- Defined in:
- lib/fake_web.rb,
lib/fake_web/utility.rb,
lib/fake_web/registry.rb,
lib/fake_web/response.rb,
lib/fake_web/responder.rb,
lib/fake_web/stub_socket.rb
Defined Under Namespace
Modules: Response, Utility Classes: MultipleMatchingURIsError, NetConnectNotAllowedError, Registry, Responder, StubSocket
Constant Summary collapse
- VERSION =
Returns the version string for the copy of FakeWeb you have loaded.
'1.3.0'
Class Method Summary collapse
-
.allow_net_connect=(allowed) ⇒ Object
Enables or disables real HTTP connections for requests that don’t match registered URIs.
-
.allow_net_connect?(uri = nil) ⇒ Boolean
Returns
trueif requests to URIs not registered with FakeWeb are passed through to Net::HTTP for normal processing (the default). -
.clean_registry ⇒ Object
Resets the FakeWeb Registry.
-
.last_request ⇒ Object
Returns the request object from the last request made via Net::HTTP.
-
.last_request=(request) ⇒ Object
:nodoc:.
-
.register_uri(*args) ⇒ Object
call-seq: FakeWeb.register_uri(method, uri, options).
-
.registered_uri?(*args) ⇒ Boolean
call-seq: FakeWeb.registered_uri?(method, uri).
-
.response_for(*args, &block) ⇒ Object
call-seq: FakeWeb.response_for(method, uri).
Class Method Details
.allow_net_connect=(allowed) ⇒ Object
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 #register_uri, 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.
If you assign a String, URI, or Regexp object, unstubbed requests will be allowed if they match that value. This is useful when you want to allow access to a local server for integration testing, while still preventing your tests from using the internet.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fake_web.rb', line 41 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 |
.allow_net_connect?(uri = nil) ⇒ Boolean
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.
If you’ve assigned a String, URI, or Regexp to FakeWeb.allow_net_connect=, you must supply a URI to check against that filter. Otherwise, an ArgumentError will be raised.
62 63 64 65 66 67 68 69 |
# File 'lib/fake_web.rb', line 62 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 |
.clean_registry ⇒ Object
Resets the FakeWeb Registry. This will force all subsequent web requests to behave as real requests.
20 21 22 |
# File 'lib/fake_web.rb', line 20 def self.clean_registry Registry.instance.clean_registry end |
.last_request ⇒ Object
Returns the request object from the last request made via Net::HTTP.
196 197 198 |
# File 'lib/fake_web.rb', line 196 def self.last_request @last_request end |
.last_request=(request) ⇒ Object
:nodoc:
200 201 202 |
# File 'lib/fake_web.rb', line 200 def self.last_request=(request) #:nodoc: @last_request = request end |
.register_uri(*args) ⇒ Object
call-seq:
FakeWeb.register_uri(method, uri, )
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,
:stringand:file, respectively. These are now deprecated.) :response-
Either a
Net::HTTPResponse, anIO, or aStringwhich is used as the full response for the request.The easier way by far is to pass the
:responseoption toregister_urias aStringor an (open for reads)IOobject 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
curlcommand, like so:curl -i http://example.com > response_from_example.comwhich can then be used in your test environment like so:
FakeWeb.register_uri(:get, "http://example.com", :response => "response_from_example.com")See the
Net::HTTPResponsedocumentation 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 response. You can optionally repeat a response more than once before rotating:
:times-
The number of times this response will be used before moving on to the next one. The last response will be repeated indefinitely, regardless of its
:timesparameter.
Two optional arguments are also accepted:
:status-
Passing
:statusas a two-value array will set the response code and message. The defaults are200andOK, respectively. Example:FakeWeb.register_uri(:get, "http://example.com", :body => "Go away!", :status => [404, "Not Found"]) :exception-
The argument passed via
:exceptionwill be raised when the specified URL is requested. AnyExceptionclass is valid. Example:FakeWeb.register_uri(:get, "http://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")
You can also pass an array of header values to include a header in the response more than once:
FakeWeb.register_uri(:get, "http://example.com", :set_cookie => ["name=value", "example=1"])
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/fake_web.rb', line 150 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 |
.registered_uri?(*args) ⇒ Boolean
call-seq:
FakeWeb.registered_uri?(method, uri)
Returns true if a method request for uri is registered with FakeWeb. Specify a method of :any to check against all HTTP methods.
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/fake_web.rb', line 183 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 |
.response_for(*args, &block) ⇒ Object
call-seq:
FakeWeb.response_for(method, uri)
Returns the faked Net::HTTPResponse object associated with method and uri.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/fake_web.rb', line 166 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 |