1 require 'abstract_unit'
3 ActionController
::UrlRewriter
5 class UrlRewriterTests
< ActionController
::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
< ActionController
::TestCase
90 include ActionController
::UrlWriter
94 W
.default_url_options
.clear
98 W
.default_url_options
[:host] = 'www.basecamphq.com'
101 def test_exception_is_thrown_without_host
102 assert_raise RuntimeError
do
103 W
.new
.url_for
:controller => 'c', :action => 'a', :id => 'i'
108 assert_equal('/c/a#anchor',
109 W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => 'anchor')
113 def test_default_host
115 assert_equal('http://www.basecamphq.com/c/a/i',
116 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i')
120 def test_host_may_be_overridden
122 assert_equal('http://37signals.basecamphq.com/c/a/i',
123 W
.new
.url_for(:host => '37signals.basecamphq.com', :controller => 'c', :action => 'a', :id => 'i')
129 assert_equal('http://www.basecamphq.com:3000/c/a/i',
130 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :port => 3000)
136 assert_equal('https://www.basecamphq.com/c/a/i',
137 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
141 def test_protocol_with_and_without_separator
143 assert_equal('https://www.basecamphq.com/c/a/i',
144 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
146 assert_equal('https://www.basecamphq.com/c/a/i',
147 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https://')
151 def test_trailing_slash
153 options
= {:controller => 'foo', :trailing_slash => true, :action => 'bar', :id => '33'}
154 assert_equal('http://www.basecamphq.com/foo/bar/33/', W
.new
.url_for(options
) )
157 def test_trailing_slash_with_protocol
159 options
= { :trailing_slash => true,:protocol => 'https', :controller => 'foo', :action => 'bar', :id => '33'}
160 assert_equal('https://www.basecamphq.com/foo/bar/33/', W
.new
.url_for(options
) )
161 assert_equal
'https://www.basecamphq.com/foo/bar/33/?query=string', W
.new
.url_for(options
.merge({:query => 'string'}))
164 def test_trailing_slash_with_only_path
165 options
= {:controller => 'foo', :trailing_slash => true}
166 assert_equal
'/foo/', W
.new
.url_for(options
.merge({:only_path => true}))
167 options
.update({:action => 'bar', :id => '33'})
168 assert_equal
'/foo/bar/33/', W
.new
.url_for(options
.merge({:only_path => true}))
169 assert_equal
'/foo/bar/33/?query=string', W
.new
.url_for(options
.merge({:query => 'string',:only_path => true}))
172 def test_trailing_slash_with_anchor
173 options
= {:trailing_slash => true, :controller => 'foo', :action => 'bar', :id => '33', :only_path => true, :anchor=> 'chapter7'}
174 assert_equal
'/foo/bar/33/#chapter7', W
.new
.url_for(options
)
175 assert_equal
'/foo/bar/33/?query=string#chapter7', W
.new
.url_for(options
.merge({:query => 'string'}))
178 def test_trailing_slash_with_params
179 url
= W
.new
.url_for(:trailing_slash => true, :only_path => true, :controller => 'cont', :action => 'act', :p1 => 'cafe', :p2 => 'link')
180 params
= extract_params(url
)
181 assert_equal params
[0], { :p1 => 'cafe' }.to_query
182 assert_equal params
[1], { :p2 => 'link' }.to_query
185 def test_relative_url_root_is_respected
186 orig_relative_url_root
= ActionController
::Base.relative_url_root
187 ActionController
::Base.relative_url_root
= '/subdir'
190 assert_equal('https://www.basecamphq.com/subdir/c/a/i',
191 W
.new
.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
194 ActionController
::Base.relative_url_root
= orig_relative_url_root
197 def test_named_routes
198 ActionController
::Routing::Routes.draw
do |map
|
199 map
.no_args
'/this/is/verbose', :controller => 'home', :action => 'index'
200 map
.home
'/home/sweet/home/:user', :controller => 'home', :action => 'index'
201 map
.connect
':controller/:action/:id'
204 # We need to create a new class in order to install the new named route.
205 kls
= Class
.new
{ include ActionController
::UrlWriter }
207 assert controller
.respond_to
?(:home_url)
208 assert_equal
'http://www.basecamphq.com/home/sweet/home/again',
209 controller
.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
211 assert_equal("/home/sweet/home/alabama", controller
.send(:home_path, :user => 'alabama', :host => 'unused'))
212 assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller
.send(:home_url, :user => 'alabama', :host => 'www.basecamphq.com'))
213 assert_equal("http://www.basecamphq.com/this/is/verbose", controller
.send(:no_args_url, :host=>'www.basecamphq.com'))
215 ActionController
::Routing::Routes.load
!
218 def test_relative_url_root_is_respected_for_named_routes
219 orig_relative_url_root
= ActionController
::Base.relative_url_root
220 ActionController
::Base.relative_url_root
= '/subdir'
222 ActionController
::Routing::Routes.draw
do |map
|
223 map
.home
'/home/sweet/home/:user', :controller => 'home', :action => 'index'
226 kls
= Class
.new
{ include ActionController
::UrlWriter }
229 assert_equal
'http://www.basecamphq.com/subdir/home/sweet/home/again',
230 controller
.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
232 ActionController
::Routing::Routes.load
!
233 ActionController
::Base.relative_url_root
= orig_relative_url_root
237 ActionController
::Routing::Routes.draw
do |map
|
238 map
.home
'/home/sweet/home/:user', :controller => 'home', :action => 'index'
239 map
.connect
':controller/:action/:id'
242 # We need to create a new class in order to install the new named route.
243 kls
= Class
.new
{ include ActionController
::UrlWriter }
245 assert controller
.respond_to
?(:home_url)
246 assert_equal
'/brave/new/world',
247 controller
.send(:url_for, :controller => 'brave', :action => 'new', :id => 'world', :only_path => true)
249 assert_equal("/home/sweet/home/alabama", controller
.send(:home_url, :user => 'alabama', :host => 'unused', :only_path => true))
250 assert_equal("/home/sweet/home/alabama", controller
.send(:home_path, 'alabama'))
252 ActionController
::Routing::Routes.load
!
255 def test_one_parameter
256 assert_equal('/c/a?param=val',
257 W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :param => 'val')
261 def test_two_parameters
262 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :p1 => 'X1', :p2 => 'Y2')
263 params
= extract_params(url
)
264 assert_equal params
[0], { :p1 => 'X1' }.to_query
265 assert_equal params
[1], { :p2 => 'Y2' }.to_query
268 def test_hash_parameter
269 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => {:name => 'Bob', :category => 'prof'})
270 params
= extract_params(url
)
271 assert_equal params
[0], { 'query[category]' => 'prof' }.to_query
272 assert_equal params
[1], { 'query[name]' => 'Bob' }.to_query
275 def test_array_parameter
276 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => ['Bob', 'prof'])
277 params
= extract_params(url
)
278 assert_equal params
[0], { 'query[]' => 'Bob' }.to_query
279 assert_equal params
[1], { 'query[]' => 'prof' }.to_query
282 def test_hash_recursive_parameters
283 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => {:person => {:name => 'Bob', :position => 'prof'}, :hobby => 'piercing'})
284 params
= extract_params(url
)
285 assert_equal params
[0], { 'query[hobby]' => 'piercing' }.to_query
286 assert_equal params
[1], { 'query[person][name]' => 'Bob' }.to_query
287 assert_equal params
[2], { 'query[person][position]' => 'prof' }.to_query
290 def test_hash_recursive_and_array_parameters
291 url
= W
.new
.url_for(:only_path => true, :controller => 'c', :action => 'a', :id => 101, :query => {:person => {:name => 'Bob', :position => ['prof', 'art director']}, :hobby => 'piercing'})
292 assert_match
%r(^
/c/a
/101), url
293 params
= extract_params(url
)
294 assert_equal params
[0], { 'query[hobby]' => 'piercing' }.to_query
295 assert_equal params
[1], { 'query[person][name]' => 'Bob' }.to_query
296 assert_equal params
[2], { 'query[person][position][]' => 'prof' }.to_query
297 assert_equal params
[3], { 'query[person][position][]' => 'art director' }.to_query
300 def test_path_generation_for_symbol_parameter_keys
301 assert_generates("/image", :controller=> :image)
304 def test_named_routes_with_nil_keys
305 ActionController
::Routing::Routes.clear
!
306 ActionController
::Routing::Routes.draw
do |map
|
307 map
.main
'', :controller => 'posts'
309 map
.connect
':controller/:action/:id'
311 # We need to create a new class in order to install the new named route.
312 kls
= Class
.new
{ include ActionController
::UrlWriter }
313 kls
.default_url_options
[:host] = 'www.basecamphq.com'
316 params
= {:action => :index, :controller => :posts, :format => :xml}
317 assert_equal("http://www.basecamphq.com/posts.xml", controller
.send(:url_for, params
))
318 params
[:format] = nil
319 assert_equal("http://www.basecamphq.com/", controller
.send(:url_for, params
))
321 ActionController
::Routing::Routes.load
!
324 def test_formatted_url_methods_are_deprecated
325 ActionController
::Routing::Routes.draw
do |map
|
328 # We need to create a new class in order to install the new named route.
329 kls
= Class
.new
{ include ActionController
::UrlWriter }
331 params
= {:id => 1, :format => :xml}
333 assert_equal("/posts/1.xml", controller
.send(:formatted_post_path, params
))
336 assert_equal("/posts/1.xml", controller
.send(:formatted_post_path, 1, :xml))
339 ActionController
::Routing::Routes.load
!
342 def test_multiple_includes_maintain_distinct_options
343 first_class
= Class
.new
{ include ActionController
::UrlWriter }
344 second_class
= Class
.new
{ include ActionController
::UrlWriter }
346 first_host
, second_host
= 'firsthost.com', 'secondhost.com'
348 first_class
.default_url_options
[:host] = first_host
349 second_class
.default_url_options
[:host] = second_host
351 assert_equal first_class
.default_url_options
[:host], first_host
352 assert_equal second_class
.default_url_options
[:host], second_host
356 def extract_params(url
)
357 url
.split('?', 2).last
.split('&')