From 6241141bd84d8fc494c71eb22c6144ad8427d950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20=C3=98stemark?= Date: Tue, 8 Mar 2022 14:19:37 +0100 Subject: [PATCH 1/4] allow content type application/json variations --- CHANGELOG.md | 4 ++++ README.md | 2 +- lib/quickpay/api/client.rb | 2 +- lib/quickpay/api/version.rb | 2 +- quickpay-ruby-client.gemspec | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 952fbaf..b5e2835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.0.1 + +* Fixed bug where variations of Content-Type application/json got scrubbed, fx application/json;charset=UTF-8 () + ## 3.0.0 ### Breaking changes diff --git a/README.md b/README.md index 2d06c22..f1108d5 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ or install from Rubygems: $ gem install quickpay-ruby-client ``` -It is currently tested with Ruby ( >= 2.5.x) +It is currently tested with Ruby ( >= 2.6.x) * MRI * Rubinius (2.0) diff --git a/lib/quickpay/api/client.rb b/lib/quickpay/api/client.rb index 18514a9..4b4c066 100644 --- a/lib/quickpay/api/client.rb +++ b/lib/quickpay/api/client.rb @@ -72,7 +72,7 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net def scrub_body(body, content_type) return "" if body.to_s.empty? - if ["application/json", "application/x-www-form-urlencoded"].include?(content_type) + if [%r{application/.*json.*}, %r{application/x-www-form-urlencoded}].any? { |regex| regex.match(content_type) } body else "" diff --git a/lib/quickpay/api/version.rb b/lib/quickpay/api/version.rb index 81c719e..2903962 100644 --- a/lib/quickpay/api/version.rb +++ b/lib/quickpay/api/version.rb @@ -1,5 +1,5 @@ module QuickPay module API - VERSION = "3.0.0".freeze + VERSION = "3.0.1".freeze end end diff --git a/quickpay-ruby-client.gemspec b/quickpay-ruby-client.gemspec index fafe041..f36d55e 100644 --- a/quickpay-ruby-client.gemspec +++ b/quickpay-ruby-client.gemspec @@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "quickpay/api/version" Gem::Specification.new do |spec| - spec.required_ruby_version = ">= 2.5.0" # rubocop:disable Gemspec/RequiredRubyVersion + spec.required_ruby_version = ">= 2.6.0" # rubocop:disable Gemspec/RequiredRubyVersion spec.name = "quickpay-ruby-client" spec.version = QuickPay::API::VERSION From 765d4df3b50e0f69716848ab50d4f2d8f7f83f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20=C3=98stemark?= Date: Tue, 8 Mar 2022 14:20:18 +0100 Subject: [PATCH 2/4] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5e2835..31775d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 3.0.1 -* Fixed bug where variations of Content-Type application/json got scrubbed, fx application/json;charset=UTF-8 () +* Fixed bug where variations of Content-Type application/json got scrubbed, fx application/json;charset=UTF-8 (https://github.com/QuickPay/quickpay-ruby-client/pull/41) ## 3.0.0 From cb33d4be7bb9f387fbf218fd769c1232a10e1052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20=C3=98stemark?= Date: Wed, 13 Apr 2022 11:20:36 +0200 Subject: [PATCH 3/4] add tests --- lib/quickpay/api/client.rb | 8 +++++--- test/client.rb | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/quickpay/api/client.rb b/lib/quickpay/api/client.rb index 4b4c066..a86bda6 100644 --- a/lib/quickpay/api/client.rb +++ b/lib/quickpay/api/client.rb @@ -11,6 +11,8 @@ class Client "Accept-Version" => "v10" }.freeze + CONTENT_TYPE_JSON_REGEX = %r{application/.*json}.freeze + Request = Struct.new(:method, :path, :body, :headers, :query) # rubocop:disable Lint/StructNewOverride def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net", options: {}) @@ -32,7 +34,7 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net headers = DEFAULT_HEADERS.merge(options.fetch(:headers, {})) body = begin data = options.fetch(:body, "") - if headers["Content-Type"] == "application/json" && data.instance_of?(Hash) + if CONTENT_TYPE_JSON_REGEX.match(headers["Content-Type"]) && data.instance_of?(Hash) data.to_json else data @@ -50,7 +52,7 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net res = @connection.request(**req.to_h) error = QuickPay::API::Error.by_status_code(res.status, res.body, res.headers, req) - if !options.fetch(:raw, false) && res.headers["Content-Type"] =~ %r{application/json} + if !options.fetch(:raw, false) && res.headers["Content-Type"] =~ CONTENT_TYPE_JSON_REGEX res.body = JSON.parse(res.body, options[:json_opts] || @connection.data[:json_opts]) end @@ -72,7 +74,7 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net def scrub_body(body, content_type) return "" if body.to_s.empty? - if [%r{application/.*json.*}, %r{application/x-www-form-urlencoded}].any? { |regex| regex.match(content_type) } + if [CONTENT_TYPE_JSON_REGEX, %r{application/x-www-form-urlencoded}].any? { |regex| regex.match(content_type) } body else "" diff --git a/test/client.rb b/test/client.rb index a6ec3ba..aa1fcb6 100644 --- a/test/client.rb +++ b/test/client.rb @@ -116,6 +116,28 @@ _(response).must_equal({ :foo => "bar" }) end end + + it "returns a ruby Hash if content type is weird application/json" do + Excon.stub( + { path: "/ping" }, + lambda do |request_params| + { + body: request_params[:body], + headers: { "Content-Type" => "application/stuff+json" }, + status: 200 + } + end + ) + + # client returns Ruby Hash with string keys + subject.post( + "/ping", + body: { "foo" => "bob" }, + headers: { "Content-Type" => "application/stuff+json" } + ).tap do |response,| + _(response).must_equal({ "foo" => "bob" }) + end + end end describe "request with block" do From 2f6fd4911c4057afd2562c01d83d0da2bc2902d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20=C3=98stemark?= Date: Wed, 13 Apr 2022 11:32:50 +0200 Subject: [PATCH 4/4] no change to required ruby version --- quickpay-ruby-client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickpay-ruby-client.gemspec b/quickpay-ruby-client.gemspec index f36d55e..fafe041 100644 --- a/quickpay-ruby-client.gemspec +++ b/quickpay-ruby-client.gemspec @@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "quickpay/api/version" Gem::Specification.new do |spec| - spec.required_ruby_version = ">= 2.6.0" # rubocop:disable Gemspec/RequiredRubyVersion + spec.required_ruby_version = ">= 2.5.0" # rubocop:disable Gemspec/RequiredRubyVersion spec.name = "quickpay-ruby-client" spec.version = QuickPay::API::VERSION