File: parameters_test.rb

package info (click to toggle)
ruby-faraday 0.13.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 596 kB
  • sloc: ruby: 5,821; sh: 136; makefile: 5
file content (141 lines) | stat: -rw-r--r-- 4,600 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
require File.expand_path("../helper", __FILE__)
require "rack/utils"

class TestParameters < Faraday::TestCase
  # emulates ActiveSupport::SafeBuffer#gsub
  FakeSafeBuffer = Struct.new(:string) do
    def to_s() self end
    def gsub(regex)
      string.gsub(regex) {
        match, = $&, '' =~ /a/
        yield(match)
      }
    end
  end

  def test_escaping_safe_buffer_nested
    monies = FakeSafeBuffer.new("$32,000.00")
    assert_equal "a=%2432%2C000.00", Faraday::NestedParamsEncoder.encode("a" => monies)
  end

  def test_escaping_safe_buffer_flat
    monies = FakeSafeBuffer.new("$32,000.00")
    assert_equal "a=%2432%2C000.00", Faraday::FlatParamsEncoder.encode("a" => monies)
  end

  def test_raises_typeerror_nested
    error = assert_raises TypeError do
      Faraday::NestedParamsEncoder.encode("")
    end
    assert_equal "Can't convert String into Hash.", error.message
  end

  def test_raises_typeerror_flat
    error = assert_raises TypeError do
      Faraday::FlatParamsEncoder.encode("")
    end
    assert_equal "Can't convert String into Hash.", error.message
  end

  def test_decode_array_nested
    query = "a[1]=one&a[2]=two&a[3]=three"
    expected = {"a" => ["one", "two", "three"]}
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_decode_array_flat
    query = "a=one&a=two&a=three"
    expected = {"a" => ["one", "two", "three"]}
    assert_equal expected, Faraday::FlatParamsEncoder.decode(query)
  end

  def test_nested_decode_hash
    query = "a[b1]=one&a[b2]=two&a[b][c]=foo"
    expected = {"a" => {"b1" => "one", "b2" => "two", "b" => {"c" => "foo"}}}
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_encode_nil_nested
    assert_equal "a", Faraday::NestedParamsEncoder.encode("a" => nil)
  end

  def test_encode_nil_flat
    assert_equal "a", Faraday::FlatParamsEncoder.encode("a" => nil)
  end

  def test_decode_nested_array_rack_compat
    query = "a[][one]=1&a[][two]=2&a[][one]=3&a[][two]=4"
    expected = Rack::Utils.parse_nested_query(query)
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_decode_nested_array_mixed_types
    query = "a[][one]=1&a[]=2&a[]=&a[]"
    expected = Rack::Utils.parse_nested_query(query)
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_decode_nested_ignores_invalid_array
    query = "[][a]=1&b=2"
    expected = {"a" => "1", "b" => "2"}
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_decode_nested_ignores_repeated_array_notation
    query = "a[][][]=1"
    expected = {"a" => ["1"]}
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_decode_nested_ignores_malformed_keys
    query = "=1&[]=2"
    expected = {}
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_decode_nested_subkeys_dont_have_to_be_in_brackets
    query = "a[b]c[d]e=1"
    expected = {"a" => {"b" => {"c" => {"d" => {"e" => "1"}}}}}
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_decode_nested_raises_error_when_expecting_hash
    error = assert_raises TypeError do
      Faraday::NestedParamsEncoder.decode("a=1&a[b]=2")
    end
    assert_equal "expected Hash (got String) for param `a'", error.message

    error = assert_raises TypeError do
      Faraday::NestedParamsEncoder.decode("a[]=1&a[b]=2")
    end
    assert_equal "expected Hash (got Array) for param `a'", error.message

    error = assert_raises TypeError do
      Faraday::NestedParamsEncoder.decode("a[b]=1&a[]=2")
    end
    assert_equal "expected Array (got Hash) for param `a'", error.message

    error = assert_raises TypeError do
      Faraday::NestedParamsEncoder.decode("a=1&a[]=2")
    end
    assert_equal "expected Array (got String) for param `a'", error.message

    error = assert_raises TypeError do
      Faraday::NestedParamsEncoder.decode("a[b]=1&a[b][c]=2")
    end
    assert_equal "expected Hash (got String) for param `b'", error.message
  end

  def test_decode_nested_final_value_overrides_any_type
    query = "a[b][c]=1&a[b]=2"
    expected = {"a" => {"b" => "2"}}
    assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
  end

  def test_encode_rack_compat_nested
    params = { :a => [{:one => "1", :two => "2"}, "3", ""] }
    expected = Rack::Utils.build_nested_query(params)
    assert_equal expected.split("&").sort,
      Faraday::Utils.unescape(Faraday::NestedParamsEncoder.encode(params)).split("&").sort
  end
end