Skip to content

Commit

Permalink
Bug 1790961 - Invalidate cache attachments base URL when server chang…
Browse files Browse the repository at this point in the history
…es r=gbeckley

Differential Revision: https://phabricator.services.mozilla.com/D157433
  • Loading branch information
leplatrem committed Oct 18, 2022
1 parent 4f21586 commit 646171e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
9 changes: 5 additions & 4 deletions services/settings/Attachments.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Downloader {

constructor(...folders) {
this.folders = ["settings", ...folders];
this._cdnURL = null;
this._cdnURLs = {};
}

/**
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
37 changes: 33 additions & 4 deletions services/settings/test/unit/test_attachments_downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {},
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 646171e

Please sign in to comment.