Module: FakeWeb
- Defined in:
- lib/fake_web.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 Classes: NetConnectNotAllowedError, Registry, Responder, StubSocket
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? ⇒ Boolean
Returns
true
if 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.
-
.register_uri(*args) ⇒ Object
call-seq: FakeWeb.register_uri(method, uri, options) FakeWeb.register_uri(uri, options).
-
.registered_uri?(*args) ⇒ Boolean
call-seq: FakeWeb.registered_uri?(method, uri) FakeWeb.registered_uri?(uri).
-
.response_for(*args, &block) ⇒ Object
call-seq: FakeWeb.response_for(method, uri) FakeWeb.response_for(uri).
-
.unregister_uri(*args) ⇒ Object
call-seq: FakeWeb.unregister_uri(method, uri) FakeWeb.unregister_uri(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.
29 30 31 |
# File 'lib/fake_web.rb', line 29 def self.allow_net_connect=(allowed) @allow_net_connect = allowed end |
.allow_net_connect? ⇒ 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.
39 40 41 |
# File 'lib/fake_web.rb', line 39 def self.allow_net_connect? @allow_net_connect end |
.clean_registry ⇒ Object
Resets the FakeWeb Registry. This will force all subsequent web requests to behave as real requests.
13 14 15 |
# File 'lib/fake_web.rb', line 13 def self.clean_registry Registry.instance.clean_registry end |
.register_uri(*args) ⇒ Object
call-seq:
FakeWeb.register_uri(method, uri, )
FakeWeb.register_uri(uri, )
Register requests using the HTTP method specified by the symbol method
for uri
to be handled according to options
. If no method
is specified, or you explicitly specify :any
, the response will be reigstered for any request for uri
. uri
can be a String
or a URI
object. options
must be either a Hash
or an Array
of Hashes
(see below) that must contain any one of the following keys:
:string
-
Takes a
String
argument that is returned as the body of the response.FakeWeb.register_uri(:get, 'http://example.com/', :string => "Hello World!")
:file
-
Takes a valid filesystem path to a file that is slurped and returned as the body of the response.
FakeWeb.register_uri(:post, 'http://example.com/', :file => "/tmp/my_response_body.txt")
:response
-
Either an
Net::HTTPResponse
, anIO
or aString
.The easier way by far is to pass the
:response
option toregister_uri
as aString
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
Net::HTTPResponse
documentation 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 are200
andOK
, respectively. Example:FakeWeb.register_uri('http://www.example.com/', :response => "Go away!", :status => [ 404, "Not Found" ])
:exception
-
The argument passed via
:exception
will be raised when the specified URL is requested. AnyException
class is valid. Example:FakeWeb.register_uri('http://www.example.com/', :exception => Net::HTTPError)
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/fake_web.rb', line 108 def self.register_uri(*args) method = :any case args.length when 3 then method, uri, = *args when 2 then uri, = *args else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri, options)") end Registry.instance.register_uri(method, uri, ) end |
.registered_uri?(*args) ⇒ Boolean
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/fake_web.rb', line 165 def self.registered_uri?(*args) method = :any case args.length when 2 then method, uri = args when 1 then uri = args.first else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri)") end Registry.instance.registered_uri?(method, uri) end |
.response_for(*args, &block) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/fake_web.rb', line 147 def self.response_for(*args, &block) #:nodoc: :yields: response method = :any case args.length when 2 then method, uri = args when 1 then uri = args.first else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri)") end Registry.instance.response_for(method, uri, &block) end |
.unregister_uri(*args) ⇒ Object
call-seq:
FakeWeb.unregister_uri(method, uri)
FakeWeb.unregister_uri(uri)
Unregister existing URIs with the given symbol ‘method` and `uri`. If you do not specify a `method` it will default to `:any` which will unregister any and all registered actions for the specified URI.
Returns ‘true` if an existing URI is found, `false` if it wasn’t registered to begin with.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/fake_web.rb', line 131 def self.unregister_uri(*args) method = :any case args.length when 2 then method, uri = *args when 1 then uri = *args else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri)") end Registry.instance.unregister_uri(method, uri) end |