From 2f8b9e042c053f0dc90ee032b2fb9d72c56b02e0 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Fri, 2 Aug 2024 00:27:00 +0200 Subject: [PATCH 1/6] Mock HTTP::Connection#finished_request? --- lib/webmock/http_lib_adapters/http_rb/streamer.rb | 4 ++++ spec/acceptance/http_rb/http_rb_spec.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/webmock/http_lib_adapters/http_rb/streamer.rb b/lib/webmock/http_lib_adapters/http_rb/streamer.rb index 3a70c443..ab6186c1 100644 --- a/lib/webmock/http_lib_adapters/http_rb/streamer.rb +++ b/lib/webmock/http_lib_adapters/http_rb/streamer.rb @@ -25,6 +25,10 @@ def close @io.close end + def finished_request? + @io.eof? + end + def sequence_id -1 end diff --git a/spec/acceptance/http_rb/http_rb_spec.rb b/spec/acceptance/http_rb/http_rb_spec.rb index dd600a5d..e96bbf51 100644 --- a/spec/acceptance/http_rb/http_rb_spec.rb +++ b/spec/acceptance/http_rb/http_rb_spec.rb @@ -89,6 +89,20 @@ response.connection.close end + + it "reports request finish" do + stub_request(:get, "example.com/foo") + .to_return(body: 'XX') + response = HTTP.get "http://example.com/foo" + + expect(response.connection.finished_request?).to be(false) + + response.body.readpartial(1) + expect(response.connection.finished_request?).to be(false) + + response.body.readpartial + expect(response.connection.finished_request?).to be(true) + end end it "should preserve request body encoding when matching requests" do From 69c45b857ca4168522942a59cb5d064fec230155 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Fri, 2 Aug 2024 17:22:58 +0200 Subject: [PATCH 2/6] Rescue exceptions --- lib/webmock/request_pattern.rb | 1 + spec/unit/request_pattern_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/webmock/request_pattern.rb b/lib/webmock/request_pattern.rb index 44c3f28b..124087ff 100644 --- a/lib/webmock/request_pattern.rb +++ b/lib/webmock/request_pattern.rb @@ -309,6 +309,7 @@ def body_as_hash(body, content_type) else WebMock::Util::QueryMapper.query_to_values(body, notation: Config.instance.query_values_notation) end + rescue Psych::SyntaxError, REXML::ParseException end def body_format(content_type) diff --git a/spec/unit/request_pattern_spec.rb b/spec/unit/request_pattern_spec.rb index f6fb2e51..8cfc89ac 100644 --- a/spec/unit/request_pattern_spec.rb +++ b/spec/unit/request_pattern_spec.rb @@ -562,7 +562,7 @@ def match(request_signature) it "should not match when body is not json" do expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)). not_to match(WebMock::RequestSignature.new(:post, "www.example.com", - headers: {content_type: content_type}, body: "foo bar")) + headers: {content_type: content_type}, body: "[foo bar")) end it "should not match if request body is different" do @@ -614,7 +614,7 @@ def match(request_signature) it "should not match when body is not xml" do expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)). not_to match(WebMock::RequestSignature.new(:post, "www.example.com", - headers: {content_type: content_type}, body: "foo bar")) + headers: {content_type: content_type}, body: " Date: Wed, 18 Sep 2024 21:53:14 +0200 Subject: [PATCH 3/6] Refactoring to avoid RequestPattern being coupled with JSON or XML parser specific parsing exceptions. --- lib/webmock.rb | 4 +- lib/webmock/request_pattern.rb | 7 ++- lib/webmock/util/json.rb | 69 ---------------------- lib/webmock/util/parsers/json.rb | 72 +++++++++++++++++++++++ lib/webmock/util/parsers/parse_error.rb | 7 +++ lib/webmock/util/parsers/xml.rb | 16 +++++ spec/unit/util/{ => parsers}/json_spec.rb | 19 ++++-- 7 files changed, 114 insertions(+), 80 deletions(-) delete mode 100644 lib/webmock/util/json.rb create mode 100644 lib/webmock/util/parsers/json.rb create mode 100644 lib/webmock/util/parsers/parse_error.rb create mode 100644 lib/webmock/util/parsers/xml.rb rename spec/unit/util/{ => parsers}/json_spec.rb (57%) diff --git a/lib/webmock.rb b/lib/webmock.rb index d9ffdc05..894e818d 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 124087ff..4d0212ad 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 0ad3d0ac..00000000 --- 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 00000000..98014ad4 --- /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 00000000..378314d2 --- /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 00000000..4275541c --- /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 df64e1c6..bff2c40b 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 From b74af6ac863f9b83deea7116fd0f7f70a01dd5ed Mon Sep 17 00:00:00 2001 From: Bartosz Blimke Date: Wed, 18 Sep 2024 22:09:45 +0200 Subject: [PATCH 4/6] sudo apt update on CI before apt install --- .github/workflows/CI.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index ea84efa8..ffa81b55 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -29,6 +29,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Apt Packages run: | + sudo apt update sudo apt-get install libcurl4-openssl-dev -y - uses: ruby/setup-ruby@v1 continue-on-error: false From 7fa0f4fa53ad53f9693436e0e6157704780e1127 Mon Sep 17 00:00:00 2001 From: Bartosz Blimke Date: Wed, 18 Sep 2024 22:19:10 +0200 Subject: [PATCH 5/6] Maybe jruby not jruby-head on CI is more stable? --- .github/workflows/CI.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index ffa81b55..0dfa52d5 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -20,7 +20,7 @@ jobs: - '3.0' - '2.7' - '2.6' - - jruby-head + - jruby continue-on-error: ${{ matrix.ruby == 'head' }} name: Ruby ${{ matrix.ruby }} env: From 74efd092de01b767553fd2b9d51916fb438bff51 Mon Sep 17 00:00:00 2001 From: Bartosz Blimke Date: Wed, 18 Sep 2024 22:29:22 +0200 Subject: [PATCH 6/6] Ignore jruby errors on CI for now. --- .github/workflows/CI.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 0dfa52d5..c388785c 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -20,8 +20,8 @@ jobs: - '3.0' - '2.7' - '2.6' - - jruby - continue-on-error: ${{ matrix.ruby == 'head' }} + - jruby-head + continue-on-error: ${{ matrix.ruby == 'head' || matrix.ruby == 'jruby-head' }} name: Ruby ${{ matrix.ruby }} env: JRUBY_OPTS: "--debug"