class UrlHelperTest

Attributes

controller[RW]

In a few cases, the helper proxies to 'controller' or request.

In those cases, we'll set up a simple mock

request[RW]

In a few cases, the helper proxies to 'controller' or request.

In those cases, we'll set up a simple mock

Public Instance Methods

form_authenticity_token(*args) click to toggle source
# File actionview/test/template/url_helper_test.rb, line 689
def form_authenticity_token(*args)
  "secret"
end
hash_for(options = {}) click to toggle source
# File actionview/test/template/url_helper_test.rb, line 36
def hash_for(options = {})
  { controller: "foo", action: "bar" }.merge!(options)
end
Also aliased as: url_hash
protect_against_forgery?() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 685
def protect_against_forgery?
  request_forgery
end
request_for_url(url, opts = {}) click to toggle source
# File actionview/test/template/url_helper_test.rb, line 484
def request_for_url(url, opts = {})
  env = Rack::MockRequest.env_for("http://www.example.com#{url}", opts)
  ActionDispatch::Request.new(env)
end
request_forgery_protection_token() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 693
def request_forgery_protection_token
  "form_token"
end
test_button_to_enabled_disabled() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 190
def test_button_to_enabled_disabled
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to"><input type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", disabled: false)
  )
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to"><input disabled="disabled" type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", disabled: true)
  )
end
test_button_to_with_block() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 215
def test_button_to_with_block
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to"><button type="submit"><span>Hello</span></button></form>},
    button_to("http://www.example.com") { content_tag(:span, "Hello") }
  )
end
test_button_to_with_form_class() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 128
def test_button_to_with_form_class
  assert_dom_equal %{<form method="post" action="http://www.example.com" class="custom-class"><input type="submit" value="Hello" /></form>}, button_to("Hello", "http://www.example.com", form_class: "custom-class")
end
test_button_to_with_form_class_escapes() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 132
def test_button_to_with_form_class_escapes
  assert_dom_equal %{<form method="post" action="http://www.example.com" class="&lt;script&gt;evil_js&lt;/script&gt;"><input type="submit" value="Hello" /></form>}, button_to("Hello", "http://www.example.com", form_class: "<script>evil_js</script>")
end
test_button_to_with_html_safe_URL() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 140
def test_button_to_with_html_safe_URL
  assert_dom_equal %{<form method="post" action="http://www.example.com/q1=v1&amp;q2=v2" class="button_to"><input type="submit" value="Hello" /></form>}, button_to("Hello", raw("http://www.example.com/q1=v1&amp;q2=v2"))
end
test_button_to_with_javascript_confirm() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 148
def test_button_to_with_javascript_confirm
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to"><input data-confirm="Are you sure?" type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", data: { confirm: "Are you sure?" })
  )
end
test_button_to_with_javascript_disable_with() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 155
def test_button_to_with_javascript_disable_with
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to"><input data-disable-with="Greeting..." type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", data: { disable_with: "Greeting..." })
  )
end
test_button_to_with_method_delete() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 201
def test_button_to_with_method_delete
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to"><input type="hidden" name="_method" value="delete" /><input type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", method: :delete)
  )
end
test_button_to_with_method_get() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 208
def test_button_to_with_method_get
  assert_dom_equal(
    %{<form method="get" action="http://www.example.com" class="button_to"><input type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", method: :get)
  )
end
test_button_to_with_nested_array_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 267
def test_button_to_with_nested_array_params
  assert_dom_equal(
    %{<form action="http://www.example.com" class="button_to" method="post"><input type="submit" value="Hello" /><input type="hidden" name="foo[]" value="bar" /></form>},
    button_to("Hello", "http://www.example.com", params: { foo: ["bar"] })
  )
end
test_button_to_with_nested_hash_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 260
def test_button_to_with_nested_hash_params
  assert_dom_equal(
    %{<form action="http://www.example.com" class="button_to" method="post"><input type="submit" value="Hello" /><input type="hidden" name="foo[bar]" value="baz" /></form>},
    button_to("Hello", "http://www.example.com", params: { foo: { bar: "baz" } })
  )
end
test_button_to_with_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 222
def test_button_to_with_params
  assert_dom_equal(
    %{<form action="http://www.example.com" class="button_to" method="post"><input type="submit" value="Hello" /><input type="hidden" name="baz" value="quux" /><input type="hidden" name="foo" value="bar" /></form>},
    button_to("Hello", "http://www.example.com", params: { foo: :bar, baz: "quux" })
  )
end
test_button_to_with_path() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 110
def test_button_to_with_path
  assert_dom_equal(
    %{<form method="post" action="/article/Hello" class="button_to"><input type="submit" value="Hello" /></form>},
    button_to("Hello", article_path("Hello"))
  )
end
test_button_to_with_permitted_strong_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 247
def test_button_to_with_permitted_strong_params
  assert_dom_equal(
    %{<form action="http://www.example.com" class="button_to" method="post"><input type="submit" value="Hello" /><input type="hidden" name="baz" value="quux" /><input type="hidden" name="foo" value="bar" /></form>},
    button_to("Hello", "http://www.example.com", params: FakeParams.new)
  )
end
test_button_to_with_query() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 136
def test_button_to_with_query
  assert_dom_equal %{<form method="post" action="http://www.example.com/q1=v1&amp;q2=v2" class="button_to"><input type="submit" value="Hello" /></form>}, button_to("Hello", "http://www.example.com/q1=v1&q2=v2")
end
test_button_to_with_query_and_no_name() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 144
def test_button_to_with_query_and_no_name
  assert_dom_equal %{<form method="post" action="http://www.example.com?q1=v1&amp;q2=v2" class="button_to"><input type="submit" value="http://www.example.com?q1=v1&amp;q2=v2" /></form>}, button_to(nil, "http://www.example.com?q1=v1&q2=v2")
end
test_button_to_with_remote_and_form_options() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 162
def test_button_to_with_remote_and_form_options
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="custom-class" data-remote="true" data-type="json"><input type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", remote: true, form: { class: "custom-class", "data-type" => "json" })
  )
end
test_button_to_with_remote_and_javascript_confirm() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 169
def test_button_to_with_remote_and_javascript_confirm
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to" data-remote="true"><input data-confirm="Are you sure?" type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", remote: true, data: { confirm: "Are you sure?" })
  )
end
test_button_to_with_remote_and_javascript_disable_with() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 176
def test_button_to_with_remote_and_javascript_disable_with
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to" data-remote="true"><input data-disable-with="Greeting..." type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", remote: true, data: { disable_with: "Greeting..." })
  )
end
test_button_to_with_remote_false() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 183
def test_button_to_with_remote_false
  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to"><input type="submit" value="Hello" /></form>},
    button_to("Hello", "http://www.example.com", remote: false)
  )
end
test_button_to_with_straight_url() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 106
def test_button_to_with_straight_url
  assert_dom_equal %{<form method="post" action="http://www.example.com" class="button_to"><input type="submit" value="Hello" /></form>}, button_to("Hello", "http://www.example.com")
end
test_button_to_with_straight_url_and_request_forgery() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 117
def test_button_to_with_straight_url_and_request_forgery
  self.request_forgery = true

  assert_dom_equal(
    %{<form method="post" action="http://www.example.com" class="button_to"><input type="submit" value="Hello" /><input name="form_token" type="hidden" value="secret" /></form>},
    button_to("Hello", "http://www.example.com")
  )
ensure
  self.request_forgery = false
end
test_button_to_with_unpermitted_strong_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 254
def test_button_to_with_unpermitted_strong_params
  assert_raises(ArgumentError) do
    button_to("Hello", "http://www.example.com", params: FakeParams.new(false))
  end
end
test_current_page_considering_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 508
def test_current_page_considering_params
  @request = request_for_url("/?order=desc&page=1")

  assert !current_page?(url_hash, check_parameters: true)
  assert !current_page?(url_hash.merge(check_parameters: true))
  assert !current_page?(ActionController::Parameters.new(url_hash.merge(check_parameters: true)).permit!)
  assert !current_page?("http://www.example.com/", check_parameters: true)
end
test_current_page_considering_params_when_options_does_not_respond_to_to_hash() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 517
def test_current_page_considering_params_when_options_does_not_respond_to_to_hash
  @request = request_for_url("/?order=desc&page=1")

  assert !current_page?(:back, check_parameters: false)
end
test_current_page_ignoring_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 501
def test_current_page_ignoring_params
  @request = request_for_url("/?order=desc&page=1")

  assert current_page?(url_hash)
  assert current_page?("http://www.example.com/")
end
test_current_page_with_double_escaped_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 550
def test_current_page_with_double_escaped_params
  @request = request_for_url("/category/administra%c3%a7%c3%a3o?callback_url=http%3a%2f%2fexample.com%2ffoo")

  assert current_page?(controller: "foo", action: "category", category: "administração", callback_url: "http://example.com/foo")
end
test_current_page_with_escaped_params() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 536
def test_current_page_with_escaped_params
  @request = request_for_url("/category/administra%c3%a7%c3%a3o")

  assert current_page?(controller: "foo", action: "category", category: "administração")
end
test_current_page_with_escaped_params_with_different_encoding() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 542
def test_current_page_with_escaped_params_with_different_encoding
  @request = request_for_url("/")
  @request.stub(:path, "/category/administra%c3%a7%c3%a3o".dup.force_encoding(Encoding::ASCII_8BIT)) do
    assert current_page?(controller: "foo", action: "category", category: "administração")
    assert current_page?("http://www.example.com/category/administra%c3%a7%c3%a3o")
  end
end
test_current_page_with_http_head_method() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 489
def test_current_page_with_http_head_method
  @request = request_for_url("/", method: :head)
  assert current_page?(url_hash)
  assert current_page?("http://www.example.com/")
end
test_current_page_with_not_get_verb() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 562
def test_current_page_with_not_get_verb
  @request = request_for_url("/events", method: :post)

  assert !current_page?("/events")
end
test_current_page_with_params_that_match() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 523
def test_current_page_with_params_that_match
  @request = request_for_url("/?order=desc&page=1")

  assert current_page?(hash_for(order: "desc", page: "1"))
  assert current_page?("http://www.example.com/?order=desc&page=1")
end
test_current_page_with_scope_that_match() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 530
def test_current_page_with_scope_that_match
  @request = request_for_url("/engine/")

  assert current_page?("/engine")
end
test_current_page_with_simple_url() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 495
def test_current_page_with_simple_url
  @request = request_for_url("/")
  assert current_page?(url_hash)
  assert current_page?("http://www.example.com/")
end
test_current_page_with_trailing_slash() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 556
def test_current_page_with_trailing_slash
  @request = request_for_url("/posts")

  assert current_page?("/posts/")
end
test_link_tag_using_post_javascript() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 371
def test_link_tag_using_post_javascript
  assert_dom_equal(
    %{<a href="http://www.example.com" data-method="post" rel="nofollow">Hello</a>},
    link_to("Hello", "http://www.example.com", method: :post)
  )
end
test_link_tag_using_post_javascript_and_confirm() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 399
def test_link_tag_using_post_javascript_and_confirm
  assert_dom_equal(
    %{<a href="http://www.example.com" data-method="post" rel="nofollow" data-confirm="Are you serious?">Hello</a>},
    link_to("Hello", "http://www.example.com", method: :post, data: { confirm: "Are you serious?" })
  )
end
test_link_tag_using_post_javascript_and_rel() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 392
def test_link_tag_using_post_javascript_and_rel
  assert_dom_equal(
    %{<a href="http://www.example.com" data-method="post" rel="example nofollow">Hello</a>},
    link_to("Hello", "http://www.example.com", method: :post, rel: "example")
  )
end
test_mail_to() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 616
def test_mail_to
  assert_dom_equal %{<a href="mailto:david@loudthinking.com">david@loudthinking.com</a>}, mail_to("david@loudthinking.com")
  assert_dom_equal %{<a href="mailto:david@loudthinking.com">David Heinemeier Hansson</a>}, mail_to("david@loudthinking.com", "David Heinemeier Hansson")
  assert_dom_equal(
    %{<a class="admin" href="mailto:david@loudthinking.com">David Heinemeier Hansson</a>},
    mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin")
  )
  assert_equal mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin"),
               mail_to("david@loudthinking.com", "David Heinemeier Hansson", class: "admin")
end
test_mail_to_does_not_modify_html_options_hash() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 679
def test_mail_to_does_not_modify_html_options_hash
  options = { class: "special" }
  mail_to "me@example.com", "ME!", options
  assert_equal({ class: "special" }, options)
end
test_mail_to_returns_html_safe_string() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 665
def test_mail_to_returns_html_safe_string
  assert mail_to("david@loudthinking.com").html_safe?
end
test_mail_to_with_block() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 669
def test_mail_to_with_block
  assert_dom_equal %{<a href="mailto:me@example.com"><span>Email me</span></a>},
    mail_to("me@example.com") { content_tag(:span, "Email me") }
end
test_mail_to_with_block_and_options() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 674
def test_mail_to_with_block_and_options
  assert_dom_equal %{<a class="special" href="mailto:me@example.com?cc=ccaddress%40example.com"><span>Email me</span></a>},
    mail_to("me@example.com", cc: "ccaddress@example.com", class: "special") { content_tag(:span, "Email me") }
end
test_mail_to_with_html_safe_string() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 651
def test_mail_to_with_html_safe_string
  assert_dom_equal(
    %{<a href="mailto:david@loudthinking.com">david@loudthinking.com</a>},
    mail_to(raw("david@loudthinking.com"))
  )
end
test_mail_to_with_img() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 646
def test_mail_to_with_img
  assert_dom_equal %{<a href="mailto:feedback@example.com"><img src="/feedback.png" /></a>},
    mail_to("feedback@example.com", raw('<img src="/feedback.png" />'))
end
test_mail_to_with_nil() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 658
def test_mail_to_with_nil
  assert_dom_equal(
    %{<a href="mailto:"></a>},
    mail_to(nil)
  )
end
test_mail_to_with_special_characters() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 627
def test_mail_to_with_special_characters
  assert_dom_equal(
    %{<a href="mailto:%23%21%24%25%26%27%2A%2B-%2F%3D%3F%5E_%60%7B%7D%7C@example.org">#!$%&amp;&#39;*+-/=?^_`{}|@example.org</a>},
    mail_to("#!$%&'*+-/=?^_`{}|@example.org")
  )
end
test_mail_with_options() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 634
def test_mail_with_options
  assert_dom_equal(
    %{<a href="mailto:me@example.com?cc=ccaddress%40example.com&amp;bcc=bccaddress%40example.com&amp;body=This%20is%20the%20body%20of%20the%20message.&amp;subject=This%20is%20an%20example%20email&amp;reply-to=foo%40bar.com">My email</a>},
    mail_to("me@example.com", "My email", cc: "ccaddress@example.com", bcc: "bccaddress@example.com", subject: "This is an example email", body: "This is the body of the message.", reply_to: "foo@bar.com")
  )

  assert_dom_equal(
    %{<a href="mailto:me@example.com?body=This%20is%20the%20body%20of%20the%20message.&amp;subject=This%20is%20an%20example%20email">My email</a>},
    mail_to("me@example.com", "My email", cc: "", bcc: "", subject: "This is an example email", body: "This is the body of the message.")
  )
end
test_to_form_params_with_array_nested_in_hash() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 92
def test_to_form_params_with_array_nested_in_hash
  assert_equal(
    [{ name: "countries[]", value: "Denmark" }, { name: "countries[]", value: "Sweden" }],
    to_form_params(countries: ["Denmark", "Sweden"])
  )
end
test_to_form_params_with_hash() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 78
def test_to_form_params_with_hash
  assert_equal(
    [{ name: :name, value: "David" }, { name: :nationality, value: "Danish" }],
    to_form_params(name: "David", nationality: "Danish")
  )
end
test_to_form_params_with_namespace() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 99
def test_to_form_params_with_namespace
  assert_equal(
    [{ name: "country[name]", value: "Denmark" }],
    to_form_params({ name: "Denmark" }, "country")
  )
end
test_to_form_params_with_nested_hash() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 85
def test_to_form_params_with_nested_hash
  assert_equal(
    [{ name: "country[name]", value: "Denmark" }],
    to_form_params(country: { name: "Denmark" })
  )
end
test_url_for_does_not_escape_urls() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 41
def test_url_for_does_not_escape_urls
  assert_equal "/?a=b&c=d", url_for(hash_for(a: :b, c: :d))
end
test_url_for_does_not_include_empty_hashes() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 45
def test_url_for_does_not_include_empty_hashes
  assert_equal "/", url_for(hash_for(a: {}))
end
test_url_for_with_back() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 49
def test_url_for_with_back
  referer = "http://www.example.com/referer"
  @controller = Struct.new(:request).new(Struct.new(:env).new("HTTP_REFERER" => referer))

  assert_equal "http://www.example.com/referer", url_for(:back)
end
test_url_for_with_back_and_javascript_referer() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 66
def test_url_for_with_back_and_javascript_referer
  referer = "javascript:alert(document.cookie)"
  @controller = Struct.new(:request).new(Struct.new(:env).new("HTTP_REFERER" => referer))
  assert_equal "javascript:history.back()", url_for(:back)
end
test_url_for_with_back_and_no_controller() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 61
def test_url_for_with_back_and_no_controller
  @controller = nil
  assert_equal "javascript:history.back()", url_for(:back)
end
test_url_for_with_back_and_no_referer() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 56
def test_url_for_with_back_and_no_referer
  @controller = Struct.new(:request).new(Struct.new(:env).new({}))
  assert_equal "javascript:history.back()", url_for(:back)
end
test_url_for_with_invalid_referer() click to toggle source
# File actionview/test/template/url_helper_test.rb, line 72
def test_url_for_with_invalid_referer
  referer = "THIS IS NOT A URL"
  @controller = Struct.new(:request).new(Struct.new(:env).new("HTTP_REFERER" => referer))
  assert_equal "javascript:history.back()", url_for(:back)
end
url_hash(options = {})
Alias for: hash_for