From f18f83287678555c5b5307ec0446e3b29ada6bcb Mon Sep 17 00:00:00 2001 From: Kai Rubarth Date: Thu, 2 Oct 2014 13:16:03 +0200 Subject: [PATCH 1/3] added byebug and improved dummy config.ru in root for testing --- .gitignore | 1 + config.ru | 13 ++++++++++++- lib/rack/dummy.rb | 4 +--- rewritten.gemspec | 1 + 4 files changed, 15 insertions(+), 4 deletions(-) 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/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 = < Date: Thu, 2 Oct 2014 13:17:19 +0200 Subject: [PATCH 2/3] make Umlaute and special characters work in Rewritten::URL --- lib/rack/url.rb | 1 + lib/rewritten.rb | 15 ++++++++++----- test/rack/rewritten_url_test.rb | 7 +++++++ test/rewritten_test.rb | 23 ++++++++++++++++++++++- 4 files changed, 40 insertions(+), 6 deletions(-) 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/test/rack/rewritten_url_test.rb b/test/rack/rewritten_url_test.rb index c04f14e..0ab371c 100644 --- a/test/rack/rewritten_url_test.rb +++ b/test/rack/rewritten_url_test.rb @@ -21,6 +21,7 @@ def request_url(url, params={}) Rewritten.add_translation '/foo/bar', '/products/1' Rewritten.add_translation '/foo/baz', '/products/1' Rewritten.add_translation '/foo/with/params', '/products/2?w=1' + Rewritten.add_translation '/überfoo', '/uber' } describe "redirection behavior" do @@ -243,6 +244,12 @@ def request_url(url, params={}) @initial_args['PATH_INFO'].must_equal "/products/2" end + it "must set PATH_INFO with Umlauts" do + @initial_args = request_url( URI.encode('/überfoo') ) + @rack.call(@initial_args) + @initial_args['PATH_INFO'].must_equal "/uber" + end + it "must set QUERY_STRING to w=1" do @rack.call(@initial_args) @initial_args['QUERY_STRING'].must_equal 'w=1' diff --git a/test/rewritten_test.rb b/test/rewritten_test.rb index 7d8fc36..0549316 100644 --- a/test/rewritten_test.rb +++ b/test/rewritten_test.rb @@ -34,6 +34,11 @@ Rewritten.get_current_translation('http://localhost:3000/to_without_params').must_equal 'http://localhost:3000/from_without_params' end + it "must work with Umlauts and Encoded Umlauts" do + Rewritten.add_translation('/ÜberFoo', '/to') + Rewritten.get_current_translation('/to').must_equal '/ÜberFoo' + end + end describe 'get_infinitive (always from conjugated for -> 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 From 2e0a344a8b7b72f63525c3b218ae14d1b23421af Mon Sep 17 00:00:00 2001 From: Kai Rubarth Date: Thu, 2 Oct 2014 14:49:09 +0200 Subject: [PATCH 3/3] add coveralls.yml --- .coveralls.yml | 1 + test/test_helper.rb | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 .coveralls.yml 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/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