From 01df8f59e56808d1ddf00d065ca46fb674cf4b18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Tue, 21 Feb 2017 14:26:14 +0200 Subject: [PATCH] Default to first translator for /connector/savePage if not provided --- .../content/zotero/xpcom/server_connector.js | 8 ++- test/tests/data/coins.html | 12 +++++ test/tests/server_connectorTest.js | 52 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 test/tests/data/coins.html diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js index 97ebd1f15c..f8bdad3d26 100644 --- a/chrome/content/zotero/xpcom/server_connector.js +++ b/chrome/content/zotero/xpcom/server_connector.js @@ -201,6 +201,7 @@ Zotero.Server.Connector.Detect.prototype = { * uri - The URI of the page to be saved * html - document.innerHTML or equivalent * cookie - document.cookie or equivalent + * translatorID [optional] - a translator ID as returned by /connector/detect * * Returns: * If a single item, sends response code 201 with item in body. @@ -297,8 +298,11 @@ Zotero.Server.Connector.SavePage.prototype = { } }); - // set translator and translate - translate.setTranslator(this._parsedPostData.translatorID); + if (this._parsedPostData.translatorID) { + translate.setTranslator(this._parsedPostData.translatorID); + } else { + translate.setTranslator(translators[0]); + } translate.translate(libraryID); } } diff --git a/test/tests/data/coins.html b/test/tests/data/coins.html new file mode 100644 index 0000000000..b60116665b --- /dev/null +++ b/test/tests/data/coins.html @@ -0,0 +1,12 @@ + + + + + Test Page + + + +

This is a test page

+ + + diff --git a/test/tests/server_connectorTest.js b/test/tests/server_connectorTest.js index 45e8dc5095..dd4c28756a 100644 --- a/test/tests/server_connectorTest.js +++ b/test/tests/server_connectorTest.js @@ -390,6 +390,58 @@ describe("Connector Server", function () { }); }); + describe("/connector/savePage", function() { + // TEMP: Wait for indexing to complete, which happens after a 1-second delay, after a 201 has + // been returned to the connector. Would be better to make sure indexing has completed. + afterEach(function* () { + yield Zotero.Promise.delay(1050); + }); + + it("should return 500 if no translator available for page", function* () { + var xmlhttp = yield Zotero.HTTP.request( + 'POST', + connectorServerPath + "/connector/savePage", + { + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + uri: "http://example.com", + html: "TitleBody" + }), + successCodes: false + } + ); + assert.equal(xmlhttp.status, 500); + }); + + it("should translate a page if translators are available", function* () { + var html = Zotero.File.getContentsFromURL(getTestDataUrl('coins.html')); + var promise = waitForItemEvent('add'); + var xmlhttp = yield Zotero.HTTP.request( + 'POST', + connectorServerPath + "/connector/savePage", + { + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + uri: "https://example.com/test", + html + }), + successCodes: false + } + ); + + let ids = yield promise; + var item = Zotero.Items.get(ids[0]); + var title = "Test Page"; + assert.equal(JSON.parse(xmlhttp.responseText).items[0].title, title); + assert.equal(item.getField('title'), title); + assert.equal(xmlhttp.status, 201); + }); + }); + describe('/connector/installStyle', function() { var endpoint;