diff options
| author | Stormwind <stormwind@stormwinds-page.de> | 2012-12-22 11:44:26 +0100 | 
|---|---|---|
| committer | Stormwind <stormwind@stormwinds-page.de> | 2012-12-22 11:44:26 +0100 | 
| commit | 443958f8859a47751f7e57deff6b3eda0c13e5ad (patch) | |
| tree | bd23c285edd841952cc80461e54e37dd242ed75a | |
| parent | b20c0c527c89ec08548849163388d43207609fcd (diff) | |
Add patch for URI decode_www_form
Plus some specs.
This should it make later possible to make Zero work with Ruby 1.8. But
at the moment it only routes to the original decode_www_form method, if
it exists. Otherwise it returns an empty Array. So this will still fail
on the 1.8 versions at the moment.
| -rw-r--r-- | lib/zero.rb | 1 | ||||
| -rw-r--r-- | lib/zero/patches/uri.rb | 45 | ||||
| -rw-r--r-- | spec/unit/uri/parse_query_string_spec.rb | 111 | 
3 files changed, 157 insertions, 0 deletions
| diff --git a/lib/zero.rb b/lib/zero.rb index 03c77b8..1b3d32c 100644 --- a/lib/zero.rb +++ b/lib/zero.rb @@ -1,6 +1,7 @@  if RUBY_VERSION <= '1.9'    require 'backports'  end +require 'zero/patches/uri'  module Zero    require 'zero/controller' diff --git a/lib/zero/patches/uri.rb b/lib/zero/patches/uri.rb new file mode 100644 index 0000000..fb821b2 --- /dev/null +++ b/lib/zero/patches/uri.rb @@ -0,0 +1,45 @@ +module URI + +    # Gets a query string and splits it into its key value pairs. +    # If URI already supports this functionality (decode_www_form on Ruby 1.9+), +    # it will use this. Else ... nothing at the moment. +    # +    # @param [String] query The query string +    # @return [Array] Parsed query +    # +    def self.parse_query_string(query) +      # Call the original decode_www_form method on ruby 1.9+ +      if URI::respond_to? 'decode_www_form' +        return self.decode_www_form query +      end + +      # else split the query self +      return [] +    end + +=begin +    WFKV_ = '(?:[^%#=;&]*(?:%\h\h[^%#=;&]*)*)' + +    def self.decode_www_form(str, enc= nil) +      return [] if str.empty? +      unless /\A#{WFKV_}=#{WFKV_}(?:[;&]#{WFKV_}=#{WFKV_})*\z/ =~ str +        raise ArgumentError, +          "invalid data of application/x-www-form-urlencoded (#{str})" +      end +      ary = [] +      $&.scan(/([^=;&]+)=([^;&]*)/) do +        ary << [ +          decode_www_form_component($1, enc), +          decode_www_form_component($2, enc) +        ] +      end +      ary +    end + +    def self.decode_www_form_component(str, enc= nil) +      raise ArgumentError, "invalid %-encoding (#{str})" unless /\A[^%]*(?:%\h\h[^%]*)*\z/ =~ str +      str.gsub(/\+|%\h\h/, TBLDECWWWCOMP_).force_encoding(enc) +    end +=end + +end diff --git a/spec/unit/uri/parse_query_string_spec.rb b/spec/unit/uri/parse_query_string_spec.rb new file mode 100644 index 0000000..a2c078b --- /dev/null +++ b/spec/unit/uri/parse_query_string_spec.rb @@ -0,0 +1,111 @@ +# encoding: UTF-8 + +require 'spec_helper' + +describe URI, '#parse_query_string' do + +  it 'seperates parameter into an array' do +    result = URI::parse_query_string("foo=bar&bar=foo") + +    result.should eq([['foo', 'bar'], ['bar', 'foo']]) +  end + +  it 'can handle more than two equal parameter names' do +    result = URI::parse_query_string("foo=bar1&foo=bar2") + +    result.should eq([['foo', 'bar1'], ['foo', 'bar2']]) +  end + +  it 'can handle whitespaces in query string' do +    result = URI::parse_query_string("foo=bar&bar=bar%20foo") + +    result.should eq([['foo', 'bar'], ['bar', 'bar foo']]) +  end + +  it 'accepts semi-colons as seperators' do +    result = URI::parse_query_string("foo=bar;bar=foo") + +    result.should eq([['foo', 'bar'], ['bar', 'foo']]) +  end + +  it 'seperates & and ; mixed queries properly' do +    result = URI::parse_query_string("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::parse_query_string("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::parse_query_string("foo=bar&bar=&baz=foo") + +      result.should eq([['foo', 'bar'], ['bar', ''], ['baz', 'foo']]) +  end + +  it 'understands plus as whitespace' do +    result = URI::parse_query_string("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::parse_query_string("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::parse_query_string("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::parse_query_string("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::parse_query_string("foo-bar=bar&bar=foo-bar") + +    result.should eq([['foo-bar', 'bar'], ['bar', 'foo-bar']]) +  end + +  it 'accepts . in query string' do +    result = URI::parse_query_string("foo.bar=bar&bar=foo.bar") + +    result.should eq([['foo.bar', 'bar'], ['bar', 'foo.bar']]) +  end + +  it 'accepts ~ in query string' do +    result = URI::parse_query_string("foo~bar=bar&bar=foo~bar") + +    result.should eq([['foo~bar', 'bar'], ['bar', 'foo~bar']]) +  end + +  it 'accepts _ in query string' do +    result = URI::parse_query_string("foo_bar=bar&bar=foo_bar") + +    result.should eq([['foo_bar', 'bar'], ['bar', 'foo_bar']]) +  end + +  it 'handles [ ] in query string' do +    result = URI::parse_query_string("foo[]=foo&foo[]=bar") + +    result.should eq([['foo[]', 'foo'], ['foo[]', 'bar']]) +  end + +end | 
