summaryrefslogtreecommitdiff
path: root/jni/ruby/test/webrick/test_httputils.rb
blob: 2753cbe6c900b1dcd7fae42ce7a8b48cf2d99796 (plain)
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
require "test/unit"
require "webrick/httputils"

class TestWEBrickHTTPUtils < Test::Unit::TestCase
  include WEBrick::HTTPUtils

  def test_normilize_path
    assert_equal("/foo",       normalize_path("/foo"))
    assert_equal("/foo/bar/",  normalize_path("/foo/bar/"))

    assert_equal("/",          normalize_path("/foo/../"))
    assert_equal("/",          normalize_path("/foo/.."))
    assert_equal("/",          normalize_path("/foo/bar/../../"))
    assert_equal("/",          normalize_path("/foo/bar/../.."))
    assert_equal("/",          normalize_path("/foo/bar/../.."))
    assert_equal("/baz",       normalize_path("/foo/bar/../../baz"))
    assert_equal("/baz",       normalize_path("/foo/../bar/../baz"))
    assert_equal("/baz/",      normalize_path("/foo/../bar/../baz/"))
    assert_equal("/...",       normalize_path("/bar/../..."))
    assert_equal("/.../",      normalize_path("/bar/../.../"))

    assert_equal("/foo/",      normalize_path("/foo/./"))
    assert_equal("/foo/",      normalize_path("/foo/."))
    assert_equal("/foo/",      normalize_path("/foo/././"))
    assert_equal("/foo/",      normalize_path("/foo/./."))
    assert_equal("/foo/bar",   normalize_path("/foo/./bar"))
    assert_equal("/foo/bar/",  normalize_path("/foo/./bar/."))
    assert_equal("/foo/bar/",  normalize_path("/./././foo/./bar/."))

    assert_equal("/foo/bar/",  normalize_path("//foo///.//bar/.///.//"))
    assert_equal("/",          normalize_path("//foo///..///bar/.///..//.//"))

    assert_raise(RuntimeError){ normalize_path("foo/bar") }
    assert_raise(RuntimeError){ normalize_path("..") }
    assert_raise(RuntimeError){ normalize_path("/..") }
    assert_raise(RuntimeError){ normalize_path("/./..") }
    assert_raise(RuntimeError){ normalize_path("/./../") }
    assert_raise(RuntimeError){ normalize_path("/./../..") }
    assert_raise(RuntimeError){ normalize_path("/./../../") }
    assert_raise(RuntimeError){ normalize_path("/./../") }
    assert_raise(RuntimeError){ normalize_path("/../..") }
    assert_raise(RuntimeError){ normalize_path("/../../") }
    assert_raise(RuntimeError){ normalize_path("/../../..") }
    assert_raise(RuntimeError){ normalize_path("/../../../") }
    assert_raise(RuntimeError){ normalize_path("/../foo/../") }
    assert_raise(RuntimeError){ normalize_path("/../foo/../../") }
    assert_raise(RuntimeError){ normalize_path("/foo/bar/../../../../") }
    assert_raise(RuntimeError){ normalize_path("/foo/../bar/../../") }
    assert_raise(RuntimeError){ normalize_path("/./../bar/") }
    assert_raise(RuntimeError){ normalize_path("/./../") }
  end

  def test_split_header_value
    assert_equal(['foo', 'bar'], split_header_value('foo, bar'))
    assert_equal(['"foo"', 'bar'], split_header_value('"foo", bar'))
    assert_equal(['foo', '"bar"'], split_header_value('foo, "bar"'))
    assert_equal(['*'], split_header_value('*'))
    assert_equal(['W/"xyzzy"', 'W/"r2d2xxxx"', 'W/"c3piozzzz"'],
                 split_header_value('W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"'))
  end

  def test_escape
    assert_equal("/foo/bar", escape("/foo/bar"))
    assert_equal("/~foo/bar", escape("/~foo/bar"))
    assert_equal("/~foo%20bar", escape("/~foo bar"))
    assert_equal("/~foo%20bar", escape("/~foo bar"))
    assert_equal("/~foo%09bar", escape("/~foo\tbar"))
    assert_equal("/~foo+bar", escape("/~foo+bar"))
    bug8425 = '[Bug #8425] [ruby-core:55052]'
    assert_nothing_raised(ArgumentError, Encoding::CompatibilityError, bug8425) {
      assert_equal("%E3%83%AB%E3%83%93%E3%83%BC%E3%81%95%E3%82%93", escape("\u{30EB 30D3 30FC 3055 3093}"))
    }
  end

  def test_escape_form
    assert_equal("%2Ffoo%2Fbar", escape_form("/foo/bar"))
    assert_equal("%2F~foo%2Fbar", escape_form("/~foo/bar"))
    assert_equal("%2F~foo+bar", escape_form("/~foo bar"))
    assert_equal("%2F~foo+%2B+bar", escape_form("/~foo + bar"))
  end

  def test_unescape
    assert_equal("/foo/bar", unescape("%2ffoo%2fbar"))
    assert_equal("/~foo/bar", unescape("/%7efoo/bar"))
    assert_equal("/~foo/bar", unescape("%2f%7efoo%2fbar"))
    assert_equal("/~foo+bar", unescape("/%7efoo+bar"))
  end

  def test_unescape_form
    assert_equal("//foo/bar", unescape_form("/%2Ffoo/bar"))
    assert_equal("//foo/bar baz", unescape_form("/%2Ffoo/bar+baz"))
    assert_equal("/~foo/bar baz", unescape_form("/%7Efoo/bar+baz"))
  end

  def test_escape_path
    assert_equal("/foo/bar", escape_path("/foo/bar"))
    assert_equal("/foo/bar/", escape_path("/foo/bar/"))
    assert_equal("/%25foo/bar/", escape_path("/%foo/bar/"))
  end
end