2010-12-15 16:43:39 +00:00
|
|
|
# I like coding: UTF-8
|
2011-05-11 07:44:02 +00:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-12-13 21:06:02 +00:00
|
|
|
|
2009-01-22 21:54:15 +00:00
|
|
|
# Helper method for easy route pattern matching testing
|
|
|
|
def route_def(pattern)
|
|
|
|
mock_app { get(pattern) { } }
|
|
|
|
end
|
|
|
|
|
2009-05-01 14:29:30 +00:00
|
|
|
class RegexpLookAlike
|
|
|
|
class MatchData
|
|
|
|
def captures
|
|
|
|
["this", "is", "a", "test"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def match(string)
|
|
|
|
::RegexpLookAlike::MatchData.new if string == "/this/is/a/test/"
|
|
|
|
end
|
|
|
|
|
|
|
|
def keys
|
|
|
|
["one", "two", "three", "four"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-26 15:42:13 +00:00
|
|
|
class RoutingTest < Test::Unit::TestCase
|
2011-03-18 18:14:54 +00:00
|
|
|
%w[get put post delete options patch].each do |verb|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "defines #{verb.upcase} request handlers with #{verb}" do
|
|
|
|
mock_app {
|
|
|
|
send verb, '/hello' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
response = request.request(verb.upcase, '/hello', {})
|
2009-01-14 22:00:26 +00:00
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Hello World', response.body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-25 07:41:09 +00:00
|
|
|
it "defines HEAD request handlers with HEAD" do
|
|
|
|
mock_app {
|
|
|
|
head '/hello' do
|
|
|
|
response['X-Hello'] = 'World!'
|
|
|
|
'remove me'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
response = request.request('HEAD', '/hello', {})
|
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'World!', response['X-Hello']
|
|
|
|
assert_equal '', response.body
|
|
|
|
end
|
|
|
|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "404s when no route satisfies the request" do
|
|
|
|
mock_app {
|
|
|
|
get('/foo') { }
|
|
|
|
}
|
|
|
|
get '/bar'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 404, status
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
2009-12-24 02:32:23 +00:00
|
|
|
it "404s and sets X-Cascade header when no route satisfies the request" do
|
|
|
|
mock_app {
|
|
|
|
get('/foo') { }
|
|
|
|
}
|
|
|
|
get '/bar'
|
|
|
|
assert_equal 404, status
|
|
|
|
assert_equal 'pass', response.headers['X-Cascade']
|
|
|
|
end
|
|
|
|
|
2010-12-15 16:43:39 +00:00
|
|
|
it "allows using unicode" do
|
|
|
|
mock_app do
|
|
|
|
get('/föö') { }
|
|
|
|
end
|
|
|
|
get '/f%C3%B6%C3%B6'
|
|
|
|
assert_equal 200, status
|
|
|
|
end
|
|
|
|
|
2011-05-13 15:05:35 +00:00
|
|
|
it "it handles encoded slashes correctly" do
|
|
|
|
mock_app { get("/:a") { |a| a } }
|
|
|
|
get '/foo%2Fbar'
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_body "foo/bar"
|
|
|
|
end
|
|
|
|
|
2009-04-25 16:17:11 +00:00
|
|
|
it "overrides the content-type in error handlers" do
|
2009-04-17 13:22:22 +00:00
|
|
|
mock_app {
|
|
|
|
before { content_type 'text/plain' }
|
2009-04-25 16:17:11 +00:00
|
|
|
error Sinatra::NotFound do
|
|
|
|
content_type "text/html"
|
|
|
|
"<h1>Not Found</h1>"
|
|
|
|
end
|
2009-04-17 13:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert_equal 404, status
|
2010-09-19 15:57:48 +00:00
|
|
|
assert_equal 'text/html;charset=utf-8', response["Content-Type"]
|
2009-04-25 16:17:11 +00:00
|
|
|
assert_equal "<h1>Not Found</h1>", response.body
|
2009-04-17 13:22:22 +00:00
|
|
|
end
|
|
|
|
|
2011-03-30 06:28:25 +00:00
|
|
|
it 'matches empty PATH_INFO to "/" if no route is defined for ""' do
|
|
|
|
mock_app do
|
2010-07-06 18:32:18 +00:00
|
|
|
get '/' do
|
|
|
|
'worked'
|
|
|
|
end
|
2011-03-30 06:28:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
get '/', {}, "PATH_INFO" => ""
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'worked', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches empty PATH_INFO to "" if a route is defined for ""' do
|
|
|
|
mock_app do
|
2011-10-03 21:03:24 +00:00
|
|
|
disable :protection
|
|
|
|
|
2011-03-30 06:28:25 +00:00
|
|
|
get '/' do
|
|
|
|
'did not work'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '' do
|
|
|
|
'worked'
|
|
|
|
end
|
|
|
|
end
|
2010-07-06 18:32:18 +00:00
|
|
|
|
|
|
|
get '/', {}, "PATH_INFO" => ""
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'worked', body
|
|
|
|
end
|
|
|
|
|
2009-02-22 07:42:02 +00:00
|
|
|
it 'takes multiple definitions of a route' do
|
|
|
|
mock_app {
|
|
|
|
user_agent(/Foo/)
|
|
|
|
get '/foo' do
|
|
|
|
'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/foo' do
|
|
|
|
'not foo'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo', {}, 'HTTP_USER_AGENT' => 'Foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'foo', body
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'not foo', body
|
|
|
|
end
|
|
|
|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "exposes params with indifferent hash" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do
|
2009-01-15 12:04:10 +00:00
|
|
|
assert_equal 'bar', params['foo']
|
|
|
|
assert_equal 'bar', params[:foo]
|
2008-12-13 21:06:02 +00:00
|
|
|
'well, alright'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/bar'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 'well, alright', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "merges named params and query string params in params" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do
|
2009-01-15 12:04:10 +00:00
|
|
|
assert_equal 'bar', params['foo']
|
|
|
|
assert_equal 'biz', params['baz']
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/bar?baz=biz'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports named params like /hello/:person" do
|
|
|
|
mock_app {
|
|
|
|
get '/hello/:person' do
|
|
|
|
"Hello #{params['person']}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/hello/Frank'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 'Hello Frank', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports optional named params like /?:foo?/?:bar?" do
|
|
|
|
mock_app {
|
|
|
|
get '/?:foo?/?:bar?' do
|
|
|
|
"foo=#{params[:foo]};bar=#{params[:bar]}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/hello/world'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal "foo=hello;bar=world", body
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/hello'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal "foo=hello;bar=", body
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal "foo=;bar=", body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
2011-02-17 15:11:31 +00:00
|
|
|
it "supports named captures like %r{/hello/(?<person>[^/?#]+)} on Ruby >= 1.9" do
|
|
|
|
next if RUBY_VERSION < '1.9'
|
2011-01-24 21:15:48 +00:00
|
|
|
mock_app {
|
2011-02-17 15:11:31 +00:00
|
|
|
get Regexp.new('/hello/(?<person>[^/?#]+)') do
|
2011-01-24 21:15:48 +00:00
|
|
|
"Hello #{params['person']}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/hello/Frank'
|
|
|
|
assert_equal 'Hello Frank', body
|
|
|
|
end
|
|
|
|
|
2011-02-17 15:11:31 +00:00
|
|
|
it "supports optional named captures like %r{/page(?<format>.[^/?#]+)?} on Ruby >= 1.9" do
|
|
|
|
next if RUBY_VERSION < '1.9'
|
2011-01-24 21:15:48 +00:00
|
|
|
mock_app {
|
2011-02-17 15:11:31 +00:00
|
|
|
get Regexp.new('/page(?<format>.[^/?#]+)?') do
|
2011-01-24 21:15:48 +00:00
|
|
|
"format=#{params[:format]}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/page.html'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "format=.html", body
|
|
|
|
|
|
|
|
get '/page.xml'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "format=.xml", body
|
|
|
|
|
|
|
|
get '/page'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "format=", body
|
|
|
|
end
|
|
|
|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "supports single splat params like /*" do
|
|
|
|
mock_app {
|
|
|
|
get '/*' do
|
2009-01-15 12:04:10 +00:00
|
|
|
assert params['splat'].kind_of?(Array)
|
2008-12-13 21:06:02 +00:00
|
|
|
params['splat'].join "\n"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal "foo", body
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/foo/bar/baz'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal "foo/bar/baz", body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports mixing multiple splat params like /*/foo/*/*" do
|
|
|
|
mock_app {
|
|
|
|
get '/*/foo/*/*' do
|
2009-01-15 12:04:10 +00:00
|
|
|
assert params['splat'].kind_of?(Array)
|
2008-12-13 21:06:02 +00:00
|
|
|
params['splat'].join "\n"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar/foo/bling/baz/boom'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal "bar\nbling\nbaz/boom", body
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/bar/foo/baz'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert not_found?
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports mixing named and splat params like /:foo/*" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo/*' do
|
2009-01-15 12:04:10 +00:00
|
|
|
assert_equal 'foo', params['foo']
|
|
|
|
assert_equal ['bar/baz'], params['splat']
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo/bar/baz'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
2009-02-17 17:32:52 +00:00
|
|
|
|
|
|
|
it "matches a dot ('.') as part of a named param" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo/:bar' do
|
|
|
|
params[:foo]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/user@example.com/name'
|
|
|
|
assert_equal 200, response.status
|
|
|
|
assert_equal 'user@example.com', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "matches a literal dot ('.') outside of named params" do
|
|
|
|
mock_app {
|
|
|
|
get '/:file.:ext' do
|
|
|
|
assert_equal 'pony', params[:file]
|
|
|
|
assert_equal 'jpg', params[:ext]
|
|
|
|
'right on'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/pony.jpg'
|
|
|
|
assert_equal 200, response.status
|
|
|
|
assert_equal 'right on', body
|
|
|
|
end
|
2008-12-13 21:06:02 +00:00
|
|
|
|
2011-01-24 04:40:00 +00:00
|
|
|
it "literally matches dot in paths" do
|
2009-01-22 21:54:15 +00:00
|
|
|
route_def '/test.bar'
|
|
|
|
|
|
|
|
get '/test.bar'
|
|
|
|
assert ok?
|
|
|
|
get 'test0bar'
|
|
|
|
assert not_found?
|
|
|
|
end
|
|
|
|
|
2011-01-24 04:40:00 +00:00
|
|
|
it "literally matches dollar sign in paths" do
|
2009-01-22 21:54:15 +00:00
|
|
|
route_def '/test$/'
|
|
|
|
|
|
|
|
get '/test$/'
|
|
|
|
assert ok?
|
|
|
|
end
|
|
|
|
|
2011-01-24 04:40:00 +00:00
|
|
|
it "literally matches plus sign in paths" do
|
2009-01-22 21:54:15 +00:00
|
|
|
route_def '/te+st/'
|
|
|
|
|
2009-02-17 16:07:37 +00:00
|
|
|
get '/te%2Bst/'
|
2009-01-22 21:54:15 +00:00
|
|
|
assert ok?
|
|
|
|
get '/teeeeeeest/'
|
|
|
|
assert not_found?
|
|
|
|
end
|
|
|
|
|
2011-01-24 04:40:00 +00:00
|
|
|
it "literally matches parens in paths" do
|
2009-01-22 21:54:15 +00:00
|
|
|
route_def '/test(bar)/'
|
|
|
|
|
|
|
|
get '/test(bar)/'
|
|
|
|
assert ok?
|
|
|
|
end
|
|
|
|
|
2009-01-15 12:24:45 +00:00
|
|
|
it "supports basic nested params" do
|
|
|
|
mock_app {
|
|
|
|
get '/hi' do
|
|
|
|
params["person"]["name"]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get "/hi?person[name]=John+Doe"
|
|
|
|
assert ok?
|
|
|
|
assert_equal "John Doe", body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "exposes nested params with indifferent hash" do
|
|
|
|
mock_app {
|
|
|
|
get '/testme' do
|
|
|
|
assert_equal 'baz', params['bar']['foo']
|
|
|
|
assert_equal 'baz', params['bar'][:foo]
|
|
|
|
'well, alright'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/testme?bar[foo]=baz'
|
|
|
|
assert_equal 'well, alright', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports deeply nested params" do
|
2009-03-28 15:28:18 +00:00
|
|
|
expected_params = {
|
2009-01-15 12:24:45 +00:00
|
|
|
"emacs" => {
|
|
|
|
"map" => { "goto-line" => "M-g g" },
|
|
|
|
"version" => "22.3.1"
|
|
|
|
},
|
|
|
|
"browser" => {
|
|
|
|
"firefox" => {"engine" => {"name"=>"spidermonkey", "version"=>"1.7.0"}},
|
|
|
|
"chrome" => {"engine" => {"name"=>"V8", "version"=>"1.0"}}
|
|
|
|
},
|
|
|
|
"paste" => {"name"=>"hello world", "syntax"=>"ruby"}
|
|
|
|
}
|
|
|
|
mock_app {
|
|
|
|
get '/foo' do
|
2009-03-28 15:28:18 +00:00
|
|
|
assert_equal expected_params, params
|
2009-01-15 12:24:45 +00:00
|
|
|
'looks good'
|
|
|
|
end
|
|
|
|
}
|
2009-03-28 15:28:18 +00:00
|
|
|
get '/foo', expected_params
|
2009-01-15 12:24:45 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'looks good', body
|
|
|
|
end
|
|
|
|
|
2009-01-22 02:55:33 +00:00
|
|
|
it "preserves non-nested params" do
|
|
|
|
mock_app {
|
|
|
|
get '/foo' do
|
|
|
|
assert_equal "2", params["article_id"]
|
|
|
|
assert_equal "awesome", params['comment']['body']
|
|
|
|
assert_nil params['comment[body]']
|
|
|
|
'looks good'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo?article_id=2&comment[body]=awesome'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'looks good', body
|
|
|
|
end
|
|
|
|
|
2009-02-17 16:07:37 +00:00
|
|
|
it "matches paths that include spaces encoded with %20" do
|
2008-12-13 21:06:02 +00:00
|
|
|
mock_app {
|
|
|
|
get '/path with spaces' do
|
|
|
|
'looks good'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/path%20with%20spaces'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'looks good', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
2009-02-17 16:07:37 +00:00
|
|
|
it "matches paths that include spaces encoded with +" do
|
|
|
|
mock_app {
|
|
|
|
get '/path with spaces' do
|
|
|
|
'looks good'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/path+with+spaces'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'looks good', body
|
|
|
|
end
|
|
|
|
|
2010-09-03 06:49:30 +00:00
|
|
|
it "matches paths that include ampersands" do
|
|
|
|
mock_app {
|
|
|
|
get '/:name' do
|
|
|
|
'looks good'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo&bar'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'looks good', body
|
|
|
|
end
|
|
|
|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "URL decodes named parameters and splats" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo/*' do
|
2009-01-15 12:04:10 +00:00
|
|
|
assert_equal 'hello world', params['foo']
|
|
|
|
assert_equal ['how are you'], params['splat']
|
2008-12-13 21:06:02 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/hello%20world/how%20are%20you'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports regular expressions' do
|
|
|
|
mock_app {
|
|
|
|
get(/^\/foo...\/bar$/) do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foooom/bar'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes regular expression captures available in params[:captures]' do
|
|
|
|
mock_app {
|
|
|
|
get(/^\/fo(.*)\/ba(.*)/) do
|
2009-01-15 12:04:10 +00:00
|
|
|
assert_equal ['orooomma', 'f'], params[:captures]
|
2008-12-13 21:06:02 +00:00
|
|
|
'right on'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foorooomma/baf'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'right on', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
2009-05-01 14:29:30 +00:00
|
|
|
it 'supports regular expression look-alike routes' do
|
|
|
|
mock_app {
|
|
|
|
get(RegexpLookAlike.new) do
|
|
|
|
assert_equal 'this', params[:one]
|
|
|
|
assert_equal 'is', params[:two]
|
|
|
|
assert_equal 'a', params[:three]
|
|
|
|
assert_equal 'test', params[:four]
|
|
|
|
'right on'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/this/is/a/test/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'right on', body
|
|
|
|
end
|
|
|
|
|
2009-01-20 01:58:26 +00:00
|
|
|
it 'raises a TypeError when pattern is not a String or Regexp' do
|
2009-03-28 15:28:18 +00:00
|
|
|
assert_raise(TypeError) {
|
|
|
|
mock_app { get(42){} }
|
|
|
|
}
|
2009-01-20 01:58:26 +00:00
|
|
|
end
|
|
|
|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "returns response immediately on halt" do
|
|
|
|
mock_app {
|
|
|
|
get '/' do
|
|
|
|
halt 'Hello World'
|
|
|
|
'Boo-hoo World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
2009-01-25 07:41:09 +00:00
|
|
|
it "halts with a response tuple" do
|
|
|
|
mock_app {
|
|
|
|
get '/' do
|
|
|
|
halt 295, {'Content-Type' => 'text/plain'}, 'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert_equal 295, status
|
|
|
|
assert_equal 'text/plain', response['Content-Type']
|
|
|
|
assert_equal 'Hello World', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "halts with an array of strings" do
|
|
|
|
mock_app {
|
|
|
|
get '/' do
|
|
|
|
halt %w[Hello World How Are You]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert_equal 'HelloWorldHowAreYou', body
|
|
|
|
end
|
|
|
|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "transitions to the next matching route on pass" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do
|
|
|
|
pass
|
|
|
|
'Hello Foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/*' do
|
2009-01-15 12:04:10 +00:00
|
|
|
assert !params.include?('foo')
|
2008-12-13 21:06:02 +00:00
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "transitions to 404 when passed and no subsequent route matches" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do
|
|
|
|
pass
|
|
|
|
'Hello Foo'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert not_found?
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
2009-12-24 02:32:23 +00:00
|
|
|
it "transitions to 404 and sets X-Cascade header when passed and no subsequent route matches" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do
|
|
|
|
pass
|
|
|
|
'Hello Foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/bar' do
|
|
|
|
'Hello Bar'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert not_found?
|
|
|
|
assert_equal 'pass', response.headers['X-Cascade']
|
|
|
|
end
|
|
|
|
|
2009-12-10 06:11:02 +00:00
|
|
|
it "uses optional block passed to pass as route block if no other route is found" do
|
|
|
|
mock_app {
|
|
|
|
get "/" do
|
|
|
|
pass do
|
|
|
|
"this"
|
|
|
|
end
|
|
|
|
"not this"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert ok?
|
|
|
|
assert "this", body
|
|
|
|
end
|
|
|
|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "passes when matching condition returns false" do
|
|
|
|
mock_app {
|
|
|
|
condition { params[:foo] == 'bar' }
|
|
|
|
get '/:foo' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/foo'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert not_found?
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not pass when matching condition returns nil" do
|
|
|
|
mock_app {
|
|
|
|
condition { nil }
|
|
|
|
get '/:foo' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes to next route when condition calls pass explicitly" do
|
|
|
|
mock_app {
|
|
|
|
condition { pass unless params[:foo] == 'bar' }
|
|
|
|
get '/:foo' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/foo'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert not_found?
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes to the next route when host_name does not match" do
|
|
|
|
mock_app {
|
|
|
|
host_name 'example.com'
|
|
|
|
get '/foo' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/foo'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert not_found?
|
2008-12-13 21:06:02 +00:00
|
|
|
|
2009-03-28 15:28:18 +00:00
|
|
|
get '/foo', {}, { 'HTTP_HOST' => 'example.com' }
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes to the next route when user_agent does not match" do
|
|
|
|
mock_app {
|
2009-01-09 09:49:49 +00:00
|
|
|
user_agent(/Foo/)
|
2008-12-13 21:06:02 +00:00
|
|
|
get '/foo' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/foo'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert not_found?
|
2008-12-13 21:06:02 +00:00
|
|
|
|
2009-03-28 15:28:18 +00:00
|
|
|
get '/foo', {}, { 'HTTP_USER_AGENT' => 'Foo Bar' }
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
2011-01-16 16:31:39 +00:00
|
|
|
it "treats missing user agent like an empty string" do
|
|
|
|
mock_app do
|
|
|
|
user_agent(/.*/)
|
|
|
|
get '/' do
|
|
|
|
"Hello World"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
get '/'
|
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'Hello World', body
|
|
|
|
end
|
|
|
|
|
2008-12-13 21:06:02 +00:00
|
|
|
it "makes captures in user agent pattern available in params[:agent]" do
|
|
|
|
mock_app {
|
2009-01-09 09:49:49 +00:00
|
|
|
user_agent(/Foo (.*)/)
|
2008-12-13 21:06:02 +00:00
|
|
|
get '/foo' do
|
|
|
|
'Hello ' + params[:agent].first
|
|
|
|
end
|
|
|
|
}
|
2009-03-28 15:28:18 +00:00
|
|
|
get '/foo', {}, { 'HTTP_USER_AGENT' => 'Foo Bar' }
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'Hello Bar', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
2009-01-07 19:32:31 +00:00
|
|
|
|
|
|
|
it "filters by accept header" do
|
|
|
|
mock_app {
|
|
|
|
get '/', :provides => :xml do
|
2011-04-17 10:43:22 +00:00
|
|
|
env['HTTP_ACCEPT']
|
2009-01-07 19:32:31 +00:00
|
|
|
end
|
2010-04-27 18:50:29 +00:00
|
|
|
get '/foo', :provides => :html do
|
2011-04-17 10:43:22 +00:00
|
|
|
env['HTTP_ACCEPT']
|
2010-04-27 18:50:29 +00:00
|
|
|
end
|
2009-01-07 19:32:31 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 15:28:18 +00:00
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'application/xml' }
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'application/xml', body
|
2011-02-10 08:32:47 +00:00
|
|
|
assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']
|
2009-01-07 19:32:31 +00:00
|
|
|
|
2009-03-28 15:28:18 +00:00
|
|
|
get '/', {}, { :accept => 'text/html' }
|
2009-01-14 22:00:26 +00:00
|
|
|
assert !ok?
|
2010-04-27 18:50:29 +00:00
|
|
|
|
|
|
|
get '/foo', {}, { 'HTTP_ACCEPT' => 'text/html;q=0.9' }
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'text/html;q=0.9', body
|
|
|
|
|
|
|
|
get '/foo', {}, { 'HTTP_ACCEPT' => '' }
|
|
|
|
assert !ok?
|
2009-01-07 19:32:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows multiple mime types for accept header" do
|
|
|
|
types = ['image/jpeg', 'image/pjpeg']
|
|
|
|
|
|
|
|
mock_app {
|
|
|
|
get '/', :provides => types do
|
2011-04-17 10:43:22 +00:00
|
|
|
env['HTTP_ACCEPT']
|
2009-01-07 19:32:31 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
types.each do |type|
|
2009-03-28 15:28:18 +00:00
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => type }
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal type, body
|
|
|
|
assert_equal type, response.headers['Content-Type']
|
2009-01-07 19:32:31 +00:00
|
|
|
end
|
|
|
|
end
|
2009-01-28 18:55:49 +00:00
|
|
|
|
|
|
|
it 'degrades gracefully when optional accept header is not provided' do
|
|
|
|
mock_app {
|
|
|
|
get '/', :provides => :xml do
|
2011-04-17 10:43:22 +00:00
|
|
|
env['HTTP_ACCEPT']
|
2009-01-28 18:55:49 +00:00
|
|
|
end
|
|
|
|
get '/' do
|
|
|
|
'default'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'default', body
|
|
|
|
end
|
2009-01-28 20:27:32 +00:00
|
|
|
|
2011-03-25 12:49:01 +00:00
|
|
|
it 'respects user agent prefferences for the content type' do
|
|
|
|
mock_app { get('/', :provides => [:png, :html]) { content_type }}
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'image/png;q=0.5,text/html;q=0.8' }
|
|
|
|
assert_body 'text/html;charset=utf-8'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'image/png;q=0.8,text/html;q=0.5' }
|
|
|
|
assert_body 'image/png'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts generic types' do
|
|
|
|
mock_app do
|
|
|
|
get('/', :provides => :xml) { content_type }
|
|
|
|
get('/') { 'no match' }
|
|
|
|
end
|
|
|
|
get '/'
|
|
|
|
assert_body 'no match'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'foo/*' }
|
|
|
|
assert_body 'no match'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'application/*' }
|
|
|
|
assert_body 'application/xml;charset=utf-8'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => '*/*' }
|
|
|
|
assert_body 'application/xml;charset=utf-8'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prefers concrete over partly generic types' do
|
|
|
|
mock_app { get('/', :provides => [:png, :html]) { content_type }}
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'image/*, text/html' }
|
|
|
|
assert_body 'text/html;charset=utf-8'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'image/png, text/*' }
|
|
|
|
assert_body 'image/png'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prefers concrete over fully generic types' do
|
|
|
|
mock_app { get('/', :provides => [:png, :html]) { content_type }}
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => '*/*, text/html' }
|
|
|
|
assert_body 'text/html;charset=utf-8'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'image/png, */*' }
|
|
|
|
assert_body 'image/png'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prefers partly generic over fully generic types' do
|
|
|
|
mock_app { get('/', :provides => [:png, :html]) { content_type }}
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => '*/*, text/*' }
|
|
|
|
assert_body 'text/html;charset=utf-8'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'image/*, */*' }
|
|
|
|
assert_body 'image/png'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'respects quality with generic types' do
|
|
|
|
mock_app { get('/', :provides => [:png, :html]) { content_type }}
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'image/*;q=1, text/html;q=0' }
|
|
|
|
assert_body 'image/png'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'image/png;q=0.5, text/*;q=0.7' }
|
|
|
|
assert_body 'text/html;charset=utf-8'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts both text/javascript and application/javascript for js' do
|
|
|
|
mock_app { get('/', :provides => :js) { content_type }}
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'application/javascript' }
|
|
|
|
assert_body 'application/javascript;charset=utf-8'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'text/javascript' }
|
|
|
|
assert_body 'text/javascript;charset=utf-8'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts both text/xml and application/xml for xml' do
|
|
|
|
mock_app { get('/', :provides => :xml) { content_type }}
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'application/xml' }
|
|
|
|
assert_body 'application/xml;charset=utf-8'
|
|
|
|
get '/', {}, { 'HTTP_ACCEPT' => 'text/xml' }
|
|
|
|
assert_body 'text/xml;charset=utf-8'
|
|
|
|
end
|
|
|
|
|
2009-01-28 20:27:32 +00:00
|
|
|
it 'passes a single url param as block parameters when one param is specified' do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do |foo|
|
|
|
|
assert_equal 'bar', foo
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar'
|
2009-01-31 23:07:13 +00:00
|
|
|
assert ok?
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes multiple params as block parameters when many are specified' do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo/:bar/:baz' do |foo, bar, baz|
|
|
|
|
assert_equal 'abc', foo
|
|
|
|
assert_equal 'def', bar
|
|
|
|
assert_equal 'ghi', baz
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/abc/def/ghi'
|
2009-01-31 23:07:13 +00:00
|
|
|
assert ok?
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes regular expression captures as block parameters' do
|
|
|
|
mock_app {
|
|
|
|
get(/^\/fo(.*)\/ba(.*)/) do |foo, bar|
|
|
|
|
assert_equal 'orooomma', foo
|
|
|
|
assert_equal 'f', bar
|
2009-02-02 08:18:43 +00:00
|
|
|
'looks good'
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foorooomma/baf'
|
2009-01-31 23:07:13 +00:00
|
|
|
assert ok?
|
2009-02-02 08:18:43 +00:00
|
|
|
assert_equal 'looks good', body
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports mixing multiple splat params like /*/foo/*/* as block parameters" do
|
|
|
|
mock_app {
|
|
|
|
get '/*/foo/*/*' do |foo, bar, baz|
|
|
|
|
assert_equal 'bar', foo
|
|
|
|
assert_equal 'bling', bar
|
|
|
|
assert_equal 'baz/boom', baz
|
2009-02-02 08:18:43 +00:00
|
|
|
'looks good'
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar/foo/bling/baz/boom'
|
2009-01-31 23:07:13 +00:00
|
|
|
assert ok?
|
2009-02-02 08:18:43 +00:00
|
|
|
assert_equal 'looks good', body
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
|
2009-02-02 08:18:43 +00:00
|
|
|
it 'raises an ArgumentError with block arity > 1 and too many values' do
|
2011-03-17 10:28:06 +00:00
|
|
|
mock_app do
|
2009-01-28 20:27:32 +00:00
|
|
|
get '/:foo/:bar/:baz' do |foo, bar|
|
|
|
|
'quux'
|
|
|
|
end
|
2011-03-17 10:28:06 +00:00
|
|
|
end
|
2009-01-28 20:27:32 +00:00
|
|
|
|
|
|
|
assert_raise(ArgumentError) { get '/a/b/c' }
|
|
|
|
end
|
|
|
|
|
2009-02-02 08:18:43 +00:00
|
|
|
it 'raises an ArgumentError with block param arity > 1 and too few values' do
|
2009-01-28 20:27:32 +00:00
|
|
|
mock_app {
|
2009-02-02 08:18:43 +00:00
|
|
|
get '/:foo/:bar' do |foo, bar, baz|
|
2009-01-28 20:27:32 +00:00
|
|
|
'quux'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2009-02-02 08:18:43 +00:00
|
|
|
assert_raise(ArgumentError) { get '/a/b' }
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
|
2009-02-02 08:18:43 +00:00
|
|
|
it 'succeeds if no block parameters are specified' do
|
2009-01-28 20:27:32 +00:00
|
|
|
mock_app {
|
2009-02-02 08:18:43 +00:00
|
|
|
get '/:foo/:bar' do
|
2009-01-28 20:27:32 +00:00
|
|
|
'quux'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2009-02-02 08:18:43 +00:00
|
|
|
get '/a/b'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'quux', body
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
|
2009-02-02 08:18:43 +00:00
|
|
|
it 'passes all params with block param arity -1 (splat args)' do
|
2009-01-28 20:27:32 +00:00
|
|
|
mock_app {
|
2009-02-02 08:18:43 +00:00
|
|
|
get '/:foo/:bar' do |*args|
|
|
|
|
args.join
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2009-02-02 08:18:43 +00:00
|
|
|
get '/a/b'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'ab', body
|
2009-01-28 20:27:32 +00:00
|
|
|
end
|
|
|
|
|
2009-04-07 00:57:52 +00:00
|
|
|
it 'allows custom route-conditions to be set via route options' do
|
|
|
|
protector = Module.new {
|
|
|
|
def protect(*args)
|
|
|
|
condition {
|
|
|
|
unless authorize(params["user"], params["password"])
|
|
|
|
halt 403, "go away"
|
|
|
|
end
|
|
|
|
}
|
2009-04-06 23:58:39 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
mock_app {
|
2009-04-07 00:57:52 +00:00
|
|
|
register protector
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def authorize(username, password)
|
|
|
|
username == "foo" && password == "bar"
|
|
|
|
end
|
2009-04-06 23:58:39 +00:00
|
|
|
end
|
2009-04-07 00:57:52 +00:00
|
|
|
|
|
|
|
get "/", :protect => true do
|
|
|
|
"hey"
|
2009-04-06 23:58:39 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2009-04-07 00:57:52 +00:00
|
|
|
get "/"
|
|
|
|
assert forbidden?
|
|
|
|
assert_equal "go away", body
|
2009-04-06 23:58:39 +00:00
|
|
|
|
2009-04-07 00:57:52 +00:00
|
|
|
get "/", :user => "foo", :password => "bar"
|
2009-04-06 23:58:39 +00:00
|
|
|
assert ok?
|
2009-04-07 00:57:52 +00:00
|
|
|
assert_equal "hey", body
|
2009-04-06 23:58:39 +00:00
|
|
|
end
|
|
|
|
|
2009-02-02 08:18:43 +00:00
|
|
|
# NOTE Block params behaves differently under 1.8 and 1.9. Under 1.8, block
|
|
|
|
# param arity is lax: declaring a mismatched number of block params results
|
|
|
|
# in a warning. Under 1.9, block param arity is strict: mismatched block
|
|
|
|
# arity raises an ArgumentError.
|
|
|
|
|
|
|
|
if RUBY_VERSION >= '1.9'
|
|
|
|
|
|
|
|
it 'raises an ArgumentError with block param arity 1 and no values' do
|
|
|
|
mock_app {
|
|
|
|
get '/foo' do |foo|
|
|
|
|
'quux'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) { get '/foo' }
|
|
|
|
end
|
|
|
|
|
2009-02-03 00:09:44 +00:00
|
|
|
it 'raises an ArgumentError with block param arity 1 and too many values' do
|
2009-02-02 08:18:43 +00:00
|
|
|
mock_app {
|
|
|
|
get '/:foo/:bar/:baz' do |foo|
|
|
|
|
'quux'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2009-02-03 00:09:44 +00:00
|
|
|
assert_raise(ArgumentError) { get '/a/b/c' }
|
2009-02-02 08:18:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
it 'does not raise an ArgumentError with block param arity 1 and no values' do
|
|
|
|
mock_app {
|
|
|
|
get '/foo' do |foo|
|
|
|
|
'quux'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
silence_warnings { get '/foo' }
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'quux', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise an ArgumentError with block param arity 1 and too many values' do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo/:bar/:baz' do |foo|
|
|
|
|
'quux'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
silence_warnings { get '/a/b/c' }
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'quux', body
|
|
|
|
end
|
2009-01-28 20:27:32 +00:00
|
|
|
|
|
|
|
end
|
2009-03-25 17:55:16 +00:00
|
|
|
|
|
|
|
it "matches routes defined in superclasses" do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.get('/foo') { 'foo in baseclass' }
|
|
|
|
|
|
|
|
mock_app(base) {
|
|
|
|
get('/bar') { 'bar in subclass' }
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'foo in baseclass', body
|
|
|
|
|
|
|
|
get '/bar'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar in subclass', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "matches routes in subclasses before superclasses" do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.get('/foo') { 'foo in baseclass' }
|
|
|
|
base.get('/bar') { 'bar in baseclass' }
|
|
|
|
|
|
|
|
mock_app(base) {
|
|
|
|
get('/foo') { 'foo in subclass' }
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'foo in subclass', body
|
|
|
|
|
|
|
|
get '/bar'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar in baseclass', body
|
|
|
|
end
|
2011-03-05 16:25:37 +00:00
|
|
|
|
|
|
|
it "adds hostname condition when it is in options" do
|
|
|
|
mock_app {
|
|
|
|
get '/foo', :host => 'host' do
|
|
|
|
'foo'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert not_found?
|
|
|
|
end
|
2011-04-14 07:58:44 +00:00
|
|
|
|
2011-04-17 10:56:03 +00:00
|
|
|
it 'allows using call to fire another request internally' do
|
|
|
|
mock_app do
|
|
|
|
get '/foo' do
|
|
|
|
status, headers, body = call env.merge("PATH_INFO" => '/bar')
|
2011-05-13 08:35:17 +00:00
|
|
|
[status, headers, body.each.map(&:upcase)]
|
2011-04-17 10:56:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
get '/bar' do
|
|
|
|
"bar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_body "BAR"
|
|
|
|
end
|
|
|
|
|
2011-04-17 10:46:39 +00:00
|
|
|
it 'plays well with other routing middleware' do
|
2011-04-14 07:58:44 +00:00
|
|
|
middleware = Sinatra.new
|
|
|
|
inner_app = Sinatra.new { get('/foo') { 'hello' } }
|
|
|
|
builder = Rack::Builder.new do
|
|
|
|
use middleware
|
|
|
|
map('/test') { run inner_app }
|
|
|
|
end
|
|
|
|
|
|
|
|
@app = builder.to_app
|
|
|
|
get '/test/foo'
|
|
|
|
assert ok?
|
|
|
|
assert_body 'hello'
|
|
|
|
end
|
2011-08-17 11:18:20 +00:00
|
|
|
|
|
|
|
it 'returns the route signature' do
|
|
|
|
signature = list = nil
|
|
|
|
|
|
|
|
mock_app do
|
|
|
|
signature = post('/') { }
|
|
|
|
list = routes['POST']
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal Array, signature.class
|
|
|
|
assert_equal 4, signature.length
|
|
|
|
assert list.include?(signature)
|
|
|
|
end
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|