1 require 'abstract_unit'
3 ActionController
::UrlRewriter
5 class UrlRewriterTests
< Test
::Unit::TestCase
7 @request = ActionController
::TestRequest.new
9 @rewriter = ActionController
::UrlRewriter.new(@request, @params)
13 assert_equal('http://test.host:1271/c/a/i',
14 @rewriter.rewrite(:controller => 'c', :action => 'a', :id => 'i', :port => 1271)
18 def test_protocol_with_and_without_separator
19 assert_equal('https://test.host/c/a/i',
20 @rewriter.rewrite(:protocol => 'https', :controller => 'c', :action => 'a', :id => 'i')
23 assert_equal('https://test.host/c/a/i',
24 @rewriter.rewrite(:protocol => 'https://', :controller => 'c', :action => 'a', :id => 'i')
28 def test_user_name_and_password
30 'http://david:secret@test.host/c/a/i',
31 @rewriter.rewrite(:user => "david", :password => "secret", :controller => 'c', :action => 'a', :id => 'i')
35 def test_user_name_and_password_with_escape_codes
37 'http://openid.aol.com%2Fnextangler:one+two%3F@test.host/c/a/i',
38 @rewriter.rewrite(:user => "openid.aol.com/nextangler", :password => "one two?", :controller => 'c', :action => 'a', :id => 'i')
44 'http://test.host/c/a/i#anchor',
45 @rewriter.rewrite(:controller => 'c', :action => 'a', :id => 'i', :anchor => 'anchor')
49 def test_overwrite_params
50 @params[:controller] = 'hi'
51 @params[:action] = 'bye'
54 assert_equal
'/hi/hi/2', @rewriter.rewrite(:only_path => true, :overwrite_params => {:action => 'hi'})
55 u
= @rewriter.rewrite(:only_path => false, :overwrite_params => {:action => 'hi'})
56 assert_match
%r(/hi/hi
/2$
), u
59 def test_overwrite_removes_original
60 @params[:controller] = 'search'
61 @params[:action] = 'list'
62 @params[:list_page] = 1
64 assert_equal
'/search/list?list_page=2', @rewriter.rewrite(:only_path => true, :overwrite_params => {"list_page" => 2})
65 u
= @rewriter.rewrite(:only_path => false, :overwrite_params => {:list_page => 2})
66 assert_equal
'http://test.host/search/list?list_page=2', u
70 @params[:controller] = 'hi'
71 @params[:action] = 'bye'
72 @request.parameters
[:id] = '2'
74 assert_equal
'http://, test.host, /, hi, bye, {"id"=>"2"}', @rewriter.to_str
77 def test_trailing_slash
78 options
= {:controller => 'foo', :action => 'bar', :id => '3', :only_path => true}
79 assert_equal
'/foo/bar/3', @rewriter.rewrite(options
)
80 assert_equal
'/foo/bar/3?query=string', @rewriter.rewrite(options
.merge({:query => 'string'}))
81 options
.update({:trailing_slash => true})
82 assert_equal
'/foo/bar/3/', @rewriter.rewrite(options
)
83 options
.update({:query => 'string'})
84 assert_equal
'/foo/bar/3/?query=string', @rewriter.rewrite(options
)
88 class UrlWriterTests
< Test
::Unit::TestCase
91 include ActionController
::UrlWriter
95 W
.default_url_options
.clear
99 W
.default_url_options
[:host] = 'www.basecamphq.com'
102 def test_exception_is_thrown_without_host
103 assert_raises RuntimeError
do
104 W
.new
.url_for
:controller => 'c', :action => 'a', :id => 'i'
109 assert_equal('/c/a#anchor',
110 W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => 'anchor')
114 def test_default_host
116 assert_equal('http://www.basecamphq.com/c/a/i',
117 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i')
121 def test_host_may_be_overridden
123 assert_equal('http://37signals.basecamphq.com/c/a/i',
124 W
.new
.url_for(:host => '37signals.basecamphq.com', :controller => 'c', :action => 'a', :id => 'i')
130 assert_equal('http://www.basecamphq.com:3000/c/a/i',
131 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :port => 3000)
137 assert_equal('https://www.basecamphq.com/c/a/i',
138 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
142 def test_protocol_with_and_without_separator
144 assert_equal('https://www.basecamphq.com/c/a/i',
145 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
147 assert_equal('https://www.basecamphq.com/c/a/i',
148 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https://')
152 def test_trailing_slash
154 options
= {:controller => 'foo', :trailing_slash => true, :action => 'bar', :id => '33'}
155 assert_equal('http://www.basecamphq.com/foo/bar/33/', W
.new
.url_for(options
) )
158 def test_trailing_slash_with_protocol
160 options
= { :trailing_slash => true,:protocol => 'https', :controller => 'foo', :action => 'bar', :id => '33'}
161 assert_equal('https://www.basecamphq.com/foo/bar/33/', W
.new
.url_for(options
) )
162 assert_equal
'https://www.basecamphq.com/foo/bar/33/?query=string', W
.new
.url_for(options
.merge({:query => 'string'}))
165 def test_trailing_slash_with_only_path
166 options
= {:controller => 'foo', :trailing_slash => true}
167 assert_equal
'/foo/', W
.new
.url_for(options
.merge({:only_path => true}))
168 options
.update({:action => 'bar', :id => '33'})
169 assert_equal
'/foo/bar/33/', W
.new
.url_for(options
.merge({:only_path => true}))
170 assert_equal
'/foo/bar/33/?query=string', W
.new
.url_for(options
.merge({:query => 'string',:only_path => true}))
173 def test_trailing_slash_with_anchor
174 options
= {:trailing_slash => true, :controller => 'foo', :action => 'bar', :id => '33', :only_path => true, :anchor=> 'chapter7'}
175 assert_equal
'/foo/bar/33/#chapter7', W
.new
.url_for(options
)
176 assert_equal
'/foo/bar/33/?query=string#chapter7', W
.new
.url_for(options
.merge({:query => 'string'}))
179 def test_trailing_slash_with_params
180 url
= W
.new
.url_for(:trailing_slash => true, :only_path => true, :controller => 'cont', :action => 'act', :p1 => 'cafe', :p2 => 'link')
181 params
= extract_params(url
)
182 assert_equal params
[0], { :p1 => 'cafe' }.to_query
183 assert_equal params
[1], { :p2 => 'link' }.to_query
186 def test_relative_url_root_is_respected
187 orig_relative_url_root
= ActionController
::Base.relative_url_root
188 ActionController
::Base.relative_url_root
= '/subdir'
191 assert_equal('https://www.basecamphq.com/subdir/c/a/i',
192 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
195 ActionController
::Base.relative_url_root
= orig_relative_url_root
198 def test_named_routes
199 ActionController
::Routing::Routes.draw
do |map
|
200 map
.no_args
'/this/is/verbose', :controller => 'home', :action => 'index'
201 map
.home
'/home/sweet/home/:user', :controller => 'home', :action => 'index'
202 map
.connect
':controller/:action/:id'
205 # We need to create a new class in order to install the new named route.
206 kls
= Class
.new
{ include ActionController
::UrlWriter }
208 assert controller
.respond_to
?(:home_url)
209 assert_equal
'http://www.basecamphq.com/home/sweet/home/again',
210 controller
.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
212 assert_equal("/home/sweet/home/alabama", controller
.send(:home_path, :user => 'alabama', :host => 'unused'))
213 assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller
.send(:home_url, :user => 'alabama', :host => 'www.basecamphq.com'))
214 assert_equal("http://www.basecamphq.com/this/is/verbose", controller
.send(:no_args_url, :host=>'www.basecamphq.com'))
216 ActionController
::Routing::Routes.load
!
219 def test_relative_url_root_is_respected_for_named_routes
220 orig_relative_url_root
= ActionController
::Base.relative_url_root
221 ActionController
::Base.relative_url_root
= '/subdir'
223 ActionController
::Routing::Routes.draw
do |map
|
224 map
.home
'/home/sweet/home/:user', :controller => 'home', :action => 'index'
227 kls
= Class
.new
{ include ActionController
::UrlWriter }
230 assert_equal
'http://www.basecamphq.com/subdir/home/sweet/home/again',
231 controller
.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
233 ActionController
::Routing::Routes.load
!
234 ActionController
::Base.relative_url_root
= orig_relative_url_root
238 ActionController
::Routing::Routes.draw
do |map
|
239 map
.home
'/home/sweet/home/:user', :controller => 'home', :action => 'index'
240 map
.connect
':controller/:action/:id'
243 # We need to create a new class in order to install the new named route.
244 kls
= Class
.new
{ include ActionController
::UrlWriter }
246 assert controller
.respond_to
?(:home_url)
247 assert_equal
'/brave/new/world',
248 controller
.send(:url_for, :controller => 'brave', :action => 'new', :id => 'world', :only_path => true)
250 assert_equal("/home/sweet/home/alabama", controller
.send(:home_url, :user => 'alabama', :host => 'unused', :only_path => true))
251 assert_equal("/home/sweet/home/alabama", controller
.send(:home_path, 'alabama'))
253 ActionController
::Routing::Routes.load
!
256 def test_one_parameter
257 assert_equal('/c/a?param=val',
258 W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :param => 'val')
262 def test_two_parameters
263 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :p1 => 'X1', :p2 => 'Y2')
264 params
= extract_params(url
)
265 assert_equal params
[0], { :p1 => 'X1' }.to_query
266 assert_equal params
[1], { :p2 => 'Y2' }.to_query
269 def test_hash_parameter
270 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => {:name => 'Bob', :category => 'prof'})
271 params
= extract_params(url
)
272 assert_equal params
[0], { 'query[category]' => 'prof' }.to_query
273 assert_equal params
[1], { 'query[name]' => 'Bob' }.to_query
276 def test_array_parameter
277 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => ['Bob', 'prof'])
278 params
= extract_params(url
)
279 assert_equal params
[0], { 'query[]' => 'Bob' }.to_query
280 assert_equal params
[1], { 'query[]' => 'prof' }.to_query
283 def test_hash_recursive_parameters
284 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => {:person => {:name => 'Bob', :position => 'prof'}, :hobby => 'piercing'})
285 params
= extract_params(url
)
286 assert_equal params
[0], { 'query[hobby]' => 'piercing' }.to_query
287 assert_equal params
[1], { 'query[person][name]' => 'Bob' }.to_query
288 assert_equal params
[2], { 'query[person][position]' => 'prof' }.to_query
291 def test_hash_recursive_and_array_parameters
292 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :id => 101, :query => {:person => {:name => 'Bob', :position => ['prof', 'art director']}, :hobby => 'piercing'})
293 assert_match
%r(^
/c/a
/101), url
294 params
= extract_params(url
)
295 assert_equal params
[0], { 'query[hobby]' => 'piercing' }.to_query
296 assert_equal params
[1], { 'query[person][name]' => 'Bob' }.to_query
297 assert_equal params
[2], { 'query[person][position][]' => 'prof' }.to_query
298 assert_equal params
[3], { 'query[person][position][]' => 'art director' }.to_query
301 def test_path_generation_for_symbol_parameter_keys
302 assert_generates("/image", :controller=> :image)
306 def extract_params(url
)
307 url
.split('?', 2).last
.split('&')