Skip to content

Commit

Permalink
Hdl 285 paypal sdk header: Fixing Case Sensitivity (#11)
Browse files Browse the repository at this point in the history
* Config Encoder and Client to enforce lowercase Content Types and Unit tests

* Updated version, changelog, and license

Co-authored-by: Hakim <[email protected]>
  • Loading branch information
HDLahlou and hlahlou-pp-dev authored Sep 14, 2021
1 parent 05a213d commit b444edf
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.0.1
* Fix Case Sensitivity of Content Type for deserialization process

## 1.0.0
* Fix Case Sensitivity of Acessing Headers by formatting beforehand

## 0.5.0
* Add support for multipart/form-data file uploads with JSON content FormParts.

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2009-2019 PayPal, Inc.
Copyright (c) 2009-2021 PayPal, Inc.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
1 change: 1 addition & 0 deletions lib/paypalhttp/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def deserialize_response(resp, headers)
raise UnsupportedEncodingError.new('HttpResponse did not have Content-Type header set') unless headers && (headers['content-type'])

content_type = _extract_header(headers, 'content-type')
content_type.downcase!

enc = _encoder(content_type)
raise UnsupportedEncodingError.new("Unable to deserialize response with Content-Type #{content_type}. Supported decodings are #{supported_encodings}") unless enc
Expand Down
8 changes: 7 additions & 1 deletion lib/paypalhttp/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ def has_body(request)
def format_headers(headers)
formatted_headers = {}
headers.each do |key, value|
formatted_headers[key.downcase] = value
# TODO: Since header is treated as a hash, val is in an array.
# Will this cause an issue when accessing and modifying val
# Ensure this is the case and will not propegate access issues/errors
if key.casecmp("content-type") == 0
value[0].downcase!
end
formatted_headers[key.downcase] = value
end
formatted_headers
end
Expand Down
2 changes: 1 addition & 1 deletion lib/paypalhttp/version.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "1.0.0"
VERSION = "1.0.1"
27 changes: 27 additions & 0 deletions spec/paypalhttp/encoder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,40 @@
expect(deserialized).to eq(expected)
end

it 'deserializes the response when content-type == application/json: case insensitive' do
expected = {
"string" => "value",
"number" => 1.23,
"bool" => true,
"array" => ["one", "two", "three"],
"nested" => {
"nested_string" => "nested_value",
"nested_array" => [1,2,3]
}
}

headers = {"content-type" => ["application/JSON; charset=utf8"]}
body = '{"string":"value","number":1.23,"bool":true,"array":["one","two","three"],"nested":{"nested_string":"nested_value","nested_array":[1,2,3]}}'

deserialized = Encoder.new.deserialize_response(body, headers)

expect(deserialized).to eq(expected)
end

it 'deserializes the response when content-type == text/*' do
headers = {"content-type" => ["text/plain; charset=utf8"]}
body = 'some text'

expect(Encoder.new.deserialize_response(body, headers)).to eq('some text')
end

it 'deserializes the response when content-type == text/*: case insensitive' do
headers = {"content-type" => ["TEXT/plain; charset=utf8"]}
body = 'some text'

expect(Encoder.new.deserialize_response(body, headers)).to eq('some text')
end

it 'throws when attempting to deserialize multipart/*' do
headers = {"content-type" => ["multipart/form-data"]}
body = 'some multipart encoded data here'
Expand Down
51 changes: 51 additions & 0 deletions spec/paypalhttp/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,57 @@ def _inj(req)
expect(resp.result).to eq(return_data)
end

it 'handles json array result: case insensitive' do
WebMock.enable!

return_data = ["one", "two"]

http_client = HttpClient.new(@environment)

stub_request(:get, @environment.base_url + "/v1/api")
.to_return(body: JSON.generate(return_data), status: 200, headers: {"Content-Type" => "application/JSON"})

req = OpenStruct.new({:verb => "GET", :path => "/v1/api"})

resp = http_client.execute(req)

expect(resp.result).to eq(return_data)
end

it 'handles plain text result' do
WebMock.enable!

return_data = "value"

http_client = HttpClient.new(@environment)

stub_request(:get, @environment.base_url + "/v1/api")
.to_return(body: return_data, status: 200, headers: {"Content-Type" => "text/plain; charset=utf8"})

req = OpenStruct.new({:verb => "GET", :path => "/v1/api"})

resp = http_client.execute(req)

expect(resp.result).to eq(return_data)
end

it 'handles plain text result: case insensitive' do
WebMock.enable!

return_data = "value"

http_client = HttpClient.new(@environment)

stub_request(:get, @environment.base_url + "/v1/api")
.to_return(body: return_data, status: 200, headers: {"Content-Type" => "TEXT/plain; charset=utf8"})

req = OpenStruct.new({:verb => "GET", :path => "/v1/api"})

resp = http_client.execute(req)

expect(resp.result).to eq(return_data)
end

it 'deserializes nested response object into nested openstruct response' do
WebMock.enable!

Expand Down

0 comments on commit b444edf

Please sign in to comment.