Fix well-known lookup for sliding sync labs check (#12519)

* Fix well-known lookup for sliding sync labs check

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Add test

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Iterate

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2024-05-14 13:40:10 +01:00 committed by GitHub
parent d0b30d1631
commit f0281886d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 8 deletions

View file

@ -359,10 +359,10 @@ export class SlidingSyncManager {
let proxyUrl: string | undefined;
try {
const clientWellKnown = await AutoDiscovery.findClientConfig(client.baseUrl);
const clientWellKnown = await AutoDiscovery.findClientConfig(client.getDomain()!);
proxyUrl = clientWellKnown?.["org.matrix.msc3575.proxy"]?.url;
} catch (e) {
// client.baseUrl is invalid, `AutoDiscovery.findClientConfig` has thrown
// client.getDomain() is invalid, `AutoDiscovery.findClientConfig` has thrown
}
if (proxyUrl != undefined) {
@ -401,7 +401,7 @@ export class SlidingSyncManager {
const proxyUrl = await this.getProxyFromWellKnown(client);
if (proxyUrl != undefined) {
const response = await fetch(proxyUrl + "/client/server.json", {
const response = await fetch(new URL("/client/server.json", proxyUrl), {
method: Method.Get,
signal: timeoutSignal(10 * 1000), // 10s
});

View file

@ -17,6 +17,7 @@ limitations under the License.
import { SlidingSync } from "matrix-js-sdk/src/sliding-sync";
import { mocked } from "jest-mock";
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import fetchMockJest from "fetch-mock-jest";
import { SlidingSyncManager } from "../src/SlidingSyncManager";
import { stubClient } from "./test-utils";
@ -39,6 +40,8 @@ describe("SlidingSyncManager", () => {
mocked(client.getRoom).mockReturnValue(null);
manager.configure(client, "invalid");
manager.slidingSync = slidingSync;
fetchMockJest.reset();
fetchMockJest.get("https://proxy/client/server.json", {});
});
describe("setRoomVisible", () => {
@ -236,7 +239,7 @@ describe("SlidingSyncManager", () => {
describe("checkSupport", () => {
beforeEach(() => {
SlidingSyncController.serverSupportsSlidingSync = false;
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");
});
it("shorts out if the server has 'native' sliding sync support", async () => {
jest.spyOn(manager, "nativeSlidingSyncSupport").mockResolvedValue(true);
@ -252,6 +255,25 @@ describe("SlidingSyncManager", () => {
expect(manager.getProxyFromWellKnown).toHaveBeenCalled();
expect(SlidingSyncController.serverSupportsSlidingSync).toBeTruthy();
});
it("should query well-known on server_name not baseUrl", async () => {
fetchMockJest.get("https://matrix.org/.well-known/matrix/client", {
"m.homeserver": {
base_url: "https://matrix-client.matrix.org",
server: "matrix.org",
},
"org.matrix.msc3575.proxy": {
url: "https://proxy/",
},
});
fetchMockJest.get("https://matrix-client.matrix.org/_matrix/client/versions", { versions: ["v1.4"] });
mocked(manager.getProxyFromWellKnown).mockRestore();
jest.spyOn(manager, "nativeSlidingSyncSupport").mockResolvedValue(false);
expect(SlidingSyncController.serverSupportsSlidingSync).toBeFalsy();
await manager.checkSupport(client);
expect(SlidingSyncController.serverSupportsSlidingSync).toBeTruthy();
expect(fetchMockJest).not.toHaveFetched("https://matrix-client.matrix.org/.well-known/matrix/client");
});
});
describe("nativeSlidingSyncSupport", () => {
beforeEach(() => {
@ -266,7 +288,7 @@ describe("SlidingSyncManager", () => {
expect(feature).toBe("org.matrix.msc3575");
return true;
});
const proxySpy = jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
const proxySpy = jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");
expect(SlidingSyncController.serverSupportsSlidingSync).toBeFalsy();
await manager.checkSupport(client); // first thing it does is call nativeSlidingSyncSupport
@ -287,14 +309,14 @@ describe("SlidingSyncManager", () => {
expect(manager.startSpidering).toHaveBeenCalled();
});
it("uses the proxy declared in the client well-known", async () => {
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");
await manager.setup(client);
expect(manager.configure).toHaveBeenCalled();
expect(manager.configure).toHaveBeenCalledWith(client, "proxy");
expect(manager.configure).toHaveBeenCalledWith(client, "https://proxy/");
expect(manager.startSpidering).toHaveBeenCalled();
});
it("uses the legacy `feature_sliding_sync_proxy_url` if it was set", async () => {
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
if (name === "feature_sliding_sync_proxy_url") return "legacy-proxy";
});