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 #:awesome” end
@fakews.delete ‘/awesome’ do “there, i kiiled #: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.