Skip to content

Commit 9507603

Browse files
committed
Rewrite the client code a bit and give each response object its own file
1 parent 295d288 commit 9507603

12 files changed

+1222
-154
lines changed

balance.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package messagebird
2+
3+
type Balance struct {
4+
Payment string
5+
Type string
6+
Amount int
7+
Errors []Error
8+
}

balance_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package messagebird
2+
3+
import "testing"
4+
5+
var balanceObject []byte = []byte(`{
6+
"payment":"prepaid",
7+
"type":"credits",
8+
"amount":9
9+
}`)
10+
11+
func TestBalance(t *testing.T) {
12+
SetServerResponse(200, balanceObject)
13+
14+
balance, err := mbClient.Balance()
15+
if err != nil {
16+
t.Fatalf("Didn't expect error while fetching the balance: %s", err)
17+
}
18+
19+
if balance.Payment != "prepaid" {
20+
t.Errorf("Unexpected balance payment: %s", balance.Payment)
21+
}
22+
if balance.Type != "credits" {
23+
t.Errorf("Unexpected balance type: %s", balance.Type)
24+
}
25+
if balance.Amount != 9 {
26+
t.Errorf("Unexpected balance amount: %d", balance.Amount)
27+
}
28+
}
29+
30+
func TestBalanceError(t *testing.T) {
31+
SetServerResponse(405, accessKeyErrorObject)
32+
33+
balance, err := mbClient.Balance()
34+
if err != ErrResponse {
35+
t.Fatalf("Expected ErrResponse to be returned, instead I got %s", err)
36+
}
37+
38+
if len(balance.Errors) != 1 {
39+
t.Fatalf("Unexpected number of errors: %d", len(balance.Errors))
40+
}
41+
42+
if balance.Errors[0].Code != 2 {
43+
t.Errorf("Unexpected error code: %d", balance.Errors[0].Code)
44+
}
45+
46+
if balance.Errors[0].Parameter != "access_key" {
47+
t.Errorf("Unexpected error parameter: %s", balance.Errors[0].Parameter)
48+
}
49+
}

0 commit comments

Comments
 (0)