Skip to content

Commit

Permalink
Return error on ping if using empty token (#11)
Browse files Browse the repository at this point in the history
* return error if using empty token
  • Loading branch information
tmus authored Aug 14, 2019
1 parent aa50790 commit f55b1e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions monzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package monzo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -36,6 +37,10 @@ func NewClient(token string) *Client {
// Ping attempts to connect to the Monzo API using the given
// client.
func (c *Client) Ping() error {
if c.Token == "" {
return errors.New("error pinging Monzo API. Client token cannot be empty")
}

req, err := c.NewRequest(http.MethodGet, "ping/whoami", nil)
if err != nil {
return err
Expand Down
22 changes: 22 additions & 0 deletions monzo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package monzo

import (
"fmt"
"testing"
)

func TestPing(t *testing.T) {
a1 := NewClient("")
err := a1.Ping()
if err == nil {
fmt.Println("expected an error when using empty string for token. Didn't get one")
t.FailNow()
}

a2 := NewClient("invalid_token")
err = a2.Ping()
if err == nil {
fmt.Print("expected an error when using an invalid token. Didn't get one")
t.FailNow()
}
}

0 comments on commit f55b1e0

Please sign in to comment.