Skip to content

Commit

Permalink
Merge pull request #79 from mozilla-services/deprecation-headers
Browse files Browse the repository at this point in the history
Fixes #69 - Handle deprecation header.
  • Loading branch information
n1k0 committed Jul 17, 2015
2 parents 95cd04a + c6b5d77 commit 5b0d3d8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ export const DEFAULT_REQUEST_HEADERS = {
"Content-Type": "application/json",
};

function checkForDeprecationHeader(headers) {
const alertHeader = headers.get("Alert");
if (!alertHeader)
return;
try {
const {message, url} = JSON.parse(alertHeader);
console.warn(message, url);
} catch(err) {
console.warn("Unable to parse Alert header message", alertHeader);
}
}

/**
* Performs an HTTP request to the Kinto server. Resolves with an objet
* containing the following properties:
Expand All @@ -32,6 +44,7 @@ export default function request(url, options={headers:{}}) {
const contentLength = headers.get("Content-Length");
if (!contentLength || contentLength == 0)
return null;
checkForDeprecationHeader(headers);
return res.json();
})
.catch(err => {
Expand Down
38 changes: 37 additions & 1 deletion test/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ describe("request()", () => {
status: 200,
headers: {
get(name) {
return "fake";
if (name !== "Alert")
return "fake";
}
},
json() {
Expand Down Expand Up @@ -135,4 +136,39 @@ describe("request()", () => {
.should.be.rejectedWith(Error, /HTTP 400; Invalid request parameter: data is missing/);
});
});

describe("Deprecation header", () => {
beforeEach(() => {
sandbox.stub(console, "warn");
});

it("should handle deprecation header", () => {
const eolObject = {
code: "soft-eol",
url: "http://eos-url",
message: "This service will soon be decommissioned",
};
sandbox.stub(root, "fetch").returns(
fakeServerResponse(200, {}, {Alert: JSON.stringify(eolObject)}));

return request("/")
.then(_ => {
sinon.assert.calledOnce(console.warn);
sinon.assert.calledWithExactly(
console.warn, eolObject.message, eolObject.url);
});
});

it("should handle deprecation header parse error", () => {
sandbox.stub(root, "fetch").returns(
fakeServerResponse(200, {}, {Alert: "dafuq"}));

return request("/")
.then(_ => {
sinon.assert.calledOnce(console.warn);
sinon.assert.calledWithExactly(
console.warn, "Unable to parse Alert header message", "dafuq");
});
});
});
});

0 comments on commit 5b0d3d8

Please sign in to comment.