Skip to content

Commit

Permalink
Fixed failing tests and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
SP Singh committed Sep 21, 2021
1 parent 3436219 commit 3ad03ca
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 801 deletions.
136 changes: 0 additions & 136 deletions cover.html

This file was deleted.

11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/smsglobal/smsglobal-go

go 1.15
go 1.17

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/rs/zerolog v1.20.0
github.com/stretchr/testify v1.6.1
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
github.com/rs/zerolog v1.25.0
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

require github.com/pmezard/go-difflib v1.0.0 // indirect
41 changes: 28 additions & 13 deletions go.sum
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs=
github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.25.0 h1:Rj7XygbUHKUlDPcVdoLyR91fJBsduXj5fRxyqIQj/II=
github.com/rs/zerolog v1.25.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74 h1:4cFkmztxtMslUX2SctSl+blCyXfpzhGOy9LhKAqSMA4=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 2 additions & 12 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

var lg = logger.CreateLogger(constants.DebugLevel).Lgr.With().Str("SMSGlobal", "HTTP Client").Logger()

// client defines information that can be used to make a request to SMSGlobal Rest API.
// Client client defines information that can be used to make a request to SMSGlobal Rest API.
type Client struct {
method string
path string
Expand Down Expand Up @@ -129,7 +129,7 @@ func (c *Client) generateAuthToken() string {
return fmt.Sprintf(`MAC id="%s", ts="%d", nonce="%d", mac="%s"`, c.Key, timestamp, nonce, hash)
}

// Do sends an API request adn the API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.
// Do send an API request adn the API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.
func (c *Client) Do(req *http.Request, v interface{}) error {

lg.Debug().Msgf("Sending %s request to %s", c.method, c.BaseURL)
Expand All @@ -146,13 +146,6 @@ func (c *Client) Do(req *http.Request, v interface{}) error {
return err
}


// TODO remove below log lines
lg.Debug().Msgf("Response received %+v", res)
lg.Debug().Msgf("Response body %+v", res.Body)

fmt.Println(res.Body)

if res != nil {
defer res.Body.Close()
}
Expand All @@ -173,9 +166,6 @@ func (c *Client) Do(req *http.Request, v interface{}) error {
func checkResponse(r *http.Response) error {
lg.Debug().Msgf("HTTP status code: %d", r.StatusCode)

//bodyBytes , err := ioutil.ReadAll(r.Body)
//lg.Debug().Msgf("HTTP response string %s", string(bodyBytes))

// a successful request status code must be between 200 and 299
if c := r.StatusCode; http.StatusOK <= c && c < http.StatusMultipleChoices {
return nil
Expand Down
30 changes: 17 additions & 13 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/smsglobal/smsglobal-go/types/api"
"github.com/smsglobal/smsglobal-go/types/constants"
"github.com/smsglobal/smsglobal-go/util/mocks"
"github.com/smsglobal/smsglobal-go/util/testdata"
"github.com/stretchr/testify/assert"
"net/http"
"regexp"
Expand Down Expand Up @@ -45,10 +46,10 @@ func TestNewRequest(t *testing.T) {
DoFunc: mocks.GetOk,
}

req, err := client.NewRequest("POST", "/sms", `{"balance" : 5,"currency" : "AUD"}`)
req, err := client.NewRequest("GET", "/sms", nil)

assert.NoError(t, err)
assert.Equal(t, http.MethodPost, client.method)
assert.Equal(t, http.MethodGet, client.method)
assert.Equal(t, constants.ContentType, req.Header.Get("Accept"))
assert.Equal(t, constants.ContentType, req.Header.Get("Content-Type"))
assert.Equal(t, "utf-8", req.Header.Get("Accept-Charset"))
Expand All @@ -59,25 +60,26 @@ func TestDo(t *testing.T) {

client := New("key", "secret")

mocks.ResponseJson = `{"balance" : 5,"currency" : "INR"}`
mocks.ResponseJson = testdata.SentToSingleDestinationResponse()

client.HttpClient = &mocks.MockClient{
DoFunc: mocks.GetOk,
}

req, err := client.NewRequest("POST", "/sms", `{"balance" : 5,"currency" : "AUD"}`)
p := `{ "origin":"NodeSdk", "destination":"61474950800", "message":"Test sms from GO sdk"}`
req, err := client.NewRequest("POST", "/sms", p)

assert.NoError(t, err)
assert.Equal(t, client.method, http.MethodPost)
assert.NotNil(t, req)

balance := &api.BalanceResponse{}

err = client.Do(req, balance)
sms := &api.Sms{}
err = client.Do(req, sms)

assert.NoError(t, err)
assert.EqualValues(t, 5.00, balance.Balance)
assert.EqualValues(t, "INR", balance.Currency)
assert.EqualValues(t, testdata.GetSmsResponse().Origin, sms.Origin)
assert.EqualValues(t, testdata.GetSmsResponse().Destination, sms.Destination)
assert.EqualValues(t, testdata.GetSmsResponse().Message, sms.Message)
}

func TestDoWithGarbageResponse(t *testing.T) {
Expand All @@ -87,7 +89,8 @@ func TestDoWithGarbageResponse(t *testing.T) {
DoFunc: mocks.GetGarbageResponse,
}

req, err := client.NewRequest("POST", "/sms", `{"balance" : 5,"currency" : "AUD"}`)
p := `{ "origin":"NodeSdk", "destination":"61474950800", "message":"Test sms from GO sdk"}`
req, err := client.NewRequest("POST", "/sms", p)

assert.NoError(t, err)
assert.Equal(t, client.method, http.MethodPost)
Expand All @@ -108,7 +111,8 @@ func TestAuthenticationFailure(t *testing.T) {
DoFunc: mocks.GetUnknownAuthenticationError,
}

req, err := client.NewRequest("POST", "/sms", `{"balance" : 5,"currency" : "AUD"}`)
p := `{ "origin":"NodeSdk", "destination":"61474950800", "message":"Test sms from GO sdk"}`
req, err := client.NewRequest("POST", "/sms", p)

err = client.Do(req, new(api.BalanceResponse))

Expand All @@ -119,7 +123,7 @@ func TestAuthenticationFailure(t *testing.T) {
func TestDoNoContentResponse(t *testing.T) {

client := New("key", "secret")
//

client.HttpClient = &mocks.MockClient{
DoFunc: mocks.GetNoContent,
}
Expand All @@ -131,5 +135,5 @@ func TestDoNoContentResponse(t *testing.T) {
assert.NotNil(t, req)

err = client.Do(req, nil)
//assert.NoError(t, err)
assert.NoError(t, err)
}
Loading

0 comments on commit 3ad03ca

Please sign in to comment.