2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
describe "Routing" do
|
|
|
|
%w[get put post delete head].each do |verb|
|
|
|
|
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 17:00:26 -05:00
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Hello World', response.body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "404s when no route satisfies the request" do
|
|
|
|
mock_app {
|
|
|
|
get('/foo') { }
|
|
|
|
}
|
|
|
|
get '/bar'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 404, status
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "exposes params with indifferent hash" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do
|
2009-01-14 17:00:26 -05:00
|
|
|
fail unless params['foo'] == 'bar'
|
|
|
|
fail unless params[:foo] == 'bar'
|
2008-12-13 16:06:02 -05:00
|
|
|
'well, alright'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/bar'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 'well, alright', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "merges named params and query string params in params" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do
|
2009-01-14 17:00:26 -05:00
|
|
|
fail unless params['foo'] == 'bar'
|
|
|
|
fail unless params['baz'] == 'biz'
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/bar?baz=biz'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
2008-12-13 16:06:02 -05: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 17:00:26 -05:00
|
|
|
assert_equal 'Hello Frank', body
|
2008-12-13 16:06:02 -05: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 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "foo=hello;bar=world", body
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/hello'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "foo=hello;bar=", body
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "foo=;bar=", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports single splat params like /*" do
|
|
|
|
mock_app {
|
|
|
|
get '/*' do
|
2009-01-14 17:00:26 -05:00
|
|
|
fail unless params['splat'].kind_of?(Array)
|
2008-12-13 16:06:02 -05:00
|
|
|
params['splat'].join "\n"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal "foo", body
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/foo/bar/baz'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal "foo/bar/baz", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports mixing multiple splat params like /*/foo/*/*" do
|
|
|
|
mock_app {
|
|
|
|
get '/*/foo/*/*' do
|
2009-01-14 17:00:26 -05:00
|
|
|
fail unless params['splat'].kind_of?(Array)
|
2008-12-13 16:06:02 -05:00
|
|
|
params['splat'].join "\n"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar/foo/bling/baz/boom'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal "bar\nbling\nbaz/boom", body
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/bar/foo/baz'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports mixing named and splat params like /:foo/*" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo/*' do
|
2009-01-14 17:00:26 -05:00
|
|
|
fail unless params['foo'] == 'foo'
|
|
|
|
fail unless params['splat'] == ['bar/baz']
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foo/bar/baz'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports paths that include spaces" do
|
|
|
|
mock_app {
|
|
|
|
get '/path with spaces' do
|
|
|
|
'looks good'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/path%20with%20spaces'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'looks good', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "URL decodes named parameters and splats" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo/*' do
|
2009-01-14 17:00:26 -05:00
|
|
|
fail unless params['foo'] == 'hello world'
|
|
|
|
fail unless params['splat'] == ['how are you']
|
2008-12-13 16:06:02 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/hello%20world/how%20are%20you'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports regular expressions' do
|
|
|
|
mock_app {
|
|
|
|
get(/^\/foo...\/bar$/) do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foooom/bar'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes regular expression captures available in params[:captures]' do
|
|
|
|
mock_app {
|
|
|
|
get(/^\/fo(.*)\/ba(.*)/) do
|
2009-01-14 17:00:26 -05:00
|
|
|
fail unless params[:captures] == ['orooomma', 'f']
|
2008-12-13 16:06:02 -05:00
|
|
|
'right on'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/foorooomma/baf'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'right on', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns response immediately on halt" do
|
|
|
|
mock_app {
|
|
|
|
get '/' do
|
|
|
|
halt 'Hello World'
|
|
|
|
'Boo-hoo World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "transitions to the next matching route on pass" do
|
|
|
|
mock_app {
|
|
|
|
get '/:foo' do
|
|
|
|
pass
|
|
|
|
'Hello Foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/*' do
|
2009-01-14 17:00:26 -05:00
|
|
|
fail if params.include?('foo')
|
2008-12-13 16:06:02 -05:00
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/bar'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05: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 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/foo'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05: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 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05: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 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/foo'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05: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 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/foo', :env => { 'HTTP_HOST' => 'example.com' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes to the next route when user_agent does not match" do
|
|
|
|
mock_app {
|
2009-01-09 04:49:49 -05:00
|
|
|
user_agent(/Foo/)
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/foo' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/foo'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "makes captures in user agent pattern available in params[:agent]" do
|
|
|
|
mock_app {
|
2009-01-09 04:49:49 -05:00
|
|
|
user_agent(/Foo (.*)/)
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/foo' do
|
|
|
|
'Hello ' + params[:agent].first
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal 200, status
|
|
|
|
assert_equal 'Hello Bar', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2009-01-07 14:32:31 -05:00
|
|
|
|
|
|
|
it "filters by accept header" do
|
|
|
|
mock_app {
|
|
|
|
get '/', :provides => :xml do
|
|
|
|
request.env['HTTP_ACCEPT']
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/', :env => { :accept => 'application/xml' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'application/xml', body
|
|
|
|
assert_equal 'application/xml', response.headers['Content-Type']
|
2009-01-07 14:32:31 -05:00
|
|
|
|
|
|
|
get '/', :env => { :accept => 'text/html' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert !ok?
|
2009-01-07 14:32:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows multiple mime types for accept header" do
|
|
|
|
types = ['image/jpeg', 'image/pjpeg']
|
|
|
|
|
|
|
|
mock_app {
|
|
|
|
get '/', :provides => types do
|
|
|
|
request.env['HTTP_ACCEPT']
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
types.each do |type|
|
|
|
|
get '/', :env => { :accept => type }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal type, body
|
|
|
|
assert_equal type, response.headers['Content-Type']
|
2009-01-07 14:32:31 -05:00
|
|
|
end
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|