-
-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out a seprate OutgoingRequestProcessor
- Loading branch information
Showing
4 changed files
with
266 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import MockHttpBackend from "matrix-mock-request"; | ||
import { Mocked } from "jest-mock"; | ||
import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js"; | ||
import { | ||
KeysBackupRequest, | ||
KeysClaimRequest, | ||
KeysQueryRequest, | ||
KeysUploadRequest, | ||
SignatureUploadRequest, | ||
} from "@matrix-org/matrix-sdk-crypto-js"; | ||
|
||
import { TypedEventEmitter } from "../../../src/models/typed-event-emitter"; | ||
import { HttpApiEvent, HttpApiEventHandlerMap, MatrixHttpApi } from "../../../src"; | ||
import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor"; | ||
|
||
describe("OutgoingRequestProcessor", () => { | ||
/** the OutgoingRequestProcessor implementation under test */ | ||
let processor: OutgoingRequestProcessor; | ||
|
||
/** A mock http backend which processor is connected to */ | ||
let httpBackend: MockHttpBackend; | ||
|
||
/** a mocked-up OlmMachine which processor is connected to */ | ||
let olmMachine: Mocked<RustSdkCryptoJs.OlmMachine>; | ||
|
||
/** wait for a call to olmMachine.markRequestAsSent */ | ||
function awaitCallToMarkAsSent(): Promise<void> { | ||
return new Promise((resolve, _reject) => { | ||
olmMachine.markRequestAsSent.mockImplementationOnce(async () => { | ||
console.log("received call to markAsSent"); | ||
resolve(undefined); | ||
}); | ||
}); | ||
} | ||
|
||
beforeEach(async () => { | ||
httpBackend = new MockHttpBackend(); | ||
|
||
const dummyEventEmitter = new TypedEventEmitter<HttpApiEvent, HttpApiEventHandlerMap>(); | ||
const httpApi = new MatrixHttpApi(dummyEventEmitter, { | ||
baseUrl: "https://example.com", | ||
prefix: "/_matrix", | ||
fetchFn: httpBackend.fetchFn as typeof global.fetch, | ||
onlyData: true, | ||
}); | ||
|
||
olmMachine = { | ||
markRequestAsSent: jest.fn(), | ||
} as unknown as Mocked<RustSdkCryptoJs.OlmMachine>; | ||
|
||
processor = new OutgoingRequestProcessor(olmMachine, httpApi); | ||
}); | ||
|
||
/* simple requests that map directly to the request body */ | ||
const tests: Array<[any, "POST" | "PUT", string]> = [ | ||
[KeysUploadRequest, "POST", "https://example.com/_matrix/client/v3/keys/upload"], | ||
[KeysQueryRequest, "POST", "https://example.com/_matrix/client/v3/keys/query"], | ||
[KeysClaimRequest, "POST", "https://example.com/_matrix/client/v3/keys/claim"], | ||
[SignatureUploadRequest, "POST", "https://example.com/_matrix/client/v3/keys/signatures/upload"], | ||
[KeysBackupRequest, "PUT", "https://example.com/_matrix/client/v3/room_keys/keys"], | ||
]; | ||
|
||
for (const [RequestClass, expectedMethod, expectedPath] of tests) { | ||
it(`should handle ${RequestClass.name}s`, async () => { | ||
const testBody = '{ "foo": "bar" }'; | ||
const outgoingRequest = new RequestClass("1234", testBody); | ||
|
||
const reqProm = processor.makeOutgoingRequest(outgoingRequest); | ||
|
||
const testResponse = '{ "result": 1 }'; | ||
httpBackend | ||
.when(expectedMethod, "/_matrix") | ||
.check((req) => { | ||
expect(req.path).toEqual(expectedPath); | ||
expect(req.rawData).toEqual(testBody); | ||
expect(req.headers["Accept"]).toEqual("application/json"); | ||
expect(req.headers["Content-Type"]).toEqual("application/json"); | ||
}) | ||
.respond(200, testResponse, true); | ||
|
||
const markSentCallPromise = awaitCallToMarkAsSent(); | ||
await httpBackend.flushAllExpected(); | ||
|
||
await Promise.all([reqProm, markSentCallPromise]); | ||
expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("1234", outgoingRequest.type, testResponse); | ||
httpBackend.verifyNoOutstandingRequests(); | ||
}); | ||
} | ||
|
||
it("does not explode with unknown requests", async () => { | ||
const outgoingRequest = { id: "5678", type: 987 }; | ||
const markSentCallPromise = awaitCallToMarkAsSent(); | ||
await Promise.all([processor.makeOutgoingRequest(outgoingRequest), markSentCallPromise]); | ||
expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("5678", 987, ""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.