File: uri_util_test.rb

package info (click to toggle)
ruby-json-schema 2.8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 5,806; makefile: 6
file content (206 lines) | stat: -rw-r--r-- 7,704 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require File.expand_path('../support/test_helper', __FILE__)

class UriUtilTest < Minitest::Test
  def populate_cache_with(str, &blk)
    cached_uri = Addressable::URI.parse(str)
    Addressable::URI.stub(:parse, cached_uri, &blk)
    cached_uri
  end

  def teardown
    JSON::Util::URI.clear_cache
  end

  def test_normalized_uri
    str = "https://www.google.com/search"
    uri = Addressable::URI.new(scheme: 'https',
                               host: 'www.google.com',
                               path: 'search')
    assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
  end

  def test_normalized_uri_with_empty_fragment
    str = "https://www.google.com/search#"
    uri = Addressable::URI.new(scheme: 'https',
                               host: 'www.google.com',
                               path: 'search',
                               fragment: nil)
    assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
  end

  def test_normalized_uri_with_fragment
    str = "https://www.google.com/search#foo"
    uri = Addressable::URI.new(scheme: 'https',
                               host: 'www.google.com',
                               path: 'search',
                               fragment: 'foo')
    assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
  end

  def test_normalized_uri_for_absolute_path
    str = "/foo/bar.json"
    uri = Addressable::URI.new(scheme: 'file',
                               host: '',
                               path: '/foo/bar.json')
    assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
  end

  def test_normalized_uri_for_relative_path
    str = "foo/bar.json"
    uri = Addressable::URI.new(scheme: 'file',
                               host: '',
                               path: '/home/foo/bar.json')
    assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
  end

  def test_normalized_uri_for_file_path_with_host
    str = "file://localhost/foo/bar.json"
    uri = Addressable::URI.new(scheme: 'file',
                               host: 'localhost',
                               path: '/foo/bar.json')
    assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home')
  end

  def test_uri_parse
    str = "https://www.google.com/search"
    uri = Addressable::URI.new(scheme: 'https',
                               host: 'www.google.com',
                               path: 'search')
    assert_equal uri, JSON::Util::URI.parse(str)
  end

  def test_invalid_uri_parse
    uri = ":::::::"
    assert_raises(JSON::Schema::UriError) do
      JSON::Util::URI.parse(uri)
    end
  end

  def test_normalization_cache
    cached_uri = populate_cache_with('www.google.com') do
      JSON::Util::URI.normalized_uri('foo')
    end

    assert_equal(cached_uri, JSON::Util::URI.normalized_uri('foo'))

    JSON::Util::URI.clear_cache

    refute_equal(cached_uri, JSON::Util::URI.normalized_uri('foo'))
  end

  def test_parse_cache
    cached_uri = populate_cache_with('www.google.com') do
      JSON::Util::URI.parse('foo')
    end

    assert_equal(cached_uri, JSON::Util::URI.parse('foo'))

    JSON::Util::URI.clear_cache

    refute_equal(cached_uri, JSON::Util::URI.parse('foo'))
  end

  def test_validator_clear_cache_for_normalized_uri
    cached_uri = populate_cache_with('www.google.com') do
      JSON::Util::URI.normalized_uri('foo')
    end

    assert_equal(cached_uri, JSON::Util::URI.normalized_uri('foo'))

    validation_errors({"type" => "string"}, "foo", :clear_cache => true)

    refute_equal(cached_uri, JSON::Util::URI.normalized_uri('foo'))
  end

  def test_validator_clear_cache_for_parse
    cached_uri = populate_cache_with('www.google.com') do
      JSON::Util::URI.parse('foo')
    end

    assert_equal(cached_uri, JSON::Util::URI.parse('foo'))

    validation_errors({"type" => "string"}, "foo", :clear_cache => true)

    refute_equal(cached_uri, JSON::Util::URI.parse('foo'))
  end

  def test_ref_fragment_path
    uri = '#some-thing'
    base = 'http://www.example.com/foo/#bar'

    assert_equal Addressable::URI.parse('http://www.example.com/foo/#some-thing'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://www.example.com/foo/#'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_file_path
    uri = '/some/thing'
    base = 'http://www.example.com/foo/#bar'

    assert_equal Addressable::URI.parse('http://www.example.com/some/thing#'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://www.example.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_uri
    uri = 'http://foo-bar.com'
    base = 'http://www.example.com/foo/#bar'

    assert_equal Addressable::URI.parse('http://foo-bar.com/#'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://foo-bar.com/#'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_uri_with_path
    uri = 'http://foo-bar.com/some/thing'
    base = 'http://www.example.com/foo/#bar'

    assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_uri_with_fragment
    uri = 'http://foo-bar.com/some/thing#foo'
    base = 'http://www.example.com/hello/#world'

    assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#foo'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_uri_with_fragment_and_base_with_no_fragment
    uri = 'http://foo-bar.com/some/thing#foo'
    base = 'http://www.example.com/hello'

    assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#foo'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_relative_path
    uri = 'hello/world'
    base = 'http://www.example.com/foo/#bar'

    assert_equal Addressable::URI.parse('http://www.example.com/foo/hello/world#'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://www.example.com/foo/hello/world#'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_addressable_uri_with_host
    uri = Addressable::URI.new(:host => 'foo-bar.com')
    base = 'http://www.example.com/hello/#world'

    assert_equal Addressable::URI.parse('http://www.example.com/foo-bar.com#'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://www.example.com/hello/#world'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_addressable_uri_with_host_and_path
    uri = Addressable::URI.new(:host => 'foo-bar.com', :path => '/hello/world')
    base = 'http://www.example.com/a/#b'

    assert_equal Addressable::URI.parse('http://www.example.com/foo-bar.com/hello/world#'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('http://www.example.com/hello/world'), JSON::Util::URI.absolutize_ref(uri, base)
  end

  def test_ref_addressable_uri_with_shceme_host_and_path
    uri = Addressable::URI.new(:scheme => 'https', :host => 'foo-bar.com', :path => '/hello/world')
    base = 'http://www.example.com/a/#b'

    assert_equal Addressable::URI.parse('https://foo-bar.com/hello/world#'), JSON::Util::URI.normalize_ref(uri, base)
    assert_equal Addressable::URI.parse('https://foo-bar.com/hello/world'), JSON::Util::URI.absolutize_ref(uri, base)
  end
end