sinatra/test/events_test.rb

48 lines
1.3 KiB
Ruby
Raw Normal View History

require File.dirname(__FILE__) + '/helper'
2007-11-28 04:16:12 +00:00
context "Simple Events" do
2007-11-28 05:55:05 +00:00
def simple_request_hash(method, path)
2008-03-05 00:30:06 +00:00
Rack::Request.new({
2007-11-28 05:55:05 +00:00
'REQUEST_METHOD' => method.to_s.upcase,
'PATH_INFO' => path
2008-03-05 00:30:06 +00:00
})
2007-11-28 05:55:05 +00:00
end
def invoke_simple(path, request_path, &b)
event = Sinatra::Event.new(path, &b)
2007-11-28 05:55:05 +00:00
event.invoke(simple_request_hash(:get, request_path))
end
specify "return last value" do
block = Proc.new { 'Simple' }
result = invoke_simple('/', '/', &block)
2007-11-28 04:26:48 +00:00
result.should.not.be.nil
result.block.should.be block
2007-11-28 05:55:05 +00:00
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'
2007-11-28 04:16:12 +00:00
2007-11-28 05:55:05 +00:00
# unscapes
result = invoke_simple('/:foo/:bar', '/a/blake%20mizerany')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => 'blake mizerany'
2007-11-28 04:26:48 +00:00
end
2007-11-28 05:55:05 +00:00
specify "ignores to many /'s" do
result = invoke_simple('/x/y', '/x//y')
result.should.not.be.nil
end
2008-01-12 01:01:00 +00:00
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
2007-11-28 04:16:12 +00:00
end