2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2010-09-01 06:18:22 -04:00
|
|
|
require 'stringio'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class RequestTest < Test::Unit::TestCase
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'responds to #user_agent' do
|
|
|
|
request = Sinatra::Request.new({'HTTP_USER_AGENT' => 'Test'})
|
2009-01-14 17:00:26 -05:00
|
|
|
assert request.respond_to?(:user_agent)
|
|
|
|
assert_equal 'Test', request.user_agent
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2009-01-15 09:45:38 -05:00
|
|
|
|
|
|
|
it 'parses POST params when Content-Type is form-dataish' do
|
|
|
|
request = Sinatra::Request.new(
|
|
|
|
'REQUEST_METHOD' => 'PUT',
|
|
|
|
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
|
|
|
|
'rack.input' => StringIO.new('foo=bar')
|
|
|
|
)
|
|
|
|
assert_equal 'bar', request.params['foo']
|
|
|
|
end
|
2009-08-14 17:51:58 -04:00
|
|
|
|
|
|
|
it 'is secure when the url scheme is https' do
|
|
|
|
request = Sinatra::Request.new('rack.url_scheme' => 'https')
|
|
|
|
assert request.secure?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not secure when the url scheme is http' do
|
|
|
|
request = Sinatra::Request.new('rack.url_scheme' => 'http')
|
|
|
|
assert !request.secure?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'respects X-Forwarded-Proto header for proxied SSL' do
|
|
|
|
request = Sinatra::Request.new('HTTP_X_FORWARDED_PROTO' => 'https')
|
|
|
|
assert request.secure?
|
|
|
|
end
|
2010-09-01 06:12:36 -04:00
|
|
|
|
|
|
|
it 'is possible to marshal params' do
|
|
|
|
request = Sinatra::Request.new(
|
|
|
|
'REQUEST_METHOD' => 'PUT',
|
|
|
|
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
|
|
|
|
'rack.input' => StringIO.new('foo=bar')
|
|
|
|
)
|
2011-03-12 09:52:43 -05:00
|
|
|
params = Sinatra::Base.new!.send(:indifferent_hash).replace(request.params)
|
2010-09-01 06:12:36 -04:00
|
|
|
dumped = Marshal.dump(request.params)
|
|
|
|
assert_equal 'bar', Marshal.load(dumped)['foo']
|
|
|
|
end
|
2013-01-28 20:31:08 -05:00
|
|
|
|
|
|
|
it "exposes the preferred type's parameters" do
|
|
|
|
request = Sinatra::Request.new(
|
|
|
|
'HTTP_ACCEPT' => 'image/jpeg; compress=0.25'
|
|
|
|
)
|
|
|
|
assert_equal({ 'compress' => '0.25' }, request.preferred_type.params)
|
|
|
|
end
|
|
|
|
|
2013-03-10 07:39:29 -04:00
|
|
|
it "makes accept types behave like strings" do
|
|
|
|
request = Sinatra::Request.new('HTTP_ACCEPT' => 'image/jpeg; compress=0.25')
|
|
|
|
assert_equal 'image/jpeg', request.preferred_type.to_s
|
|
|
|
assert_equal 'image/jpeg', request.preferred_type.to_str
|
|
|
|
assert_equal 'image', request.preferred_type.split('/').first
|
|
|
|
|
|
|
|
String.instance_methods.each do |method|
|
|
|
|
assert request.preferred_type.respond_to? method
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-28 20:31:08 -05:00
|
|
|
it "properly decodes MIME type parameters" do
|
|
|
|
request = Sinatra::Request.new(
|
|
|
|
'HTTP_ACCEPT' => 'image/jpeg;unquoted=0.25;quoted="0.25";chartest="\";,\x"'
|
|
|
|
)
|
|
|
|
expected = { 'unquoted' => '0.25', 'quoted' => '0.25', 'chartest' => '";,x' }
|
|
|
|
assert_equal(expected, request.preferred_type.params)
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|