test/routing_test.rb in sinatra-sinatra-0.9.0.2 vs test/routing_test.rb in sinatra-sinatra-0.9.0.4

- old
+ new

@@ -1,9 +1,14 @@ require File.dirname(__FILE__) + '/helper' +# Helper method for easy route pattern matching testing +def route_def(pattern) + mock_app { get(pattern) { } } +end + describe "Routing" do - %w[get put post delete head].each do |verb| + %w[get put post delete].each do |verb| it "defines #{verb.upcase} request handlers with #{verb}" do mock_app { send verb, '/hello' do 'Hello World' end @@ -14,10 +19,25 @@ assert response.ok? assert_equal 'Hello World', response.body end end + 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 + it "404s when no route satisfies the request" do mock_app { get('/foo') { } } get '/bar' @@ -117,10 +137,42 @@ get '/foo/bar/baz' assert ok? end + it "literally matches . in paths" do + route_def '/test.bar' + + get '/test.bar' + assert ok? + get 'test0bar' + assert not_found? + end + + it "literally matches $ in paths" do + route_def '/test$/' + + get '/test$/' + assert ok? + end + + it "literally matches + in paths" do + route_def '/te+st/' + + get '/te+st/' + assert ok? + get '/teeeeeeest/' + assert not_found? + end + + it "literally matches () in paths" do + route_def '/test(bar)/' + + get '/test(bar)/' + assert ok? + end + it "supports basic nested params" do mock_app { get '/hi' do params["person"]["name"] end @@ -169,15 +221,30 @@ get '/foo' do assert_equal expected, params 'looks good' end } - get "/foo?#{param_string(input)}" + get "/foo?#{build_query(input)}" assert ok? assert_equal 'looks good', body end + 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 + it "supports paths that include spaces" do mock_app { get '/path with spaces' do 'looks good' end @@ -224,10 +291,15 @@ get '/foorooomma/baf' assert ok? assert_equal 'right on', body end + it 'raises a TypeError when pattern is not a String or Regexp' do + @app = mock_app + assert_raise(TypeError) { @app.get(42){} } + end + it "returns response immediately on halt" do mock_app { get '/' do halt 'Hello World' 'Boo-hoo World' @@ -235,9 +307,33 @@ } get '/' assert ok? assert_equal 'Hello World', body + end + + 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 it "transitions to the next matching route on pass" do mock_app { get '/:foo' do