@@ -696,6 +696,58 @@ describe("RustCrypto", () => {
696
696
await awaitCallToMakeOutgoingRequest ( ) ;
697
697
expect ( olmMachine . outgoingRequests ) . toHaveBeenCalledTimes ( 2 ) ;
698
698
} ) ;
699
+
700
+ it ( "should encode outgoing requests properly" , async ( ) => {
701
+ // we need a real OlmMachine, so replace the one created by beforeEach
702
+ rustCrypto = await makeTestRustCrypto ( ) ;
703
+ const olmMachine : OlmMachine = rustCrypto [ "olmMachine" ] ;
704
+
705
+ const outgoingRequestProcessor = { } as unknown as OutgoingRequestProcessor ;
706
+ rustCrypto [ "outgoingRequestProcessor" ] = outgoingRequestProcessor ;
707
+ const outgoingRequestsManager = new OutgoingRequestsManager ( logger , olmMachine , outgoingRequestProcessor ) ;
708
+ rustCrypto [ "outgoingRequestsManager" ] = outgoingRequestsManager ;
709
+
710
+ // The second time we do a /keys/upload, the `device_keys` property
711
+ // should be absent from the request body
712
+ // cf. https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/issues/57
713
+ //
714
+ // On the first upload, we pretend that there are no OTKs, so it will
715
+ // try to upload more keys
716
+ let keysUploadCount = 0 ;
717
+ let deviceKeys : object ;
718
+ let deviceKeysAbsent = false ;
719
+ outgoingRequestProcessor . makeOutgoingRequest = jest . fn ( async ( request , uiaCallback ?) => {
720
+ let resp : any = { } ;
721
+ if ( request instanceof RustSdkCryptoJs . KeysUploadRequest ) {
722
+ if ( keysUploadCount == 0 ) {
723
+ deviceKeys = JSON . parse ( request . body ) . device_keys ;
724
+ resp = { one_time_key_counts : { signed_curve25519 : 0 } } ;
725
+ } else {
726
+ deviceKeysAbsent = ! ( "device_keys" in JSON . parse ( request . body ) ) ;
727
+ resp = { one_time_key_counts : { signed_curve25519 : 50 } } ;
728
+ }
729
+ keysUploadCount ++ ;
730
+ } else if ( request instanceof RustSdkCryptoJs . KeysQueryRequest ) {
731
+ resp = {
732
+ device_keys : {
733
+ [ TEST_USER ] : {
734
+ [ TEST_DEVICE_ID ] : deviceKeys ,
735
+ } ,
736
+ } ,
737
+ } ;
738
+ } else if ( request instanceof RustSdkCryptoJs . UploadSigningKeysRequest ) {
739
+ // SigningKeysUploadRequest does not implement OutgoingRequest and does not need to be marked as sent.
740
+ return ;
741
+ }
742
+ if ( request . id ) {
743
+ olmMachine . markRequestAsSent ( request . id , request . type , JSON . stringify ( resp ) ) ;
744
+ }
745
+ } ) ;
746
+ await outgoingRequestsManager . doProcessOutgoingRequests ( ) ;
747
+ await outgoingRequestsManager . doProcessOutgoingRequests ( ) ;
748
+
749
+ expect ( deviceKeysAbsent ) . toBe ( true ) ;
750
+ } ) ;
699
751
} ) ;
700
752
701
753
describe ( ".getEventEncryptionInfo" , ( ) => {
0 commit comments