aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/uri/decode_www_form_18_spec.rb
blob: 21e1b7cc0f0b9741871c10b6d86afec60ca7fb1f (plain) (blame)
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
# encoding: UTF-8

require 'spec_helper'

describe URI, '#decode_www_form_18' do

  it 'seperates parameter into an array' do
    result = URI::decode_www_form_18("foo=bar&bar=foo")

    result.should eq([['foo', 'bar'], ['bar', 'foo']])
  end

  it 'can handle more than two equal parameter names' do
    result = URI::decode_www_form_18("foo=bar1&foo=bar2")

    result.should eq([['foo', 'bar1'], ['foo', 'bar2']])
  end

  it 'can handle whitespaces in query string' do
    result = URI::decode_www_form_18("foo=bar&bar=bar%20foo")

    result.should eq([['foo', 'bar'], ['bar', 'bar foo']])
  end

  it 'accepts semi-colons as seperators' do
    result = URI::decode_www_form_18("foo=bar;bar=foo")

    result.should eq([['foo', 'bar'], ['bar', 'foo']])
  end

  it 'seperates & and ; mixed queries properly' do
    result = URI::decode_www_form_18("foo=bar&bar=foo;baz=foo")

    result.should eq([['foo', 'bar'], ['bar', 'foo'], ['baz', 'foo']])
  end

  it 'does not accept only a normal string as query string' do
    expect{
      result = URI::decode_www_form_18("foo")

      # does not work, probably should?
      #result.should eq([['foo', '']])
    }.to raise_error(
      ArgumentError,
      "invalid data of application/x-www-form-urlencoded (foo)"
    )
  end

  it 'accepts empty values' do
      result = URI::decode_www_form_18("foo=bar&bar=&baz=foo")

      result.should eq([['foo', 'bar'], ['bar', ''], ['baz', 'foo']])
  end

  it 'understands plus as whitespace' do
    result = URI::decode_www_form_18("foo=bar&bar=bar+foo")

    result.should eq([['foo', 'bar'], ['bar', 'bar foo']])
  end

  it 'does not accept whitespaces in query string' do
    result = URI::decode_www_form_18("foo=bar&bar=bar foo&baz=foo")

    # Works, it probably shouldn't?
    result.should eq([['foo', 'bar'], ['bar', 'bar foo'], ['baz', 'foo']])
  end

  it 'can handle non ascii letters in query string' do
    result = URI::decode_www_form_18("foo=bär&bar=föö")

    # Works, but it maybe shouldn't?
    result.should eq([['foo', 'bär'], ['bar', 'föö']])
  end

  it 'can handle escaped non ascii letters in query string' do
    result = URI::decode_www_form_18("foo=b%C3%A4r&bar=f%C3%B6%C3%B6")

    result.should eq([['foo', 'bär'], ['bar', 'föö']])
  end

  it 'accepts - in query string' do
    result = URI::decode_www_form_18("foo-bar=bar&bar=foo-bar")

    puts (URI::decode_www_form_18("foo-bar=bar&bar=foo-bar") == [['foo-bar', 'bar'], ['bar', 'foo-bar']]).inspect

    result.should eq([['foo-bar', 'bar'], ['bar', 'foo-bar']])
  end

  it 'accepts . in query string' do
    result = URI::decode_www_form_18("foo.bar=bar&bar=foo.bar")

    result.should eq([['foo.bar', 'bar'], ['bar', 'foo.bar']])
  end

  it 'accepts ~ in query string' do
    result = URI::decode_www_form_18("foo~bar=bar&bar=foo~bar")

    result.should eq([['foo~bar', 'bar'], ['bar', 'foo~bar']])
  end

  it 'accepts _ in query string' do
    result = URI::decode_www_form_18("foo_bar=bar&bar=foo_bar")

    result.should eq([['foo_bar', 'bar'], ['bar', 'foo_bar']])
  end

  it 'handles [ ] in query string' do
    result = URI::decode_www_form_18("foo[]=foo&foo[]=bar")

    result.should eq([['foo[]', 'foo'], ['foo[]', 'bar']])
  end

  it 'returns an empty array, if query string is empty' do
    result = URI::decode_www_form_18("")

    result.should eq([])
  end

  it 'throws an error, if more than one = without an & or ; in between' do
    expect {
      result = URI::decode_www_form_18("foo=bar=foo&bar=foo=bar")
    }.to raise_error(
      ArgumentError,
      "invalid data of application/x-www-form-urlencoded "+
       "(foo=bar=foo&bar=foo=bar)"
    )
  end

  it 'throws an error, if more than one & without an = in between' do
    expect {
      result = URI::decode_www_form_18("foo&bar=foo&bar")
    }.to raise_error(
      ArgumentError,
      "invalid data of application/x-www-form-urlencoded (foo&bar=foo&bar)"
    )
  end

  it 'throws an error, if more than one ; without an = in between' do
    expect {
      result = URI::decode_www_form_18("foo;bar=foo;bar")
    }.to raise_error(
      ArgumentError,
      "invalid data of application/x-www-form-urlencoded (foo;bar=foo;bar)"
    )
  end

end