diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index ea84efa84..c388785c7 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -21,7 +21,7 @@ jobs: - '2.7' - '2.6' - jruby-head - continue-on-error: ${{ matrix.ruby == 'head' }} + continue-on-error: ${{ matrix.ruby == 'head' || matrix.ruby == 'jruby-head' }} name: Ruby ${{ matrix.ruby }} env: JRUBY_OPTS: "--debug" @@ -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 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/http_lib_adapters/http_rb/streamer.rb b/lib/webmock/http_lib_adapters/http_rb/streamer.rb index 3a70c443e..ab6186c1a 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/lib/webmock/http_lib_adapters/patron_adapter.rb b/lib/webmock/http_lib_adapters/patron_adapter.rb index e5e227fb8..2c97f91a9 100644 --- a/lib/webmock/http_lib_adapters/patron_adapter.rb +++ b/lib/webmock/http_lib_adapters/patron_adapter.rb @@ -6,7 +6,7 @@ # patron not found end -if defined?(::Patron) +if defined?(::Patron::Session) module WebMock module HttpLibAdapters class PatronAdapter < ::WebMock::HttpLibAdapter diff --git a/lib/webmock/request_pattern.rb b/lib/webmock/request_pattern.rb index 44c3f28b9..4b18aac89 100644 --- a/lib/webmock/request_pattern.rb +++ b/lib/webmock/request_pattern.rb @@ -84,11 +84,14 @@ def create_uri_pattern(uri) URIAddressablePattern.new(uri) elsif uri.respond_to?(:call) URICallablePattern.new(uri) - else + elsif uri.is_a?(::URI::Generic) + URIStringPattern.new(uri.to_s) + elsif uri.is_a?(String) URIStringPattern.new(uri) + else + raise ArgumentError.new("URI should be a String, Regexp, Addressable::Template or a callable object. Got: #{uri.class}") end end - end @@ -303,12 +306,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 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/acceptance/http_rb/http_rb_spec.rb b/spec/acceptance/http_rb/http_rb_spec.rb index dd600a5d7..e96bbf51f 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 diff --git a/spec/unit/request_pattern_spec.rb b/spec/unit/request_pattern_spec.rb index f6fb2e514..fe6ef2e03 100644 --- a/spec/unit/request_pattern_spec.rb +++ b/spec/unit/request_pattern_spec.rb @@ -436,6 +436,14 @@ def match(request_signature) end end end + + describe "when uri is passed as Pathname" do + it "should raise an ArgumentError" do + expect { + WebMock::RequestPattern.new(:get, Pathname.new("www.example.com")) + }.to raise_error(ArgumentError, "URI should be a String, Regexp, Addressable::Template or a callable object. Got: Pathname") + end + end end describe "when matching requests with body" do @@ -562,7 +570,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 +622,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: " "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