-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathclient.go
430 lines (392 loc) · 13.4 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package client
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"testing"
"time"
"github.com/tidwall/gjson"
"github.com/matrix-org/complement/internal/b"
)
// RequestOpt is a functional option which will modify an outgoing HTTP request.
// See functions starting with `With...` in this package for more info.
type RequestOpt func(req *http.Request)
type CSAPI struct {
UserID string
AccessToken string
BaseURL string
Client *http.Client
// how long are we willing to wait for SyncUntil.... calls
SyncUntilTimeout time.Duration
// True to enable verbose logging
Debug bool
txnID int
}
// UploadContent uploads the provided content with an optional file name. Fails the test on error. Returns the MXC URI.
func (c *CSAPI) UploadContent(t *testing.T, fileBody []byte, fileName string, contentType string) string {
t.Helper()
query := url.Values{}
if fileName != "" {
query.Set("filename", fileName)
}
res := c.MustDoFunc(
t, "POST", []string{"_matrix", "media", "r0", "upload"},
WithRawBody(fileBody), WithContentType(contentType), WithQueries(query),
)
body := ParseJSON(t, res)
return GetJSONFieldStr(t, body, "content_uri")
}
// DownloadContent downloads media from the server, returning the raw bytes and the Content-Type. Fails the test on error.
func (c *CSAPI) DownloadContent(t *testing.T, mxcUri string) ([]byte, string) {
t.Helper()
mxcParts := strings.Split(strings.TrimPrefix(mxcUri, "mxc://"), "/")
origin := mxcParts[0]
mediaId := strings.Join(mxcParts[1:], "/")
res := c.MustDo(t, "GET", []string{"_matrix", "media", "r0", "download", origin, mediaId}, struct{}{})
contentType := res.Header.Get("Content-Type")
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
return b, contentType
}
// CreateRoom creates a room with an optional HTTP request body. Fails the test on error. Returns the room ID.
func (c *CSAPI) CreateRoom(t *testing.T, creationContent interface{}) string {
t.Helper()
res := c.MustDo(t, "POST", []string{"_matrix", "client", "r0", "createRoom"}, creationContent)
body := ParseJSON(t, res)
return GetJSONFieldStr(t, body, "room_id")
}
// JoinRoom joins the room ID or alias given, else fails the test. Returns the room ID.
func (c *CSAPI) JoinRoom(t *testing.T, roomIDOrAlias string, serverNames []string) string {
t.Helper()
// construct URL query parameters
query := make(url.Values, len(serverNames))
for _, serverName := range serverNames {
query.Add("server_name", serverName)
}
// join the room
res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "r0", "join", roomIDOrAlias}, WithQueries(query))
// return the room ID if we joined with it
if roomIDOrAlias[0] == '!' {
return roomIDOrAlias
}
// otherwise we should be told the room ID if we joined via an alias
body := ParseJSON(t, res)
return GetJSONFieldStr(t, body, "room_id")
}
// LeaveRoom joins the room ID, else fails the test.
func (c *CSAPI) LeaveRoom(t *testing.T, roomID string) {
t.Helper()
// leave the room
c.MustDoFunc(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "leave"})
}
// InviteRoom invites userID to the room ID, else fails the test.
func (c *CSAPI) InviteRoom(t *testing.T, roomID string, userID string) {
t.Helper()
// Invite the user to the room
body := map[string]interface{}{
"user_id": userID,
}
c.MustDo(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "invite"}, body)
}
// SendEventSynced sends `e` into the room and waits for its event ID to come down /sync.
// Returns the event ID of the sent event.
func (c *CSAPI) SendEventSynced(t *testing.T, roomID string, e b.Event) string {
t.Helper()
c.txnID++
paths := []string{"_matrix", "client", "r0", "rooms", roomID, "send", e.Type, strconv.Itoa(c.txnID)}
if e.StateKey != nil {
paths = []string{"_matrix", "client", "r0", "rooms", roomID, "state", e.Type, *e.StateKey}
}
res := c.MustDo(t, "PUT", paths, e.Content)
body := ParseJSON(t, res)
eventID := GetJSONFieldStr(t, body, "event_id")
t.Logf("SendEventSynced waiting for event ID %s", eventID)
c.SyncUntilTimelineHas(t, roomID, func(r gjson.Result) bool {
return r.Get("event_id").Str == eventID
})
return eventID
}
// SyncUntilTimelineHas blocks and continually calls /sync until the `check` function returns true.
// If the `check` function fails the test, the failing event will be automatically logged.
// Will time out after CSAPI.SyncUntilTimeout.
func (c *CSAPI) SyncUntilTimelineHas(t *testing.T, roomID string, check func(gjson.Result) bool) {
t.Helper()
c.SyncUntil(t, "", "", "rooms.join."+GjsonEscape(roomID)+".timeline.events", check)
}
// SyncUntil blocks and continually calls /sync until the `check` function returns true.
// If the `check` function fails the test, the failing event will be automatically logged.
// Will time out after CSAPI.SyncUntilTimeout.
func (c *CSAPI) SyncUntil(t *testing.T, since, filter, key string, check func(gjson.Result) bool) {
t.Helper()
start := time.Now()
checkCounter := 0
// Print failing events in a defer() so we handle t.Fatalf in the same way as t.Errorf
var wasFailed = t.Failed()
var lastEvent *gjson.Result
timedOut := false
defer func() {
if !wasFailed && t.Failed() {
raw := ""
if lastEvent != nil {
raw = lastEvent.Raw
}
if !timedOut {
t.Logf("SyncUntil: failing event %s", raw)
}
}
}()
for {
if time.Since(start) > c.SyncUntilTimeout {
timedOut = true
t.Fatalf("SyncUntil: timed out. Called check function %d times", checkCounter)
}
query := url.Values{
"timeout": []string{"1000"},
}
if since != "" {
query["since"] = []string{since}
}
if filter != "" {
query["filter"] = []string{filter}
}
res := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "sync"}, WithQueries(query))
body := ParseJSON(t, res)
since = GetJSONFieldStr(t, body, "next_batch")
keyRes := gjson.GetBytes(body, key)
if keyRes.IsArray() {
events := keyRes.Array()
for i, ev := range events {
lastEvent = &events[i]
if check(ev) {
return
}
wasFailed = t.Failed()
checkCounter++
}
}
}
}
//RegisterUser will register the user with given parameters and
// return user ID & access token, and fail the test on network error
func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID, accessToken string) {
t.Helper()
reqBody := map[string]interface{}{
"auth": map[string]string{
"type": "m.login.dummy",
},
"username": localpart,
"password": password,
}
res := c.MustDo(t, "POST", []string{"_matrix", "client", "r0", "register"}, reqBody)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("unable to read response body: %v", err)
}
userID = gjson.GetBytes(body, "user_id").Str
accessToken = gjson.GetBytes(body, "access_token").Str
return userID, accessToken
}
// MustDo will do the HTTP request and fail the test if the response is not 2xx
func (c *CSAPI) MustDo(t *testing.T, method string, paths []string, jsonBody interface{}) *http.Response {
t.Helper()
res := c.DoFunc(t, method, paths, WithJSONBody(t, jsonBody))
if res.StatusCode < 200 || res.StatusCode >= 300 {
t.Fatalf("CSAPI.MustDo %s %s returned HTTP %d", method, res.Request.URL.String(), res.StatusCode)
}
return res
}
// WithRawBody sets the HTTP request body to `body`
func WithRawBody(body []byte) RequestOpt {
return func(req *http.Request) {
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
// we need to manually set this because we don't set the body
// in http.NewRequest due to using functional options, and only in NewRequest
// does the stdlib set this for us.
req.ContentLength = int64(len(body))
}
}
// WithContentType sets the HTTP request Content-Type header to `cType`
func WithContentType(cType string) RequestOpt {
return func(req *http.Request) {
req.Header.Set("Content-Type", cType)
}
}
// WithJSONBody sets the HTTP request body to the JSON serialised form of `obj`
func WithJSONBody(t *testing.T, obj interface{}) RequestOpt {
return func(req *http.Request) {
t.Helper()
b, err := json.Marshal(obj)
if err != nil {
t.Fatalf("CSAPI.Do failed to marshal JSON body: %s", err)
}
WithRawBody(b)(req)
}
}
// WithQueries sets the query parameters on the request.
// This function should not be used to set an "access_token" parameter for Matrix authentication.
// Instead, set CSAPI.AccessToken.
func WithQueries(q url.Values) RequestOpt {
return func(req *http.Request) {
req.URL.RawQuery = q.Encode()
}
}
// MustDoFunc is the same as DoFunc but fails the test if the returned HTTP response code is not 2xx.
func (c *CSAPI) MustDoFunc(t *testing.T, method string, paths []string, opts ...RequestOpt) *http.Response {
t.Helper()
res := c.DoFunc(t, method, paths, opts...)
if res.StatusCode < 200 || res.StatusCode >= 300 {
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
t.Fatalf("CSAPI.MustDoFunc response return non-2xx code: %s - body: %s", res.Status, string(body))
}
return res
}
// DoFunc performs an arbitrary HTTP request to the server. This function supports RequestOpts to set
// extra information on the request such as an HTTP request body, query parameters and content-type.
// See all functions in this package starting with `With...`.
//
// Fails the test if an HTTP request could not be made or if there was a network error talking to the
// server. To do assertions on the HTTP response, see the `must` package. For example:
// must.MatchResponse(t, res, match.HTTPResponse{
// StatusCode: 400,
// JSON: []match.JSON{
// match.JSONKeyEqual("errcode", "M_INVALID_USERNAME"),
// },
// })
func (c *CSAPI) DoFunc(t *testing.T, method string, paths []string, opts ...RequestOpt) *http.Response {
t.Helper()
for i := range paths {
paths[i] = url.PathEscape(paths[i])
}
reqURL := c.BaseURL + "/" + strings.Join(paths, "/")
req, err := http.NewRequest(method, reqURL, nil)
if err != nil {
t.Fatalf("CSAPI.DoFunc failed to create http.NewRequest: %s", err)
}
// set defaults before RequestOpts
if c.AccessToken != "" {
req.Header.Set("Authorization", "Bearer "+c.AccessToken)
}
// set functional options
for _, o := range opts {
o(req)
}
// set defaults after RequestOpts
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
}
// debug log the request
if c.Debug {
t.Logf("Making %s request to %s", method, req.URL)
contentType := req.Header.Get("Content-Type")
if contentType == "application/json" || strings.HasPrefix(contentType, "text/") {
if req.Body != nil {
body, _ := ioutil.ReadAll(req.Body)
t.Logf("Request body: %s", string(body))
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
}
} else {
t.Logf("Request body: <binary:%s>", contentType)
}
}
// Perform the HTTP request
res, err := c.Client.Do(req)
if err != nil {
t.Fatalf("CSAPI.DoFunc response returned error: %s", err)
}
// debug log the response
if c.Debug && res != nil {
var dump []byte
dump, err = httputil.DumpResponse(res, true)
if err != nil {
t.Fatalf("CSAPI.DoFunc failed to dump response body: %s", err)
}
t.Logf("%s", string(dump))
}
return res
}
// NewLoggedClient returns an http.Client which logs requests/responses
func NewLoggedClient(t *testing.T, hsName string, cli *http.Client) *http.Client {
t.Helper()
if cli == nil {
cli = &http.Client{
Timeout: 30 * time.Second,
}
}
transport := cli.Transport
if transport == nil {
transport = http.DefaultTransport
}
cli.Transport = &loggedRoundTripper{t, hsName, transport}
return cli
}
type loggedRoundTripper struct {
t *testing.T
hsName string
wrap http.RoundTripper
}
func (t *loggedRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
start := time.Now()
res, err := t.wrap.RoundTrip(req)
if err != nil {
t.t.Logf("%s %s%s => error: %s (%s)", req.Method, t.hsName, req.URL.Path, err, time.Since(start))
} else {
t.t.Logf("%s %s%s => %s (%s)", req.Method, t.hsName, req.URL.Path, res.Status, time.Since(start))
}
return res, err
}
// GetJSONFieldStr extracts a value from a byte-encoded JSON body given a search key
func GetJSONFieldStr(t *testing.T, body []byte, wantKey string) string {
t.Helper()
res := gjson.GetBytes(body, wantKey)
if !res.Exists() {
t.Fatalf("JSONFieldStr: key '%s' missing from %s", wantKey, string(body))
}
if res.Str == "" {
t.Fatalf("JSONFieldStr: key '%s' is not a string, body: %s", wantKey, string(body))
}
return res.Str
}
func GetJSONFieldStringArray(t *testing.T, body []byte, wantKey string) []string {
t.Helper()
res := gjson.GetBytes(body, wantKey)
if !res.Exists() {
t.Fatalf("JSONFieldStr: key '%s' missing from %s", wantKey, string(body))
}
arrLength := len(res.Array())
arr := make([]string, arrLength)
i := 0
res.ForEach(func(key, value gjson.Result) bool {
arr[i] = value.Str
// Keep iterating
i++
return true
})
return arr
}
// ParseJSON parses a JSON-encoded HTTP Response body into a byte slice
func ParseJSON(t *testing.T, res *http.Response) []byte {
t.Helper()
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("MustParseJSON: reading HTTP response body returned %s", err)
}
if !gjson.ValidBytes(body) {
t.Fatalf("MustParseJSON: Response is not valid JSON")
}
return body
}
// GjsonEscape escapes . and * from the input so it can be used with gjson.Get
func GjsonEscape(in string) string {
in = strings.ReplaceAll(in, ".", `\.`)
in = strings.ReplaceAll(in, "*", `\*`)
return in
}