Skip to content

Commit

Permalink
Merge 23ce741 into c363ead
Browse files Browse the repository at this point in the history
  • Loading branch information
learnjin committed Sep 18, 2014
2 parents c363ead + 23ce741 commit 52e2fc1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/rack/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@ module Rewritten

class Url

attr_accessor :base_url

def initialize(app, &block)
@app = app
@translate_backwards = false
@downcase_before_lookup = false
@translate_partial = false
@base_url = ''
instance_eval(&block) if block_given?
end

def is_internal_target?(url)
url.nil? or url.start_with?('/') or url.start_with?(@base_url)
end

def is_external_target?(url)
!is_internal_target?(url)
end

def call(env)
req = Rack::Request.new(env)

Expand All @@ -22,7 +33,13 @@ def call(env)
path = "#{subdomain}#{req.path_info}"
path.downcase! if downcase_before_lookup?

if ::Rewritten.includes?(path.chomp("/")) or backwards=( translate_backwards? && ::Rewritten.exist_translation_for?(path) )
target_url = ::Rewritten.translate(path)

if is_external_target?(target_url)
r = Rack::Response.new
r.redirect(target_url, 301)
r.finish
elsif ::Rewritten.includes?(path.chomp("/")) or backwards=( translate_backwards? && ::Rewritten.exist_translation_for?(path) )

to = ::Rewritten.includes?(path.chomp("/")) || path

Expand Down
20 changes: 20 additions & 0 deletions test/rack/rewritten_url_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ def request_url(url, params={})
ret[0].must_equal 200
end

describe 'external redirection' do

before {
@app = MiniTest::Mock.new
@rack = Rack::Rewritten::Url.new(@app) do |config|
config.base_url = 'http://www.example.org'
end

Rewritten.add_translation '/external/target', 'http://www.external.com'
}

it "must redirect to external target" do
ret = @rack.call request_url('/external/target')
@app.verify
ret[0].must_equal 301
ret[1]['Location'].must_equal "http://www.external.com"
end

end

describe "partial translation" do

before do
Expand Down

0 comments on commit 52e2fc1

Please sign in to comment.