Namespace
Methods
A
T
Included Modules
Instance Public methods
add_host!()
# File actionpack/test/controller/url_for_test.rb, line 15
def add_host!
  W.default_url_options[:host] = 'www.basecamphq.com'
end
add_numeric_host!()
# File actionpack/test/controller/url_for_test.rb, line 19
def add_numeric_host!
  W.default_url_options[:host] = '127.0.0.1'
end
teardown()
# File actionpack/test/controller/url_for_test.rb, line 11
def teardown
  W.default_url_options.clear
end
test_anchor()
# File actionpack/test/controller/url_for_test.rb, line 29
def test_anchor
  assert_equal('/c/a#anchor',
    W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => 'anchor')
  )
end
test_anchor_should_call_to_param()
# File actionpack/test/controller/url_for_test.rb, line 35
def test_anchor_should_call_to_param
  assert_equal('/c/a#anchor',
    W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => Struct.new(:to_param).new('anchor'))
  )
end
test_anchor_should_escape_unsafe_pchar()
# File actionpack/test/controller/url_for_test.rb, line 41
def test_anchor_should_escape_unsafe_pchar
  assert_equal('/c/a#%23anchor',
    W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => Struct.new(:to_param).new('#anchor'))
  )
end
test_anchor_should_not_escape_safe_pchar()
# File actionpack/test/controller/url_for_test.rb, line 47
def test_anchor_should_not_escape_safe_pchar
  assert_equal('/c/a#name=user&email=user@domain.com',
    W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => Struct.new(:to_param).new('name=user&email=user@domain.com'))
  )
end
test_array_parameter()
# File actionpack/test/controller/url_for_test.rb, line 272
def test_array_parameter
  url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => ['Bob', 'prof'])
  params = extract_params(url)
  assert_equal params[0], { 'query[]' => 'Bob'  }.to_query
  assert_equal params[1], { 'query[]' => 'prof' }.to_query
end
test_default_host()
# File actionpack/test/controller/url_for_test.rb, line 53
def test_default_host
  add_host!
  assert_equal('http://www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i')
  )
end
test_domain_may_be_changed()
# File actionpack/test/controller/url_for_test.rb, line 103
def test_domain_may_be_changed
  add_host!
  assert_equal('http://www.37signals.com/c/a/i',
    W.new.url_for(:domain => '37signals.com', :controller => 'c', :action => 'a', :id => 'i')
  )
end
test_exception_is_thrown_without_host()
# File actionpack/test/controller/url_for_test.rb, line 23
def test_exception_is_thrown_without_host
  assert_raise ArgumentError do
    W.new.url_for :controller => 'c', :action => 'a', :id => 'i'
  end
end
test_false_url_params_are_included_in_query()
# File actionpack/test/controller/url_for_test.rb, line 351
def test_false_url_params_are_included_in_query
  assert_equal("/c/a?show=false", W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :show => false))
end
test_hash_parameter()
# File actionpack/test/controller/url_for_test.rb, line 265
def test_hash_parameter
  url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => {:name => 'Bob', :category => 'prof'})
  params = extract_params(url)
  assert_equal params[0], { 'query[category]' => 'prof' }.to_query
  assert_equal params[1], { 'query[name]'     => 'Bob'  }.to_query
end
test_hash_recursive_and_array_parameters()
# File actionpack/test/controller/url_for_test.rb, line 287
def test_hash_recursive_and_array_parameters
  url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :id => 101, :query => {:person => {:name => 'Bob', :position => ['prof', 'art director']}, :hobby => 'piercing'})
  assert_match(%r(^/c/a/101), url)
  params = extract_params(url)
  assert_equal params[0], { 'query[hobby]'              => 'piercing'     }.to_query
  assert_equal params[1], { 'query[person][name]'       => 'Bob'          }.to_query
  assert_equal params[2], { 'query[person][position][]' => 'art director' }.to_query
  assert_equal params[3], { 'query[person][position][]' => 'prof'         }.to_query
end
test_hash_recursive_parameters()
# File actionpack/test/controller/url_for_test.rb, line 279
def test_hash_recursive_parameters
  url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => {:person => {:name => 'Bob', :position => 'prof'}, :hobby => 'piercing'})
  params = extract_params(url)
  assert_equal params[0], { 'query[hobby]'            => 'piercing' }.to_query
  assert_equal params[1], { 'query[person][name]'     => 'Bob'      }.to_query
  assert_equal params[2], { 'query[person][position]' => 'prof'     }.to_query
end
test_host_may_be_overridden()
# File actionpack/test/controller/url_for_test.rb, line 60
def test_host_may_be_overridden
  add_host!
  assert_equal('http://37signals.basecamphq.com/c/a/i',
    W.new.url_for(:host => '37signals.basecamphq.com', :controller => 'c', :action => 'a', :id => 'i')
  )
end
test_multiple_includes_maintain_distinct_options()
# File actionpack/test/controller/url_for_test.rb, line 320
def test_multiple_includes_maintain_distinct_options
  first_class = Class.new { include ActionController::UrlFor }
  second_class = Class.new { include ActionController::UrlFor }

  first_host, second_host = 'firsthost.com', 'secondhost.com'

  first_class.default_url_options[:host] = first_host
  second_class.default_url_options[:host] = second_host

  assert_equal  first_host, first_class.default_url_options[:host]
  assert_equal second_host, second_class.default_url_options[:host]
end
test_multiple_subdomains_may_be_removed()
# File actionpack/test/controller/url_for_test.rb, line 89
def test_multiple_subdomains_may_be_removed
  W.default_url_options[:host] = 'mobile.www.api.basecamphq.com'
  assert_equal('http://basecamphq.com/c/a/i',
    W.new.url_for(:subdomain => false, :controller => 'c', :action => 'a', :id => 'i')
  )
end
test_named_routes()
# File actionpack/test/controller/url_for_test.rb, line 198
def test_named_routes
  with_routing do |set|
    set.draw do
      match 'this/is/verbose', :to => 'home#index', :as => :no_args
      match 'home/sweet/home/:user', :to => 'home#index', :as => :home
    end

    # We need to create a new class in order to install the new named route.
    kls = Class.new { include set.url_helpers }

    controller = kls.new
    assert controller.respond_to?(:home_url)
    assert_equal 'http://www.basecamphq.com/home/sweet/home/again',
      controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')

    assert_equal("/home/sweet/home/alabama", controller.send(:home_path, :user => 'alabama', :host => 'unused'))
    assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'www.basecamphq.com'))
    assert_equal("http://www.basecamphq.com/this/is/verbose", controller.send(:no_args_url, :host=>'www.basecamphq.com'))
  end
end
test_named_routes_with_nil_keys()
# File actionpack/test/controller/url_for_test.rb, line 301
def test_named_routes_with_nil_keys
  with_routing do |set|
    set.draw do
      match 'posts.:format', :to => 'posts#index', :as => :posts
      match '/', :to => 'posts#index', :as => :main
    end

    # We need to create a new class in order to install the new named route.
    kls = Class.new { include set.url_helpers }
    kls.default_url_options[:host] = 'www.basecamphq.com'

    controller = kls.new
    params = {:action => :index, :controller => :posts, :format => :xml}
    assert_equal("http://www.basecamphq.com/posts.xml", controller.send(:url_for, params))
    params[:format] = nil
    assert_equal("http://www.basecamphq.com/", controller.send(:url_for, params))
  end
end
test_one_parameter()
# File actionpack/test/controller/url_for_test.rb, line 252
def test_one_parameter
  assert_equal('/c/a?param=val',
    W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :param => 'val')
  )
end
test_only_path()
# File actionpack/test/controller/url_for_test.rb, line 233
def test_only_path
  with_routing do |set|
    set.draw do
      match 'home/sweet/home/:user', :to => 'home#index', :as => :home
      match ':controller/:action/:id'
    end

    # We need to create a new class in order to install the new named route.
    kls = Class.new { include set.url_helpers }
    controller = kls.new
    assert controller.respond_to?(:home_url)
    assert_equal '/brave/new/world',
      controller.send(:url_for, :controller => 'brave', :action => 'new', :id => 'world', :only_path => true)

    assert_equal("/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'unused', :only_path => true))
    assert_equal("/home/sweet/home/alabama", controller.send(:home_path, 'alabama'))
  end
end
test_path_generation_for_symbol_parameter_keys()
# File actionpack/test/controller/url_for_test.rb, line 297
def test_path_generation_for_symbol_parameter_keys
  assert_generates("/image", :controller=> :image)
end
test_port()
# File actionpack/test/controller/url_for_test.rb, line 117
def test_port
  add_host!
  assert_equal('http://www.basecamphq.com:3000/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :port => 3000)
  )
end
test_protocol()
# File actionpack/test/controller/url_for_test.rb, line 124
def test_protocol
  add_host!
  assert_equal('https://www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
  )
end
test_protocol_with_and_without_separators()
# File actionpack/test/controller/url_for_test.rb, line 131
def test_protocol_with_and_without_separators
  add_host!
  assert_equal('https://www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
  )
  assert_equal('https://www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https:')
  )
  assert_equal('https://www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https://')
  )
end
test_relative_url_root_is_respected()
# File actionpack/test/controller/url_for_test.rb, line 188
def test_relative_url_root_is_respected
  # ROUTES TODO: Tests should not have to pass :relative_url_root directly. This
  # should probably come from routes.

  add_host!
  assert_equal('https://www.basecamphq.com/subdir/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https', :script_name => '/subdir')
  )
end
test_relative_url_root_is_respected_for_named_routes()
# File actionpack/test/controller/url_for_test.rb, line 219
def test_relative_url_root_is_respected_for_named_routes
  with_routing do |set|
    set.draw do
      match '/home/sweet/home/:user', :to => 'home#index', :as => :home
    end

    kls = Class.new { include set.url_helpers }
    controller = kls.new

    assert_equal 'http://www.basecamphq.com/subdir/home/sweet/home/again',
      controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again', :script_name => "/subdir")
  end
end
test_subdomain_may_be_accepted_with_numeric_host()
# File actionpack/test/controller/url_for_test.rb, line 96
def test_subdomain_may_be_accepted_with_numeric_host
  add_numeric_host!
  assert_equal('http://127.0.0.1/c/a/i',
    W.new.url_for(:subdomain => 'api', :controller => 'c', :action => 'a', :id => 'i')
  )
end
test_subdomain_may_be_changed()
# File actionpack/test/controller/url_for_test.rb, line 67
def test_subdomain_may_be_changed
  add_host!
  assert_equal('http://api.basecamphq.com/c/a/i',
    W.new.url_for(:subdomain => 'api', :controller => 'c', :action => 'a', :id => 'i')
  )
end
test_subdomain_may_be_object()
# File actionpack/test/controller/url_for_test.rb, line 74
def test_subdomain_may_be_object
  model = mock(:to_param => 'api')
  add_host!
  assert_equal('http://api.basecamphq.com/c/a/i',
    W.new.url_for(:subdomain => model, :controller => 'c', :action => 'a', :id => 'i')
  )
end
test_subdomain_may_be_removed()
# File actionpack/test/controller/url_for_test.rb, line 82
def test_subdomain_may_be_removed
  add_host!
  assert_equal('http://basecamphq.com/c/a/i',
    W.new.url_for(:subdomain => false, :controller => 'c', :action => 'a', :id => 'i')
  )
end
test_tld_length_may_be_changed()
# File actionpack/test/controller/url_for_test.rb, line 110
def test_tld_length_may_be_changed
  add_host!
  assert_equal('http://mobile.www.basecamphq.com/c/a/i',
    W.new.url_for(:subdomain => 'mobile', :tld_length => 2, :controller => 'c', :action => 'a', :id => 'i')
  )
end
test_trailing_slash()
# File actionpack/test/controller/url_for_test.rb, line 154
def test_trailing_slash
  add_host!
  options = {:controller => 'foo', :trailing_slash => true, :action => 'bar', :id => '33'}
  assert_equal('http://www.basecamphq.com/foo/bar/33/', W.new.url_for(options) )
end
test_trailing_slash_with_anchor()
# File actionpack/test/controller/url_for_test.rb, line 175
def test_trailing_slash_with_anchor
  options = {:trailing_slash => true, :controller => 'foo', :action => 'bar', :id => '33', :only_path => true, :anchor=> 'chapter7'}
  assert_equal '/foo/bar/33/#chapter7', W.new.url_for(options)
  assert_equal '/foo/bar/33/?query=string#chapter7', W.new.url_for(options.merge({:query => 'string'}))
end
test_trailing_slash_with_only_path()
# File actionpack/test/controller/url_for_test.rb, line 167
def test_trailing_slash_with_only_path
  options = {:controller => 'foo', :trailing_slash => true}
  assert_equal '/foo/', W.new.url_for(options.merge({:only_path => true}))
  options.update({:action => 'bar', :id => '33'})
  assert_equal '/foo/bar/33/', W.new.url_for(options.merge({:only_path => true}))
  assert_equal '/foo/bar/33/?query=string', W.new.url_for(options.merge({:query => 'string',:only_path => true}))
end
test_trailing_slash_with_params()
# File actionpack/test/controller/url_for_test.rb, line 181
def test_trailing_slash_with_params
  url = W.new.url_for(:trailing_slash => true, :only_path => true, :controller => 'cont', :action => 'act', :p1 => 'cafe', :p2 => 'link')
  params = extract_params(url)
  assert_equal params[0], { :p1 => 'cafe' }.to_query
  assert_equal params[1], { :p2 => 'link' }.to_query
end
test_trailing_slash_with_protocol()
# File actionpack/test/controller/url_for_test.rb, line 160
def test_trailing_slash_with_protocol
  add_host!
  options = { :trailing_slash => true,:protocol => 'https', :controller => 'foo', :action => 'bar', :id => '33'}
  assert_equal('https://www.basecamphq.com/foo/bar/33/', W.new.url_for(options) )
  assert_equal 'https://www.basecamphq.com/foo/bar/33/?query=string', W.new.url_for(options.merge({:query => 'string'}))
end
test_two_parameters()
# File actionpack/test/controller/url_for_test.rb, line 258
def test_two_parameters
  url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :p1 => 'X1', :p2 => 'Y2')
  params = extract_params(url)
  assert_equal params[0], { :p1 => 'X1' }.to_query
  assert_equal params[1], { :p2 => 'Y2' }.to_query
end
test_url_params_with_nil_to_param_are_not_in_url()
# File actionpack/test/controller/url_for_test.rb, line 347
def test_url_params_with_nil_to_param_are_not_in_url
  assert_equal("/c/a", W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :id => Struct.new(:to_param).new(nil)))
end
test_with_hash_with_indifferent_access()
# File actionpack/test/controller/url_for_test.rb, line 338
def test_with_hash_with_indifferent_access
  W.default_url_options[:controller] = 'd'
  W.default_url_options[:only_path]  = false
  assert_equal("/c", W.new.url_for(HashWithIndifferentAccess.new('controller' => 'c', 'only_path' => true)))

  W.default_url_options[:action] = 'b'
  assert_equal("/c/a", W.new.url_for(HashWithIndifferentAccess.new('controller' => 'c', 'action' => 'a', 'only_path' => true)))
end
test_with_stringified_keys()
# File actionpack/test/controller/url_for_test.rb, line 333
def test_with_stringified_keys
  assert_equal("/c", W.new.url_for('controller' => 'c', 'only_path' => true))
  assert_equal("/c/a", W.new.url_for('controller' => 'c', 'action' => 'a', 'only_path' => true))
end
test_without_protocol()
# File actionpack/test/controller/url_for_test.rb, line 144
def test_without_protocol
  add_host!
  assert_equal('//www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => '//')
  )
  assert_equal('//www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => false)
  )
end