• Stars
    star
    166
  • Rank 225,997 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 15 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

run Rack applications in-process, without a server

ShamRack

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'

ShamRack.at("www.greetings.com") do |env|
  ["200 OK", { "Content-type" => "text/plain" }, ["Hello, world!"]]
end

require 'open-uri'
open("http://www.greetings.com/").read            #=> "Hello, world!"

Sinatra integration

ShamRack.at("sinatra.xyz").sinatra do
  get "/hello/:subject" do
    "Hello, #{params[:subject]}"
  end
end

open("http://sinatra.xyz/hello/stranger").read  #=> "Hello, stranger"

Rackup support

ShamRack.at("rackup.xyz").rackup do
  use Some::Middleware
  use Some::Other::Middleware
  run MyApp.new
end

Any old Rack app

ShamRack.at("google.com").mount(my_google_stub)

General-purpose stubbing

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

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

On a specific port

ShamRack.at("example.com", 8080) do |env|
  ["200 OK", { "Content-type" => "text/plain" }, ["Hello, world!"]]
end

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

Avoiding (accidental) real network connections

ShamRack.prevent_network_connections

When you're done testing

ShamRack.reset

open("http://stubbed.com/greeting").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("http://www.greetings.com/")
Net::HTTP.get_response(uri).body                      #=> "Hello, world!"

require 'open-uri'
open("http://www.greetings.com/").read                #=> "Hello, world!"

require 'restclient'
RestClient.get("http://www.greetings.com/").to_s      #=> "Hello, world!"

require 'mechanize'
Mechanize.new.get("http://www.greetings.com/").body   #=> "Hello, world!"

Patron (experimental)

We've recently added support for Patron:

require 'sham_rack/patron'

patron = Patron::Session.new
patron.get("http://www.greetings.com/").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.

More Repositories

1

clamp

a Ruby command-line application framework
Ruby
422
star
2

representative

XML/JSON representations of your Ruby objects
Ruby
78
star
3

pith

a static website generator
Ruby
38
star
4

lazily

Lazy Enumerables for everybody!
Ruby
33
star
5

representative_view

Integrate Representative as an ActionView template format
Ruby
29
star
6

enumerating

Ruby Enumerator plumbing (ala Unix pipes)
Ruby
25
star
7

ssssh

It's a secret.
Ruby
13
star
8

arboreal

Efficient tree structures for ActiveRecord
Ruby
10
star
9

rspec-longrun

Ruby
10
star
10

green_log

a logging library
Ruby
8
star
11

bamboozled

adapts Bamboo build status to CruiseControl XML format
Ruby
7
star
12

ituner-server

a web interface to iTunes
JavaScript
7
star
13

config_mapper

Maps config data onto plain old Ruby objects
Ruby
6
star
14

cloud_shaped

Ruby DSL for AWS CloudFormation templates
Ruby
6
star
15

gem_collector

simple automatic installation of RubyGems
Ruby
4
star
16

localdocker

a Vagrant-based Docker host, for local development
Shell
4
star
17

newrelic-webmachine

NewRelic instrumentation for webmachine-ruby
Ruby
3
star
18

ituner

control iTunes using Ruby
Ruby
3
star
19

basketcase

making ClearCase ever-so-slightly less painful
Ruby
3
star
20

swa

an alternative CLI for AWS
Ruby
3
star
21

haskell-kata

Haskell
2
star
22

booker

a simple CMS
JavaScript
2
star
23

hoick

A command-line HTTP client
Ruby
2
star
24

shoebox

a place to put your photos
Ruby
2
star
25

saucy

experiments in event sourcing
Ruby
1
star
26

dogbiscuit.org

My website
JavaScript
1
star
27

schema_validations

Automatic ActiveRecord validations based on database reflection
Ruby
1
star
28

trunction

Knock your HTML down to size
Ruby
1
star
29

art-of-being-lazy

Enumerable, and the art of being lazy
Haml
1
star
30

gem_require

Quick gem check/upgrade
Ruby
1
star
31

jruby-tomcat-syslog-fracked

Demo of JRUBY-4227
1
star
32

wherever

Ruby
1
star
33

wikiwah

WikiWah turns text into HTML
Ruby
1
star
34

platform1

A thin wrapper around Passenger Standalone
Ruby
1
star