This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
client_test.go
157 lines (138 loc) · 4.49 KB
/
client_test.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
package gomatrix
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
func TestClient_LeaveRoom(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "POST" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/leave" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
if _, err := cli.LeaveRoom("!foo:bar"); err != nil {
t.Fatalf("LeaveRoom: error, got %s", err.Error())
}
}
func TestClient_GetAvatarUrl(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"avatar_url":"mxc://matrix.org/iJaUjkshgdfsdkjfn"}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
if response, err := cli.GetAvatarURL(); err != nil {
t.Fatalf("GetAvatarURL: Got error: %s", err.Error())
} else if response == "" {
t.Fatal("GetAvatarURL: Got empty response")
} else if response != "mxc://matrix.org/iJaUjkshgdfsdkjfn" {
t.Fatalf("Unexpected response URL: %s", response)
}
}
func TestClient_SetAvatarUrl(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "PUT" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
if err := cli.SetAvatarURL("https://foo.com/bar.png"); err != nil {
t.Fatalf("GetAvatarURL: Got error: %s", err.Error())
}
}
func TestClient_StateEvent(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/state/m.room.name" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"name":"Room Name Goes Here"}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
content := struct {
Name string `json:"name"`
}{}
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil {
t.Fatalf("StateEvent: error, got %s", err.Error())
}
if content.Name != "Room Name Goes Here" {
t.Fatalf("StateEvent: got %s, want %s", content.Name, "Room Name Goes Here")
}
}
func TestClient_PublicRooms(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/publicRooms" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{
"chunk": [
{
"aliases": [
"#murrays:cheese.bar"
],
"avatar_url": "mxc://bleeker.street/CHEDDARandBRIE",
"guest_can_join": false,
"name": "CHEESE",
"num_joined_members": 37,
"room_id": "!ol19s:bleecker.street",
"topic": "Tasty tasty cheese",
"world_readable": true
}
],
"next_batch": "p190q",
"prev_batch": "p1902",
"total_room_count_estimate": 115
}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
publicRooms, err := cli.PublicRooms(0, "", "")
if err != nil {
t.Fatalf("PublicRooms: error, got %s", err.Error())
}
if publicRooms.TotalRoomCountEstimate != 115 {
t.Fatalf("PublicRooms: got %d, want %d", publicRooms.TotalRoomCountEstimate, 115)
}
if len(publicRooms.Chunk) != 1 {
t.Fatalf("PublicRooms: got %d, want %d", len(publicRooms.Chunk), 1)
}
if publicRooms.Chunk[0].Name != "CHEESE" {
t.Fatalf("PublicRooms: got %s, want %s", publicRooms.Chunk[0].Name, "CHEESE")
}
if publicRooms.Chunk[0].NumJoinedMembers != 37 {
t.Fatalf("PublicRooms: got %d, want %d", publicRooms.Chunk[0].NumJoinedMembers, 37)
}
}
func mockClient(fn func(*http.Request) (*http.Response, error)) *Client {
mrt := MockRoundTripper{
RT: fn,
}
cli, _ := NewClient("https://test.gomatrix.org", "@user:test.gomatrix.org", "abcdef")
cli.Client = &http.Client{
Transport: mrt,
}
return cli
}
type MockRoundTripper struct {
RT func(*http.Request) (*http.Response, error)
}
func (t MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Header.Get("Authorization") == "" {
panic("no auth")
}
return t.RT(req)
}