Bug 1790961 - Invalidate cache attachments base URL when server changes r=gbeckley

Differential Revision: https://phabricator.services.mozilla.com/D157433
This commit is contained in:
Mathieu Leplatre 2022-10-18 14:54:30 +00:00
Родитель b3cd02b7ed
Коммит eaa291bf95
2 изменённых файлов: 38 добавлений и 8 удалений

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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) => {