aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/url_rewriter_test.rb
blob: 7b46a48a1da2c82d1823001079edcf44138f7b25 (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
require 'abstract_unit'
require 'controller/fake_controllers'

class UrlRewriterTests < ActionController::TestCase
  class Rewriter
    def initialize(request)
      @options = {
        :host => request.host_with_port,
        :protocol => request.protocol
      }
    end

    def rewrite(router, options)
      router.url_for(@options.merge(options))
    end
  end

  def setup
    @request = ActionController::TestRequest.new
    @params = {}
    @rewriter = Rewriter.new(@request) #.new(@request, @params)
  end

  def test_port
    assert_equal('http://test.host:1271/c/a/i',
      @rewriter.rewrite(@router, :controller => 'c', :action => 'a', :id => 'i', :port => 1271)
    )
  end

  def test_protocol_with_and_without_separator
    assert_equal('https://test.host/c/a/i',
      @rewriter.rewrite(@router, :protocol => 'https', :controller => 'c', :action => 'a', :id => 'i')
    )

    assert_equal('https://test.host/c/a/i',
      @rewriter.rewrite(@router, :protocol => 'https://', :controller => 'c', :action => 'a', :id => 'i')
    )
  end

  def test_user_name_and_password
    assert_equal(
      'http://david:secret@test.host/c/a/i',
      @rewriter.rewrite(@router, :user => "david", :password => "secret", :controller => 'c', :action => 'a', :id => 'i')
    )
  end

  def test_user_name_and_password_with_escape_codes
    assert_equal(
      'http://openid.aol.com%2Fnextangler:one+two%3F@test.host/c/a/i',
      @rewriter.rewrite(@router, :user => "openid.aol.com/nextangler", :password => "one two?", :controller => 'c', :action => 'a', :id => 'i')
    )
  end

  def test_anchor
    assert_equal(
      'http://test.host/c/a/i#anchor',
      @rewriter.rewrite(@router, :controller => 'c', :action => 'a', :id => 'i', :anchor => 'anchor')
    )
  end

  def test_anchor_should_call_to_param
    assert_equal(
      'http://test.host/c/a/i#anchor',
      @rewriter.rewrite(@router, :controller => 'c', :action => 'a', :id => 'i', :anchor => Struct.new(:to_param).new('anchor'))
    )
  end

  def test_anchor_should_be_cgi_escaped
    assert_equal(
      'http://test.host/c/a/i#anc%2Fhor',
      @rewriter.rewrite(@router, :controller => 'c', :action => 'a', :id => 'i', :anchor => Struct.new(:to_param).new('anc/hor'))
    )
  end

  def test_trailing_slash
    options = {:controller => 'foo', :action => 'bar', :id => '3', :only_path => true}
    assert_equal '/foo/bar/3', @rewriter.rewrite(@router, options)
    assert_equal '/foo/bar/3?query=string', @rewriter.rewrite(@router, options.merge({:query => 'string'}))
    options.update({:trailing_slash => true})
    assert_equal '/foo/bar/3/', @rewriter.rewrite(@router, options)
    options.update({:query => 'string'})
    assert_equal '/foo/bar/3/?query=string', @rewriter.rewrite(@router, options)
  end
end