Run a sinatra app in your test environment that will operate as a Fake web service
Pull request Compare This branch is 8 commits ahead of eladmeidar:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Sinatra Fake Web Service

Sinatra Fake Web Service 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 just use FakeWeb?

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


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

gem 'sinatra_fake_web_service', :git =>, :group => :test


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

@sinatra_fws = :host => 'localhost', :port => 7000

Then you will need to execute the run! method to start the service!

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

@sinatra_fws.get '/awesome' do “YAY!!” end '/omglol' do “YAY!! i posted #{params}” end

@sinatra_fws.delete '/awesome' do “there, i kiiled #{params}” end

@sinatra_fws.put '/awesome' do “yay, i saved the world” end



I added a few methods to wrap HTTP requests:

In the SinatraFakeWebService::TestClient you have the following methods

  • get(path)

  • post(path, data, headers, dest)

  • put(path, data, headers, dest)

  • delete(path, data, headers, dest)

and you can use it like (using the instance of the SinatraFakeWebService that we created before)

test_client =

test_client.get("/awesome").body                  #=> YAY!!"/omglol", "awesome=Whoop").body #=> YAY!! i posted Whoop
test_client.put("/awesome", "id=jfk").body        #=> there, i kiiled jfk
test_client.delete("/awesome").body               #=> yay, i saved the world

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 © 2010 Elad Meidar. See LICENSE for details.