1
0
Fork 0
mirror of https://github.com/matrix-org/gomatrix synced 2025-12-19 04:38:03 +00:00

Add Client.StateEvent method with test and example. Move examples to different file

This commit is contained in:
Kegan Dougal 2016-12-02 15:36:09 +00:00
parent 1842392399
commit 8bd1c5a89b
3 changed files with 74 additions and 25 deletions

View file

@ -8,31 +8,6 @@ import (
"testing"
)
func ExampleClient_BuildURLWithQuery() {
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456")
out := cli.BuildURLWithQuery([]string{"sync"}, map[string]string{
"filter_id": "5",
})
fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/sync?access_token=abcdef123456&filter_id=5
}
func ExampleClient_BuildURL() {
userID := "@example:matrix.org"
cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
out := cli.BuildURL("user", userID, "filter")
fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter?access_token=abcdef123456
}
func ExampleClient_BuildBaseURL() {
userID := "@example:matrix.org"
cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org")
fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456
}
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" {
@ -49,6 +24,32 @@ func TestClient_LeaveRoom(t *testing.T) {
}
}
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"`
}{}
expectContent := struct {
Name string `json:"name"`
}{"Room Name Goes Here"}
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil {
t.Fatalf("StateEvent: error, got %s", err.Error())
}
if content.Name != expectContent.Name {
t.Fatalf("StateEvent: got %s, want %s", content.Name, expectContent.Name)
}
}
func mockClient(fn func(*http.Request) (*http.Response, error)) *Client {
mrt := MockRoundTripper{
RT: fn,