Skip to content

Commit 8c84bfc

Browse files
authored
Merge pull request #41 from QuickPay/allow_content_type_json_variation
Allow content type application/json variations
2 parents 89cbf2e + 2f6fd49 commit 8c84bfc

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 3.0.1
4+
5+
* 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)
6+
37
## 3.0.0
48

59
### Breaking changes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ or install from Rubygems:
2020
$ gem install quickpay-ruby-client
2121
```
2222

23-
It is currently tested with Ruby ( >= 2.5.x)
23+
It is currently tested with Ruby ( >= 2.6.x)
2424

2525
* MRI
2626
* Rubinius (2.0)

lib/quickpay/api/client.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Client
1111
"Accept-Version" => "v10"
1212
}.freeze
1313

14+
CONTENT_TYPE_JSON_REGEX = %r{application/.*json}.freeze
15+
1416
Request = Struct.new(:method, :path, :body, :headers, :query) # rubocop:disable Lint/StructNewOverride
1517

1618
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
3234
headers = DEFAULT_HEADERS.merge(options.fetch(:headers, {}))
3335
body = begin
3436
data = options.fetch(:body, "")
35-
if headers["Content-Type"] == "application/json" && data.instance_of?(Hash)
37+
if CONTENT_TYPE_JSON_REGEX.match(headers["Content-Type"]) && data.instance_of?(Hash)
3638
data.to_json
3739
else
3840
data
@@ -50,7 +52,7 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net
5052
res = @connection.request(**req.to_h)
5153
error = QuickPay::API::Error.by_status_code(res.status, res.body, res.headers, req)
5254

53-
if !options.fetch(:raw, false) && res.headers["Content-Type"] =~ %r{application/json}
55+
if !options.fetch(:raw, false) && res.headers["Content-Type"] =~ CONTENT_TYPE_JSON_REGEX
5456
res.body = JSON.parse(res.body, options[:json_opts] || @connection.data[:json_opts])
5557
end
5658

@@ -72,7 +74,7 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net
7274
def scrub_body(body, content_type)
7375
return "" if body.to_s.empty?
7476

75-
if ["application/json", "application/x-www-form-urlencoded"].include?(content_type)
77+
if [CONTENT_TYPE_JSON_REGEX, %r{application/x-www-form-urlencoded}].any? { |regex| regex.match(content_type) }
7678
body
7779
else
7880
"<scrubbed for Content-Type #{content_type}>"

lib/quickpay/api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module QuickPay
22
module API
3-
VERSION = "3.0.0".freeze
3+
VERSION = "3.0.1".freeze
44
end
55
end

test/client.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@
116116
_(response).must_equal({ :foo => "bar" })
117117
end
118118
end
119+
120+
it "returns a ruby Hash if content type is weird application/json" do
121+
Excon.stub(
122+
{ path: "/ping" },
123+
lambda do |request_params|
124+
{
125+
body: request_params[:body],
126+
headers: { "Content-Type" => "application/stuff+json" },
127+
status: 200
128+
}
129+
end
130+
)
131+
132+
# client returns Ruby Hash with string keys
133+
subject.post(
134+
"/ping",
135+
body: { "foo" => "bob" },
136+
headers: { "Content-Type" => "application/stuff+json" }
137+
).tap do |response,|
138+
_(response).must_equal({ "foo" => "bob" })
139+
end
140+
end
119141
end
120142

121143
describe "request with block" do

0 commit comments

Comments
 (0)