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

Started test for Sinatra::Test

This commit is contained in:
Simon Rozet 2009-01-28 22:53:40 +01:00 committed by Ryan Tomayko
parent fa6eb90bf3
commit c011f2b571

View file

@ -1,21 +1,108 @@
require File.dirname(__FILE__) + '/helper'
describe "Sinatra::Test" do
it "support nested parameters" do
describe 'Sinatra::Test' do
def request
YAML.load(body)
end
def request_body
request['test.body']
end
def request_params
YAML.load(request['test.params'])
end
before do
mock_app {
get '/' do
params[:post][:title]
%w[get head post put delete].each { |verb|
send(verb, '/') do
env.update('test.body' => request.body.read)
env.update('test.params' => params.to_yaml)
env.to_yaml
end
}
get '/redirect_me' do
redirect '/there'
end
post '/' do
params[:post][:content]
get '/there' do
"you've been redirected"
end
}
end
get '/', :post => { :title => 'My Post Title' }
assert_equal 'My Post Title', body
it 'allows GET/POST/PUT/DELETE' do
get '/'
assert_equal('GET', request['REQUEST_METHOD'])
post '/', :post => { :content => 'Post Content' }
assert_equal 'Post Content', body
post '/'
assert_equal('POST', request['REQUEST_METHOD'])
put '/'
assert_equal('PUT', request['REQUEST_METHOD'])
delete '/'
assert_equal('DELETE', request['REQUEST_METHOD'])
end
it 'allows to specify a body' do
post '/', '42'
assert_equal '42', request_body
end
it 'allows to specify params' do
get '/', :foo => 'bar'
assert_equal 'bar', request_params['foo']
end
it 'supports nested params' do
get '/', :foo => { :x => 'y', :chunky => 'bacon' }
assert_equal "y", request_params['foo']['x']
assert_equal "bacon", request_params['foo']['chunky']
end
it 'provides easy access to response status and body' do
get '/'
assert_equal 200, status
assert body =~ /^---/
end
it 'delegates methods to @response' do
get '/'
assert ok?
end
it 'follows redirect' do
get '/redirect_me'
follow!
assert_equal "you've been redirected", body
end
it 'provides sugar for common HTTP headers' do
get '/', :env => { :accept => 'text/plain' }
assert_equal 'text/plain', request['HTTP_ACCEPT']
get '/', :env => { :agent => 'TATFT' }
assert_equal 'TATFT', request['HTTP_USER_AGENT']
get '/', :env => { :host => '1.2.3.4' }
assert_equal '1.2.3.4', request['HTTP_HOST']
get '/', :env => { :session => 'foo' }
assert_equal 'foo', request['HTTP_COOKIE']
get '/', :env => { :cookies => 'foo' }
assert_equal 'foo', request['HTTP_COOKIE']
get '/', :env => { :content_type => 'text/plain' }
assert_equal 'text/plain', request['CONTENT_TYPE']
end
def test_TestHarness
session = Sinatra::TestHarness.new(@app)
response = session.get('/')
assert_equal 200, response.status
end
end