ShamRack plumbs HTTP requests into Rack.
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.
gem install sham_rack
require 'sham_rack'
ShamRack.at("www.greetings.com") do |env|
["200 OK", { "Content-type" => "text/plain" }, ["Hello, world!"]]
end
require 'open-uri'
open("https://www.greetings.com/").read #=> "Hello, world!"
ShamRack.at("sinatra.xyz").sinatra do
get "/hello/:subject" do
"Hello, #{params[:subject]}"
end
end
open("https://sinatra.xyz/hello/stranger").read #=> "Hello, stranger"
ShamRack.at("rackup.xyz").rackup do
use Some::Middleware
use Some::Other::Middleware
run MyApp.new
end
ShamRack.at("google.com").mount(my_google_stub)
@stub_app = ShamRack.at("stubbed.com").stub
@stub_app.register_resource("/greeting", "Hello, world!", "text/plain")
open("https://stubbed.com/greeting").read #=> "Hello, world!"
@stub_app.last_request.path #=> "/greeting"
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.
ShamRack.prevent_network_connections
ShamRack.reset
open("https://stubbed.com/greeting").read #=> OpenURI::HTTPError
ShamRack supports requests made using Net::HTTP, or any of the numerous APIs built on top of it:
uri = URI.parse("https://www.greetings.com/")
Net::HTTP.get_response(uri).body #=> "Hello, world!"
require 'open-uri'
open("https://www.greetings.com/").read #=> "Hello, world!"
require 'restclient'
RestClient.get("https://www.greetings.com/").to_s #=> "Hello, world!"
require 'mechanize'
Mechanize.new.get("https://www.greetings.com/").body #=> "Hello, world!"
We've recently added support for Patron:
require 'sham_rack/patron'
patron = Patron::Session.new
patron.get("https://www.greetings.com/").body #=> "Hello, world!"
- Your Rack request-handling code runs in the same Ruby VM, in fact the same Thread, as your request.