public
Fork of chrisk/fakeweb
Description: Fork of Blaine Cook's FakeWeb for fixes and new features
Homepage: http://fakeweb.rubyforge.org
Clone URL: git://github.com/technicalpickles/fakeweb.git
name age message
file .gitignore Sun Dec 21 16:24:01 -0800 2008 Ignore coverage directory [chrisk]
file .manifest Sun Jan 04 17:45:07 -0800 2009 Updated manifest. [technicalpickles]
file CHANGELOG Sat Dec 27 23:30:22 -0800 2008 Update CHANGELOG [chrisk]
file LICENSE.txt Thu Jan 01 14:03:07 -0800 2009 DRY up license/copyright info [chrisk]
file README.rdoc Sat Jan 03 16:42:03 -0800 2009 Update the README to reflect new HTTP method su... [chrisk]
file Rakefile Thu Jan 01 14:03:07 -0800 2009 DRY up license/copyright info [chrisk]
file fakeweb.gemspec Sun Jan 04 17:45:07 -0800 2009 Updated manifest. [technicalpickles]
directory lib/ Sun Jan 04 17:41:11 -0800 2009 Removed OpenURI::HTTPError since it's part of s... [technicalpickles]
directory test/ Fri Jan 02 19:27:29 -0800 2009 Add HTTP method support throughout FakeWeb Now... [chrisk]
README.rdoc
= FakeWeb

FakeWeb is a helper for faking web requests in Ruby. It works at a global
level, without modifying code or writing extensive stubs.

== Installation

This fork of Blaine Cook's original code has lots of fixes, stability
improvements, and a few new features. To get it, install the latest gem
directly from GitHub (currently 1.1.2.6):

  sudo gem install chrisk-fakeweb --source http://gems.github.com

== Examples

Start by requiring FakeWeb:

  require 'rubygems'
  require 'fake_web'

=== Registering basic string responses

  FakeWeb.register_uri(:get, "http://example.com/test1", :string => "Hello World!")

  Net::HTTP.get(URI.parse("http://example.com/test1"))
  => "Hello World!"

  Net::HTTP.get(URI.parse("http://example.com/test2"))
  => FakeWeb is bypassed and the response from a real request is returned

=== Replaying a recorded response

  page = `curl -is http://www.google.com/`
  FakeWeb.register_uri(:get, "http://www.google.com/", :response => page)

  Net::HTTP.get(URI.parse("http://www.google.com/"))
  # => Full response, including headers

=== Adding a custom status to the response

  FakeWeb.register_uri(:get, "http://example.com/", :string => "Nothing to be found 'round here",
                                                    :status => ["404", "Not Found"])

  Net::HTTP.start("example.com") do |req|
    response = req.get("/")
    response.code     # => "404"
    response.message  # => "Not Found"
    response.body     # => "Nothing to be found 'round here"
  end

=== Responding to any HTTP method

  FakeWeb.register_uri(:any, "http://example.com", :string => "response for any HTTP method")

If you use the <tt>:any</tt> symbol, the URI you specify will be completely
stubbed out (regardless of the HTTP method of the request). This can be useful
for RPC-like services, where the HTTP method isn't significant. (Older
versions of FakeWeb always behaved like this, and didn't accept the first
+method+ argument above; this syntax is still supported, for
backwards-compatibility, but it will probably be deprecated at some point.)

=== Rotating responses

You can optionally call FakeWeb.register_uri with an array of options hashes;
these are used, in order, to respond to repeated requests. Once you run out of
responses, further requests always receive the last response. (You can also send
a response more than once before rotating, by specifying a <tt>:times</tt>
option for that response.)

  FakeWeb.register_uri(:delete, "http://example.com/posts/1",
                       [{:string => "Post 1 deleted.", :status => ["200", "OK"]},
                        {:string => "Post not found",  :status => ["404", "Not Found"]}])

  Net::HTTP.start("example.com") do |req|
    req.delete("/posts/1").body  # => "Post 1 deleted"
    req.delete("/posts/1").body  # => "Post not found"
    req.delete("/posts/1").body  # => "Post not found"
  end

=== Clearing registered URIs

The FakeWeb registry is a singleton that lasts for the duration of your
program, maintaining every fake response you register. If needed, you
can clean out the registry and remove all registered URIs:

  FakeWeb.clean_registry

=== Blocking all real requests

When you're using FakeWeb to replace _all_ of your requests, it's useful to
catch when requests are made for unregistered URIs (unlike the default
behavior, which is to pass those requests through to Net::HTTP as usual).

  FakeWeb.allow_net_connect = false
  Net::HTTP.get(URI.parse("http://example.com/"))
  => raises FakeWeb::NetConnectNotAllowedError

  FakeWeb.allow_net_connect = true
  Net::HTTP.get(URI.parse("http://example.com/"))
  => FakeWeb is bypassed and the response from a real request is returned

This is handy when you want to make sure your tests are self-contained, or you
want to catch the scenario when a URI is changed in implementation code
without a corresponding test change.

== More info

FakeWeb lets you decouple your test environment from live services without
modifying code or writing extensive stubs.

In addition to the conceptual advantage of having idempotent request
behaviour, FakeWeb makes tests run faster than if they were made to remote (or
even local) web servers. It also makes it possible to run tests without a
network connection or in situations where the server is behind a firewall or
has host-based access controls.

FakeWeb works with anything based on Net::HTTP--both higher-level wrappers,
like OpenURI, as well as a ton of libraries for popular web services.


== Known Issues

* Request bodies are ignored, including PUT and POST parameters. If you need
  different responses for different request bodies, you need to request
  different URLs, and register different responses for each. (Query strings
  are fully supported, though.)


== Copyright

Copyright 2006-2007 Blaine Cook

Copyright 2008-2009 various contributors

  FakeWeb is free software; you can redistribute it and/or modify it under the
  terms of the GNU General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later
  version.

  FakeWeb is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  details.

  You should have received a copy of the GNU General Public License along
  with FakeWeb; if not, write to the Free Software Foundation, Inc., 51
  Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

See LICENSE.txt[link:files/LICENSE_txt.html] for the full terms.