Permalink
Browse files

Don't use JSON to normalize hash body in request pattern. Removed dep…

…endency on json gem.
  • Loading branch information...
1 parent f45ac01 commit 7bca10a31e3be4ce303b95a974ea48540f1fc4be @bblimke committed Nov 8, 2010
View
2 CHANGELOG.md
@@ -18,6 +18,8 @@
* Fixed problem with assert_requested, which doesn't work if used outside rspec or test/unit
+* Removed dependency on json gem
+
## 1.5.0
* Support for dynamically evaluated raw responses recorded with `curl -is` <br/>
View
1 lib/webmock.rb
@@ -16,6 +16,7 @@
require 'webmock/util/uri'
require 'webmock/util/headers'
require 'webmock/util/hash_counter'
+require 'webmock/util/hash_keys_stringifier'
require 'webmock/request_pattern'
require 'webmock/request_signature'
View
2 lib/webmock/request_pattern.rb
@@ -169,7 +169,7 @@ def empty_string?(string)
end
def normalize_hash(hash)
- JSON.parse(JSON.generate(hash))
+ WebMock::Util::HashKeysStringifier.stringify_keys!(hash)
end
end
View
23 lib/webmock/util/hash_keys_stringifier.rb
@@ -0,0 +1,23 @@
+module WebMock
+ module Util
+ class HashKeysStringifier
+
+ def self.stringify_keys!(arg)
+ case arg
+ when Array
+ arg.map { |elem| stringify_keys!(elem) }
+ when Hash
+ Hash[
+ *arg.map { |key, value|
+ k = key.is_a?(Symbol) ? key.to_s : key
+ v = stringify_keys!(value)
+ [k,v]
+ }.inject([]) {|r,x| r + x}]
+ else
+ arg
+ end
+ end
+
+ end
+ end
+end
View
27 spec/util/hash_keys_stringifier_spec.rb
@@ -0,0 +1,27 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe WebMock::Util::HashKeysStringifier do
+
+ it "should recursively stringify all symbol keys" do
+ hash = {
+ :a => {
+ :b => [
+ {
+ :c => [{:d => "1"}]
+ }
+ ]
+ }
+ }
+ stringified = {
+ 'a' => {
+ 'b' => [
+ {
+ 'c' => [{'d' => "1"}]
+ }
+ ]
+ }
+ }
+ WebMock::Util::HashKeysStringifier.stringify_keys!(hash).should == stringified
+ end
+
+end

0 comments on commit 7bca10a

Please sign in to comment.