Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 99 additions & 10 deletions rest/channels_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package rest

import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"net/url"
"testing"

"github.com/RocketChat/Rocket.Chat.Go.SDK/models"
Expand Down Expand Up @@ -31,17 +36,101 @@ func TestRocket_LeaveChannel(t *testing.T) {
assert.Nil(t, err)
}

type testDoer struct {
response string
responseCode int
http.Header
}

func (nd testDoer) Do(*http.Request) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte(nd.response))),
StatusCode: nd.responseCode,
Header: nd.Header,
}, nil
}

func CreateTestRestClient(d Doer) *Client {
rockerServer := &url.URL{Host: "rocketchat.localhost", Scheme: "https"}
client := NewClient(rockerServer, false)
client.myDoer = d
return client
}

func TestRocket_GetChannelInfo(t *testing.T) {
rocket := getDefaultClient(t)

general := &models.Channel{ID: "GENERAL"}
updatedChannelInfo, err := rocket.GetChannelInfo(general)
assert.Nil(t, err)
assert.NotNil(t, updatedChannelInfo)
type fields struct {
myDoer Doer
channel *models.Channel
}
tests := []struct {
name string
fields fields
want models.Channel
wantErr error
}{
{
name: "GetChannelInfo OK",
fields: fields{
myDoer: testDoer{
responseCode: 200,
response: `{
"channel": {
"_id": "ByehQjC44FwMeiLbX",
"ts": "2016-11-30T21:23:04.737Z",
"t": "c",
"name": "testing",
"usernames": [
"testing",
"testing1",
"testing2"
],
"msgs": 1,
"default": true,
"_updatedAt": "2016-12-09T12:50:51.575Z",
"lm": "2016-12-09T12:50:51.555Z"
},
"success": true
}`,
},
channel: &models.Channel{ID: "GENERAL"},
},
want: models.Channel{
ID: "ByehQjC44FwMeiLbX",
Name: "testing",
Type: "c",
},
wantErr: nil,
},
{
name: "GetChannelInfo Err",
fields: fields{
myDoer: testDoer{
responseCode: 200,
response: `{
"status": "error",
"message": "you must be logged in to do this"
}`,
},
channel: &models.Channel{ID: "GENERAL"},
},
want: models.Channel{},
wantErr: errors.New("status: error, message: you must be logged in to do this"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rt := CreateTestRestClient(tt.fields.myDoer)
got, err := rt.GetChannelInfo(tt.fields.channel)

assert.Equal(t, err, tt.wantErr, "Unexpected error")

assert.Equal(t, general.ID, updatedChannelInfo.ID)
assert.NotEmpty(t, updatedChannelInfo.Name)
assert.NotEmpty(t, updatedChannelInfo.Type)
assert.NotEmpty(t, updatedChannelInfo.UpdatedAt)
assert.NotEmpty(t, updatedChannelInfo.Timestamp)
if err == nil {
assert.NotNil(t, got)
assert.Equal(t, got.ID, tt.want.ID, "ID did not match")
assert.Equal(t, got.Name, tt.want.Name, "Name did not match")
assert.Equal(t, got.Type, tt.want.Type, "Name did not match")
}
})
}
}
29 changes: 26 additions & 3 deletions rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,26 @@ type Client struct {
Port string
Version string

// http interface can be used when testing
myDoer Doer

service

// Use this switch to see all network communication.
Debug bool

auth *authInfo
}

// Doer to make testing easer !
type Doer interface {
Do(*http.Request) (*http.Response, error)
}

type service struct {
client *Client
}

type Status struct {
Success bool `json:"success"`
Error string `json:"error"`
Expand Down Expand Up @@ -71,7 +85,7 @@ type StatusResponse struct {
Channel string `json:"channel"`
}

func NewClient(serverUrl *url.URL, debug bool) *Client {
func NewClient(serverUrl *url.URL, debug bool) (c *Client) {
protocol := "http"
port := "80"

Expand All @@ -84,7 +98,16 @@ func NewClient(serverUrl *url.URL, debug bool) *Client {
port = serverUrl.Port()
}

return &Client{Host: serverUrl.Hostname(), Port: port, Protocol: protocol, Version: "v1", Debug: debug}
c.Host = serverUrl.Hostname()
c.Port = port
c.Protocol = protocol
c.Version = "v1"
c.Debug = debug

// set default http interface to use
c.myDoer = http.DefaultClient

return
}

func (c *Client) getUrl() string {
Expand Down Expand Up @@ -141,7 +164,7 @@ func (c *Client) doRequest(method, api string, params url.Values, body io.Reader
log.Println(request)
}

resp, err := http.DefaultClient.Do(request)
resp, err := c.myDoer.Do(request)

if err != nil {
return err
Expand Down