From be96e72a97415dc647f27ee1dad6f8840f338f72 Mon Sep 17 00:00:00 2001 From: Chris Joyce Date: Wed, 29 Aug 2018 07:12:10 +1000 Subject: [PATCH] Added a http in terfacr to client --- rest/channels_test.go | 109 ++++++++++++++++++++++++++++++++++++++---- rest/client.go | 29 +++++++++-- 2 files changed, 125 insertions(+), 13 deletions(-) diff --git a/rest/channels_test.go b/rest/channels_test.go index c4f21da..2648735 100644 --- a/rest/channels_test.go +++ b/rest/channels_test.go @@ -1,6 +1,11 @@ package rest import ( + "bytes" + "errors" + "io/ioutil" + "net/http" + "net/url" "testing" "github.com/RocketChat/Rocket.Chat.Go.SDK/models" @@ -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") + } + }) + } } diff --git a/rest/client.go b/rest/client.go index fdf97fa..50fe791 100644 --- a/rest/client.go +++ b/rest/client.go @@ -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"` @@ -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" @@ -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 { @@ -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