Skip to content

Commit 34756a0

Browse files
committed
Use body bytes instead of reforming a stream buffer again
See #68 (comment)
1 parent d63fb7f commit 34756a0

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

tests/msc2716_test.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ func TestBackfillingHistory(t *testing.T) {
115115
// Status
116116
200,
117117
)
118-
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
119-
nextChunkID := getNextChunkIdFromBatchSendResponse(t, backfillRes)
118+
backfillResBody := client.ParseJSON(t, backfillRes)
119+
historicalEventIDs := getEventsFromBatchSendResponseBody(t, backfillResBody)
120+
nextChunkID := getNextChunkIdFromBatchSendResponseBody(t, backfillResBody)
120121

121122
// Insert another older chunk of backfilled history from the same user.
122123
// Make sure the meta data and joins still work on the subsequent chunk
@@ -132,7 +133,8 @@ func TestBackfillingHistory(t *testing.T) {
132133
// Status
133134
200,
134135
)
135-
historicalEventIDs2 := getEventsFromBatchSendResponse(t, backfillRes2)
136+
backfillResBody2 := client.ParseJSON(t, backfillRes2)
137+
historicalEventIDs2 := getEventsFromBatchSendResponseBody(t, backfillResBody2)
136138

137139
var expectedEventIDOrder []string
138140
expectedEventIDOrder = append(expectedEventIDOrder, eventIDsBefore...)
@@ -210,7 +212,8 @@ func TestBackfillingHistory(t *testing.T) {
210212
// Status
211213
200,
212214
)
213-
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
215+
backfillResBody := client.ParseJSON(t, backfillRes)
216+
historicalEventIDs := getEventsFromBatchSendResponseBody(t, backfillResBody)
214217

215218
messagesRes := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
216219
"dir": []string{"b"},
@@ -253,7 +256,8 @@ func TestBackfillingHistory(t *testing.T) {
253256
// Status
254257
200,
255258
)
256-
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
259+
backfillResBody := client.ParseJSON(t, backfillRes)
260+
historicalEventIDs := getEventsFromBatchSendResponseBody(t, backfillResBody)
257261
backfilledEventId := historicalEventIDs[0]
258262

259263
// This is just a dummy event we search for after the backfilledEventId
@@ -353,7 +357,8 @@ func TestBackfillingHistory(t *testing.T) {
353357
// Status
354358
200,
355359
)
356-
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
360+
backfillResBody := client.ParseJSON(t, backfillRes)
361+
historicalEventIDs := getEventsFromBatchSendResponseBody(t, backfillResBody)
357362

358363
// Join the room from a remote homeserver after the backfilled messages were sent
359364
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
@@ -411,7 +416,8 @@ func TestBackfillingHistory(t *testing.T) {
411416
// Status
412417
200,
413418
)
414-
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
419+
backfillResBody := client.ParseJSON(t, backfillRes)
420+
historicalEventIDs := getEventsFromBatchSendResponseBody(t, backfillResBody)
415421

416422
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
417423
"dir": []string{"b"},
@@ -467,7 +473,8 @@ func TestBackfillingHistory(t *testing.T) {
467473
// Status
468474
200,
469475
)
470-
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
476+
backfillResBody := client.ParseJSON(t, backfillRes)
477+
historicalEventIDs := getEventsFromBatchSendResponseBody(t, backfillResBody)
471478

472479
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
473480
"dir": []string{"b"},
@@ -637,21 +644,13 @@ func batchSendHistoricalMessages(
637644
return res
638645
}
639646

640-
func getEventsFromBatchSendResponse(t *testing.T, res *http.Response) (eventIDs []string) {
641-
body := client.ParseJSON(t, res)
642-
// Since the original body can only be read once, create a new one from the body bytes we just read
643-
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
644-
647+
func getEventsFromBatchSendResponseBody(t *testing.T, body []byte) (eventIDs []string) {
645648
eventIDs = client.GetJSONFieldStringArray(t, body, "events")
646649

647650
return eventIDs
648651
}
649652

650-
func getNextChunkIdFromBatchSendResponse(t *testing.T, res *http.Response) (nextChunkID string) {
651-
body := client.ParseJSON(t, res)
652-
// Since the original body can only be read once, create a new one from the body bytes we just read
653-
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
654-
653+
func getNextChunkIdFromBatchSendResponseBody(t *testing.T, body []byte) (nextChunkID string) {
655654
nextChunkID = client.GetJSONFieldStr(t, body, "next_chunk_id")
656655

657656
return nextChunkID

0 commit comments

Comments
 (0)