From d9550bb5df722d58621b103141e754072daed050 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Fri, 6 Dec 2024 00:48:00 -0500 Subject: [PATCH] Server: Expose actual bound port (#4903) And: - Use a different port for tests so server tests don't break when Zotero is already running - Remove no-op httpServer.enabled pref updates in tests - It's already enabled, no test disables it, and any test that did would clean up after itself - Updating that pref has no effect without a separate call to Zotero.Server.init() - Remove unused arg to Zotero.Server.init() --- .../zotero/preferences/preferences_advanced.js | 2 +- .../zotero/xpcom/localAPI/server_localAPI.js | 2 +- chrome/content/zotero/xpcom/server.js | 13 +++++++++++-- test/runtests.sh | 1 + test/tests/attachmentsTest.js | 1 - test/tests/serverTest.js | 4 +--- test/tests/server_connectorIntegrationTest.js | 4 +--- test/tests/server_connectorTest.js | 5 ++--- test/tests/server_integrationTest.js | 4 +--- test/tests/server_localAPITest.js | 3 +-- 10 files changed, 20 insertions(+), 19 deletions(-) diff --git a/chrome/content/zotero/preferences/preferences_advanced.js b/chrome/content/zotero/preferences/preferences_advanced.js index 4b929f9316..b4884be39c 100644 --- a/chrome/content/zotero/preferences/preferences_advanced.js +++ b/chrome/content/zotero/preferences/preferences_advanced.js @@ -536,7 +536,7 @@ Zotero_Preferences.Advanced = { serverDisabledSection.hidden = true; document.l10n.setArgs(availableMessage, { - url: `http://localhost:${Zotero.Prefs.get('httpServer.port')}/api/` + url: `http://localhost:${Zotero.Server.port}/api/` }); }, diff --git a/chrome/content/zotero/xpcom/localAPI/server_localAPI.js b/chrome/content/zotero/xpcom/localAPI/server_localAPI.js index 2ddd9bd93b..a142a285db 100644 --- a/chrome/content/zotero/xpcom/localAPI/server_localAPI.js +++ b/chrome/content/zotero/xpcom/localAPI/server_localAPI.js @@ -808,7 +808,7 @@ async function toResponseJSON(dataObjectOrObjects, searchParams) { let dataObject = dataObjectOrObjects; let responseJSON = dataObject.toResponseJSONAsync ? await dataObject.toResponseJSONAsync({ - apiURL: `http://localhost:${Zotero.Prefs.get('httpServer.port')}/api/`, + apiURL: `http://localhost:${Zotero.Server.port}/api/`, includeGroupDetails: true }) : dataObject; diff --git a/chrome/content/zotero/xpcom/server.js b/chrome/content/zotero/xpcom/server.js index bcdfac7ec6..7270e630bb 100755 --- a/chrome/content/zotero/xpcom/server.js +++ b/chrome/content/zotero/xpcom/server.js @@ -42,10 +42,19 @@ Zotero.Server = new function() { 504:"Gateway Timeout" }; + Object.defineProperty(this, 'port', { + get() { + if (!serv) { + throw new Error('Server not initialized'); + } + return serv.port; + } + }); + /** * initializes a very rudimentary web server */ - this.init = function(port, bindAllAddr, maxConcurrentConnections) { + this.init = function(port, bindAllAddr) { if (Zotero.HTTP.browserIsOffline()) { Zotero.debug('Browser is offline -- not initializing HTTP server'); _registerOnlineObserver(); @@ -702,4 +711,4 @@ Zotero.Server.DataListener.prototype._decodeMultipartData = function(data, bound * * See connector/server_connector.js for examples */ -Zotero.Server.Endpoints = {} \ No newline at end of file +Zotero.Server.Endpoints = {} diff --git a/test/runtests.sh b/test/runtests.sh index 41ec881483..a7dcd6b43f 100755 --- a/test/runtests.sh +++ b/test/runtests.sh @@ -137,6 +137,7 @@ user_pref("extensions.zotero.firstRunGuidance", false); user_pref("extensions.zotero.firstRun2", false); user_pref("extensions.zotero.reportTranslationFailure", false); user_pref("extensions.zotero.httpServer.enabled", true); +user_pref("extensions.zotero.httpServer.port", 23124); // ascii "ZT" user_pref("extensions.zotero.httpServer.localAPI.enabled", true); user_pref("extensions.zotero.backup.numBackups", 0); user_pref("extensions.zotero.sync.autoSync", false); diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index c56619196e..410b238028 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -381,7 +381,6 @@ describe("Zotero.Attachments", function() { before(async function () { this.timeout(20000); - Zotero.Prefs.set("httpServer.enabled", true); }); beforeEach(async function () { diff --git a/test/tests/serverTest.js b/test/tests/serverTest.js index f73ed5b3b7..05cbb8e2dd 100644 --- a/test/tests/serverTest.js +++ b/test/tests/serverTest.js @@ -5,9 +5,7 @@ describe("Zotero.Server", function () { var serverPath; before(function* () { - Zotero.Prefs.set("httpServer.enabled", true); - Zotero.Server.init(); - serverPath = 'http://127.0.0.1:' + Zotero.Prefs.get('httpServer.port'); + serverPath = 'http://127.0.0.1:' + Zotero.Server.port; }); describe('DataListener', function() { diff --git a/test/tests/server_connectorIntegrationTest.js b/test/tests/server_connectorIntegrationTest.js index 11c9920b3e..5e59c0e303 100644 --- a/test/tests/server_connectorIntegrationTest.js +++ b/test/tests/server_connectorIntegrationTest.js @@ -5,14 +5,12 @@ describe("Connector HTTP Integration Server", function () { before(function* () { this.timeout(20000); - Zotero.Prefs.set("httpServer.enabled", true); yield resetDB({ thisArg: this, skipBundledFiles: true }); - const serverPort = Zotero.Prefs.get('httpServer.port'); - serverURL = `http://127.0.0.1:${serverPort}/connector/document`; + serverURL = `http://127.0.0.1:${Zotero.Server.port}/connector/document`; }); describe('/connector/document/execCommand', function () { diff --git a/test/tests/server_connectorTest.js b/test/tests/server_connectorTest.js index 21322617c0..5d660869f3 100644 --- a/test/tests/server_connectorTest.js +++ b/test/tests/server_connectorTest.js @@ -8,7 +8,6 @@ describe("Connector Server", function () { before(function* () { this.timeout(20000); - Zotero.Prefs.set("httpServer.enabled", true); yield resetDB({ thisArg: this, skipBundledFiles: true @@ -16,7 +15,7 @@ describe("Connector Server", function () { yield Zotero.Translators.init(); win = yield loadZoteroPane(); - connectorServerPath = 'http://127.0.0.1:' + Zotero.Prefs.get('httpServer.port'); + connectorServerPath = 'http://127.0.0.1:' + Zotero.Server.port; }); beforeEach(function () { @@ -2689,7 +2688,7 @@ describe("Connector Server", function () { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ method: 'GET', - url: `http://localhost:${Zotero.Prefs.get('httpServer.port')}/` + url: `http://localhost:${Zotero.Server.port}/` }), successCodes: false } diff --git a/test/tests/server_integrationTest.js b/test/tests/server_integrationTest.js index 3f98f3800c..ef56e9816d 100644 --- a/test/tests/server_integrationTest.js +++ b/test/tests/server_integrationTest.js @@ -5,14 +5,12 @@ describe("MacOS Integration Server", function () { before(function* () { this.timeout(20000); - Zotero.Prefs.set("httpServer.enabled", true); yield resetDB({ thisArg: this, skipBundledFiles: true }); - const serverPort = Zotero.Prefs.get('httpServer.port'); - serverURL = `http://127.0.0.1:${serverPort}/integration`; + serverURL = `http://127.0.0.1:${Zotero.Server.port}/integration`; }); describe('/integration/macWordCommand', function () { diff --git a/test/tests/server_localAPITest.js b/test/tests/server_localAPITest.js index 208a05677b..cab89a8646 100644 --- a/test/tests/server_localAPITest.js +++ b/test/tests/server_localAPITest.js @@ -22,8 +22,7 @@ describe("Local API Server", function () { } before(async function () { - Zotero.Prefs.set('httpServer.enabled', true); - apiRoot = 'http://127.0.0.1:' + Zotero.Prefs.get('httpServer.port') + '/api'; + apiRoot = 'http://127.0.0.1:' + Zotero.Server.port + '/api'; await resetDB({ thisArg: this