Skip to content

Commit

Permalink
Merge pull request #41 from QuickPay/allow_content_type_json_variation
Browse files Browse the repository at this point in the history
Allow content type application/json variations
  • Loading branch information
cramt authored Apr 13, 2022
2 parents 89cbf2e + 2f6fd49 commit 8c84bfc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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 (https://github.com/QuickPay/quickpay-ruby-client/pull/41)

## 3.0.0

### Breaking changes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions lib/quickpay/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {})
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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 ["application/json", "application/x-www-form-urlencoded"].include?(content_type)
if [CONTENT_TYPE_JSON_REGEX, %r{application/x-www-form-urlencoded}].any? { |regex| regex.match(content_type) }
body
else
"<scrubbed for Content-Type #{content_type}>"
Expand Down
2 changes: 1 addition & 1 deletion lib/quickpay/api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module QuickPay
module API
VERSION = "3.0.0".freeze
VERSION = "3.0.1".freeze
end
end
22 changes: 22 additions & 0 deletions test/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c84bfc

Please sign in to comment.