public
Fork of bmizerany/sinatra
Description: Classy web-development dressed in a DSL
Homepage: http://sinatrarb.com
Clone URL: git://github.com/JackDanger/sinatra.git
sinatra / test / events_test.rb
100644 51 lines (40 sloc) 1.361 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require File.dirname(__FILE__) + '/../lib/sinatra'
 
require 'rubygems'
require 'test/spec'
 
context "Simple Events" do
 
  def simple_request_hash(method, path)
    Rack::Request.new({
      'REQUEST_METHOD' => method.to_s.upcase,
      'PATH_INFO' => path
    })
  end
 
  def invoke_simple(path, request_path, &b)
    event = Sinatra::Event.new(path, &b)
    event.invoke(simple_request_hash(:get, request_path))
  end
  
  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
    result = invoke_simple('/:foo/:bar', '/a/blake%20mizerany')
    result.should.not.be.nil
    result.params.should.equal "foo" => 'a', "bar" => 'blake mizerany'
  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