Skip to content

Commit 007877f

Browse files
committed
Refactor to use functional client functions
See 3acc9bb
1 parent 3890321 commit 007877f

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

tests/msc2716_test.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestBackfillingHistory(t *testing.T) {
9494
eventsAfter := createMessagesInRoom(t, alice, roomID, 2)
9595

9696
// Register and join the virtual user
97-
ensureRegistered(t, as, virtualUserLocalpart)
97+
ensureVirtualUserRegistered(t, as, virtualUserLocalpart)
9898

9999
// TODO: Try adding avatar and displayName and see if historical messages get this info
100100

@@ -138,19 +138,19 @@ func TestBackfillingHistory(t *testing.T) {
138138
// Order events from newest to oldest
139139
expectedMessageOrder = reversed(expectedMessageOrder)
140140

141-
messagesRes := alice.MustDoRaw(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, nil, "application/json", url.Values{
141+
messagesRes := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
142142
"dir": []string{"b"},
143143
"limit": []string{"100"},
144-
})
144+
}))
145145
messsageResBody := client.ParseJSON(t, messagesRes)
146146
eventIDsFromResponse := getEventIDsFromResponseBody(t, messsageResBody)
147147
// Since the original body can only be read once, create a new one from the body bytes we just read
148148
messagesRes.Body = ioutil.NopCloser(bytes.NewBuffer(messsageResBody))
149149

150150
// TODO: Remove, the context request is just for TARDIS visualizations
151-
contextRes := alice.MustDoRaw(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "context", eventsAfter[len(eventsAfter)-1]}, nil, "application/json", url.Values{
151+
contextRes := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "context", eventsAfter[len(eventsAfter)-1]}, client.WithContentType("application/json"), client.WithQueries(url.Values{
152152
"limit": []string{"100"},
153-
})
153+
}))
154154
contextResBody := client.ParseJSON(t, contextRes)
155155
logrus.WithFields(logrus.Fields{
156156
"contextResBody": string(contextResBody),
@@ -320,7 +320,7 @@ func TestBackfillingHistory(t *testing.T) {
320320
createMessagesInRoom(t, alice, roomID, 3)
321321

322322
// Register and join the virtual user
323-
ensureRegistered(t, as, virtualUserLocalpart)
323+
ensureVirtualUserRegistered(t, as, virtualUserLocalpart)
324324

325325
backfillRes := backfillBatchHistoricalMessages(
326326
t,
@@ -339,10 +339,10 @@ func TestBackfillingHistory(t *testing.T) {
339339
// Join the room from a remote homeserver after the backfilled messages were sent
340340
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
341341

342-
messagesRes := remoteCharlie.MustDoRaw(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, nil, "application/json", url.Values{
342+
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
343343
"dir": []string{"b"},
344344
"limit": []string{"100"},
345-
})
345+
}))
346346

347347
must.MatchResponse(t, messagesRes, match.HTTPResponse{
348348
JSON: []match.JSON{
@@ -370,14 +370,14 @@ func TestBackfillingHistory(t *testing.T) {
370370
createMessagesInRoom(t, alice, roomID, 10)
371371

372372
// Mimic scrollback just through the latest messages
373-
remoteCharlie.MustDoRaw(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, nil, "application/json", url.Values{
373+
remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
374374
"dir": []string{"b"},
375375
// Limited so we can only see a few of the latest messages
376376
"limit": []string{"5"},
377-
})
377+
}))
378378

379379
// Register and join the virtual user
380-
ensureRegistered(t, as, virtualUserLocalpart)
380+
ensureVirtualUserRegistered(t, as, virtualUserLocalpart)
381381

382382
backfillRes := backfillBatchHistoricalMessages(
383383
t,
@@ -393,10 +393,10 @@ func TestBackfillingHistory(t *testing.T) {
393393
)
394394
_, historicalEvents := getEventsFromBatchSendResponse(t, backfillRes)
395395

396-
messagesRes := remoteCharlie.MustDoRaw(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, nil, "application/json", url.Values{
396+
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
397397
"dir": []string{"b"},
398398
"limit": []string{"100"},
399-
})
399+
}))
400400

401401
must.MatchResponse(t, messagesRes, match.HTTPResponse{
402402
JSON: []match.JSON{
@@ -424,14 +424,14 @@ func TestBackfillingHistory(t *testing.T) {
424424
createMessagesInRoom(t, alice, roomID, 3)
425425

426426
// Register and join the virtual user
427-
ensureRegistered(t, as, virtualUserLocalpart)
427+
ensureVirtualUserRegistered(t, as, virtualUserLocalpart)
428428

429429
// Mimic scrollback to all of the messages
430430
// scrollbackMessagesRes
431-
remoteCharlie.MustDoRaw(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, nil, "application/json", url.Values{
431+
remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
432432
"dir": []string{"b"},
433433
"limit": []string{"100"},
434-
})
434+
}))
435435

436436
// Historical messages are inserted where we have already scrolled back to
437437
backfillRes := backfillBatchHistoricalMessages(
@@ -448,10 +448,10 @@ func TestBackfillingHistory(t *testing.T) {
448448
)
449449
_, historicalEvents := getEventsFromBatchSendResponse(t, backfillRes)
450450

451-
messagesRes := remoteCharlie.MustDoRaw(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, nil, "application/json", url.Values{
451+
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
452452
"dir": []string{"b"},
453453
"limit": []string{"100"},
454-
})
454+
}))
455455

456456
must.MatchResponse(t, messagesRes, match.HTTPResponse{
457457
JSON: []match.JSON{
@@ -518,36 +518,31 @@ func sendEvent(t *testing.T, c *client.CSAPI, virtualUserID string, roomID strin
518518
t.Fatalf("msc2716.sendEvent failed to marshal JSON body: %s", err)
519519
}
520520

521-
res := c.MustDoRaw(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "send", e.Type, txnPrefix + strconv.Itoa(txnID)}, b, "application/json", query)
521+
res := c.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "send", e.Type, txnPrefix + strconv.Itoa(txnID)}, client.WithRawBody(b), client.WithContentType("application/json"), client.WithQueries(query))
522522
body := client.ParseJSON(t, res)
523523
eventID := client.GetJSONFieldStr(t, body, "event_id")
524524

525525
return eventID
526526
}
527527

528-
// ensureRegistered makes sure the user is registered for the homeserver regardless
528+
// ensureVirtualUserRegistered makes sure the user is registered for the homeserver regardless
529529
// if they are already registered or not. If unable to register, fails the test
530-
func ensureRegistered(t *testing.T, c *client.CSAPI, virtualUserLocalpart string) {
530+
func ensureVirtualUserRegistered(t *testing.T, c *client.CSAPI, virtualUserLocalpart string) {
531531
// b, err := json.Marshal(map[string]interface{}{
532532
// "username": virtualUserLocalpart,
533533
// })
534534
// if err != nil {
535-
// t.Fatalf("msc2716.ensureRegistered failed to marshal JSON body: %s", err)
535+
// t.Fatalf("msc2716.ensureVirtualUserRegistered failed to marshal JSON body: %s", err)
536536
// }
537537

538-
res, err := c.DoWithAuthRaw(
538+
res := c.DoFunc(
539539
t,
540540
"POST",
541541
[]string{"_matrix", "client", "r0", "register"},
542-
json.RawMessage(fmt.Sprintf(`{ "type": "m.login.application_service", "username": "%s" }`, virtualUserLocalpart)),
543-
"application/json",
544-
url.Values{},
542+
client.WithRawBody(json.RawMessage(fmt.Sprintf(`{ "type": "m.login.application_service", "username": "%s" }`, virtualUserLocalpart))),
543+
client.WithContentType("application/json"),
545544
)
546545

547-
if err != nil {
548-
t.Error(err)
549-
}
550-
551546
if res.StatusCode == 200 {
552547
return
553548
}
@@ -559,7 +554,7 @@ func ensureRegistered(t *testing.T, c *client.CSAPI, virtualUserLocalpart string
559554
return
560555
} else {
561556
errorMessage := client.GetJSONFieldStr(t, body, "error")
562-
t.Fatalf("msc2716.ensureRegistered failed to register: (%s) %s", errcode, errorMessage)
557+
t.Fatalf("msc2716.ensureVirtualUserRegistered failed to register: (%s) %s", errcode, errorMessage)
563558
}
564559
}
565560

@@ -640,15 +635,24 @@ func backfillBatchHistoricalMessages(
640635
t.Fatalf("msc2716.backfillBatchHistoricalMessages failed to marshal JSON body: %s", err)
641636
}
642637

643-
res = c.MustDoWithStatusRaw(
638+
res = c.DoFunc(
644639
t,
645640
"POST",
646641
[]string{"_matrix", "client", "unstable", "org.matrix.msc2716", "rooms", roomID, "batch_send"},
647-
b,
648-
"application/json",
649-
query,
650-
status,
642+
client.WithRawBody(b),
643+
client.WithContentType("application/json"),
644+
client.WithQueries(query),
651645
)
646+
// Save the body so we can re-create after the buffer closes
647+
body := client.ParseJSON(t, res)
648+
// Since the original body can only be read once, create a new one from the body bytes we just read
649+
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
650+
must.MatchResponse(t, res, match.HTTPResponse{
651+
StatusCode: status,
652+
})
653+
// After using up the body in the must.MatchResponse above, create the body again
654+
// Since the original body can only be read once, create a new one from the body bytes we just read
655+
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
652656

653657
chunkCount++
654658

0 commit comments

Comments
 (0)