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
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class RequestTest < Minitest::Test
|
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')
|
|
|
|
)
|
2013-03-14 13:03:53 -04:00
|
|
|
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')
|
2013-06-05 01:56:28 -04:00
|
|
|
assert request.accept?('image/jpeg')
|
2013-03-10 07:39:29 -04:00
|
|
|
assert_equal 'image/jpeg', request.preferred_type.to_s
|
2014-06-19 18:40:25 -04:00
|
|
|
assert_equal 'image/jpeg; compress=0.25', request.preferred_type.to_s(true)
|
2013-03-10 07:39:29 -04:00
|
|
|
assert_equal 'image/jpeg', request.preferred_type.to_str
|
|
|
|
assert_equal 'image', request.preferred_type.split('/').first
|
|
|
|
|
|
|
|
String.instance_methods.each do |method|
|
2013-03-10 08:04:06 -04:00
|
|
|
next unless "".respond_to? method
|
|
|
|
assert request.preferred_type.respond_to?(method), "responds to #{method}"
|
2013-03-10 07:39:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-12 16:27:43 -04:00
|
|
|
it "accepts types when wildcards are requested" do
|
|
|
|
request = Sinatra::Request.new('HTTP_ACCEPT' => 'image/*')
|
|
|
|
assert request.accept?('image/jpeg')
|
|
|
|
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
|
2013-09-15 14:53:08 -04:00
|
|
|
|
|
|
|
it 'accepts */* when HTTP_ACCEPT is not present in the request' do
|
|
|
|
request = Sinatra::Request.new Hash.new
|
|
|
|
assert_equal 1, request.accept.size
|
|
|
|
assert request.accept?('text/html')
|
|
|
|
assert_equal '*/*', request.preferred_type.to_s
|
2014-06-19 18:40:25 -04:00
|
|
|
assert_equal '*/*', request.preferred_type.to_s(true)
|
2013-09-15 14:53:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts */* when HTTP_ACCEPT is blank in the request' do
|
|
|
|
request = Sinatra::Request.new 'HTTP_ACCEPT' => ''
|
|
|
|
assert_equal 1, request.accept.size
|
|
|
|
assert request.accept?('text/html')
|
|
|
|
assert_equal '*/*', request.preferred_type.to_s
|
2014-06-19 18:40:25 -04:00
|
|
|
assert_equal '*/*', request.preferred_type.to_s(true)
|
2013-09-15 14:53:08 -04:00
|
|
|
end
|
2013-10-25 20:46:40 -04:00
|
|
|
|
2013-11-01 14:04:14 -04:00
|
|
|
it 'will not accept types not specified in HTTP_ACCEPT when HTTP_ACCEPT is provided' do
|
2013-10-25 20:46:40 -04:00
|
|
|
request = Sinatra::Request.new 'HTTP_ACCEPT' => 'application/json'
|
|
|
|
assert !request.accept?('text/html')
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|