Project

rack-stubs

0.0
No commit activity in last 3 years
No release in over 3 years
Rack middleware for stubbing responses from HTTP web services
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.10.0
~> 1.1.5
~> 2.2.0

Runtime

~> 1.4.6
~> 1.3.1
~> 1.6.3
 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 127.0.0.1:9292

To 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-stub

All subsequent requests to 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!