-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert query values to strings before comparison
- Loading branch information
1 parent
640925c
commit b1cb946
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module WebMock::Util | ||
class QueryValueStringifier | ||
class << self | ||
def stringify(value) | ||
return value if value.is_a?(String) | ||
return value.to_s unless value.respond_to? :map | ||
|
||
case value | ||
when Array | ||
value.map { |v| stringify(v) } | ||
when Hash | ||
Hash[value.map { |k, v| [k, stringify(v)] }] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe WebMock::Util::QueryValueStringifier do | ||
describe ".stringify" do | ||
it "handles integers" do | ||
expect(described_class.stringify(1)).to eq("1") | ||
end | ||
|
||
it "handles booleans" do | ||
expect(described_class.stringify(true)).to eq("true") | ||
end | ||
|
||
it "handles symbols" do | ||
expect(described_class.stringify(:hello)).to eq("hello") | ||
end | ||
|
||
it "handles hashes" do | ||
input = { :a => :a, :b => :b, :c => 1, :d => [true, false] } | ||
expect(described_class.stringify(input)).to eq( | ||
{ :a => "a", :b => "b", :c => "1", :d => ["true", "false"] } | ||
) | ||
end | ||
|
||
it "handles arrays" do | ||
input = [ :a, 1, [true, false], {"a" => :b} ] | ||
expect(described_class.stringify(input)).to eq( | ||
[ "a", "1", ["true", "false"], {"a" => "b"} ] | ||
) | ||
end | ||
end | ||
end |