Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / test / controller / url_rewriter_test.rb
1 require 'abstract_unit'
2
3 ActionController::UrlRewriter
4
5 class UrlRewriterTests < ActionController::TestCase
6 def setup
7 @request = ActionController::TestRequest.new
8 @params = {}
9 @rewriter = ActionController::UrlRewriter.new(@request, @params)
10 end
11
12 def test_port
13 assert_equal('http://test.host:1271/c/a/i',
14 @rewriter.rewrite(:controller => 'c', :action => 'a', :id => 'i', :port => 1271)
15 )
16 end
17
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')
21 )
22
23 assert_equal('https://test.host/c/a/i',
24 @rewriter.rewrite(:protocol => 'https://', :controller => 'c', :action => 'a', :id => 'i')
25 )
26 end
27
28 def test_user_name_and_password
29 assert_equal(
30 'http://david:secret@test.host/c/a/i',
31 @rewriter.rewrite(:user => "david", :password => "secret", :controller => 'c', :action => 'a', :id => 'i')
32 )
33 end
34
35 def test_user_name_and_password_with_escape_codes
36 assert_equal(
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')
39 )
40 end
41
42 def test_anchor
43 assert_equal(
44 'http://test.host/c/a/i#anchor',
45 @rewriter.rewrite(:controller => 'c', :action => 'a', :id => 'i', :anchor => 'anchor')
46 )
47 end
48
49 def test_overwrite_params
50 @params[:controller] = 'hi'
51 @params[:action] = 'bye'
52 @params[:id] = '2'
53
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
57 end
58
59 def test_overwrite_removes_original
60 @params[:controller] = 'search'
61 @params[:action] = 'list'
62 @params[:list_page] = 1
63
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
67 end
68
69 def test_to_str
70 @params[:controller] = 'hi'
71 @params[:action] = 'bye'
72 @request.parameters[:id] = '2'
73
74 assert_equal 'http://, test.host, /, hi, bye, {"id"=>"2"}', @rewriter.to_str
75 end
76
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)
85 end
86 end
87
88 class UrlWriterTests < ActionController::TestCase
89 class W
90 include ActionController::UrlWriter
91 end
92
93 def teardown
94 W.default_url_options.clear
95 end
96
97 def add_host!
98 W.default_url_options[:host] = 'www.basecamphq.com'
99 end
100
101 def test_exception_is_thrown_without_host
102 assert_raise RuntimeError do
103 W.new.url_for :controller => 'c', :action => 'a', :id => 'i'
104 end
105 end
106
107 def test_anchor
108 assert_equal('/c/a#anchor',
109 W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => 'anchor')
110 )
111 end
112
113 def test_default_host
114 add_host!
115 assert_equal('http://www.basecamphq.com/c/a/i',
116 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i')
117 )
118 end
119
120 def test_host_may_be_overridden
121 add_host!
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')
124 )
125 end
126
127 def test_port
128 add_host!
129 assert_equal('http://www.basecamphq.com:3000/c/a/i',
130 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :port => 3000)
131 )
132 end
133
134 def test_protocol
135 add_host!
136 assert_equal('https://www.basecamphq.com/c/a/i',
137 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
138 )
139 end
140
141 def test_protocol_with_and_without_separator
142 add_host!
143 assert_equal('https://www.basecamphq.com/c/a/i',
144 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
145 )
146 assert_equal('https://www.basecamphq.com/c/a/i',
147 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https://')
148 )
149 end
150
151 def test_trailing_slash
152 add_host!
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) )
155 end
156
157 def test_trailing_slash_with_protocol
158 add_host!
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'}))
162 end
163
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}))
170 end
171
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'}))
176 end
177
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
183 end
184
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'
188
189 add_host!
190 assert_equal('https://www.basecamphq.com/subdir/c/a/i',
191 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
192 )
193 ensure
194 ActionController::Base.relative_url_root = orig_relative_url_root
195 end
196
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'
202 end
203
204 # We need to create a new class in order to install the new named route.
205 kls = Class.new { include ActionController::UrlWriter }
206 controller = kls.new
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')
210
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'))
214 ensure
215 ActionController::Routing::Routes.load!
216 end
217
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'
221
222 ActionController::Routing::Routes.draw do |map|
223 map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index'
224 end
225
226 kls = Class.new { include ActionController::UrlWriter }
227 controller = kls.new
228
229 assert_equal 'http://www.basecamphq.com/subdir/home/sweet/home/again',
230 controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
231 ensure
232 ActionController::Routing::Routes.load!
233 ActionController::Base.relative_url_root = orig_relative_url_root
234 end
235
236 def test_only_path
237 ActionController::Routing::Routes.draw do |map|
238 map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index'
239 map.connect ':controller/:action/:id'
240 end
241
242 # We need to create a new class in order to install the new named route.
243 kls = Class.new { include ActionController::UrlWriter }
244 controller = kls.new
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)
248
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'))
251 ensure
252 ActionController::Routing::Routes.load!
253 end
254
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')
258 )
259 end
260
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
266 end
267
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
273 end
274
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
280 end
281
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
288 end
289
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
298 end
299
300 def test_path_generation_for_symbol_parameter_keys
301 assert_generates("/image", :controller=> :image)
302 end
303
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'
308 map.resources :posts
309 map.connect ':controller/:action/:id'
310 end
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'
314
315 controller = kls.new
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))
320 ensure
321 ActionController::Routing::Routes.load!
322 end
323
324 def test_formatted_url_methods_are_deprecated
325 ActionController::Routing::Routes.draw do |map|
326 map.resources :posts
327 end
328 # We need to create a new class in order to install the new named route.
329 kls = Class.new { include ActionController::UrlWriter }
330 controller = kls.new
331 params = {:id => 1, :format => :xml}
332 assert_deprecated do
333 assert_equal("/posts/1.xml", controller.send(:formatted_post_path, params))
334 end
335 assert_deprecated do
336 assert_equal("/posts/1.xml", controller.send(:formatted_post_path, 1, :xml))
337 end
338 ensure
339 ActionController::Routing::Routes.load!
340 end
341
342 def test_multiple_includes_maintain_distinct_options
343 first_class = Class.new { include ActionController::UrlWriter }
344 second_class = Class.new { include ActionController::UrlWriter }
345
346 first_host, second_host = 'firsthost.com', 'secondhost.com'
347
348 first_class.default_url_options[:host] = first_host
349 second_class.default_url_options[:host] = second_host
350
351 assert_equal first_class.default_url_options[:host], first_host
352 assert_equal second_class.default_url_options[:host], second_host
353 end
354
355 private
356 def extract_params(url)
357 url.split('?', 2).last.split('&')
358 end
359 end