2007-11-28 00:55:05 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
|
|
|
|
2008-04-11 16:36:26 -04:00
|
|
|
require 'uri'
|
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
class TesterWithEach
|
|
|
|
def each
|
|
|
|
yield 'foo'
|
|
|
|
yield 'bar'
|
|
|
|
yield 'baz'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-28 01:31:31 -05:00
|
|
|
context "Looking up a request" do
|
2007-11-28 00:55:05 -05:00
|
|
|
|
|
|
|
setup do
|
2008-02-24 21:30:04 -05:00
|
|
|
Sinatra.application = nil
|
2007-11-28 00:55:05 -05:00
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
# Deprecated. The lookup method is no longer used.
|
|
|
|
xspecify "returns what's at the end" do
|
2007-11-28 01:31:31 -05:00
|
|
|
block = Proc.new { 'Hello' }
|
2008-02-24 21:30:04 -05:00
|
|
|
get '/', &block
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
result = Sinatra.application.lookup(
|
2008-03-04 19:30:06 -05:00
|
|
|
Rack::Request.new(
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
'PATH_INFO' => '/'
|
|
|
|
)
|
2007-11-28 00:55:05 -05:00
|
|
|
)
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 00:55:05 -05:00
|
|
|
result.should.not.be.nil
|
2007-11-28 01:31:31 -05:00
|
|
|
result.block.should.be block
|
2007-11-28 00:55:05 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
# Deprecated. The lookup method is no longer used.
|
|
|
|
xspecify "takes params in path" do
|
2007-11-28 01:31:31 -05:00
|
|
|
block = Proc.new { 'Hello' }
|
2008-02-24 21:30:04 -05:00
|
|
|
get '/:foo', &block
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
result = Sinatra.application.lookup(
|
2008-03-04 19:30:06 -05:00
|
|
|
Rack::Request.new(
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
'PATH_INFO' => '/bar'
|
|
|
|
)
|
2007-11-28 00:55:05 -05:00
|
|
|
)
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 00:55:05 -05:00
|
|
|
result.should.not.be.nil
|
2007-11-28 01:31:31 -05:00
|
|
|
result.block.should.be block
|
2008-03-31 21:48:06 -04:00
|
|
|
result.params.should.equal "foo" => 'bar'
|
2007-11-28 00:55:05 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 00:55:05 -05:00
|
|
|
end
|
2007-11-28 01:52:52 -05:00
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
context "An app returns" do
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 01:52:52 -05:00
|
|
|
setup do
|
2008-02-24 21:30:04 -05:00
|
|
|
Sinatra.application = nil
|
2007-11-28 01:52:52 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 01:52:52 -05:00
|
|
|
specify "404 if no events found" do
|
|
|
|
request = Rack::MockRequest.new(@app)
|
2008-02-24 21:30:04 -05:00
|
|
|
get_it '/'
|
|
|
|
should.be.not_found
|
|
|
|
body.should.equal '<h1>Not Found</h1>'
|
2007-11-28 01:52:52 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 01:52:52 -05:00
|
|
|
specify "200 if success" do
|
2008-02-24 21:30:04 -05:00
|
|
|
get '/' do
|
2007-11-28 01:52:52 -05:00
|
|
|
'Hello World'
|
|
|
|
end
|
2008-02-24 21:30:04 -05:00
|
|
|
get_it '/'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Hello World'
|
2007-11-28 01:52:52 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
specify "an objects result from each if it has it" do
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
get '/' do
|
2007-11-28 03:10:12 -05:00
|
|
|
TesterWithEach.new
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
get_it '/'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'foobarbaz'
|
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
# Deprecated. The body method no longer halts.
|
|
|
|
xspecify "the body set if set before the last" do
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
get '/' do
|
2007-11-28 16:25:14 -05:00
|
|
|
body 'Blake'
|
2007-11-28 03:10:12 -05:00
|
|
|
'Mizerany'
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
get_it '/'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Blake'
|
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-11-30 19:16:42 -05:00
|
|
|
specify "404 if NotFound is raised" do
|
|
|
|
|
|
|
|
get '/' do
|
|
|
|
raise Sinatra::NotFound
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/'
|
|
|
|
should.be.not_found
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
end
|
2008-04-14 19:59:31 -04:00
|
|
|
|
|
|
|
context "Application#configure blocks" do
|
|
|
|
|
|
|
|
setup do
|
|
|
|
Sinatra.application = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "run when no environment specified" do
|
|
|
|
ref = false
|
|
|
|
configure { ref = true }
|
|
|
|
ref.should.equal true
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "run when matching environment specified" do
|
|
|
|
ref = false
|
|
|
|
configure(:test) { ref = true }
|
|
|
|
ref.should.equal true
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "do not run when no matching environment specified" do
|
|
|
|
configure(:foo) { flunk "block should not have been executed" }
|
|
|
|
configure(:development, :production, :foo) { flunk "block should not have been executed" }
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "accept multiple environments" do
|
|
|
|
ref = false
|
|
|
|
configure(:foo, :test, :bar) { ref = true }
|
|
|
|
ref.should.equal true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-04-15 22:57:25 -04:00
|
|
|
context "Default Application Configuration" do
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
# Sinatra::ServerError is no longer used
|
|
|
|
xspecify "includes 404 and 500 error handlers" do
|
2008-04-15 22:57:25 -04:00
|
|
|
Sinatra.application.errors.should.include(Sinatra::ServerError)
|
|
|
|
Sinatra.application.errors[Sinatra::ServerError].should.not.be.nil
|
|
|
|
Sinatra.application.errors.should.include(Sinatra::NotFound)
|
|
|
|
Sinatra.application.errors[Sinatra::NotFound].should.not.be.nil
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
# Deprecated. No such thing as a Static event anymore.
|
|
|
|
xspecify "includes Static event" do
|
2008-04-15 22:57:25 -04:00
|
|
|
assert Sinatra.application.events[:get].any? { |e| Sinatra::Static === e }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
context "Events in an app" do
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
setup do
|
2008-02-24 21:30:04 -05:00
|
|
|
Sinatra.application = nil
|
2007-11-28 03:10:12 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
specify "evaluate in a clean context" do
|
2008-02-24 21:30:04 -05:00
|
|
|
helpers do
|
2007-11-28 02:24:13 -05:00
|
|
|
def foo
|
|
|
|
'foo'
|
|
|
|
end
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
get '/foo' do
|
2007-11-28 02:24:13 -05:00
|
|
|
foo
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
get_it '/foo'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'foo'
|
2007-11-28 02:24:13 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2007-11-28 03:10:12 -05:00
|
|
|
specify "get access to request, response, and params" do
|
2008-02-24 21:30:04 -05:00
|
|
|
get '/:foo' do
|
2008-03-31 21:48:06 -04:00
|
|
|
params["foo"] + params["bar"]
|
2007-11-28 02:47:48 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
get_it '/foo?bar=baz'
|
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'foobaz'
|
2007-11-28 02:47:48 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-01-11 20:01:00 -05:00
|
|
|
specify "can filters by agent" do
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-02-24 21:30:04 -05:00
|
|
|
get '/', :agent => /Windows/ do
|
2008-01-11 20:01:00 -05:00
|
|
|
request.env['HTTP_USER_AGENT']
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-09 23:30:42 -04:00
|
|
|
get_it '/', :env => { :agent => 'Windows' }
|
2008-02-24 21:30:04 -05:00
|
|
|
should.be.ok
|
|
|
|
body.should.equal 'Windows'
|
2008-01-11 20:01:00 -05:00
|
|
|
|
2008-04-20 21:03:41 -04:00
|
|
|
get_it '/', :env => { :agent => 'Mac' }
|
2008-02-24 21:30:04 -05:00
|
|
|
should.not.be.ok
|
2008-01-11 20:01:00 -05:00
|
|
|
|
|
|
|
end
|
2008-03-24 21:04:51 -04:00
|
|
|
|
2008-04-20 21:03:41 -04:00
|
|
|
specify "can use regex to get parts of user-agent" do
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-03-24 21:04:51 -04:00
|
|
|
get '/', :agent => /Windows (NT)/ do
|
|
|
|
params[:agent].first
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-09 23:30:42 -04:00
|
|
|
get_it '/', :env => { :agent => 'Windows NT' }
|
2008-03-24 21:04:51 -04:00
|
|
|
|
|
|
|
body.should.equal 'NT'
|
|
|
|
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-11 16:36:26 -04:00
|
|
|
specify "can deal with spaces in paths" do
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-11 16:36:26 -04:00
|
|
|
path = '/path with spaces'
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-11 16:36:26 -04:00
|
|
|
get path do
|
|
|
|
"Look ma, a path with spaces!"
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-11 16:36:26 -04:00
|
|
|
get_it URI.encode(path)
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-04-11 16:36:26 -04:00
|
|
|
body.should.equal "Look ma, a path with spaces!"
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-09-07 09:23:02 -04:00
|
|
|
specify "route based on host" do
|
|
|
|
|
|
|
|
get '/' do
|
|
|
|
'asdf'
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal('asdf', body)
|
|
|
|
|
|
|
|
get '/foo', :host => 'foo.sinatrarb.com' do
|
|
|
|
'in foo!'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/foo', :host => 'bar.sinatrarb.com' do
|
|
|
|
'in bar!'
|
|
|
|
end
|
|
|
|
|
|
|
|
get_it '/foo', {}, 'HTTP_HOST' => 'foo.sinatrarb.com'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'in foo!', body
|
|
|
|
|
|
|
|
get_it '/foo', {}, 'HTTP_HOST' => 'bar.sinatrarb.com'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'in bar!', body
|
|
|
|
|
|
|
|
get_it '/foo'
|
|
|
|
assert not_found?
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-11-28 01:52:52 -05:00
|
|
|
end
|
2007-11-28 03:10:12 -05:00
|
|
|
|
|
|
|
|
2008-04-15 04:25:09 -04:00
|
|
|
context "Options in an app" do
|
|
|
|
|
|
|
|
setup do
|
|
|
|
Sinatra.application = nil
|
|
|
|
@app = Sinatra::application
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "can be set singly on app" do
|
|
|
|
@app.set :foo, 1234
|
|
|
|
@app.options.foo.should.equal 1234
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "can be set singly from top-level" do
|
|
|
|
set_option :foo, 1234
|
|
|
|
@app.options.foo.should.equal 1234
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "can be set multiply on app" do
|
|
|
|
@app.options.foo.should.be.nil
|
|
|
|
@app.set :foo => 1234,
|
|
|
|
:bar => 'hello, world'
|
|
|
|
@app.options.foo.should.equal 1234
|
|
|
|
@app.options.bar.should.equal 'hello, world'
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "can be set multiply from top-level" do
|
|
|
|
@app.options.foo.should.be.nil
|
|
|
|
set_options :foo => 1234,
|
|
|
|
:bar => 'hello, world'
|
|
|
|
@app.options.foo.should.equal 1234
|
|
|
|
@app.options.bar.should.equal 'hello, world'
|
|
|
|
end
|
|
|
|
|
2008-04-17 00:06:25 -04:00
|
|
|
specify "can be enabled on app" do
|
|
|
|
@app.options.foo.should.be.nil
|
|
|
|
@app.enable :sessions, :foo, :bar
|
|
|
|
@app.options.sessions.should.equal true
|
|
|
|
@app.options.foo.should.equal true
|
|
|
|
@app.options.bar.should.equal true
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "can be enabled from top-level" do
|
|
|
|
@app.options.foo.should.be.nil
|
|
|
|
enable :sessions, :foo, :bar
|
|
|
|
@app.options.sessions.should.equal true
|
|
|
|
@app.options.foo.should.equal true
|
|
|
|
@app.options.bar.should.equal true
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "can be disabled on app" do
|
|
|
|
@app.options.foo.should.be.nil
|
|
|
|
@app.disable :sessions, :foo, :bar
|
|
|
|
@app.options.sessions.should.equal false
|
|
|
|
@app.options.foo.should.equal false
|
|
|
|
@app.options.bar.should.equal false
|
|
|
|
end
|
|
|
|
|
|
|
|
specify "can be enabled from top-level" do
|
|
|
|
@app.options.foo.should.be.nil
|
|
|
|
disable :sessions, :foo, :bar
|
|
|
|
@app.options.sessions.should.equal false
|
|
|
|
@app.options.foo.should.equal false
|
|
|
|
@app.options.bar.should.equal false
|
|
|
|
end
|
|
|
|
|
2008-04-15 04:25:09 -04:00
|
|
|
end
|