Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Run a sinatra app in your test environment that will operate as a Fake web service

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 metal
Octocat-spinner-32 test
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 sinatra.log
Octocat-spinner-32 sinatra_fake_webservice.gemspec
README.rdoc

Sinatra Fake Webservice

SFW provides an easy and simple wrapper for a sinatra application you can use in your tests in order to test/simulate remote API and HTTP calls and responses.

Why shouldn't i use FakeWeb?

FakeWeb is awesome, but it allows you only to register one response per url, although in some cases you might need more than one response per url depend on your usage for example: testing an XMLRPC service or a WSDL that both might have only one url and respond based on parameters.

Installation

Command line: sudo gem install sinatra_fake_webservice

Or in bundler (0.9.x) add this line to your .gemfile:

gem 'sinatra_fake_webservice', :group => :test

Usage

First you'll need to create a SinatraWebService instance that can accept :host and :port options:

@fakews = SinatraWebService.new :host => 'localhost', :port => 7000

and then simply use the familiar sinatra DSL to create methods and responses.

@fakews.get '/awesome' do “YAY!!” end

@fakews.post '/omglol' do “YAY!! i posted #{params[:awesome]}” end

@fakews.delete '/awesome' do “there, i kiiled #{params[:id]}” end @fakews.put '/awesome' do “yay, i saved the worlds” end tada!

Tests

I added a few methods to wrap HTTP requests:

  • get_response(path)

  • post_response(path, data, headers, dest)

  • put_response(path, data, headers, dest)

  • delete_response(path, data, headers, dest)

The most important thing to remember, is that you'll have to point your webservice api wrapper (twitter_auth or whatever) to use “localhost” and the fake sinatra app's port, available via a simple getter (#port).

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2010 Elad Meidar. See LICENSE for details.

Something went wrong with that request. Please try again.