Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit f028188

Browse files
authored
Fix well-known lookup for sliding sync labs check (#12519)
* Fix well-known lookup for sliding sync labs check Signed-off-by: Michael Telatynski <[email protected]> * Add test Signed-off-by: Michael Telatynski <[email protected]> * Iterate Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent d0b30d1 commit f028188

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/SlidingSyncManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,10 @@ export class SlidingSyncManager {
359359
let proxyUrl: string | undefined;
360360

361361
try {
362-
const clientWellKnown = await AutoDiscovery.findClientConfig(client.baseUrl);
362+
const clientWellKnown = await AutoDiscovery.findClientConfig(client.getDomain()!);
363363
proxyUrl = clientWellKnown?.["org.matrix.msc3575.proxy"]?.url;
364364
} catch (e) {
365-
// client.baseUrl is invalid, `AutoDiscovery.findClientConfig` has thrown
365+
// client.getDomain() is invalid, `AutoDiscovery.findClientConfig` has thrown
366366
}
367367

368368
if (proxyUrl != undefined) {
@@ -401,7 +401,7 @@ export class SlidingSyncManager {
401401

402402
const proxyUrl = await this.getProxyFromWellKnown(client);
403403
if (proxyUrl != undefined) {
404-
const response = await fetch(proxyUrl + "/client/server.json", {
404+
const response = await fetch(new URL("/client/server.json", proxyUrl), {
405405
method: Method.Get,
406406
signal: timeoutSignal(10 * 1000), // 10s
407407
});

test/SlidingSyncManager-test.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
import { SlidingSync } from "matrix-js-sdk/src/sliding-sync";
1818
import { mocked } from "jest-mock";
1919
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
20+
import fetchMockJest from "fetch-mock-jest";
2021

2122
import { SlidingSyncManager } from "../src/SlidingSyncManager";
2223
import { stubClient } from "./test-utils";
@@ -39,6 +40,8 @@ describe("SlidingSyncManager", () => {
3940
mocked(client.getRoom).mockReturnValue(null);
4041
manager.configure(client, "invalid");
4142
manager.slidingSync = slidingSync;
43+
fetchMockJest.reset();
44+
fetchMockJest.get("https://proxy/client/server.json", {});
4245
});
4346

4447
describe("setRoomVisible", () => {
@@ -236,7 +239,7 @@ describe("SlidingSyncManager", () => {
236239
describe("checkSupport", () => {
237240
beforeEach(() => {
238241
SlidingSyncController.serverSupportsSlidingSync = false;
239-
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
242+
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");
240243
});
241244
it("shorts out if the server has 'native' sliding sync support", async () => {
242245
jest.spyOn(manager, "nativeSlidingSyncSupport").mockResolvedValue(true);
@@ -252,6 +255,25 @@ describe("SlidingSyncManager", () => {
252255
expect(manager.getProxyFromWellKnown).toHaveBeenCalled();
253256
expect(SlidingSyncController.serverSupportsSlidingSync).toBeTruthy();
254257
});
258+
it("should query well-known on server_name not baseUrl", async () => {
259+
fetchMockJest.get("https://matrix.org/.well-known/matrix/client", {
260+
"m.homeserver": {
261+
base_url: "https://matrix-client.matrix.org",
262+
server: "matrix.org",
263+
},
264+
"org.matrix.msc3575.proxy": {
265+
url: "https://proxy/",
266+
},
267+
});
268+
fetchMockJest.get("https://matrix-client.matrix.org/_matrix/client/versions", { versions: ["v1.4"] });
269+
270+
mocked(manager.getProxyFromWellKnown).mockRestore();
271+
jest.spyOn(manager, "nativeSlidingSyncSupport").mockResolvedValue(false);
272+
expect(SlidingSyncController.serverSupportsSlidingSync).toBeFalsy();
273+
await manager.checkSupport(client);
274+
expect(SlidingSyncController.serverSupportsSlidingSync).toBeTruthy();
275+
expect(fetchMockJest).not.toHaveFetched("https://matrix-client.matrix.org/.well-known/matrix/client");
276+
});
255277
});
256278
describe("nativeSlidingSyncSupport", () => {
257279
beforeEach(() => {
@@ -266,7 +288,7 @@ describe("SlidingSyncManager", () => {
266288
expect(feature).toBe("org.matrix.msc3575");
267289
return true;
268290
});
269-
const proxySpy = jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
291+
const proxySpy = jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");
270292

271293
expect(SlidingSyncController.serverSupportsSlidingSync).toBeFalsy();
272294
await manager.checkSupport(client); // first thing it does is call nativeSlidingSyncSupport
@@ -287,14 +309,14 @@ describe("SlidingSyncManager", () => {
287309
expect(manager.startSpidering).toHaveBeenCalled();
288310
});
289311
it("uses the proxy declared in the client well-known", async () => {
290-
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
312+
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");
291313
await manager.setup(client);
292314
expect(manager.configure).toHaveBeenCalled();
293-
expect(manager.configure).toHaveBeenCalledWith(client, "proxy");
315+
expect(manager.configure).toHaveBeenCalledWith(client, "https://proxy/");
294316
expect(manager.startSpidering).toHaveBeenCalled();
295317
});
296318
it("uses the legacy `feature_sliding_sync_proxy_url` if it was set", async () => {
297-
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
319+
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");
298320
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
299321
if (name === "feature_sliding_sync_proxy_url") return "legacy-proxy";
300322
});

0 commit comments

Comments
 (0)