1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/test/readme_test.rb

131 lines
2.7 KiB
Ruby
Raw Normal View History

2011-05-01 07:51:11 -04:00
# Tests to check if all the README examples work.
require File.expand_path('../helper', __FILE__)
2011-05-01 07:51:11 -04:00
class ReadmeTest < Minitest::Test
example do
mock_app { get('/') { 'Hello world!' } }
get '/'
assert_body 'Hello world!'
2011-05-01 07:51:11 -04:00
end
section "Routes" do
example do
mock_app do
get('/') { ".. show something .." }
2011-05-01 07:51:11 -04:00
post('/') { ".. create something .." }
2011-05-01 07:51:11 -04:00
put('/') { ".. replace something .." }
2011-05-01 07:51:11 -04:00
patch('/') { ".. modify something .." }
2011-05-01 07:51:11 -04:00
delete('/') { ".. annihilate something .." }
2011-05-01 07:51:11 -04:00
options('/') { ".. appease something .." }
link('/') { ".. affiliate something .." }
unlink('/') { ".. separate something .." }
2011-05-01 07:51:11 -04:00
end
get '/'
assert_body '.. show something ..'
post '/'
assert_body '.. create something ..'
put '/'
assert_body '.. replace something ..'
patch '/'
assert_body '.. modify something ..'
delete '/'
assert_body '.. annihilate something ..'
options '/'
assert_body '.. appease something ..'
link '/'
assert_body '.. affiliate something ..'
unlink '/'
assert_body '.. separate something ..'
2011-05-01 07:51:11 -04:00
end
example do
mock_app do
get('/hello/:name') do
2011-05-01 07:51:11 -04:00
# matches "GET /hello/foo" and "GET /hello/bar"
# params[:name] is 'foo' or 'bar'
"Hello #{params[:name]}!"
end
end
get '/hello/foo'
assert_body 'Hello foo!'
get '/hello/bar'
assert_body 'Hello bar!'
end
example do
mock_app { get('/hello/:name') { |n| "Hello #{n}!" } }
2011-05-01 07:51:11 -04:00
get '/hello/foo'
assert_body 'Hello foo!'
get '/hello/bar'
assert_body 'Hello bar!'
end
example do
mock_app do
get('/say/*/to/*') do
2011-05-01 07:51:11 -04:00
# matches /say/hello/to/world
params[:splat].inspect # => ["hello", "world"]
end
get('/download/*.*') do
2011-05-01 07:51:11 -04:00
# matches /download/path/to/file.xml
params[:splat].inspect # => ["path/to/file", "xml"]
end
end
get "/say/hello/to/world"
assert_body '["hello", "world"]'
get "/download/path/to/file.xml"
assert_body '["path/to/file", "xml"]'
end
example do
mock_app do
get(%r{/hello/([\w]+)}) {
2011-05-01 07:51:11 -04:00
"Hello, #{params[:captures].first}!"
}
2011-05-01 07:51:11 -04:00
end
get '/hello/foo'
assert_body 'Hello, foo!'
get '/hello/bar'
assert_body 'Hello, bar!'
end
example do
mock_app do
get( %r{/hello/([\w]+)}) { |c|
2011-05-01 07:51:11 -04:00
"Hello, #{c}!"
}
2011-05-01 07:51:11 -04:00
end
get '/hello/foo'
assert_body 'Hello, foo!'
get '/hello/bar'
assert_body 'Hello, bar!'
end
end
end