File: uri_spec.rb

package info (click to toggle)
ruby-webmock 3.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: ruby: 12,905; makefile: 6
file content (371 lines) | stat: -rw-r--r-- 13,265 bytes parent folder | download | duplicates (4)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
require 'spec_helper'

URIS_WITHOUT_PATH_OR_PARAMS =
[
  "www.example.com",
  "www.example.com/",
  "www.example.com:80",
  "www.example.com:80/",
  "http://www.example.com",
  "http://www.example.com/",
  "http://www.example.com:80",
  "http://www.example.com:80/"
].sort

URIS_WITHOUT_PATH_BUT_WITH_PARAMS =
[
  "www.example.com?a=b",
  "www.example.com/?a=b",
  "www.example.com:80?a=b",
  "www.example.com:80/?a=b",
  "http://www.example.com?a=b",
  "http://www.example.com/?a=b",
  "http://www.example.com:80?a=b",
  "http://www.example.com:80/?a=b"
].sort

URIS_WITH_AUTH =
[
  "a b:pass@www.example.com",
  "a b:pass@www.example.com/",
  "a b:pass@www.example.com:80",
  "a b:pass@www.example.com:80/",
  "http://a b:pass@www.example.com",
  "http://a b:pass@www.example.com/",
  "http://a b:pass@www.example.com:80",
  "http://a b:pass@www.example.com:80/",
  "a%20b:pass@www.example.com",
  "a%20b:pass@www.example.com/",
  "a%20b:pass@www.example.com:80",
  "a%20b:pass@www.example.com:80/",
  "http://a%20b:pass@www.example.com",
  "http://a%20b:pass@www.example.com/",
  "http://a%20b:pass@www.example.com:80",
  "http://a%20b:pass@www.example.com:80/"
].sort

URIS_WITH_PATH_AND_PARAMS =
[
  "www.example.com/my path/?a=my param&b=c d",
  "www.example.com/my%20path/?a=my%20param&b=c%20d",
  "www.example.com:80/my path/?a=my param&b=c d",
  "www.example.com:80/my%20path/?a=my%20param&b=c%20d",
  "http://www.example.com/my path/?a=my param&b=c d",
  "http://www.example.com/my%20path/?a=my%20param&b=c%20d",
  "http://www.example.com:80/my path/?a=my param&b=c d",
  "http://www.example.com:80/my%20path/?a=my%20param&b=c%20d",
  ].sort

URIS_WITH_DIFFERENT_PORT =
[
  "www.example.com:88",
  "www.example.com:88/",
  "http://www.example.com:88",
  "http://www.example.com:88/"
].sort

URIS_FOR_HTTPS =
[
  "https://www.example.com",
  "https://www.example.com/",
  "https://www.example.com:443",
  "https://www.example.com:443/"
].sort

URIS_FOR_LOCALHOST =
[
  "localhost",
  "localhost/",
  "localhost:80",
  "localhost:80/",
  "http://localhost",
  "http://localhost/",
  "http://localhost:80",
  "http://localhost:80/"
].sort

URIS_WITH_SCHEME =
[
  "http://www.example.com",
  "http://www.example.com/",
  "http://www.example.com:80",
  "http://www.example.com:80/"
].sort

URIS_WITH_COLON_IN_PATH =
[
  [
    "https://example.com/a/b:80",
    "https://example.com:443/a/b:80",
  ].sort,
  [
    "https://example.com:443/a/b:443",
    "https://example.com/a/b:443",
  ].sort,
  [
    "http://example.com/a/b:443",
    "example.com/a/b:443",
    "http://example.com:80/a/b:443",
    "example.com:80/a/b:443",
  ].sort,
  [
    "http://example.com/a/b:80",
    "example.com/a/b:80",
    "http://example.com:80/a/b:80",
    "example.com:80/a/b:80",
  ].sort
]

describe WebMock::Util::URI do

  describe "reporting variations of uri" do

    it "should find all variations of the same uri for all variations of uri with params and path" do
      URIS_WITH_PATH_AND_PARAMS.each do |uri|
        expect(WebMock::Util::URI.variations_of_uri_as_strings(uri).sort).to eq(URIS_WITH_PATH_AND_PARAMS)
      end
    end

    it "should find all variations of the same uri for all variations of uri with params but without path" do
      URIS_WITHOUT_PATH_BUT_WITH_PARAMS.each do |uri|
        expect(WebMock::Util::URI.variations_of_uri_as_strings(uri).sort).to eq(URIS_WITHOUT_PATH_BUT_WITH_PARAMS)
      end
    end

    it "should find all variations of the same uri for all variations of uri without params or path" do
      URIS_WITHOUT_PATH_OR_PARAMS.each do |uri|
        expect(WebMock::Util::URI.variations_of_uri_as_strings(uri).sort).to eq(URIS_WITHOUT_PATH_OR_PARAMS)
      end
    end

    it "should find all variations of the same uri for all variations of uri with auth" do
      URIS_WITH_AUTH.each do |uri|
        expect(WebMock::Util::URI.variations_of_uri_as_strings(uri).sort).to eq(URIS_WITH_AUTH)
      end
    end

    it "should find all variations of the same uri for all variations of uri with different port" do
      URIS_WITH_DIFFERENT_PORT.each do |uri|
        expect(WebMock::Util::URI.variations_of_uri_as_strings(uri).sort).to eq(URIS_WITH_DIFFERENT_PORT)
      end
    end

    it "should find all variations of the same uri for all variations of https uris" do
      URIS_FOR_HTTPS.each do |uri|
        expect(WebMock::Util::URI.variations_of_uri_as_strings(uri).sort).to eq(URIS_FOR_HTTPS)
      end
    end

    it "should find all variations of the same uri for all variations of host names uris without a period" do
      URIS_FOR_LOCALHOST.each do |uri|
        expect(WebMock::Util::URI.variations_of_uri_as_strings(uri).sort).to eq(URIS_FOR_LOCALHOST)
      end
    end

    it "should find all variations of the same uri with scheme for all variations when only_with_scheme is true" do
      URIS_WITHOUT_PATH_OR_PARAMS.each do |uri|
        variations_of_uri_with_scheme = WebMock::Util::URI.variations_of_uri_as_strings(uri, only_with_scheme: true)
        expect(variations_of_uri_with_scheme.sort).to eq(URIS_WITH_SCHEME)
      end
    end

    it "should not replace :80 or :443 in path" do
      URIS_WITH_COLON_IN_PATH.each do |uris|
        uris.each do |uri|
          expect(WebMock::Util::URI.variations_of_uri_as_strings(uri).sort).to eq(uris)
        end
      end
    end

    it "should find all variations of uris with https, basic auth, a non-standard port and a path" do
      uri = "https://~%8A:pass@www.example.com:9000/foo"
      variations = [
        "https://~%8A:pass@www.example.com:9000/foo",
        "https://~\x8A:pass@www.example.com:9000/foo".force_encoding(Encoding::ASCII_8BIT)
      ]

      expect(WebMock::Util::URI.variations_of_uri_as_strings(uri)).to eq(variations)
    end

  end

  describe "normalized uri equality" do

    it "should successfully compare all variations of the same uri with path and params" do
      URIS_WITH_PATH_AND_PARAMS.each do |uri_a|
        URIS_WITH_PATH_AND_PARAMS.each do |uri_b|
          expect(WebMock::Util::URI.normalize_uri(uri_a)).to be ===  WebMock::Util::URI.normalize_uri(uri_b)
        end
      end
    end

    it "should successfully compare all variations of the same uri with path but with params" do
      URIS_WITHOUT_PATH_BUT_WITH_PARAMS.each do |uri_a|
        URIS_WITHOUT_PATH_BUT_WITH_PARAMS.each do |uri_b|
          expect(WebMock::Util::URI.normalize_uri(uri_a)).to be ===  WebMock::Util::URI.normalize_uri(uri_b)
        end
      end
    end

    it "should successfully compare all variations of the same uri without path or params" do
      URIS_WITHOUT_PATH_OR_PARAMS.each do |uri_a|
        URIS_WITHOUT_PATH_OR_PARAMS.each do |uri_b|
          expect(WebMock::Util::URI.normalize_uri(uri_a)).to be ===  WebMock::Util::URI.normalize_uri(uri_b)
        end
      end
    end

    it "should successfully compare all variations of the same uri with authority" do
      URIS_WITH_AUTH.each do |uri_a|
        URIS_WITH_AUTH.each do |uri_b|
          expect(WebMock::Util::URI.normalize_uri(uri_a)).to be ===  WebMock::Util::URI.normalize_uri(uri_b)
        end
      end
    end

    it "should successfully compare all variations of the same uri custom port" do
      URIS_WITH_DIFFERENT_PORT.each do |uri_a|
        URIS_WITH_DIFFERENT_PORT.each do |uri_b|
          expect(WebMock::Util::URI.normalize_uri(uri_a)).to be ===  WebMock::Util::URI.normalize_uri(uri_b)
        end
      end
    end

    it "should successfully compare all variations of the same https uri" do
      URIS_FOR_HTTPS.each do |uri_a|
        URIS_FOR_HTTPS.each do |uri_b|
          expect(WebMock::Util::URI.normalize_uri(uri_a)).to be ===  WebMock::Util::URI.normalize_uri(uri_b)
        end
      end
    end

    it "should successfully handle array parameters" do
      uri_string = 'http://www.example.com:80/path?a[]=b&a[]=c'
      uri = WebMock::Util::URI.normalize_uri(uri_string)
      expect(WebMock::Util::QueryMapper.query_to_values(uri.query)).to eq({"a"=>["b", "c"]})
    end

    it "should successfully handle hash parameters" do
      uri_string = 'http://www.example.com:80/path?a[d]=b&a[e]=c&a[b][c]=1'
      uri = WebMock::Util::URI.normalize_uri(uri_string)
      expect(WebMock::Util::QueryMapper.query_to_values(uri.query)).to eq({"a"=>{"d"=>"b", "e"=>"c", "b"=>{"c"=>"1"}}})
    end

    it "should successfully handle nested hash parameters" do
      uri_string = 'http://www.example.com:80/path?one[two][three][]=four&one[two][three][]=five'
      uri = WebMock::Util::URI.normalize_uri(uri_string)
      expect(WebMock::Util::QueryMapper.query_to_values(uri.query)).to eq({"one"=>{"two"=>{"three" => ["four", "five"]}}})
    end

    it "should successfully handle mixed array and hash parameters" do
      # derived from an elasticsearch query:
      # load => {
      #   :include => [
      #     { :staff => :email },
      #     :business_name
      #   ]
      # }
      uri_string = "http://www.example.com:80/path?load[include][][staff]=email&load[include][]=business_name"
      uri = WebMock::Util::URI.normalize_uri(uri_string)
      expect(WebMock::Util::QueryMapper.query_to_values(uri.query)).to eq({"load"=>{"include"=>[{"staff"=>"email"},"business_name"]}})
    end

    context "when query notation is set to :flat_array" do
      before :all do
        WebMock::Config.instance.query_values_notation = :flat_array
      end

      it "should successfully handle repeated paramters" do
        uri_string = "http://www.example.com:80/path?target=host1&target=host2"
        uri = WebMock::Util::URI.normalize_uri(uri_string)
        expect(WebMock::Util::QueryMapper.query_to_values(uri.query, notation: WebMock::Config.instance.query_values_notation)).to eq([['target', 'host1'], ['target', 'host2']])
      end
    end
  end

  describe "sorting query values" do

    context "when query values is a Hash" do
      it "returns an alphabetically sorted hash" do
        sorted_query = WebMock::Util::URI.sort_query_values({"b"=>"one", "a"=>"two"})
        expect(sorted_query).to eq({"a"=>"two", "b"=>"one"})
      end
    end

    context "when query values is an Array" do
      it "returns an alphabetically sorted array" do
        sorted_query = WebMock::Util::URI.sort_query_values([["b","two"],["a","one_b"],["a","one_a"]])
        expect(sorted_query).to eq([["a","one_a"],["a","one_b"],["b","two"]])
      end
    end
  end

  describe "stripping default port" do

    it "should strip_default_port_from_uri strips 80 from http with path" do
      uri = "http://example.com:80/foo/bar"
      stripped_uri = WebMock::Util::URI.strip_default_port_from_uri_string(uri)
      expect(stripped_uri).to eq("http://example.com/foo/bar")
    end

    it "should strip_default_port_from_uri strips 80 from http without path" do
      uri = "http://example.com:80"
      stripped_uri = WebMock::Util::URI.strip_default_port_from_uri_string(uri)
      expect(stripped_uri).to eq("http://example.com")
    end

    it "should strip_default_port_from_uri strips 443 from https without path" do
      uri = "https://example.com:443"
      stripped_uri = WebMock::Util::URI.strip_default_port_from_uri_string(uri)
      expect(stripped_uri).to eq("https://example.com")
    end

    it "should strip_default_port_from_uri strips 443 from https" do
      uri = "https://example.com:443/foo/bar"
      stripped_uri = WebMock::Util::URI.strip_default_port_from_uri_string(uri)
      expect(stripped_uri).to eq("https://example.com/foo/bar")
    end

    it "should strip_default_port_from_uri does not strip 8080 from http" do
      uri = "http://example.com:8080/foo/bar"
      expect(WebMock::Util::URI.strip_default_port_from_uri_string(uri)).to eq(uri)
    end

    it "should strip_default_port_from_uri does not strip 443 from http" do
      uri = "http://example.com:443/foo/bar"
      expect(WebMock::Util::URI.strip_default_port_from_uri_string(uri)).to eq(uri)
    end

    it "should strip_default_port_from_uri does not strip 80 from query string" do
      uri = "http://example.com/?a=:80&b=c"
      expect(WebMock::Util::URI.strip_default_port_from_uri_string(uri)).to eq(uri)
    end

    it "should strip_default_port_from_uri does not modify strings that do not start with http or https" do
      uri = "httpz://example.com:80/"
      expect(WebMock::Util::URI.strip_default_port_from_uri_string(uri)).to eq(uri)
    end

  end


  describe "encoding userinfo" do

    it "should encode unsafe chars in userinfo does not encode userinfo safe punctuation" do
      userinfo = "user;&=+$,:secret"
      expect(WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)).to eq(userinfo)
    end

    it "should encode unsafe chars in userinfo does not encode rfc 3986 unreserved characters" do
      userinfo = "-.!~*'()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:secret"
      expect(WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)).to eq(userinfo)
    end

    it "should encode unsafe chars in userinfo does encode other characters" do
      userinfo, safe_userinfo = 'us#rn@me:sec//ret?"', 'us%23rn%40me:sec%2F%2Fret%3F%22'
      expect(WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)).to eq(safe_userinfo)
    end

  end

end