Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusted wrapCallback to not fail if no content-type returned (e.g. errors) #25

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/utils/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ 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;
if (invalidContentType) {
const invalidContentType = res && res.header && res.header["content-type"] && res.header["content-type"].indexOf(expectedContentType) === -1;
if (!err && invalidContentType) {
callback([{
message: "Invalid Content-Type specified in Response Header",
}], res ? res.body : undefined, res);
Expand Down
25 changes: 25 additions & 0 deletions test/utils/ApiClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});