Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Run a sinatra app in your test environment that will operate as a Fake web service
Ruby
Branch: master
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.
lib
test
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION
sinatra_fake_web_service.gemspec

README.rdoc

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.

Installation

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

gem 'sinatra_fake_web_service', :git => git@github.com:philly-mac/sinatra_fake_web_service.git, :group => :test

Usage

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

@sinatra_fws = SinatraFakeWebService.new :host => 'localhost', :port => 7000

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

@sinatra_fws.run!

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

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

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

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

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

tada!

Tests

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 = SinatraFakeWebService::TestClient.new(@sinatra_fws)

test_client.get("/awesome").body                  #=> YAY!!
test_client.post("/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

Copyright © 2010 Elad Meidar. See LICENSE for details.

Something went wrong with that request. Please try again.