From 0d9d89ad3cfdcbdbdc78928471ad2f87264ff607 Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Thu, 20 Jan 2005 01:00:47 +0000 Subject: [PATCH] URI rewriting should be very liberal towards page names --- libraries/url_rewriting_hack.rb | 15 ++++++++++++--- test/functional/wiki_controller_test.rb | 7 +++++-- test/unit/url_rewriting_hack_test.rb | 22 ++++++++++++++++++---- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/libraries/url_rewriting_hack.rb b/libraries/url_rewriting_hack.rb index 72160cb7..2d8ffbc1 100755 --- a/libraries/url_rewriting_hack.rb +++ b/libraries/url_rewriting_hack.rb @@ -25,7 +25,8 @@ class DispatchServlet def self.parse_uri(path) ApplicationController.logger.debug "Parsing URI '#{path}'" - component = /([-_a-zA-Z0-9]+)/ + component = '([-_a-zA-Z0-9]+)' + page_name = '(.*)' case path.sub(%r{^/(?:fcgi|mruby|cgi)/}, "/") when '/wiki/' { :web => nil, :controller => 'wiki', :action => 'index' } @@ -33,13 +34,21 @@ class DispatchServlet { :web => nil, :controller => 'wiki', :action => $1 } when %r{^/#{component}/#{component}/?$} { :web => $1, :controller => 'wiki', :action => $2 } - when %r{^/#{component}/#{component}/#{component}/?$} - { :web => $1, :controller => 'wiki', :action => $2, :id => $3 } + when %r{^/#{component}/#{component}/(.*)/?$} + { :web => $1, :controller => 'wiki', :action => $2, :id => drop_trailing_slash($3) } else false end end + def self.drop_trailing_slash(line) + if line[-1] == ?/ + line.chop + else + line + end + end + end diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index b477a0d1..dd608c1b 100755 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -472,6 +472,8 @@ class WikiControllerTest < Test::Unit::TestCase def test_rss_with_headlines setup_wiki_with_three_pages + @title_with_spaces = @wiki.write_page('wiki1', 'Title With Spaces', + 'About spaces', 1.hour.ago, Author.new('TreeHugger', '127.0.0.2')) @request.host = 'localhost' @request.port = 8080 @@ -480,7 +482,7 @@ class WikiControllerTest < Test::Unit::TestCase assert_success pages = r.template_objects['pages_by_revision'] - assert_equal [@home, @oak, @elephant], pages, + assert_equal [@home, @oak, @elephant, @title_with_spaces], pages, "Pages are not as expected: #{pages.map {|p| p.name}.inspect}" assert r.template_objects['hide_description'] @@ -489,7 +491,8 @@ class WikiControllerTest < Test::Unit::TestCase expected_page_links = ['http://localhost:8080/wiki1/show/HomePage', 'http://localhost:8080/wiki1/show/Oak', - 'http://localhost:8080/wiki1/show/Elephant'] + 'http://localhost:8080/wiki1/show/Elephant', + 'http://localhost:8080/wiki1/show/Title With Spaces'] assert_template_xpath_match '/rss/channel/link', 'http://localhost:8080/wiki1/show/HomePage' diff --git a/test/unit/url_rewriting_hack_test.rb b/test/unit/url_rewriting_hack_test.rb index 7b018364..008fd0d7 100755 --- a/test/unit/url_rewriting_hack_test.rb +++ b/test/unit/url_rewriting_hack_test.rb @@ -35,10 +35,25 @@ class UrlRewritingHackTest < Test::Unit::TestCase assert_equal false, DispatchServlet.parse_uri('') assert_equal false, DispatchServlet.parse_uri('//') - assert_equal false, DispatchServlet.parse_uri('/web/show/$HOME_PAGE') - assert_equal false, DispatchServlet.parse_uri('/web/show/HomePage/something_else') assert_equal false, DispatchServlet.parse_uri('web') - assert_equal false, DispatchServlet.parse_uri('/web/show/HomePage?arg1=value1&arg2=value2') + end + + def test_parse_uri_liberal_with_pagenames + + assert_equal({:controller => 'wiki', :web => 'web', :action => 'show', :id => '$HOME_PAGE'}, + DispatchServlet.parse_uri('/web/show/$HOME_PAGE')) + + assert_equal({:controller => 'wiki', :web => 'web', :action => 'show', + :id => 'HomePage/something_else'}, + DispatchServlet.parse_uri('/web/show/HomePage/something_else')) + + assert_equal({:controller => 'wiki', :web => 'web', :action => 'show', + :id => 'HomePage?arg1=value1&arg2=value2'}, + DispatchServlet.parse_uri('/web/show/HomePage?arg1=value1&arg2=value2')) + + assert_equal({:controller => 'wiki', :web => 'web', :action => 'show', + :id => 'Page+With+Spaces'}, + DispatchServlet.parse_uri('/web/show/Page+With+Spaces')) end def test_url_rewriting @@ -58,5 +73,4 @@ class UrlRewritingHackTest < Test::Unit::TestCase ur.rewrite(:controller => 'wiki') end - end \ No newline at end of file