Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / url_rewriter_test.rb
1 require 'abstract_unit'
2
3 ActionController::UrlRewriter
4
5 class UrlRewriterTests < Test::Unit::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 < Test::Unit::TestCase
89
90 class W
91 include ActionController::UrlWriter
92 end
93
94 def teardown
95 W.default_url_options.clear
96 end
97
98 def add_host!
99 W.default_url_options[:host] = 'www.basecamphq.com'
100 end
101
102 def test_exception_is_thrown_without_host
103 assert_raises RuntimeError do
104 W.new.url_for :controller => 'c', :action => 'a', :id => 'i'
105 end
106 end
107
108 def test_anchor
109 assert_equal('/c/a#anchor',
110 W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => 'anchor')
111 )
112 end
113
114 def test_default_host
115 add_host!
116 assert_equal('http://www.basecamphq.com/c/a/i',
117 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i')
118 )
119 end
120
121 def test_host_may_be_overridden
122 add_host!
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')
125 )
126 end
127
128 def test_port
129 add_host!
130 assert_equal('http://www.basecamphq.com:3000/c/a/i',
131 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :port => 3000)
132 )
133 end
134
135 def test_protocol
136 add_host!
137 assert_equal('https://www.basecamphq.com/c/a/i',
138 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
139 )
140 end
141
142 def test_protocol_with_and_without_separator
143 add_host!
144 assert_equal('https://www.basecamphq.com/c/a/i',
145 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
146 )
147 assert_equal('https://www.basecamphq.com/c/a/i',
148 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https://')
149 )
150 end
151
152 def test_trailing_slash
153 add_host!
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) )
156 end
157
158 def test_trailing_slash_with_protocol
159 add_host!
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'}))
163 end
164
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}))
171 end
172
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'}))
177 end
178
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
184 end
185
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'
189
190 add_host!
191 assert_equal('https://www.basecamphq.com/subdir/c/a/i',
192 W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
193 )
194 ensure
195 ActionController::Base.relative_url_root = orig_relative_url_root
196 end
197
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'
203 end
204
205 # We need to create a new class in order to install the new named route.
206 kls = Class.new { include ActionController::UrlWriter }
207 controller = kls.new
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')
211
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'))
215 ensure
216 ActionController::Routing::Routes.load!
217 end
218
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'
222
223 ActionController::Routing::Routes.draw do |map|
224 map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index'
225 end
226
227 kls = Class.new { include ActionController::UrlWriter }
228 controller = kls.new
229
230 assert_equal 'http://www.basecamphq.com/subdir/home/sweet/home/again',
231 controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
232 ensure
233 ActionController::Routing::Routes.load!
234 ActionController::Base.relative_url_root = orig_relative_url_root
235 end
236
237 def test_only_path
238 ActionController::Routing::Routes.draw do |map|
239 map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index'
240 map.connect ':controller/:action/:id'
241 end
242
243 # We need to create a new class in order to install the new named route.
244 kls = Class.new { include ActionController::UrlWriter }
245 controller = kls.new
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)
249
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'))
252 ensure
253 ActionController::Routing::Routes.load!
254 end
255
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')
259 )
260 end
261
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
267 end
268
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
274 end
275
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
281 end
282
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
289 end
290
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
299 end
300
301 def test_path_generation_for_symbol_parameter_keys
302 assert_generates("/image", :controller=> :image)
303 end
304
305 private
306 def extract_params(url)
307 url.split('?', 2).last.split('&')
308 end
309 end