File: escape_test.rb

package info (click to toggle)
ruby-escape-utils 1.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 388 kB
  • sloc: ansic: 1,742; ruby: 1,079; sh: 7; makefile: 4
file content (69 lines) | stat: -rw-r--r-- 2,442 bytes parent folder | download | duplicates (3)
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
require File.expand_path("../../helper", __FILE__)
require 'cgi'

class UriComponentEscapeTest < Minitest::Test
  def test_basic_url
    assert_equal "http%3A%2F%2Fwww.homerun.com%2F", EscapeUtils.escape_uri_component("http://www.homerun.com/")
  end

  def test_cgi_equivalence
    (0..127).each do |i|
      c = i.chr
      # Escaping URI path components should match CGI parameter escaping, except
      # spaces should be escaped as "%20" instead of "+" and "~" should be
      # escaped as "%7E".
      assert_equal CGI.escape(c).sub("+", "%20").sub("~", "%7E"), EscapeUtils.escape_uri_component(c)
    end
  end

  def test_uri_component_containing_tags
    assert_equal "fo%3Co%3Ebar", EscapeUtils.escape_uri_component("fo<o>bar")
  end

  def test_uri_component_containing_tags_containing_spaces
    assert_equal "a%20space", EscapeUtils.escape_uri_component("a space")
    assert_equal "a%20%20%20sp%20ace%20", EscapeUtils.escape_uri_component("a   sp ace ")
  end

  def test_uri_component_containing_mixed_characters
    assert_equal "q1%212%22%27w%245%267%2Fz8%29%3F%5C", EscapeUtils.escape_uri_component("q1!2\"'w$5&7/z8)?\\")
  end

  def test_multibyte_characters
    matz_name = "\xE3\x81\xBE\xE3\x81\xA4\xE3\x82\x82\xE3\x81\xA8" # Matsumoto
    assert_equal '%E3%81%BE%E3%81%A4%E3%82%82%E3%81%A8', EscapeUtils.escape_uri_component(matz_name)
    matz_name_sep = "\xE3\x81\xBE\xE3\x81\xA4 \xE3\x82\x82\xE3\x81\xA8" # Matsu moto
    assert_equal '%E3%81%BE%E3%81%A4%20%E3%82%82%E3%81%A8', EscapeUtils.escape_uri_component(matz_name_sep)
  end

  def test_uri_component_containing_pluses
    assert_equal "a%2Bplus", EscapeUtils.escape_uri_component("a+plus")
  end

  def test_uri_component_containing_slashes
    assert_equal "a%2Fslash", EscapeUtils.escape_uri_component("a/slash")
  end

  if RUBY_VERSION =~ /^1.9/
    def test_input_must_be_utf8_or_ascii
      str = "fo<o>bar"

      str.force_encoding 'ISO-8859-1'
      assert_raises Encoding::CompatibilityError do
        EscapeUtils.escape_uri_component(str)
      end

      str.force_encoding 'UTF-8'
      begin
        EscapeUtils.escape_uri_component(str)
      rescue Encoding::CompatibilityError => e
        assert_nil e, "#{e.class.name} raised, expected not to"
      end
    end

    def test_return_value_is_tagged_as_utf8
      str = "fo<o>bar"
      assert_equal Encoding.find('UTF-8'), EscapeUtils.escape_uri_component(str).encoding
    end
  end
end