From eaa291bf95a622013b4807d9d815dc3e0007d5f2 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Tue, 18 Oct 2022 14:54:30 +0000 Subject: [PATCH] Bug 1790961 - Invalidate cache attachments base URL when server changes r=gbeckley Differential Revision: https://phabricator.services.mozilla.com/D157433 --- services/settings/Attachments.jsm | 9 +++-- .../test/unit/test_attachments_downloader.js | 37 +++++++++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/services/settings/Attachments.jsm b/services/settings/Attachments.jsm index 00bf059d55dc..72361c23b01e 100644 --- a/services/settings/Attachments.jsm +++ b/services/settings/Attachments.jsm @@ -106,7 +106,7 @@ class Downloader { constructor(...folders) { this.folders = ["settings", ...folders]; - this._cdnURL = null; + this._cdnURLs = {}; } /** @@ -414,7 +414,7 @@ class Downloader { } async _baseAttachmentsURL() { - if (!this._cdnURL) { + if (!this._cdnURLs[lazy.Utils.SERVER_URL]) { const resp = await lazy.Utils.fetch(`${lazy.Utils.SERVER_URL}/`); let serverInfo; try { @@ -429,9 +429,10 @@ class Downloader { }, } = serverInfo; // Make sure the URL always has a trailing slash. - this._cdnURL = base_url + (base_url.endsWith("/") ? "" : "/"); + this._cdnURLs[lazy.Utils.SERVER_URL] = + base_url + (base_url.endsWith("/") ? "" : "/"); } - return this._cdnURL; + return this._cdnURLs[lazy.Utils.SERVER_URL]; } async _fetchAttachment(url) { diff --git a/services/settings/test/unit/test_attachments_downloader.js b/services/settings/test/unit/test_attachments_downloader.js index a72dd9c2c38b..4332a3343541 100644 --- a/services/settings/test/unit/test_attachments_downloader.js +++ b/services/settings/test/unit/test_attachments_downloader.js @@ -60,15 +60,15 @@ function run_test() { do_get_file("test_attachments_downloader") ); + run_next_test(); +} + +async function clear_state() { Services.prefs.setCharPref( "services.settings.server", `http://localhost:${server.identity.primaryPort}/v1` ); - run_next_test(); -} - -async function clear_state() { downloader = new Downloader("main", "some-collection"); const dummyCacheImpl = { get: async attachmentId => {}, @@ -101,6 +101,35 @@ async function clear_state() { add_task(clear_state); +add_task(async function test_base_attachment_url_depends_on_server() { + const before = await downloader._baseAttachmentsURL(); + + Services.prefs.setCharPref( + "services.settings.server", + `http://localhost:${server.identity.primaryPort}/v2` + ); + + server.registerPathHandler("/v2/", (request, response) => { + response.write( + JSON.stringify({ + capabilities: { + attachments: { + base_url: "http://some-cdn-url.org", + }, + }, + }) + ); + response.setHeader("Content-Type", "application/json; charset=UTF-8"); + response.setStatusLine(null, 200, "OK"); + }); + + const after = await downloader._baseAttachmentsURL(); + + Assert.notEqual(before, after, "base URL was changed"); + Assert.equal(after, "http://some-cdn-url.org/", "A trailing slash is added"); +}); +add_task(clear_state); + add_task( async function test_download_throws_server_info_error_if_invalid_response() { server.registerPathHandler("/v1/", (request, response) => {