Merge pull request #1 from matrix-org/kegan/leave-room

Add LeaveRoom(roomID) with test
This commit is contained in:
Kegsay 2016-12-01 17:37:55 +00:00 committed by GitHub
commit a531d0e149
3 changed files with 61 additions and 2 deletions

View File

@ -185,7 +185,7 @@ func (cli *Client) SendJSON(method string, httpURL string, contentJSON interface
if res.StatusCode >= 300 || res.StatusCode < 200 { if res.StatusCode >= 300 || res.StatusCode < 200 {
var wrap error var wrap error
var respErr RespError var respErr RespError
if _ = json.Unmarshal(contents, respErr); respErr.ErrCode != "" { if _ = json.Unmarshal(contents, &respErr); respErr.ErrCode != "" {
wrap = respErr wrap = respErr
} }
@ -315,6 +315,20 @@ func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) {
TextMessage{"m.text", text}) TextMessage{"m.text", text})
} }
// LeaveRoom leaves the given room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-leave
func (cli *Client) LeaveRoom(roomID string) (*RespLeaveRoom, error) {
u := cli.BuildURL("rooms", roomID, "leave")
resBytes, err := cli.SendJSON("POST", u, struct{}{})
if err != nil {
return nil, err
}
var resp RespLeaveRoom
if err = json.Unmarshal(resBytes, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// UploadLink uploads an HTTP URL and then returns an MXC URI. // UploadLink uploads an HTTP URL and then returns an MXC URI.
func (cli *Client) UploadLink(link string) (*RespMediaUpload, error) { func (cli *Client) UploadLink(link string) (*RespMediaUpload, error) {
res, err := cli.Client.Get(link) res, err := cli.Client.Get(link)

View File

@ -1,6 +1,12 @@
package gomatrix package gomatrix
import "fmt" import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
func ExampleClient_BuildURLWithQuery() { func ExampleClient_BuildURLWithQuery() {
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456") cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456")
@ -26,3 +32,39 @@ func ExampleClient_BuildBaseURL() {
fmt.Println(out) fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456 // 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" {
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 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) {
return t.RT(req)
}

View File

@ -22,6 +22,9 @@ type RespJoinRoom struct {
RoomID string `json:"room_id"` RoomID string `json:"room_id"`
} }
// RespLeaveRoom is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-leave
type RespLeaveRoom struct{}
// RespSendEvent is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid // RespSendEvent is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
type RespSendEvent struct { type RespSendEvent struct {
EventID string `json:"event_id"` EventID string `json:"event_id"`