Treat missing User Agent like an empty User Agent. Fixes #164.

This commit is contained in:
Konstantin Haase 2011-01-16 17:31:39 +01:00
parent a28ed5076c
commit c1eb806bbb
2 changed files with 13 additions and 1 deletions

View File

@ -990,7 +990,7 @@ module Sinatra
# Will set params[:agent].
def user_agent(pattern)
condition do
if request.user_agent =~ pattern
if request.user_agent.to_s =~ pattern
@params[:agent] = $~[1..-1]
true
else

View File

@ -611,6 +611,18 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'Hello World', body
end
it "treats missing user agent like an empty string" do
mock_app do
user_agent(/.*/)
get '/' do
"Hello World"
end
end
get '/'
assert_equal 200, status
assert_equal 'Hello World', body
end
it "makes captures in user agent pattern available in params[:agent]" do
mock_app {
user_agent(/Foo (.*)/)