diff --git a/README.md b/README.md index f3cce33..92a607e 100644 --- a/README.md +++ b/README.md @@ -29,63 +29,77 @@ Using it ### A simple inline application - require 'sham_rack' +```ruby +require 'sham_rack' - ShamRack.at("www.greetings.com") do |env| - ["200 OK", { "Content-type" => "text/plain" }, ["Hello, world!"]] - end +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!" +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" +```ruby +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 +```ruby +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) +```ruby +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" +```ruby +@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 +```ruby +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 +```ruby +ShamRack.prevent_network_connections +``` ### When you're done testing - ShamRack.reset +```ruby +ShamRack.reset - open("http://stubbed.com/greeting").read #=> OpenURI::HTTPError +open("http://stubbed.com/greeting").read #=> OpenURI::HTTPError +``` Supported HTTP client libraries ------------------------------- @@ -94,26 +108,30 @@ Supported HTTP client libraries 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!" +```ruby +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 'open-uri' +open("http://www.greetings.com/").read #=> "Hello, world!" - require 'restclient' - RestClient.get("http://www.greetings.com/").to_s #=> "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!" +require 'mechanize' +Mechanize.new.get("http://www.greetings.com/").body #=> "Hello, world!" +``` ### Patron (experimental) We've recently added support for [Patron][patron]: - require 'sham_rack/patron' +```ruby +require 'sham_rack/patron' - patron = Patron::Session.new - patron.get("http://www.greetings.com/").body #=> "Hello, world!" +patron = Patron::Session.new +patron.get("http://www.greetings.com/").body #=> "Hello, world!" +``` What's the catch? ----------------- @@ -125,13 +143,13 @@ Thanks to * Blaine Cook for [FakeWeb][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][rack]. +* Leah Neukirchen et al for the chewy goodness that is [Rack][rack]. -[rack]: http://rack.rubyforge.org/ +[rack]: http://github.com/rack/rack [sinatra]: http://www.sinatrarb.com/ -[rest-client]: http://github.com/adamwiggins/rest-client -[httparty]: http://github.com/jnunemaker/httparty -[oauth]: http://oauth.rubyforge.org/ -[fakeweb]: http://fakeweb.rubyforge.org/ -[mechanize]: http://mechanize.rubyforge.org -[patron]: http://github.com/toland/Patron +[rest-client]: https://github.com/adamwiggins/rest-client +[httparty]: https://github.com/jnunemaker/httparty +[oauth]: https://github.com/oauth-xx/oauth-ruby +[fakeweb]: https://github.com/chrisk/fakeweb +[mechanize]: https://github.com/sparklemotion/mechanize +[patron]: https://github.com/toland/Patron