-
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.
Fixed problem with matching requests with json body, when json string…
…s had date format.
- Loading branch information
Showing
5 changed files
with
95 additions
and
1 deletion.
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
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,54 @@ | ||
# This is a copy of https://github.com/jnunemaker/crack/blob/master/lib/crack/json.rb | ||
# with date parsing removed | ||
module WebMock | ||
module Util | ||
class JSON | ||
def self.parse(json) | ||
YAML.load(unescape(convert_json_to_yaml(json))) | ||
rescue ArgumentError => e | ||
raise ParseError, "Invalid JSON string" | ||
end | ||
|
||
protected | ||
def self.unescape(str) | ||
str.gsub(/\\u([0-9a-f]{4})/) { [$1.hex].pack("U") } | ||
end | ||
|
||
# Ensure that ":" and "," are always followed by a space | ||
def self.convert_json_to_yaml(json) #:nodoc: | ||
scanner, quoting, marks, pos, times = StringScanner.new(json), false, [], nil, [] | ||
while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/) | ||
case char = scanner[1] | ||
when '"', "'" | ||
if !quoting | ||
quoting = char | ||
pos = scanner.pos | ||
elsif quoting == char | ||
quoting = false | ||
end | ||
when ":","," | ||
marks << scanner.pos - 1 unless quoting | ||
when "\\" | ||
scanner.skip(/\\/) | ||
end | ||
end | ||
|
||
if marks.empty? | ||
json.gsub(/\\\//, '/') | ||
else | ||
left_pos = [-1].push(*marks) | ||
right_pos = marks << json.length | ||
output = [] | ||
left_pos.each_with_index do |left, i| | ||
output << json[left.succ..right_pos[i]] | ||
end | ||
output = output * " " | ||
|
||
times.each { |i| output[i-1] = ' ' } | ||
output.gsub!(/\\\//, '/') | ||
output | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | ||
|
||
describe WebMock::Util::JSON do | ||
it "should parse json without parsing dates" do | ||
WebMock::Util::JSON.parse("\"a\":\"2011-01-01\"").should == {"a" => "2011-01-01"} | ||
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