From 7ad4ed1109213bd23425a2fd69649778c9bdd9ab Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 12 Nov 2012 19:37:55 +0100 Subject: [PATCH] Redirect to old version of travis for mobile clients We're planning support for a new mobile client, but for the time being we will just redirect to the old app. --- lib/travis/web/app.rb | 3 +++ lib/travis/web/app/mobile_redirect.rb | 12 ++++++++++++ spec/mobile_redirect_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 lib/travis/web/app/mobile_redirect.rb create mode 100644 spec/mobile_redirect_spec.rb diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index a14fa460..2146d8b7 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -5,6 +5,8 @@ require 'delegate' require 'time' class Travis::Web::App + autoload :MobileRedirect, 'travis/web/app/mobile_redirect' + # Simple Rack router that behaves like a hash. # Key is the path, value the response. class Router < DelegateClass(Hash) @@ -129,6 +131,7 @@ class Travis::Web::App builder.use Rack::Deflater builder.use Rack::Head builder.use Rack::ConditionalGet + builder.use MobileRedirect builder.run router builder end diff --git a/lib/travis/web/app/mobile_redirect.rb b/lib/travis/web/app/mobile_redirect.rb new file mode 100644 index 00000000..5593b9e5 --- /dev/null +++ b/lib/travis/web/app/mobile_redirect.rb @@ -0,0 +1,12 @@ +class Travis::Web::App::MobileRedirect < Struct.new(:app) + def call(env) + request = Rack::Request.new env + + if request.params['mobile'] || env['HTTP_AGENT'] =~ /Mobile|webOS/ + location = 'https://secure.travis-ci.org' + request.fullpath + [301, { 'Content-Type' => 'text/plain', 'Location' => location }, []] + else + app.call env + end + end +end diff --git a/spec/mobile_redirect_spec.rb b/spec/mobile_redirect_spec.rb new file mode 100644 index 00000000..03871d53 --- /dev/null +++ b/spec/mobile_redirect_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe Travis::Web::App::MobileRedirect do + + describe 'with mobile client' do + let(:agent) { 'blah blah Mobile blablah' } + + it 'redirects to secure.travis-ci.org' do + get('/foo/bar?baz', {}, 'HTTP_AGENT' => agent).should be_redirect + last_response.headers['Location'].should == 'https://secure.travis-ci.org/foo/bar?baz' + end + end + + describe 'with mobile param' do + it 'redirects to secure.travis-ci.org' do + get('/foo/bar?baz', mobile: true).should be_redirect + last_response.headers['Location'].should == 'https://secure.travis-ci.org/foo/bar?mobile=true&baz' + end + end +end