From 3b0485b1329e4cd39dd922dea74292eb48cf3ce4 Mon Sep 17 00:00:00 2001 From: Aaron Lozier Date: Mon, 30 Sep 2019 13:25:03 -0600 Subject: [PATCH 1/2] Added test for 429 error which failed due to TypeError; adjusted wrapCallback to not fail if no content-type returned --- src/utils/ApiClient.js | 2 +- test/utils/ApiClient.spec.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/utils/ApiClient.js b/src/utils/ApiClient.js index 59438f7..bb63bf2 100644 --- a/src/utils/ApiClient.js +++ b/src/utils/ApiClient.js @@ -173,7 +173,7 @@ export default class ApiClient { wrapCallback(httpMethod, callback = () => null) { return (err, res) => { const expectedContentType = (this.isEncrypted) ? "application/jose+json" : "application/json"; - const invalidContentType = res && res.header && res.status !== 204 && res.header["content-type"].indexOf(expectedContentType) === -1; + const invalidContentType = res && res.header && res.header["content-type"] && res.header["content-type"].indexOf(expectedContentType) === -1; if (invalidContentType) { callback([{ message: "Invalid Content-Type specified in Response Header", diff --git a/test/utils/ApiClient.spec.js b/test/utils/ApiClient.spec.js index cf6dbe0..ddbe29f 100644 --- a/test/utils/ApiClient.spec.js +++ b/test/utils/ApiClient.spec.js @@ -1168,5 +1168,30 @@ describe("utils/ApiClient", () => { }); callback(undefined, rawRes); }); + + it("should call callback with COMMUNICATION_ERROR if status is 429", (cb) => { + const client = new ApiClient("test-username", "test-password", "test-server"); + + const rawRes = { + body: "test", + status: 429, + header: {}, + }; + + const rawErr = { + status: 429, + }; + + const callback = client.wrapCallback("POST", (err, body, res) => { + body.should.be.equal("test"); + rawRes.should.be.deep.equal(res); + err.should.be.deep.equal([{ + message: "Could not communicate with test-server", + code: "COMMUNICATION_ERROR", + }]); + cb(); + }); + callback(rawErr, rawRes); + }); }); }); From 3bc1703f33a080f83ab4266dc3922ab1e7cec4b6 Mon Sep 17 00:00:00 2001 From: Aaron Lozier Date: Wed, 9 Oct 2019 15:39:40 -0500 Subject: [PATCH 2/2] Do not throw Invalid Content-Type error if other error is included in response --- src/utils/ApiClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/ApiClient.js b/src/utils/ApiClient.js index bb63bf2..8efe7b1 100644 --- a/src/utils/ApiClient.js +++ b/src/utils/ApiClient.js @@ -174,7 +174,7 @@ export default class ApiClient { return (err, res) => { const expectedContentType = (this.isEncrypted) ? "application/jose+json" : "application/json"; const invalidContentType = res && res.header && res.header["content-type"] && res.header["content-type"].indexOf(expectedContentType) === -1; - if (invalidContentType) { + if (!err && invalidContentType) { callback([{ message: "Invalid Content-Type specified in Response Header", }], res ? res.body : undefined, res);