README

Path: README
Last Update: Mon May 22 22:52:11 PDT 2006

Name

FakeWeb - Helper for Faking Web Requests

Synopsis

  $:.unshift "#{File.dirname(__FILE__)}/../../lib"

  require 'test/unit'
  require 'fake_web'
  require 'open-uri'

  class FakeWebExampleTest < Test::Unit::TestCase
    def test_request
      FakeWeb.register_uri('http://example.com/test_me', :string => "Hello World!")
      content = Net::HTTP.get(URI.parse('http://example.com/test_me'))
      assert_equal "Hello World!", content
    end

    def test_request_with_response
      FakeWeb.register_uri('http://www.google.com/', :response => `curl -is http://www.google.com/`)
      Net::HTTP.start('www.google.com') do |req|
        response = req.get('/')
        assert_equal "200", response.code
        assert_equal "OK", response.message
        assert response.body.include?('<title>Google')
      end
    end

    def test_request_with_custom_status
      FakeWeb.register_uri('http://example.com/', :string => "Nothing to be found 'round here",
                                                  :status => ['404', 'Not Found'])
      Net::HTTP.start('example.com') do |req|
        response = req.get('/')
        assert_equal "404", response.code
        assert_equal "Not Found", response.message
        assert_equal "Nothing to be found 'round here", response.body
      end
    end

    def test_open_uri
      FakeWeb.register_uri('http://example.com/', :string => "Hello, World!")
      content = open('http://example.com/').string
      assert_equal "Hello, World!", content
    end
  end

Description

FakeWeb is a helper for faking web requests. This makes testing easier, because you can decouple your test environment from live services without modifying code. It allows for a range of request behaviour, from simple stubbing of HTTP responses to re-playing complete recorded responses.

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 is tested with Net::HTTP and OpenURI. It should work with any web client library that uses Net::HTTP for its underlying requests (e.g., Flickr.rb, Ruby/Amazon, soap4r, etc.)

Known Issues

  • The port-specific nature of requests is ignored; requests to test.com and test.com:8080 are treated identically.
  • Request bodies are ignored, including GET, 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.

Copyright

FakeWeb - Ruby Helper for Faking Web Requests Copyright 2006 Blaine Cook <romeda@gmail.com>.

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

[Validate]