Skip to content

Commit

Permalink
Merge pull request zotero#1180 from adomasven/fix/savePage-no-translator
Browse files Browse the repository at this point in the history
  • Loading branch information
dstillman committed Feb 21, 2017
2 parents 333675d + 01df8f5 commit 3c5912f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
8 changes: 6 additions & 2 deletions chrome/content/zotero/xpcom/server_connector.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/tests/data/coins.html
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Test Page</title>
</head>
<body>
<span class='Z3988' title='url_ver=Z39.88-2004&amp;ctx_ver=Z39.88-2004&amp;rfr_id=info%3Asid%2Fzotero.org%3A2&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rft.type=blogPost&amp;rft.title=Test%20Page&amp;rft.identifier=https%3A%2F%2Fexample.com%2Ftest&amp;rft.aulast=Zotero&amp;rft.au=Zotero&amp;rft.date=2017-02-21'></span>
<p>This is a test page</p>
</footer>
</body>
</html>
52 changes: 52 additions & 0 deletions test/tests/server_connectorTest.js
Expand Up @@ -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: "<html><head><title>Title</title><body>Body</body></html>"
}),
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;

Expand Down

0 comments on commit 3c5912f

Please sign in to comment.