Created
December 29, 2008 13:45
-
-
Save sr/41270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "rack" | |
$:.unshift File.dirname(__FILE__) + "/lib" | |
require "sinatra/base" | |
class Sinatra::Application | |
get "/" do | |
"Hello World" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "test/unit" | |
require "rack_session" | |
require "app" | |
class AppTest < Test::Unit::TestCase | |
def setup | |
@app = RackSession.new(Sinatra::Application) | |
end | |
def test_it_works | |
@app.get "/" | |
assert_equal 200 , @app.response.status | |
assert_equal "Hello World", @app.response.body | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/code/sinatra (hoboken-test) % ruby app_test.rb | |
Loaded suite app_test | |
Started | |
. | |
Finished in 0.006002 seconds. | |
1 tests, 2 assertions, 0 failures, 0 errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RackSession | |
attr_reader :response, :request, :app | |
def initialize(app) | |
@app = app | |
end | |
def get(path, data={}, headers={}) | |
do_request(:get, path, data, headers) | |
end | |
def post(path, data={}, headers={}) | |
do_request(:post, path, data, headers) | |
end | |
def put(path, data={}, headers={}) | |
do_request(:put, path, data, headers) | |
end | |
def delete(path, data={}, headers={}) | |
do_request(:delete, path, data, headers) | |
end | |
private | |
def do_request(verb, path, data={}, headers={}) | |
if data.is_a?(String) | |
headers.update(:input => data) | |
data = {} | |
end | |
@request = Rack::MockRequest.new(app) | |
end | |
def uri(path, data={}) | |
uri = URI(path) | |
uri.query = data.map {|k,v| "#{k}=#{v}" }.join("&") | |
uri.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment