Add followRedirects: false option to Zotero.HTTP.request()

Currently only .status and .getResponseHeader() (for getting 'Location')
are available in the returned object, but we could make the body
available if necessary.
This commit is contained in:
Dan Stillman 2018-09-11 01:23:15 -04:00
parent b8db83af08
commit b782120840
3 changed files with 83 additions and 16 deletions

View file

@ -1,6 +1,9 @@
describe("Zotero.HTTP", function () {
var httpd;
var port = 16213;
var baseURL = `http://127.0.0.1:${port}/`
var testURL = baseURL + 'test.html';
var redirectLocation = baseURL + 'test2.html';
before(function* () {
Components.utils.import("resource://zotero-unit/httpd.js");
@ -16,6 +19,16 @@ describe("Zotero.HTTP", function () {
}
}
);
httpd.registerPathHandler(
'/redirect',
{
handle: function (request, response) {
response.setHeader('Location', redirectLocation);
response.setStatusLine(null, 301, "Moved Permanently");
response.write(`<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>301 Moved Permanently</title>\n</head><body>\n<h1>Moved Permanently</h1>\n<p>The document has moved <a href="${redirectLocation}">here</a>.</p>\n</body></html>`);
}
}
);
});
after(function* () {
@ -24,14 +37,29 @@ describe("Zotero.HTTP", function () {
yield defer.promise;
});
describe("#request()", function () {
it("should succeed with 3xx status if followRedirects is false", async function () {
var req = await Zotero.HTTP.request(
'GET',
baseURL + 'redirect',
{
followRedirects: false
}
);
assert.equal(req.status, 301);
assert.equal(req.getResponseHeader('Location'), redirectLocation);
});
});
describe("#processDocuments()", function () {
it("should provide a document object", function* () {
var called = false;
var url = `http://127.0.0.1:${port}/test.html`;
yield Zotero.HTTP.processDocuments(
url,
testURL,
function (doc) {
assert.equal(doc.location.href, url);
assert.equal(doc.location.href, testURL);
assert.equal(doc.querySelector('p').textContent, 'Test');
var p = doc.evaluate('//p', doc, null, XPathResult.ANY_TYPE, null).iterateNext();
assert.equal(p.textContent, 'Test');
@ -56,12 +84,11 @@ describe("Zotero.HTTP", function () {
it("should provide a document object", function* () {
var called = false;
var url = `http://127.0.0.1:${port}/test.html`;
yield new Zotero.Promise((resolve) => {
Zotero.HTTP.loadDocuments(
url,
testURL,
function (doc) {
assert.equal(doc.location.href, url);
assert.equal(doc.location.href, testURL);
assert.equal(doc.querySelector('p').textContent, 'Test');
var p = doc.evaluate('//p', doc, null, XPathResult.ANY_TYPE, null).iterateNext();
assert.equal(p.textContent, 'Test');