Skip to content

Instantly share code, notes, and snippets.

@larrytheliquid
Created June 12, 2009 21:30
Show Gist options
  • Save larrytheliquid/128933 to your computer and use it in GitHub Desktop.
Save larrytheliquid/128933 to your computer and use it in GitHub Desktop.
require "rubygems"
require "rack/client"
require "rack/contrib"
puts "PUT'ing /store/fruit (with strawberry)"
puts
Rack::Client.put "http://localhost:9292/store/fruit", "strawberry"
puts "GET'ing /store/fruit"
response = Rack::Client.get "http://localhost:9292/store/fruit"
puts ">> status: #{response.status}"
puts ">> body: #{response.body.inspect}"
puts
puts "PUT'ing /store/json (with {\"rack\" => [1, 3, 3, 7]})"
puts
Rack::Client.put "http://localhost:9292/store/json", "{\"rack\" => [1, 3, 3, 7]}"
puts "GET'ing /store/json"
response = Rack::Client.new do
use Rack::PostBodyContentTypeParser
end.get "http://localhost:9292/store/json"
puts ">> status: #{response.status}"
puts ">> body: #{response.body}"
puts
puts "DELETE'ing /store"
Rack::Client.delete("http://localhost:9292/store")
require "demo"
require "rubygems"
require "rack"
run Demo::App
require "rubygems"
require "rack"
require "sinatra"
require "redis"
module Demo
Store = Redis.new
class App < Sinatra::Base
get "/store/:id" do
if item = Store.get(params[:id])
item
else
status 404
""
end
end
put "/store/:id" do
Store.set params[:id], request.body.read
end
delete "/store" do
Store.flush_db
""
end
delete "/store/:id" do
Store.delete params[:id]
end
end
end
require "demo"
require "rubygems"
require "rack"
require "rack/test"
# require "rack/client"
describe Demo, "/store resource" do
include Rack::Test::Methods
def app
Demo::App.new
# Rack::Client.new
end
before(:all) { delete "http://localhost:9292/store" }
after { delete "http://localhost:9292/store" }
it "should return a 404 if a resource does not exist" do
get "http://localhost:9292/store/does-not-exist"
last_response.status.should == 404
last_response.body.should be_empty
end
it "should be able to store and retrieve invididual items" do
put "http://localhost:9292/store/fruit", "strawberry"
put "http://localhost:9292/store/car", "lotus"
get "http://localhost:9292/store/fruit"
last_response.status.should == 200
last_response.body.should == "strawberry"
get "http://localhost:9292/store/car"
last_response.status.should == 200
last_response.body.should == "lotus"
end
it "should be able to clear the store of all items" do
put "http://localhost:9292/store/fruit", "strawberry"
put "http://localhost:9292/store/car", "lotus"
delete "http://localhost:9292/store"
get "http://localhost:9292/store/fruit"
last_response.status.should == 404
last_response.body.should be_empty
get "http://localhost:9292/store/car"
last_response.status.should == 404
last_response.body.should be_empty
end
it "should be able to clear the store of an invididual item" do
put "http://localhost:9292/store/fruit", "strawberry"
delete "http://localhost:9292/store/fruit"
get "http://localhost:9292/store/fruit"
last_response.status.should == 404
last_response.body.should be_empty
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment