aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/url_test.rb
blob: bf6a7aab75282f84550a5773ca1dae64f1727b99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
require File.dirname(__FILE__) + '/../abstract_unit'
require 'action_controller/url_rewriter'

MockRequest = Struct.new("MockRequest", :protocol, :host, :port, :path, :parameters)
class MockRequest
  def host_with_port
    if (protocol == "http://" && port == 80) || (protocol == "https://" && port == 443)
      host
    else
      host + ":#{port}"
    end
  end
end

class UrlTest < Test::Unit::TestCase
  def setup
    @library_url = ActionController::UrlRewriter.new(MockRequest.new(
      "http://",
      "www.singlefile.com", 
      80,
      "/library/books/ISBN/0743536703/show",
      { "type" => "ISBN", "code" => "0743536703" }
    ), "books", "show")

    @library_url_on_index = ActionController::UrlRewriter.new(MockRequest.new(
      "http://",
      "www.singlefile.com", 
      80,
      "/library/books/ISBN/0743536703/", 
      { "type" => "ISBN", "code" => "0743536703" }
    ), "books", "index")
    
    @clean_urls = [
      ActionController::UrlRewriter.new(MockRequest.new(
        "http://", "www.singlefile.com", 80, "/identity/", {}
      ), "identity", "index"),
      ActionController::UrlRewriter.new(MockRequest.new(
        "http://", "www.singlefile.com", 80, "/identity", {}
      ), "identity", "index")
    ]

    @clean_url_with_id = ActionController::UrlRewriter.new(MockRequest.new(
      "http://", "www.singlefile.com", 80, "/identity/show/5", { "id" => "5" }
    ), "identity", "show")

    @clean_url_with_id_as_char = ActionController::UrlRewriter.new(MockRequest.new(
      "http://", "www.singlefile.com", 80, "/teachers/show/t", { "id" => "t" }
    ), "teachers", "show")
  end

  def test_clean_action
    assert_equal "http://www.singlefile.com/library/books/ISBN/0743536703/edit", @library_url.rewrite(:action => "edit")
  end

  def test_clean_action_with_only_path
    assert_equal "/library/books/ISBN/0743536703/edit", @library_url.rewrite(:action => "edit", :only_path => true)
  end

  def test_action_from_index
    assert_equal "http://www.singlefile.com/library/books/ISBN/0743536703/edit", @library_url_on_index.rewrite(:action => "edit")
  end

  def test_action_from_index_on_clean
    @clean_urls.each do |url|
      assert_equal "http://www.singlefile.com/identity/edit", url.rewrite(:action => "edit")
    end
  end

  def test_action_without_prefix
    assert_equal "http://www.singlefile.com/library/books/", @library_url.rewrite(:action => "index", :action_prefix => "")
  end

  def test_action_with_prefix
    assert_equal(
      "http://www.singlefile.com/library/books/XTC/123/show", 
      @library_url.rewrite(:action => "show", :action_prefix => "XTC/123")
    )
  end

  def test_action_prefix_alone
    assert_equal(
      "http://www.singlefile.com/library/books/XTC/123/",
      @library_url.rewrite(:action_prefix => "XTC/123")
    )
  end

  def test_action_with_suffix
    assert_equal(
      "http://www.singlefile.com/library/books/show/XTC/123",
      @library_url.rewrite(:action => "show", :action_prefix => "", :action_suffix => "XTC/123")
    )
  end

  def test_clean_controller
    assert_equal "http://www.singlefile.com/library/settings/", @library_url.rewrite(:controller => "settings")
  end

  def test_clean_controller_prefix
    assert_equal "http://www.singlefile.com/shop/", @library_url.rewrite(:controller_prefix => "shop")
  end

  def test_clean_controller_with_module
    assert_equal "http://www.singlefile.com/shop/purchases/", @library_url.rewrite(:module => "shop", :controller => "purchases")
  end

  def test_controller_and_action
    assert_equal "http://www.singlefile.com/library/settings/show", @library_url.rewrite(:controller => "settings", :action => "show")
  end

  def test_controller_and_action_and_anchor
    assert_equal(
      "http://www.singlefile.com/library/settings/show#5", 
      @library_url.rewrite(:controller => "settings", :action => "show", :anchor => "5")
    )
  end

  def test_controller_and_action_and_empty_overwrite_params_and_anchor
    assert_equal(
      "http://www.singlefile.com/library/settings/show?code=0743536703&type=ISBN#5", 
      @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {},  :anchor => "5")
    )
  end
  
  def test_controller_and_action_and_overwrite_params_and_anchor
    assert_equal(
      "http://www.singlefile.com/library/settings/show?code=0000001&type=ISBN#5", 
      @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {"code"=>"0000001"},  :anchor => "5")
    )
  end

  def test_controller_and_action_and_overwrite_params_with_nil_value_and_anchor
    assert_equal(
      "http://www.singlefile.com/library/settings/show?type=ISBN#5", 
      @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {"code" => nil},  :anchor => "5")
    )
  end

  def test_controller_and_action_params_and_overwrite_params_and_anchor
    assert_equal(
      "http://www.singlefile.com/library/settings/show?code=0000001&version=5.0#5", 
      @library_url.rewrite(:controller => "settings", :action => "show", :params=>{"version" => "5.0"},  :overwrite_params => {"code"=>"0000001"},  :anchor => "5")
    )
  end

  def test_controller_and_action_and_params_anchor
    assert_equal(
      "http://www.singlefile.com/library/settings/show?update=1#5", 
      @library_url.rewrite(:controller => "settings", :action => "show", :params => { "update" => "1"}, :anchor => "5")
    )
  end

  def test_controller_and_index_action
    assert_equal "http://www.singlefile.com/library/settings/", @library_url.rewrite(:controller => "settings", :action => "index")
  end

  def test_controller_and_action_with_same_name_as_controller
    @clean_urls.each do |url|
      assert_equal "http://www.singlefile.com/anything/identity", url.rewrite(:controller => "anything", :action => "identity")
    end
  end

  def test_controller_and_index_action_without_controller_prefix
    assert_equal(
      "http://www.singlefile.com/settings/", 
      @library_url.rewrite(:controller => "settings", :action => "index", :controller_prefix => "")
    )
  end

  def test_controller_and_index_action_with_controller_prefix
    assert_equal(
      "http://www.singlefile.com/fantastic/settings/show", 
      @library_url.rewrite(:controller => "settings", :action => "show", :controller_prefix => "fantastic")
    )
  end

  def test_path_parameters
    assert_equal "http://www.singlefile.com/library/books/EXBC/0743536703/show", @library_url.rewrite(:path_params => {"type" => "EXBC"})
  end
  
  def test_parameters
    assert_equal(
      "http://www.singlefile.com/library/books/ISBN/0743536703/show?delete=1&name=David", 
      @library_url.rewrite(:params => {"delete" => "1", "name" => "David"})
    )
  end

  def test_parameters_with_id
    @clean_urls.each do |url|
      assert_equal(
        "http://www.singlefile.com/identity/show?name=David&id=5", 
        url.rewrite(
          :action => "show",
          :params => { "id" => "5", "name" => "David" }
        )
      )
    end
  end
  
  def test_action_with_id
   assert_equal(
      "http://www.singlefile.com/identity/show/7", 
      @clean_url_with_id.rewrite(
        :action => "show", 
        :id => 7
      )
    )
    @clean_urls.each do |url|
      assert_equal(
        "http://www.singlefile.com/identity/index/7", 
        url.rewrite(:id => 7)
      )
    end
  end

  def test_parameters_with_id_and_away
    assert_equal(
      "http://www.singlefile.com/identity/show/25?name=David", 
      @clean_url_with_id.rewrite(
        :path_params => { "id" => "25" },
        :params => { "name" => "David" }
      )
    )
  end

  def test_parameters_with_index_and_id
    @clean_urls.each do |url|
      assert_equal(
        "http://www.singlefile.com/identity/index/25?name=David", 
        url.rewrite(
          :path_params => { "id" => "25" },
          :params => { "name" => "David" }
        )
      )
    end
  end

  def test_action_going_away_from_id
    assert_equal(
      "http://www.singlefile.com/identity/list", 
      @clean_url_with_id.rewrite(
        :action => "list"
      )
    )
  end

  def test_parameters_with_direct_id_and_away
    assert_equal(
      "http://www.singlefile.com/identity/show/25?name=David", 
      @clean_url_with_id.rewrite(
        :id => "25",
        :params => { "name" => "David" }
      )
    )
  end

  def test_parameters_with_direct_id_and_away
    assert_equal(
      "http://www.singlefile.com/store/open/25?name=David", 
      @clean_url_with_id.rewrite(
        :controller => "store",
        :action => "open",
        :id => "25",
        :params => { "name" => "David" }
      )
    )
  end

  def test_parameters_to_id
    @clean_urls.each do |url|
      %w(show index).each do |action|
        assert_equal(
          "http://www.singlefile.com/identity/#{action}/25?name=David", 
          url.rewrite(
            :action => action,
            :path_params => { "id" => "25" },
            :params => { "name" => "David" }
          )
        )
      end
    end
  end

  def test_parameters_from_id
    assert_equal(
      "http://www.singlefile.com/identity/", 
      @clean_url_with_id.rewrite(
        :action => "index"
      )
    )
  end
  
  def test_id_as_char_and_part_of_controller
    assert_equal(
      "http://www.singlefile.com/teachers/skill/5", 
      @clean_url_with_id_as_char.rewrite(
        :action => "skill",
        :id => 5
      )
    )
  end

  def test_from_clean_to_library
    @clean_urls.each do |url|
      assert_equal(
        "http://www.singlefile.com/library/books/ISBN/0743536703/show?delete=1&name=David", 
        url.rewrite(
          :controller_prefix => "library",
          :controller => "books", 
          :action_prefix => "ISBN/0743536703",
          :action => "show", 
          :params => { "delete" => "1", "name" => "David" }
        )
      )
    end
  end
  
  def test_from_library_to_clean
    assert_equal(
      "http://www.singlefile.com/identity/", 
      @library_url.rewrite(
        :controller => "identity", :controller_prefix => ""
      )
    )
  end
  
  def test_from_another_port
    @library_url = ActionController::UrlRewriter.new(MockRequest.new(
      "http://",
      "www.singlefile.com", 
      8080,
      "/library/books/ISBN/0743536703/show", 
      { "type" => "ISBN", "code" => "0743536703" }
    ), "books", "show")

    assert_equal(
      "http://www.singlefile.com:8080/identity/", 
      @library_url.rewrite(
        :controller => "identity", :controller_prefix => ""
      )
    )
  end
  
  def test_basecamp
    basecamp_url = ActionController::UrlRewriter.new(MockRequest.new(
      "http://",
      "projects.basecamp", 
      80,
      "/clients/disarray/1/msg/transcripts/", 
      {"category_name"=>"transcripts", "client_name"=>"disarray", "action"=>"index", "controller"=>"msg", "project_name"=>"1"}
    ), "msg", "index")
    
    assert_equal(
      "http://projects.basecamp/clients/disarray/1/msg/transcripts/1/comments", 
      basecamp_url.rewrite(:action_prefix => "transcripts/1", :action => "comments")
    )
  end

  def test_on_explicit_index_page # My index page is very modest, thank you...
    url = ActionController::UrlRewriter.new(
      MockRequest.new(
        "http://", "example.com", 80, "/controller/index",
        {"controller"=>"controller", "action"=>"index"}
      ), "controller", "index"
    )
    assert_equal("http://example.com/controller/foo", url.rewrite(:action => 'foo'))
  end

end