Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More tests for #to_query

  • Loading branch information...
commit 97046166686503df99fba18d02e06c67336b3f16 1 parent 963abd8
@simonoff simonoff authored
View
2  lib/webmock/util/query_mapper.rb
@@ -46,7 +46,7 @@ def query_to_values(query, options={})
unless %w(flat dot subscript flat_array).include?(options[:notation].to_s)
raise ArgumentError,
'Invalid notation. Must be one of: ' +
- '[:flat, :dot, :subscript, :flat_array].'
+ '[:flat, :dot, :subscript, :flat_array].'
end
empty_accumulator = :flat_array == options[:notation] ? [] : {}
View
108 spec/unit/util/query_mapper_spec.rb
@@ -1,41 +1,91 @@
require 'spec_helper'
describe WebMock::Util::QueryMapper do
- it "should parse hash queries" do
- # {"one" => {"two" => {"three" => ["four", "five"]}}}
- query = "one%5Btwo%5D%5Bthree%5D%5B%5D=four&one%5Btwo%5D%5Bthree%5D%5B%5D=five"
- hsh = WebMock::Util::QueryMapper.query_to_values(query)
- hsh["one"]["two"]["three"].should == ["four", "five"]
- end
+ subject { described_class }
- it "should parse one nil value queries" do
- # {'a' => nil, 'b' => 'c'}
- query = "a=&b=c"
- hsh = WebMock::Util::QueryMapper.query_to_values(query)
- hsh['a'].should be_empty
- hsh['b'].should == 'c'
- end
+ context '#query_to_values' do
+ it 'should raise on invalid notation' do
+ query = 'a=&b=c'
+ expect { subject.query_to_values(query, {notation: 'foo'}) }.to raise_error(
+ ArgumentError,
+ 'Invalid notation. Must be one of: [:flat, :dot, :subscript, :flat_array].'
+ )
+ end
+
+ it 'should parse hash queries' do
+ # {"one" => {"two" => {"three" => ["four", "five"]}}}
+ query = 'one%5Btwo%5D%5Bthree%5D%5B%5D=four&one%5Btwo%5D%5Bthree%5D%5B%5D=five'
+ hsh = subject.query_to_values(query)
+ expect(hsh['one']['two']['three']).to eq(%w(four five))
+ end
+
+ it 'should parse one nil value queries' do
+ # {'a' => nil, 'b' => 'c'}
+ query = 'a=&b=c'
+ hsh = subject.query_to_values(query)
+ expect(hsh['a']).to be_empty
+ expect(hsh['b']).to eq('c')
+ end
- it "should parse array queries" do
- # {"one" => ["foo", "bar"]}
- query = "one%5B%5D=foo&one%5B%5D=bar"
- hsh = WebMock::Util::QueryMapper.query_to_values(query)
- hsh["one"].should == ["foo", "bar"]
+ it 'should parse array queries' do
+ # {"one" => ["foo", "bar"]}
+ query = 'one%5B%5D=foo&one%5B%5D=bar'
+ hsh = subject.query_to_values(query)
+ expect(hsh['one']).to eq(%w(foo bar))
+ end
+
+ it 'should parse string queries' do
+ # {"one" => "two", "three" => "four"}
+ query = 'one=two&three=four'
+ hsh = subject.query_to_values(query)
+ expect(hsh).to eq({'one' => 'two', 'three' => 'four'})
+ end
+
+ it 'should parse nested queries' do
+ # [{"b"=>[{"c"=>[{"d"=>["1", {"e"=>"2"}]}]}]}]
+ query = 'a%5B%5D%5Bb%5D%5B%5D%5Bc%5D%5B%5D%5Bd%5D%5B%5D=1&a%5B%5D%5Bb%5D%5B%5D%5Bc%5D%5B%5D%5Bd%5D%5B%5D%5Be%5D=2'
+ hsh = subject.query_to_values(query)
+ expect(hsh['a'][0]['b'][0]['c'][0]['d'][0]).to eq('1')
+ expect(hsh['a'][0]['b'][0]['c'][0]['d'][1]['e']).to eq('2')
+ end
end
- it "should parse string queries" do
- # {"one" => "two", "three" => "four"}
- query = "one=two&three=four"
- hsh = WebMock::Util::QueryMapper.query_to_values(query)
- hsh.should == {"one" => "two", "three" => "four"}
+ context '#to_query' do
+ it 'should transform nil value' do
+ expect(subject.to_query('a', nil)).to eq('a=')
+ end
+ it 'should transform string value' do
+ expect(subject.to_query('a', 'b')).to eq('a=b')
+ end
+ it 'should transform hash value' do
+ expect(subject.to_query('a', {'key' => 'value'})).to eq('a[key]=value')
+ end
+ it 'should transform array value' do
+ expect(subject.to_query('a', ['b', 'c'])).to eq('a[0]=b&a[1]=c')
+ end
+ it 'should transform TrueClass value' do
+ expect(subject.to_query('a', true)).to eq('a')
+ end
end
- it "should parse nested queries" do
- # [{"b"=>[{"c"=>[{"d"=>["1", {"e"=>"2"}]}]}]}]
- query = "a%5B%5D%5Bb%5D%5B%5D%5Bc%5D%5B%5D%5Bd%5D%5B%5D=1&a%5B%5D%5Bb%5D%5B%5D%5Bc%5D%5B%5D%5Bd%5D%5B%5D%5Be%5D=2"
- hsh = WebMock::Util::QueryMapper.query_to_values(query)
- hsh["a"][0]["b"][0]["c"][0]["d"][0].should == "1"
- hsh["a"][0]["b"][0]["c"][0]["d"][1]["e"].should == "2"
+ context '#values_to_query' do
+ it 'converts values to a query string' do
+ query = "key=value&other_key=other_value"
+ values = [['key','value'],['other_key','other_value']]
+ expect(subject.values_to_query values).to eq query
+ end
+
+ it 'converts values with missing keys to a query string' do
+ query = "=value"
+ values = { '' => 'value' }
+ expect(subject.values_to_query values).to eq query
+ end
+
+ it 'converts values with nil keys to a query string' do
+ query = "=value"
+ values = { nil => 'value' }
+ expect(subject.values_to_query values).to eq query
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.