Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-ci
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
*.swp
13 changes: 12 additions & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -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

4 changes: 1 addition & 3 deletions lib/rack/dummy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ def call(env)
lines << req.env.inspect
lines << "SUBDOMAIN: #{env['SUBDOMAIN']}"
lines << '<a href="/some/resource">'
[200, {"Content-Type" => "text/plain"}, lines.join("\n")]
[200, {"Content-Type" => "text/plain"}, [lines.join("\n")]]
end

end

end



1 change: 1 addition & 0 deletions lib/rack/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions lib/rewritten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions rewritten.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<description
Expand Down
7 changes: 7 additions & 0 deletions test/rack/rewritten_url_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
23 changes: 22 additions & 1 deletion test/rewritten_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,7 +70,7 @@

end

describe "basic interface" do
describe ".translate" do

it "must translate froms" do
Rewritten.translate('/from').must_equal '/to'
Expand All @@ -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


Expand Down
6 changes: 3 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'coveralls'
Coveralls.wear!

require 'rewritten'
require 'minitest/autorun'
require 'pry'
require 'coveralls'

Coveralls.wear!

class Minitest::Spec

Expand Down