diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..9160059 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1 @@ +service_name: travis-ci diff --git a/.gitignore b/.gitignore index 4040c6c..68f1257 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .bundle Gemfile.lock pkg/* +*.swp diff --git a/config.ru b/config.ru index b503fde..5c4a5e5 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,15 @@ +require 'rack' require 'rewritten' +require 'rewritten/server' +require 'pry' +require 'byebug' -run Rewritten::Server +map "/rewritten" do + run Rewritten::Server +end + +map "/" do + use Rack::Rewritten::Url + run Rack::Dummy.new +end diff --git a/lib/rack/dummy.rb b/lib/rack/dummy.rb index 3b92ab2..8f93fcc 100644 --- a/lib/rack/dummy.rb +++ b/lib/rack/dummy.rb @@ -14,12 +14,10 @@ def call(env) lines << req.env.inspect lines << "SUBDOMAIN: #{env['SUBDOMAIN']}" lines << '' - [200, {"Content-Type" => "text/plain"}, lines.join("\n")] + [200, {"Content-Type" => "text/plain"}, [lines.join("\n")]] end end end - - diff --git a/lib/rack/url.rb b/lib/rack/url.rb index 98a24d2..7d97708 100644 --- a/lib/rack/url.rb +++ b/lib/rack/url.rb @@ -46,6 +46,7 @@ def call(env) current_path = ::Rewritten.get_current_translation(to) current_path = current_path.split(":").last current_path = current_path.split('?')[0] + current_path = URI.encode(current_path) if (current_path == req.path_info) or (::Rewritten.base_from(req.path_info) == current_path) or ::Rewritten.has_flag?(path, 'L') # if this is the current path, rewrite path and parameters diff --git a/lib/rewritten.rb b/lib/rewritten.rb index d77a710..8016747 100644 --- a/lib/rewritten.rb +++ b/lib/rewritten.rb @@ -188,7 +188,9 @@ def all_tos end def translate(from) - redis.hget("from:#{from}", :to) + return nil if from.nil? + decoded_from = URI.decode(from) + redis.hget("from:#{decoded_from}", :to) end def get_all_translations(to) @@ -197,11 +199,12 @@ def get_all_translations(to) def get_current_translation(path, tail=nil) - uri = URI.parse(path) + uri = URI.parse( URI.encode(path) ) - # find directly + # try to find absolute path translation = Rewritten.z_range("to:#{path}", -1) + # try to find relative path unless translation translation = Rewritten.z_range("to:#{uri.path}", -1) end @@ -218,11 +221,11 @@ def get_current_translation(path, tail=nil) end complete_path = (tail ? translation+"/"+tail : translation) - translated_uri = URI.parse(complete_path) + translated_uri = URI.parse( URI.encode(complete_path) ) uri.path = translated_uri.path uri.query = [translated_uri.query, uri.query].compact.join('&') uri.query = nil if uri.query == '' - uri.to_s + URI.decode(uri.to_s) end @@ -302,6 +305,8 @@ def all_hits def includes?(path) + path = URI.decode(path) + result = Rewritten.redis.hget("from:#{path.chomp('/')}", :to) result = Rewritten.redis.hget("from:#{path.split('?')[0]}", :to) unless result diff --git a/rewritten.gemspec b/rewritten.gemspec index 8b6ce56..06e3d77 100644 --- a/rewritten.gemspec +++ b/rewritten.gemspec @@ -25,6 +25,7 @@ Gem::Specification.new do |s| s.add_development_dependency "rake" s.add_development_dependency "minitest" s.add_development_dependency "pry" + s.add_development_dependency "byebug" s.add_development_dependency "coveralls" s.description = < for)' do @@ -65,7 +70,7 @@ end - describe "basic interface" do + describe ".translate" do it "must translate froms" do Rewritten.translate('/from').must_equal '/to' @@ -74,15 +79,31 @@ Rewritten.translate('/with/flags').must_equal '/to3' end + it 'must translate encoded umlauts' do + Rewritten.add_translation('/überfoo', '/uber') + Rewritten.translate(URI.encode('/überfoo')).must_equal '/uber' + end + + end + + describe ".full_line" do it "must return the complete line with flags" do Rewritten.full_line('/from').must_equal '/from' Rewritten.full_line('/with/flags').must_equal '/with/flags [L12]' end + end + describe ".all_tos" do it "must give all tos" do Rewritten.all_tos.sort.must_equal ["/to", "/to2", "/to3"] end + end + describe '.includes?' do + it 'works for Umlauts' do + Rewritten.add_translation('/überfoo', '/uber') + Rewritten.includes?( URI.encode('/überfoo') ).wont_equal nil + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5ad0471..0ab5915 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,9 +1,9 @@ +require 'coveralls' +Coveralls.wear! + require 'rewritten' require 'minitest/autorun' require 'pry' -require 'coveralls' - -Coveralls.wear! class Minitest::Spec