Giter Site home page Giter Site logo

sham_rack's Introduction

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.

sham_rack's People

Contributors

markburns avatar mdub avatar tmecklem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sham_rack's Issues

Rails 5 error

Using ShamRack with Rails 5 generates a weird error:
LoadError: cannot load such file -- rack/showexceptions

Searching for it I found that the problem is the Sinatra version vs Rails version.*

Temporary Solution -> Add gem "sinatra", "2.0.0.beta2" to the gemfile.

However, it would be nice to resolve this dependency in the ShamRack's gemfile

References:

Doesn't work with HTTPS

I haven't looked into it too thoroughly, but when I setup ShamRack with a domain, say example.com, then hit https://example.com, it still hits example.com (doesn't go through ShamRack).

Implicit requirement on Sinatra

Added sham_rack to my gemfile, and then tried: ShamRack.at(url).sinatra do ... end

Failed because I don't have sinatra... Should it be part of sham_racks dependencies in gem files?

what is the license for sham_rack?

Hi,

I'm packaging sham_rack for debian and would like to know what license sham-rack uses. It would be nice if you could add a LICENSE file in the repo itself.

test failures: Rack environment supports POST/PUT with rest client 2.0.2

Failures:

  1) ShamRack Rack environment supports POST
     Failure/Error: expect(env["rack.input"].read).to eq("q=rack")

       expected: "q=rack"
            got: ""

       (compared using ==)
     # ./spec/sham_rack_spec.rb:301:in `block (3 levels) in <top (required)>'

  2) ShamRack Rack environment supports PUT
     Failure/Error: expect(env["rack.input"].read).to eq("stuff")

       expected: "stuff"
            got: ""

       (compared using ==)
     # ./spec/sham_rack_spec.rb:335:in `block (3 levels) in <top (required)>'

Finished in 0.62369 seconds (files took 3.11 seconds to load)
40 examples, 2 failures, 4 pending

Failed examples:

rspec ./spec/sham_rack_spec.rb:295 # ShamRack Rack environment supports POST
rspec ./spec/sham_rack_spec.rb:329 # ShamRack Rack environment supports PUT
Failures:

  1) ShamRack Rack environment supports POST
     Failure/Error: expect(env["rack.input"].read).to eq("q=rack")

       expected: "q=rack"
            got: ""

       (compared using ==)
     # ./spec/sham_rack_spec.rb:301:in `block (3 levels) in <top (required)>'

  2) ShamRack Rack environment supports PUT
     Failure/Error: expect(env["rack.input"].read).to eq("stuff")

       expected: "stuff"
            got: ""

       (compared using ==)
     # ./spec/sham_rack_spec.rb:335:in `block (3 levels) in <top (required)>'

Finished in 0.62369 seconds (files took 3.11 seconds to load)
40 examples, 2 failures, 4 pending

Failed examples:

rspec ./spec/sham_rack_spec.rb:295 # ShamRack Rack environment supports POST
rspec ./spec/sham_rack_spec.rb:329 # ShamRack Rack environment supports PUT

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=868750

reproduced with both rack 1.6 and 2.0.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.