Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update matrix-mock-request #239

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"get-port": "^5",
"jest": "^28.1.0",
"jsdoc": "^3.6.10",
"matrix-mock-request": "^1.2.3",
"matrix-mock-request": "^2.1.0",
"simple-mock": "^0.8.0",
"tmp": "^0.2.1",
"ts-jest": "^28.0.3",
Expand Down
4 changes: 2 additions & 2 deletions test/AdminApisTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as MockHttpBackend from 'matrix-mock-request';
import HttpBackend from 'matrix-mock-request';

import { AdminApis, IStorageProvider, MatrixClient, WhoisInfo } from "../src";
import { createTestClient } from "./TestUtils";

export function createTestAdminClient(storage: IStorageProvider = null): { client: AdminApis, mxClient: MatrixClient, http: MockHttpBackend, hsUrl: string, accessToken: string } {
export function createTestAdminClient(storage: IStorageProvider = null): { client: AdminApis, mxClient: MatrixClient, http: HttpBackend, hsUrl: string, accessToken: string } {
const result = createTestClient(storage);
const mxClient = result.client;
const client = new AdminApis(mxClient);
Expand Down
14 changes: 7 additions & 7 deletions test/IdentityClientTest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as MockHttpBackend from 'matrix-mock-request';
import HttpBackend from 'matrix-mock-request';
import * as simple from "simple-mock";

import { IdentityClient, MatrixClient, setRequestFn, Threepid } from "../src";
import { createTestClient } from "./TestUtils";

export async function createTestIdentityClient(): Promise<{ client: IdentityClient, mxClient: MatrixClient, http: MockHttpBackend, identityUrl: string, accessToken: string }> {
export async function createTestIdentityClient(): Promise<{ client: IdentityClient, mxClient: MatrixClient, http: HttpBackend, identityUrl: string, accessToken: string }> {
const result = createTestClient();
const mxClient = result.client;

Expand Down Expand Up @@ -517,7 +517,7 @@ describe('IdentityClient', () => {

const expectedInput = { test: 1234 };
http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.qs).toMatchObject(expectedInput);
expect(req.queryParams).toMatchObject(expectedInput);
return {};
});

Expand All @@ -528,7 +528,7 @@ describe('IdentityClient', () => {
const { client, http, accessToken } = await createTestIdentityClient();

http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.headers["Authorization"]).toEqual(`Bearer ${accessToken}`);
expect(req.headers["Authorization"]).toEqual(`Bearer ${accessToken}`);
return {};
});

Expand All @@ -539,7 +539,7 @@ describe('IdentityClient', () => {
const { client, http } = await createTestIdentityClient();

http.when("PUT", "/test").respond(200, (path, content, req) => {
expect(req.opts.headers["Content-Type"]).toEqual("application/json");
expect(req.headers["Content-Type"]).toEqual("application/json");
return {};
});

Expand All @@ -554,7 +554,7 @@ describe('IdentityClient', () => {
Buffer.isBuffer = <any>(i => i === fakeJson);

http.when("PUT", "/test").respond(200, (path, content, req) => {
expect(req.opts.headers["Content-Type"]).toEqual(contentType);
expect(req.headers["Content-Type"]).toEqual(contentType);
return {};
});

Expand Down Expand Up @@ -585,7 +585,7 @@ describe('IdentityClient', () => {
const timeout = 10;

http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.timeout).toBe(timeout);
expect((req as any).opts.timeout).toBe(timeout);
});

await Promise.all([client.doRequest("GET", "/test", null, null, timeout), http.flushAllExpected()]);
Expand Down
4 changes: 2 additions & 2 deletions test/MatrixAuthTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as MockHttpBackend from 'matrix-mock-request';
import HttpBackend from 'matrix-mock-request';

import { MatrixAuth } from "../src";
import { createTestClient } from "./TestUtils";

export function createTestAuth(): { auth: MatrixAuth, http: MockHttpBackend, hsUrl: string } {
export function createTestAuth(): { auth: MatrixAuth, http: HttpBackend, hsUrl: string } {
const result = createTestClient();

const mxClient = result.client;
Expand Down
62 changes: 31 additions & 31 deletions test/MatrixClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('MatrixClient', () => {

// noinspection TypeScriptValidateJSTypes
http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.qs).toMatchObject(expectedInput);
expect(req.queryParams).toMatchObject(expectedInput);
return {};
});

Expand All @@ -157,7 +157,7 @@ describe('MatrixClient', () => {

// noinspection TypeScriptValidateJSTypes
http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.headers["Authorization"]).toEqual(`Bearer ${accessToken}`);
expect(req.headers["Authorization"]).toEqual(`Bearer ${accessToken}`);
return {};
});

Expand All @@ -169,7 +169,7 @@ describe('MatrixClient', () => {

// noinspection TypeScriptValidateJSTypes
http.when("PUT", "/test").respond(200, (path, content, req) => {
expect(req.opts.headers["Content-Type"]).toEqual("application/json");
expect(req.headers["Content-Type"]).toEqual("application/json");
return {};
});

Expand All @@ -185,7 +185,7 @@ describe('MatrixClient', () => {

// noinspection TypeScriptValidateJSTypes
http.when("PUT", "/test").respond(200, (path, content, req) => {
expect(req.opts.headers["Content-Type"]).toEqual(contentType);
expect(req.headers["Content-Type"]).toEqual(contentType);
return {};
});

Expand Down Expand Up @@ -218,7 +218,7 @@ describe('MatrixClient', () => {

// noinspection TypeScriptValidateJSTypes
http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.timeout).toBe(timeout);
expect((req as any).opts.timeout).toBe(timeout);
});

await Promise.all([client.doRequest("GET", "/test", null, null, timeout), http.flushAllExpected()]);
Expand All @@ -234,8 +234,8 @@ describe('MatrixClient', () => {

// noinspection TypeScriptValidateJSTypes
http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.qs["user_id"]).toBe(userId);
expect(req.opts.qs["org.matrix.msc3202.device_id"]).toBe(undefined);
expect(req.queryParams["user_id"]).toBe(userId);
expect(req.queryParams["org.matrix.msc3202.device_id"]).toBe(undefined);
});

await Promise.all([client.doRequest("GET", "/test"), http.flushAllExpected()]);
Expand All @@ -250,8 +250,8 @@ describe('MatrixClient', () => {

// noinspection TypeScriptValidateJSTypes
http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.qs["user_id"]).toBe(userId);
expect(req.opts.qs["org.matrix.msc3202.device_id"]).toBe(deviceId);
expect(req.queryParams["user_id"]).toBe(userId);
expect(req.queryParams["org.matrix.msc3202.device_id"]).toBe(deviceId);
});

await Promise.all([client.doRequest("GET", "/test"), http.flushAllExpected()]);
Expand All @@ -266,8 +266,8 @@ describe('MatrixClient', () => {

// noinspection TypeScriptValidateJSTypes
http.when("GET", "/test").respond(200, (path, content, req) => {
expect(req.opts.qs?.["user_id"]).toBe(undefined);
expect(req.opts.qs?.["org.matrix.msc3202.device_id"]).toBe(undefined);
expect(req.queryParams?.["user_id"]).toBe(undefined);
expect(req.queryParams?.["org.matrix.msc3202.device_id"]).toBe(undefined);
});

await Promise.all([client.doRequest("GET", "/test"), http.flushAllExpected()]);
Expand Down Expand Up @@ -1255,7 +1255,7 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/client/v3/sync").respond(200, (path, content, req) => {
expect(req).toBeDefined();
expect(req.opts.qs.filter).toEqual(filterId);
expect(req.queryParams.filter).toEqual(filterId);
client.stop();
return { next_batch: "1234" };
});
Expand Down Expand Up @@ -1293,13 +1293,13 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/client/v3/sync").respond(200, (path, content, req) => {
expect(req).toBeDefined();
expect(req.opts.qs.since).toBeUndefined();
expect(req.queryParams.since).toBeUndefined();
return { next_batch: secondToken };
});
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/client/v3/sync").respond(200, (path, content, req) => {
expect(req).toBeDefined();
expect(req.opts.qs.since).toEqual(secondToken);
expect(req.queryParams.since).toEqual(secondToken);
client.stop();
return { next_batch: secondToken };
});
Expand Down Expand Up @@ -1340,7 +1340,7 @@ describe('MatrixClient', () => {
http.when("GET", "/_matrix/client/v3/sync").respond(200, (path, content, req) => {
expect(req).toBeDefined();

expect(req.opts.qs.since).toEqual(syncToken);
expect(req.queryParams.since).toEqual(syncToken);
client.stop();

return { next_batch: syncToken };
Expand Down Expand Up @@ -1375,14 +1375,14 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/client/v3/sync").respond(200, (path, content, req) => {
expect(req).toBeDefined();
expect(req.opts.qs.presence).toBeUndefined();
expect(req.queryParams.presence).toBeUndefined();
client.syncingPresence = presence;
return { next_batch: "testing" };
});
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/client/v3/sync").respond(200, (path, content, req) => {
expect(req).toBeDefined();
expect(req.opts.qs.presence).toEqual(presence);
expect(req.queryParams.presence).toEqual(presence);
client.stop();
return { next_batch: "testing" };
});
Expand Down Expand Up @@ -2736,7 +2736,7 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/client/v3/rooms").respond(200, (path, content, req) => {
expect(path).toEqual(`${hsUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/context/${encodeURIComponent(targetEvent.eventId)}`);
expect(req.opts.qs['limit']).toEqual(limit);
expect(req.queryParams['limit']).toEqual(limit);
return {
event: targetEvent,
events_before: before,
Expand Down Expand Up @@ -2893,9 +2893,9 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("POST", "/_matrix/client/v3/join").respond(200, (path, content, req) => {
expect(path).toEqual(`${hsUrl}/_matrix/client/v3/join/${encodeURIComponent(roomId)}`);
expect(req.opts.qs['server_name'].length).toEqual(serverNames.length);
expect(req.queryParams['server_name'].length).toEqual(serverNames.length);
for (let i = 0; i < serverNames.length; i++) {
expect(req.opts.qs['server_name'][i]).toEqual(serverNames[i]);
expect(req.queryParams['server_name'][i]).toEqual(serverNames[i]);
}
return { room_id: roomId };
});
Expand Down Expand Up @@ -3109,7 +3109,7 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/client/v3/rooms").respond(200, (path, content, req) => {
expect(path).toEqual(`${hsUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/members`);
expect(req.opts.qs.at).toEqual(atToken);
expect(req.queryParams.at).toEqual(atToken);
return { chunk: memberEvents };
});

Expand Down Expand Up @@ -3149,8 +3149,8 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/client/v3/rooms").respond(200, (path, content, req) => {
expect(path).toEqual(`${hsUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/members`);
expectArrayEquals(forMemberships, req.opts.qs.membership);
expectArrayEquals(forNotMemberships, req.opts.qs.not_membership);
expectArrayEquals(forMemberships, (req.queryParams as any).membership);
expectArrayEquals(forNotMemberships, (req.queryParams as any).not_membership);
return { chunk: memberEvents };
});

Expand Down Expand Up @@ -5443,9 +5443,9 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("POST", "/_matrix/media/v3/upload").respond(200, (path, content, req) => {
expect(content).toBeDefined();
expect(req.opts.qs.filename).toEqual(filename);
expect(req.opts.headers["Content-Type"]).toEqual(contentType);
expect(req.opts.body).toEqual(data);
expect(req.queryParams.filename).toEqual(filename);
expect(req.headers["Content-Type"]).toEqual(contentType);
expect(req.rawData).toEqual(data);
return { content_uri: uri };
});

Expand All @@ -5466,9 +5466,9 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("POST", "/_matrix/media/v3/upload").respond(200, (path, content, req) => {
expect(content).toBeDefined();
expect(req.opts.qs.filename).toEqual(filename);
expect(req.opts.headers["Content-Type"]).toEqual(contentType);
expect(req.opts.body).toEqual(data);
expect(req.queryParams.filename).toEqual(filename);
expect(req.headers["Content-Type"]).toEqual(contentType);
expect(req.rawData).toEqual(data);
return { content_uri: uri };
});

Expand All @@ -5487,7 +5487,7 @@ describe('MatrixClient', () => {
// noinspection TypeScriptValidateJSTypes
http.when("GET", "/_matrix/media/v3/download/").respond(200, (path, _, req) => {
expect(path).toContain("/_matrix/media/v3/download/" + urlPart);
expect(req.opts.encoding).toEqual(null);
expect((req as any).opts.encoding).toEqual(null);
// TODO: Honestly, I have no idea how to coerce the mock library to return headers or buffers,
// so this is left as a fun activity.
// return {
Expand Down Expand Up @@ -5525,7 +5525,7 @@ describe('MatrixClient', () => {
http.when("POST", "/_matrix/media/v3/upload").respond(200, (path, content, req) => {
expect(content).toBeDefined();
// HACK: We know the mock library will return JSON
expect(req.opts.headers["Content-Type"]).toEqual("application/json");
expect(req.headers["Content-Type"]).toEqual("application/json");
//expect(req.opts.body).toEqual(data); // XXX: We can't verify that the content was uploaded correctly
return { content_uri: uri };
});
Expand Down
14 changes: 7 additions & 7 deletions test/SynapseAdminApisTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as MockHttpBackend from 'matrix-mock-request';
import HttpBackend from 'matrix-mock-request';

import {
IStorageProvider,
Expand All @@ -20,7 +20,7 @@ export function createTestSynapseAdminClient(
): {
client: SynapseAdminApis;
mxClient: MatrixClient;
http: MockHttpBackend;
http: HttpBackend;
hsUrl: string;
accessToken: string;
} {
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('SynapseAdminApis', () => {
};

http.when("GET", "/_synapse/admin/v2/users").respond(200, (path, _content, req) => {
expect(req.opts.qs).toEqual(request);
expect(req.queryParams).toEqual(request);
expect(path).toEqual(`${hsUrl}/_synapse/admin/v2/users`);
return response;
});
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('SynapseAdminApis', () => {

http.when("GET", "/_synapse/admin/v2/users").respond(200, (path, _content, req) => {
expect(path).toEqual(`${hsUrl}/_synapse/admin/v2/users`);
expect(req.opts.qs).toEqual(request);
expect(req.queryParams).toEqual(request);
return {
next_token: 'from-token',
total: 2,
Expand All @@ -269,7 +269,7 @@ describe('SynapseAdminApis', () => {

http.when("GET", "/_synapse/admin/v2/users").respond(200, (path, _content, req) => {
expect(path).toEqual(`${hsUrl}/_synapse/admin/v2/users`);
expect(req.opts.qs).toEqual({ ...request, from: 'from-token' });
expect(req.queryParams).toEqual({ ...request, from: 'from-token' });
return {
total: 2,
users: [user2],
Expand Down Expand Up @@ -319,7 +319,7 @@ describe('SynapseAdminApis', () => {
};

http.when("GET", "/_synapse/admin/v1/rooms").respond(200, (path, _content, req) => {
expect(req.opts.qs).toEqual(request);
expect(req.queryParams).toEqual(request);
expect(path).toEqual(`${hsUrl}/_synapse/admin/v1/rooms`);
return response;
});
Expand Down Expand Up @@ -358,7 +358,7 @@ describe('SynapseAdminApis', () => {
const roomId = "!room:example.org";

http.when("DELETE", "/_synapse/admin/v2/rooms").respond(200, (path, _content, req) => {
expect(JSON.parse(req.opts.body)).toMatchObject({ purge: true });
expect(JSON.parse(req.rawData)).toMatchObject({ purge: true });
expect(path).toEqual(`${hsUrl}/_synapse/admin/v2/rooms/${encodeURIComponent(roomId)}`);
return {};
});
Expand Down
6 changes: 3 additions & 3 deletions test/SynchronousMatrixClientTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as simple from "simple-mock";
import * as MockHttpBackend from 'matrix-mock-request';
import HttpBackend from 'matrix-mock-request';

import { IStorageProvider, MatrixClient, setRequestFn, SynchronousMatrixClient } from "../src";

Expand All @@ -15,8 +15,8 @@ class TestSyncMatrixClient extends SynchronousMatrixClient {
}
}

export function createSyncTestClient(storage: IStorageProvider = null): { client: TestSyncMatrixClient, http: MockHttpBackend, hsUrl: string, accessToken: string } {
const http = new MockHttpBackend();
export function createSyncTestClient(storage: IStorageProvider = null): { client: TestSyncMatrixClient, http: HttpBackend, hsUrl: string, accessToken: string } {
const http = new HttpBackend();
const hsUrl = "https://localhost";
const accessToken = "s3cret";
const client = new MatrixClient(hsUrl, accessToken, storage);
Expand Down
Loading