https://github.com/featurist/rack-stubs
Rack middleware for stubbing responses from HTTP web services
https://github.com/featurist/rack-stubs
Last synced: 5 months ago
JSON representation
Rack middleware for stubbing responses from HTTP web services
- Host: GitHub
- URL: https://github.com/featurist/rack-stubs
- Owner: featurist
- License: bsd-2-clause
- Created: 2011-03-09T08:23:39.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2011-09-06T15:43:46.000Z (over 13 years ago)
- Last Synced: 2024-04-24T03:23:55.908Z (about 1 year ago)
- Language: Ruby
- Homepage:
- Size: 107 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.rdoc
- License: LICENSE
Awesome Lists containing this project
README
= Rack Stubs
Rack middleware for stubbing responses from HTTP web services.
== Installation
gem install rack-stubs
== Using the server middleware
rack-stubs is implemented as rack middleware, so you would typically host it as part of a rack web app. You can write a rack configuration (config.ru) like this if you want to get started:
require 'rubygems'
require 'rack-stubs'use RackStubs::Middleware
run lambda { |e| [404, { 'Content-Type' => 'text/plain' }, ["Not found"]] }
Then you can run the rackup binary:rackup
...and you'll have a server with rack-stubs running at http://127.0.0.1:9292To create a stub response, let's say we want HTTP GET requests to /foo to return an HTTP 200 status, with the body "bar" and the content type "text/plain". Using a REST tool of some kind (you could use the rest-client ruby gem or the poster firefox plugin) you can now set this up by making the following HTTP request:
method : POST
action : http://127.0.0.1:9292/foo
body : { "GET" : [200, { "Content-Type": "text/plain" }, ["bar"]] }
headers : Content-Type=application/json+rack-stubAll subsequent requests to http://127.0.0.1:9292/foo should now respond with HTTP 200 status, Content-Type=text/plain and the body "bar". The body is json, but it's essentially implemented as a hash of simple rack-style response tuples.
You can also see a list of all stub responses via GET /rack_stubs/list and clear all stub responses via POST /rack_stubs/clear
== Using the client
To use rack-stubs from Ruby without diving down into REST, rack-stubs includes a small client library. To set up the same behaviour as specified above, you would use the client like this:
client = RackStubs::Client.new("http://127.0.0.1:9292")
client.get("/foo").returns(200, { "Content-Type" => "text/plain" }, "bar")
You might want to clear all stub responses, say between test cases, like so:client.clear_all!