run Rack applications in-process, without a server

License: MIT

Language: Ruby

Keywords: rack, ruby, sinatra


Gem Version Build Status

ShamRack plumbs HTTP requests into Rack.

What's it for, again?

Well, it makes it easy to stub out external (HTTP) services, which is handy in development and testing environments, or when you want to test your HTTP client code.

You can also use it to test your Rack application (or Sinatra, or Rails, or Merb) using a variety of HTTP client libraries, to check interoperability. For instance, you could test your app using:

all without having to boot it in a server.

Installing it

gem install sham_rack

Using it

A simple inline application

require 'sham_rack'"") do |env|
  ["200 OK", { "Content-type" => "text/plain" }, ["Hello, world!"]]

require 'open-uri'
open("").read            #=> "Hello, world!"

Sinatra integration"").sinatra do
  get "/hello/:subject" do
    "Hello, #{params[:subject]}"

open("").read  #=> "Hello, stranger"

Rackup support"").rackup do
  use Some::Middleware
  use Some::Other::Middleware

Any old Rack app"").mount(my_google_stub)

General-purpose stubbing

@stub_app ="").stub
@stub_app.register_resource("/greeting", "Hello, world!", "text/plain")

open("").read       #=> "Hello, world!"
@stub_app.last_request.path                    #=> "/greeting"

On a specific port"", 8080) do |env|
  ["200 OK", { "Content-type" => "text/plain" }, ["Hello, world!"]]

Or, just use Sinatra, as described above ... it's almost as succinct, and heaps more powerful.

Avoiding (accidental) real network connections


When you're done testing


open("").read       #=> OpenURI::HTTPError

Supported HTTP client libraries

Net::HTTP and friends

ShamRack supports requests made using Net::HTTP, or any of the numerous APIs built on top of it:

uri = URI.parse("")
Net::HTTP.get_response(uri).body                      #=> "Hello, world!"

require 'open-uri'
open("").read                #=> "Hello, world!"

require 'restclient'
RestClient.get("").to_s      #=> "Hello, world!"

require 'mechanize'"").body   #=> "Hello, world!"

Patron (experimental)

We've recently added support for Patron:

require 'sham_rack/patron'

patron =
patron.get("").body          #=> "Hello, world!"

What's the catch?

  • Your Rack request-handling code runs in the same Ruby VM, in fact the same Thread, as your request.

Thanks to

  • Blaine Cook for FakeWeb, which was an inspiration for ShamRack.
  • Perryn Fowler for his efforts plumbing Net::HTTP into ActionController::TestProcess.
  • Christian Neukirchen et al for the chewy goodness that is Rack.

Project Statistics

Sourcerank 13
Repository Size 104 KB
Stars 165
Forks 13
Watchers 2
Open issues 0
Dependencies 7
Contributors 5
Tags 10
Last updated
Last pushed

Top Contributors See all

Mike Williams Jon Yurek Jessie A. Young Mark Burns Tim Mecklem

Packages Referencing this Repo

ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP testing.
Latest release 1.1.2 - Updated - 165 stars
ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP testing.
Latest release 1.4.1 - Updated - 165 stars

Recent Tags See all

v1.4.1 August 20, 2017
v1.4.0 January 06, 2017
v1.3.6 April 05, 2013
v1.3.5 March 25, 2013
v1.3.4 May 01, 2012
v1.3.3 December 22, 2010
v1.3.2 September 01, 2010
v1.3.0 March 11, 2010
v1.2.1 January 15, 2010
v1.0.0 May 06, 2009

Something wrong with this page? Make a suggestion

Last synced: 2017-08-20 11:50:46 UTC

Login to resync this repository