@@ -115,7 +115,7 @@ func (c *CSAPI) UploadContent(t *testing.T, fileBody []byte, fileName string, co
115115func  (c  * CSAPI ) DownloadContent (t  * testing.T , mxcUri  string ) ([]byte , string ) {
116116	t .Helper ()
117117	origin , mediaId  :=  SplitMxc (mxcUri )
118- 	res  :=  c .MustDo (t , "GET" , []string {"_matrix" , "media" , "v3" , "download" , origin , mediaId },  struct {}{ })
118+ 	res  :=  c .MustDoFunc (t , "GET" , []string {"_matrix" , "media" , "v3" , "download" , origin , mediaId })
119119	contentType  :=  res .Header .Get ("Content-Type" )
120120	b , err  :=  ioutil .ReadAll (res .Body )
121121	if  err  !=  nil  {
@@ -127,7 +127,7 @@ func (c *CSAPI) DownloadContent(t *testing.T, mxcUri string) ([]byte, string) {
127127// CreateRoom creates a room with an optional HTTP request body. Fails the test on error. Returns the room ID. 
128128func  (c  * CSAPI ) CreateRoom (t  * testing.T , creationContent  interface {}) string  {
129129	t .Helper ()
130- 	res  :=  c .MustDo (t , "POST" , []string {"_matrix" , "client" , "v3" , "createRoom" }, creationContent )
130+ 	res  :=  c .MustDoFunc (t , "POST" , []string {"_matrix" , "client" , "v3" , "createRoom" }, WithJSONBody ( t ,  creationContent ) )
131131	body  :=  ParseJSON (t , res )
132132	return  GetJSONFieldStr (t , body , "room_id" )
133133}
@@ -166,7 +166,7 @@ func (c *CSAPI) InviteRoom(t *testing.T, roomID string, userID string) {
166166	body  :=  map [string ]interface {}{
167167		"user_id" : userID ,
168168	}
169- 	c .MustDo (t , "POST" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "invite" }, body )
169+ 	c .MustDoFunc (t , "POST" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "invite" }, WithJSONBody ( t ,  body ) )
170170}
171171
172172func  (c  * CSAPI ) GetGlobalAccountData (t  * testing.T , eventType  string ) * http.Response  {
@@ -186,7 +186,7 @@ func (c *CSAPI) SendEventSynced(t *testing.T, roomID string, e b.Event) string {
186186	if  e .StateKey  !=  nil  {
187187		paths  =  []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "state" , e .Type , * e .StateKey }
188188	}
189- 	res  :=  c .MustDo (t , "PUT" , paths , e .Content )
189+ 	res  :=  c .MustDoFunc (t , "PUT" , paths , WithJSONBody ( t ,  e .Content ) )
190190	body  :=  ParseJSON (t , res )
191191	eventID  :=  GetJSONFieldStr (t , body , "event_id" )
192192	t .Logf ("SendEventSynced waiting for event ID %s" , eventID )
@@ -333,7 +333,7 @@ func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID,
333333		"username" : localpart ,
334334		"password" : password ,
335335	}
336- 	res  :=  c .MustDo (t , "POST" , []string {"_matrix" , "client" , "v3" , "register" }, reqBody )
336+ 	res  :=  c .MustDoFunc (t , "POST" , []string {"_matrix" , "client" , "v3" , "register" }, WithJSONBody ( t ,  reqBody ) )
337337
338338	body , err  :=  ioutil .ReadAll (res .Body )
339339	if  err  !=  nil  {
@@ -411,21 +411,6 @@ func (c *CSAPI) GetDefaultRoomVersion(t *testing.T) gomatrixserverlib.RoomVersio
411411	return  gomatrixserverlib .RoomVersion (defaultVersion .Str )
412412}
413413
414- // MustDo will do the HTTP request and fail the test if the response is not 2xx 
415- // 
416- // Deprecated: Prefer MustDoFunc. MustDo is the older format which doesn't allow for vargs 
417- // and will be removed in the future. MustDoFunc also logs HTTP response bodies on error. 
418- func  (c  * CSAPI ) MustDo (t  * testing.T , method  string , paths  []string , jsonBody  interface {}) * http.Response  {
419- 	t .Helper ()
420- 	res  :=  c .DoFunc (t , method , paths , WithJSONBody (t , jsonBody ))
421- 	if  res .StatusCode  <  200  ||  res .StatusCode  >=  300  {
422- 		defer  res .Body .Close ()
423- 		body , _  :=  ioutil .ReadAll (res .Body )
424- 		t .Fatalf ("CSAPI.MustDo %s %s returned HTTP %d : %s" , method , res .Request .URL .String (), res .StatusCode , string (body ))
425- 	}
426- 	return  res 
427- }
428- 
429414// WithRawBody sets the HTTP request body to `body` 
430415func  WithRawBody (body  []byte ) RequestOpt  {
431416	return  func (req  * http.Request ) {
@@ -482,7 +467,7 @@ func (c *CSAPI) MustDoFunc(t *testing.T, method string, paths []string, opts ...
482467	if  res .StatusCode  <  200  ||  res .StatusCode  >=  300  {
483468		defer  res .Body .Close ()
484469		body , _  :=  ioutil .ReadAll (res .Body )
485- 		t .Fatalf ("CSAPI.MustDoFunc response return  non-2xx code: %s - body: %s" , res .Status , string (body ))
470+ 		t .Fatalf ("CSAPI.MustDoFunc %s %s returned  non-2xx code: %s - body: %s" ,  method ,  res . Request . URL . String () , res .Status , string (body ))
486471	}
487472	return  res 
488473}
@@ -860,3 +845,48 @@ func SplitMxc(mxcUri string) (string, string) {
860845
861846	return  origin , mediaId 
862847}
848+ 
849+ // SendToDeviceMessages sends to-device messages over /sendToDevice/. 
850+ // 
851+ // The messages parameter is nested as follows: 
852+ // user_id -> device_id -> content (map[string]interface{}) 
853+ func  (c  * CSAPI ) SendToDeviceMessages (t  * testing.T , evType  string , messages  map [string ]map [string ]map [string ]interface {}) {
854+ 	t .Helper ()
855+ 	c .txnID ++ 
856+ 	c .MustDoFunc (
857+ 		t ,
858+ 		"PUT" ,
859+ 		[]string {"_matrix" , "client" , "v3" , "sendToDevice" , evType , strconv .Itoa (c .txnID )},
860+ 		WithJSONBody (
861+ 			t ,
862+ 			map [string ]map [string ]map [string ]map [string ]interface {}{
863+ 				"messages" : messages ,
864+ 			},
865+ 		),
866+ 	)
867+ }
868+ 
869+ // Check that sync has received a to-device message, 
870+ // with optional user filtering. 
871+ // 
872+ // If fromUser == "", all messages will be passed through to the check function. 
873+ // `check` will be called for all messages that have passed the filter. 
874+ // 
875+ // `check` gets passed the full event, including sender and type. 
876+ func  SyncToDeviceHas (fromUser  string , check  func (gjson.Result ) bool ) SyncCheckOpt  {
877+ 	return  func (clientUserID  string , topLevelSyncJSON  gjson.Result ) error  {
878+ 		err  :=  loopArray (
879+ 			topLevelSyncJSON , "to_device.events" , func (result  gjson.Result ) bool  {
880+ 				if  fromUser  !=  ""  &&  result .Get ("sender" ).Str  !=  fromUser  {
881+ 					return  false 
882+ 				} else  {
883+ 					return  check (result )
884+ 				}
885+ 			},
886+ 		)
887+ 		if  err  ==  nil  {
888+ 			return  nil 
889+ 		}
890+ 		return  fmt .Errorf ("SyncToDeviceHas(%v): %s" , fromUser , err )
891+ 	}
892+ }
0 commit comments