Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 4cbb1e8

Browse files
Hdl 285 paypal sdk header: Fixing Case Sensitivity (#9)
* Config Encoder and Client to enforce lowercase Content Types and Unit tests * Updated version, changelog, and license Co-authored-by: Hakim <[email protected]>
1 parent 58fe583 commit 4cbb1e8

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
## 1.0.1
2+
* Fix Case Sensitivity of Content Type for deserialization process
3+
14
## 1.0.0
25
* First Release

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2009-2019 PayPal, Inc.
1+
Copyright (c) 2009-2021 PayPal, Inc.
22

33
Permission is hereby granted, free of charge, to any person
44
obtaining a copy of this software and associated documentation

lib/paypalhttp/encoder.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class Encoder {
3737
if (!contentType) {
3838
throw new Error('HttpRequest does not have Content-Type header set');
3939
}
40+
// Forcing Lowercase to ensure deserializing happens properly
41+
contentType = contentType.toLowerCase();
4042

4143
let encoder = this._encoder(contentType);
4244

lib/paypalhttp/http_client.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ class HttpClient {
5353

5454
Object.keys(headers).forEach(function (key) {
5555
if (key != null) {
56-
formattedHeader[key.toLowerCase()] = headers[key];
56+
if (key.toLowerCase() === 'content-type') {
57+
formattedHeader[key.toLowerCase()] = headers[key].toLowerCase();
58+
} else {
59+
formattedHeader[key.toLowerCase()] = headers[key];
60+
}
5761
}
5862
});
5963
return formattedHeader;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@paypal/paypalhttp",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A library for integrating with PayPalHttp.",
55
"keywords": [
66
"paypalhttp",

spec/unit/encoder_spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ describe('encoder', function () {
144144
assert.deepEqual(deserialized.three, ['one', 'two', 'three']);
145145
});
146146

147+
it('deserializes a case insensitive response with content-type == application/JSON', function () {
148+
let headers = {
149+
'content-type': 'application/JSON; charset=utf8'
150+
};
151+
let body = '{"one":"two","three":["one","two","three"]}';
152+
let deserialized = encoder.deserializeResponse(body, headers);
153+
154+
assert.equal(deserialized.one, 'two');
155+
assert.deepEqual(deserialized.three, ['one', 'two', 'three']);
156+
});
157+
147158
it('deserializes a response with content-type == text/*', function () {
148159
let headers = {
149160
'content-type': 'text/asdf; charset=utf8'
@@ -153,6 +164,15 @@ describe('encoder', function () {
153164
assert.equal(encoder.deserializeResponse(body, headers), body);
154165
});
155166

167+
it('deserializes a case insensitive response with content-type == TEXT/*', function () {
168+
let headers = {
169+
'content-type': 'TEXT/asdf; charset=utf8'
170+
};
171+
let body = 'some asdf text';
172+
173+
assert.equal(encoder.deserializeResponse(body, headers), body);
174+
});
175+
156176
it('throws when response content-type == multipart/*', function () {
157177
let headers = {
158178
'content-type': 'multipart/form-data; charset=utf8'

spec/unit/http_client_spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ describe('HttpClient', function () {
2929
});
3030
});
3131

32+
describe('formatHeaders', function () {
33+
it('returns formatted headers', function () {
34+
let headers = {
35+
'Content-Type': 'application/JSON',
36+
key: 'value'
37+
};
38+
39+
let expected = {
40+
'content-type': 'application/json',
41+
key: 'value'
42+
};
43+
44+
let formattedHeaders = this.http.formatHeaders(headers);
45+
46+
assert.equal(formattedHeaders['content-type'], expected['content-type']);
47+
});
48+
});
49+
3250
describe('addInjector', function () {
3351
it('adds to the injectors array', function () {
3452
function injector(request) {}

0 commit comments

Comments
 (0)