1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/compat/events_test.rb
Ryan Tomayko c00a25ee41 Test framework refactoring
* Adds test/helper.rb and moves mock_app and other code specific
  to testing the framework out of Sinatra::Test.
* Do not require test/unit. The sinatra/test/unit,
  sinatra/test/spec, and sinatra/test/rspec files can be used to
  choose the framework.
* Add Sinatra::TestHarness, which should act similar to the
  Rack::Session proposal here: http://gist.github.com/41270
* Update the README with information on using the different test
  frameworks.
2009-01-14 07:52:04 -08:00

78 lines
2.1 KiB
Ruby

require File.dirname(__FILE__) + '/helper'
context "Simple Events" do
def simple_request_hash(method, path)
Rack::Request.new({
'REQUEST_METHOD' => method.to_s.upcase,
'PATH_INFO' => path
})
end
class MockResult < Struct.new(:block, :params)
end
def invoke_simple(path, request_path, &b)
params = nil
get path do
params = self.params
b.call if b
end
get_it request_path
MockResult.new(b, params)
end
setup { Sinatra.application = nil }
specify "return last value" do
block = Proc.new { 'Simple' }
result = invoke_simple('/', '/', &block)
result.should.not.be.nil
result.block.should.be block
result.params.should.equal Hash.new
end
specify "takes params in path" do
result = invoke_simple('/:foo/:bar', '/a/b')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => 'b'
# unscapes
Sinatra.application = nil
result = invoke_simple('/:foo/:bar', '/a/blake%20mizerany')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => 'blake mizerany'
end
specify "takes optional params in path" do
result = invoke_simple('/?:foo?/?:bar?', '/a/b')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => 'b'
Sinatra.application = nil
result = invoke_simple('/?:foo?/?:bar?', '/a/')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => nil
Sinatra.application = nil
result = invoke_simple('/?:foo?/?:bar?', '/a')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => nil
Sinatra.application = nil
result = invoke_simple('/:foo?/?:bar?', '/')
result.should.not.be.nil
result.params.should.equal "foo" => nil, "bar" => nil
end
specify "ignores to many /'s" do
result = invoke_simple('/x/y', '/x//y')
result.should.not.be.nil
end
specify "understands splat" do
invoke_simple('/foo/*', '/foo/bar').should.not.be.nil
invoke_simple('/foo/*', '/foo/bar/baz').should.not.be.nil
invoke_simple('/foo/*', '/foo/baz').should.not.be.nil
end
end