diff --git a/lib/webmock.rb b/lib/webmock.rb index d9ffdc050..894e818d3 100644 --- a/lib/webmock.rb +++ b/lib/webmock.rb @@ -4,7 +4,6 @@ require 'addressable/uri' require 'addressable/template' -require 'crack/xml' require_relative 'webmock/deprecation' require_relative 'webmock/version' @@ -17,7 +16,8 @@ require_relative 'webmock/util/hash_counter' require_relative 'webmock/util/hash_keys_stringifier' require_relative 'webmock/util/values_stringifier' -require_relative 'webmock/util/json' +require_relative 'webmock/util/parsers/json' +require_relative 'webmock/util/parsers/xml' require_relative 'webmock/util/version_checker' require_relative 'webmock/util/hash_validator' diff --git a/lib/webmock/request_pattern.rb b/lib/webmock/request_pattern.rb index 124087ff6..4d0212ada 100644 --- a/lib/webmock/request_pattern.rb +++ b/lib/webmock/request_pattern.rb @@ -303,13 +303,14 @@ def to_s def body_as_hash(body, content_type) case body_format(content_type) when :json then - WebMock::Util::JSON.parse(body) + WebMock::Util::Parsers::JSON.parse(body) when :xml then - Crack::XML.parse(body) + WebMock::Util::Parsers::XML.parse(body) else WebMock::Util::QueryMapper.query_to_values(body, notation: Config.instance.query_values_notation) end - rescue Psych::SyntaxError, REXML::ParseException + rescue WebMock::Util::Parsers::ParseError + nil end def body_format(content_type) diff --git a/lib/webmock/util/json.rb b/lib/webmock/util/json.rb deleted file mode 100644 index 0ad3d0acb..000000000 --- a/lib/webmock/util/json.rb +++ /dev/null @@ -1,69 +0,0 @@ -# frozen_string_literal: true - -# This is a copy of https://github.com/jnunemaker/crack/blob/master/lib/crack/json.rb -# with date parsing removed -# Copyright (c) 2004-2008 David Heinemeier Hansson -# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -module WebMock - module Util - class JSON - class ParseError < StandardError; end - - def self.parse(json) - yaml = unescape(convert_json_to_yaml(json)) - YAML.load(yaml) - rescue ArgumentError => e - raise ParseError, "Invalid JSON string: #{yaml}, Error: #{e.inspect}" - 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, times = StringScanner.new(json), false, [], [] - while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/) - case char = scanner[1] - when '"', "'" - if !quoting - quoting = char - 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.bytesize - output = [] - - left_pos.each_with_index do |left, i| - if json.respond_to?(:byteslice) - output << json.byteslice(left.succ..right_pos[i]) - else - output << json[left.succ..right_pos[i]] - end - end - - output = output * " " - - times.each { |i| output[i-1] = ' ' } - output.gsub!(/\\\//, '/') - output - end - end - end - end -end diff --git a/lib/webmock/util/parsers/json.rb b/lib/webmock/util/parsers/json.rb new file mode 100644 index 000000000..98014ad48 --- /dev/null +++ b/lib/webmock/util/parsers/json.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +# This is a copy of https://github.com/jnunemaker/crack/blob/master/lib/crack/json.rb +# with date parsing removed +# Copyright (c) 2004-2008 David Heinemeier Hansson +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +require_relative "parse_error" + +module WebMock + module Util + module Parsers + class JSON + def self.parse(json) + yaml = unescape(convert_json_to_yaml(json)) + YAML.load(yaml) + rescue ArgumentError, Psych::SyntaxError => e + raise ParseError, "Invalid JSON string: #{yaml}, Error: #{e.inspect}" + 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, times = StringScanner.new(json), false, [], [] + while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/) + case char = scanner[1] + when '"', "'" + if !quoting + quoting = char + 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.bytesize + output = [] + + left_pos.each_with_index do |left, i| + if json.respond_to?(:byteslice) + output << json.byteslice(left.succ..right_pos[i]) + else + output << json[left.succ..right_pos[i]] + end + end + + output = output * " " + + times.each { |i| output[i-1] = ' ' } + output.gsub!(/\\\//, '/') + output + end + end + end + end + end +end \ No newline at end of file diff --git a/lib/webmock/util/parsers/parse_error.rb b/lib/webmock/util/parsers/parse_error.rb new file mode 100644 index 000000000..378314d27 --- /dev/null +++ b/lib/webmock/util/parsers/parse_error.rb @@ -0,0 +1,7 @@ +module WebMock + module Util + module Parsers + class ParseError < StandardError; end + end + end +end \ No newline at end of file diff --git a/lib/webmock/util/parsers/xml.rb b/lib/webmock/util/parsers/xml.rb new file mode 100644 index 000000000..4275541c4 --- /dev/null +++ b/lib/webmock/util/parsers/xml.rb @@ -0,0 +1,16 @@ +require_relative "parse_error" +require "crack/xml" + +module WebMock + module Util + module Parsers + class XML + def self.parse(xml) + ::Crack::XML.parse(xml) + rescue ::REXML::ParseException => e + raise ParseError, "Invalid XML string: #{xml}, Error: #{e.inspect}" + end + end + end + end +end diff --git a/spec/unit/util/json_spec.rb b/spec/unit/util/parsers/json_spec.rb similarity index 57% rename from spec/unit/util/json_spec.rb rename to spec/unit/util/parsers/json_spec.rb index df64e1c69..bff2c40bc 100644 --- a/spec/unit/util/json_spec.rb +++ b/spec/unit/util/parsers/json_spec.rb @@ -1,16 +1,16 @@ # encoding: utf-8 require 'spec_helper' -describe WebMock::Util::JSON do +describe WebMock::Util::Parsers::JSON do describe ".parse" do it "should parse json without parsing dates" do - expect(WebMock::Util::JSON.parse("\"a\":\"2011-01-01\"")).to eq( + expect(described_class.parse("\"a\":\"2011-01-01\"")).to eq( {"a" => "2011-01-01"} ) end it "can parse json with multibyte characters" do - expect(WebMock::Util::JSON.parse( + expect(described_class.parse( "{\"name\":\"山田太郎\"\,\"job\":\"会社員\"}" )).to eq({"name" => "山田太郎", "job" => "会社員"}) end @@ -18,14 +18,21 @@ it "rescues ArgumentError's from YAML.load" do allow(YAML).to receive(:load).and_raise(ArgumentError) expect { - WebMock::Util::JSON.parse("Bad JSON") - }.to raise_error WebMock::Util::JSON::ParseError + described_class.parse("Bad JSON") + }.to raise_error WebMock::Util::Parsers::ParseError + end + + it "rescues Psych::SyntaxError's from YAML.load" do + allow(YAML).to receive(:load).and_raise(Psych::SyntaxError) + expect { + described_class.parse("Bad JSON") + }.to raise_error WebMock::Util::Parsers::ParseError end end describe ".convert_json_to_yaml" do it "parses multibyte characters" do - expect(WebMock::Util::JSON.convert_json_to_yaml( + expect(described_class.convert_json_to_yaml( "{\"name\":\"山田太郎\"\,\"job\":\"会社員\"}" )).to eq "{\"name\": \"山田太郎\", \"job\": \"会社員\"}" end