From 0ac9bb6fbbbbe6370752a8e27db309c6eeaaecfe Mon Sep 17 00:00:00 2001 From: Julio Cesar Ody Date: Tue, 14 Apr 2009 04:54:44 +0000 Subject: [PATCH] initial commit --- README.markdown | 35 +++++++++++++++++++++++++++++++++++ Rakefile | 8 ++++++++ lib/noie.rb | 21 +++++++++++++++++++++ test/noie_test.rb | 29 +++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 README.markdown create mode 100644 Rakefile create mode 100644 lib/noie.rb create mode 100644 test/noie_test.rb diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..7625f87 --- /dev/null +++ b/README.markdown @@ -0,0 +1,35 @@ +# rack-noie + +rack-noie is the coolest rack middleware ever created. And it is so because it does _everyone_ +a favor: it shows the way out of your website to poor souls out there using Internet Explorer 6. + +I don't wanna hear excuses. IE6 stinks. If we keep developing software that plays well with it, +they'll come up with yet more crap (read: IE7, although a little bit less bad than IE6), until +Microsoft single-handedly manages to hold us back for a few more years. + +# usage + +just + + require 'noie' # or 'juliocesar-noie' when i gemify it + +and + + use NoIE, :redirect => '/noieplease.html' + +the above will redirect to a page noieplease.html in your website. You can redirect to +a URL as well, like so + + use NoIE, :redirect => 'http://slashdot.org' + +or let the default kick in + + use NoIE + +# disclaimer + +I'm a nice guy. I'm so nice that the default URL points to Microsoft's IE7 upgrade page. + +# license + +MIT, as usual. \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..3a525f0 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +require 'rake/testtask' + +task :default => [:test] + +Rake::TestTask.new do |task| + task.pattern = 'test/noie_test.rb' + task.warning, task.verbose = true, true +end \ No newline at end of file diff --git a/lib/noie.rb b/lib/noie.rb new file mode 100644 index 0000000..f9165f1 --- /dev/null +++ b/lib/noie.rb @@ -0,0 +1,21 @@ +class NoIE + def initialize(app, options = {}) + @app = app + @options = options + # Defaults to a slighly less of an excuse that passes for a browser + @options[:redirect] ||= 'http://www.microsoft.com/windows/downloads/ie/getitnow.mspx' + end + + def call(env) + ie6_found_in?(env) ? kick_it : @app.call(env) + end + + private + def ie6_found_in?(env) + env['HTTP_USER_AGENT'][/MSIE 6.0/] and @options[:redirect] != env['PATH_INFO'] + end + + def kick_it + [301, {'Location' => @options[:redirect]}, 'Fail browser is fail'] + end +end \ No newline at end of file diff --git a/test/noie_test.rb b/test/noie_test.rb new file mode 100644 index 0000000..38a2045 --- /dev/null +++ b/test/noie_test.rb @@ -0,0 +1,29 @@ +require 'test/unit' + +require 'rubygems' +require 'rack/mock' + +require File.join(File.dirname(__FILE__), '..', 'lib', 'noie') + +class TestApp + def call(env) + [200, {}, 'Hi Internets!'] + end +end + +class NoieTest < Test::Unit::TestCase + + def test_redirects_to_where_it_should_if_ie6 + request = Rack::MockRequest.new(NoIE.new(TestApp.new, {:redirect => 'http://slashdot.org'})) + response = request.get('/', {'HTTP_USER_AGENT' => 'MSIE 6.0' }) + assert_equal 301, response.status + assert_equal response.location, 'http://slashdot.org' + end + + def test_allows_local_local_urls + request = Rack::MockRequest.new(NoIE.new(TestApp.new, {:redirect => '/foo'})) + response = request.get('/foo', {'HTTP_USER_AGENT' => 'MSIE 6.0' }) + assert_equal "Hi Internets!", response.body + end + +end \ No newline at end of file