2009-11-09 17:09:51 -05:00
|
|
|
require 'sinatra/base'
|
|
|
|
require 'rack'
|
2009-12-19 05:52:08 -05:00
|
|
|
require 'yaml'
|
2009-11-09 17:09:51 -05:00
|
|
|
|
2009-11-04 17:32:35 -05:00
|
|
|
class TestApp < Sinatra::Base
|
2009-11-05 11:35:45 -05:00
|
|
|
set :root, File.dirname(__FILE__)
|
|
|
|
set :static, true
|
2009-11-04 17:32:35 -05:00
|
|
|
|
|
|
|
get '/' do
|
|
|
|
'Hello world!'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/foo' do
|
|
|
|
'Another World'
|
|
|
|
end
|
2010-06-24 09:51:44 -04:00
|
|
|
|
2009-11-12 12:58:27 -05:00
|
|
|
get '/redirect' do
|
|
|
|
redirect '/redirect_again'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/redirect_again' do
|
|
|
|
redirect '/landed'
|
|
|
|
end
|
|
|
|
|
2011-03-25 05:01:59 -04:00
|
|
|
get '/host' do
|
2011-04-02 10:58:13 -04:00
|
|
|
"Current host is #{request.scheme}://#{request.host}"
|
2011-03-25 05:01:59 -04:00
|
|
|
end
|
|
|
|
|
2010-06-29 17:11:24 -04:00
|
|
|
get '/redirect/:times/times' do
|
|
|
|
times = params[:times].to_i
|
|
|
|
if times.zero?
|
|
|
|
"redirection complete"
|
|
|
|
else
|
|
|
|
redirect "/redirect/#{times - 1}/times"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-12 12:58:27 -05:00
|
|
|
get '/landed' do
|
|
|
|
"You landed"
|
|
|
|
end
|
2009-11-28 18:32:16 -05:00
|
|
|
|
|
|
|
get '/with-quotes' do
|
|
|
|
%q{"No," he said, "you can't do that."}
|
|
|
|
end
|
|
|
|
|
2009-11-24 15:32:25 -05:00
|
|
|
get '/form/get' do
|
2009-11-14 04:44:46 -05:00
|
|
|
'<pre id="results">' + params[:form].to_yaml + '</pre>'
|
2009-11-07 19:13:16 -05:00
|
|
|
end
|
2010-06-24 09:51:44 -04:00
|
|
|
|
2011-05-22 04:37:02 -04:00
|
|
|
post '/relative' do
|
2011-05-22 03:34:46 -04:00
|
|
|
'<pre id="results">' + params[:form].to_yaml + '</pre>'
|
|
|
|
end
|
|
|
|
|
2009-11-24 15:32:25 -05:00
|
|
|
get '/favicon.ico' do
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/redirect' do
|
|
|
|
redirect '/redirect_again'
|
|
|
|
end
|
|
|
|
|
2010-04-09 08:01:17 -04:00
|
|
|
delete "/delete" do
|
|
|
|
"The requested object was deleted"
|
|
|
|
end
|
|
|
|
|
2010-02-12 08:41:41 -05:00
|
|
|
get '/redirect_back' do
|
|
|
|
redirect back
|
|
|
|
end
|
|
|
|
|
2011-02-22 09:53:14 -05:00
|
|
|
get '/slow_response' do
|
|
|
|
sleep 2
|
|
|
|
'Finally!'
|
|
|
|
end
|
|
|
|
|
2010-06-24 09:51:44 -04:00
|
|
|
get '/set_cookie' do
|
|
|
|
cookie_value = 'test_cookie'
|
|
|
|
response.set_cookie('capybara', cookie_value)
|
|
|
|
"Cookie set to #{cookie_value}"
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/get_cookie' do
|
|
|
|
request.cookies['capybara']
|
|
|
|
end
|
|
|
|
|
2011-01-19 06:29:20 -05:00
|
|
|
get '/get_header' do
|
|
|
|
env['HTTP_FOO']
|
|
|
|
end
|
|
|
|
|
2010-02-12 08:41:41 -05:00
|
|
|
get '/:view' do |view|
|
|
|
|
erb view.to_sym
|
|
|
|
end
|
|
|
|
|
2009-11-24 15:32:25 -05:00
|
|
|
post '/form' do
|
2009-11-17 17:36:27 -05:00
|
|
|
'<pre id="results">' + params[:form].to_yaml + '</pre>'
|
|
|
|
end
|
2009-11-12 11:07:43 -05:00
|
|
|
|
2010-12-06 02:22:59 -05:00
|
|
|
post '/upload_empty' do
|
|
|
|
if params[:form][:file].nil?
|
|
|
|
'Successfully ignored empty file field.'
|
|
|
|
else
|
|
|
|
'Something went wrong.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-12 11:07:43 -05:00
|
|
|
post '/upload' do
|
2010-01-06 11:30:48 -05:00
|
|
|
begin
|
|
|
|
buffer = []
|
|
|
|
buffer << "Content-type: #{params[:form][:document][:type]}"
|
|
|
|
buffer << "File content: #{params[:form][:document][:tempfile].read}"
|
|
|
|
buffer.join(' | ')
|
|
|
|
rescue
|
|
|
|
'No file uploaded'
|
|
|
|
end
|
2009-11-12 11:07:43 -05:00
|
|
|
end
|
2010-11-10 17:43:58 -05:00
|
|
|
|
|
|
|
post '/upload_multiple' do
|
|
|
|
begin
|
|
|
|
buffer = []
|
|
|
|
buffer << "Content-type: #{params[:form][:multiple_documents][0][:type]}"
|
|
|
|
buffer << "File content: #{params[:form][:multiple_documents][0][:tempfile].read}"
|
|
|
|
buffer.join(' | ')
|
|
|
|
rescue
|
|
|
|
'No files uploaded'
|
|
|
|
end
|
|
|
|
end
|
2009-11-07 19:13:16 -05:00
|
|
|
end
|
2009-11-09 17:09:51 -05:00
|
|
|
|
|
|
|
if __FILE__ == $0
|
2010-12-17 03:19:47 -05:00
|
|
|
Rack::Handler::WEBrick.run TestApp, :Port => 8070
|
2009-11-09 17:09:51 -05:00
|
|
|
end
|