Skip to content

Commit

Permalink
Allow a minimum IE version to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
wjessop committed May 26, 2009
1 parent b7f1f6c commit 04adb58
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ or let the default kick in

use Rack::NoIE6

You can even specify a minimum version of IE like so

use Rack::NoIE6, :redirect => 'http://slashdot.org', :minimum => 6.0

# Rails usage

inside environment.rb's Rails::Initializer.run
Expand Down
12 changes: 11 additions & 1 deletion lib/noie6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def initialize(app, options = {})
@app = app
@options = options
@options[:redirect] ||= 'http://www.microsoft.com/windows/internet-explorer/default.aspx'
@options[:minimum] ||= 7.0
end

def call(env)
Expand All @@ -13,9 +14,18 @@ def call(env)
private
def ie6_found_in?(env)
if env['HTTP_USER_AGENT']
env['HTTP_USER_AGENT'][/MSIE 6.0/] and @options[:redirect] != env['PATH_INFO']
is_ie?(env['HTTP_USER_AGENT']) and ie_version(env['HTTP_USER_AGENT']) < @options[:minimum] and @options[:redirect] != env['PATH_INFO']
end
end

def is_ie?(ua_string)
# We need at least one digit to be able to get the version, hence the \d
ua_string.match(/MSIE \d/) ? true : false
end

def ie_version(ua_string)
ua_string.match(/MSIE (\S+)/)[1].to_f
end

def kick_it
[301, {'Location' => @options[:redirect]}, 'Fail browser is fail']
Expand Down
19 changes: 19 additions & 0 deletions test/noie6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def test_redirects_to_where_it_should_if_ie6
assert_equal response.location, 'http://slashdot.org'
end

def test_redirects_to_where_it_should_if_user_specified_minimum_not_met
request = Rack::MockRequest.new(Rack::NoIE6.new(TestApp.new, {:redirect => 'http://slashdot.org', :minimum => 6.0}))
response = request.get('/', {'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE 5.5b1; Mac_PowerPC)' })
assert_equal 301, response.status
assert_equal response.location, 'http://slashdot.org'
end

def test_redirects_to_local_urls
request = Rack::MockRequest.new(Rack::NoIE6.new(TestApp.new, {:redirect => '/foo'}))
response = request.get('/foo', {'HTTP_USER_AGENT' => 'MSIE 6.0' })
Expand All @@ -32,6 +39,18 @@ def test_allows_if_not_ie6
assert_equal "Hi Internets!", response.body
end

def test_allows_if_UA_version_greater_than_minimum
request = Rack::MockRequest.new(Rack::NoIE6.new(TestApp.new, {:redirect => 'http://slashdot.org'}))
response = request.get('/', {'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows XP)'})
assert_equal "Hi Internets!", response.body
end

def test_allows_if_no_UA_version_no_available
request = Rack::MockRequest.new(Rack::NoIE6.new(TestApp.new, {:redirect => 'http://slashdot.org'}))
response = request.get('/', {'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE l4me; Windows XP)'})
assert_equal "Hi Internets!", response.body
end

def test_allows_if_no_user_agent_specified
request = Rack::MockRequest.new(Rack::NoIE6.new(TestApp.new, {:redirect => 'http://slashdot.org'}))
response = request.get('/')
Expand Down

0 comments on commit 04adb58

Please sign in to comment.