Skip to content

Commit 8f43891

Browse files
author
Dirk Hoekstra
committed
Improve tests and add resource path constants
1 parent 962ba30 commit 8f43891

File tree

4 files changed

+314
-285
lines changed

4 files changed

+314
-285
lines changed

client.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ const (
2222
Endpoint = "https://rest.messagebird.com"
2323
)
2424

25+
const (
26+
// HLRPath represents the path to the HLR resource.
27+
HLRPath = "hlr"
28+
// MessagePath represents the path to the Message resource.
29+
MessagePath = "messages"
30+
// VoiceMessagePath represents the path to the VoiceMessage resource.
31+
VoiceMessagePath = "voicemessages"
32+
// VerifyPath represents the path to the Verify resource.
33+
VerifyPath = "verify"
34+
// LookupPath represents the path to the Lookup resource.
35+
LookupPath = "lookup"
36+
)
37+
2538
var (
2639
ErrResponse = errors.New("The MessageBird API returned an error")
2740
ErrUnexpectedResponse = errors.New("The MessageBird API is currently unavailable")
@@ -129,7 +142,7 @@ func (c *Client) Balance() (*Balance, error) {
129142
// created by the NewHLR function.
130143
func (c *Client) HLR(id string) (*HLR, error) {
131144
hlr := &HLR{}
132-
if err := c.request(hlr, "hlr/"+id, nil); err != nil {
145+
if err := c.request(hlr, HLRPath+"/"+id, nil); err != nil {
133146
if err == ErrResponse {
134147
return hlr, err
135148
}
@@ -144,7 +157,7 @@ func (c *Client) HLR(id string) (*HLR, error) {
144157
// function.
145158
func (c *Client) HLRs() (*HLRList, error) {
146159
hlrList := &HLRList{}
147-
if err := c.request(hlrList, "hlr", nil); err != nil {
160+
if err := c.request(hlrList, HLRPath, nil); err != nil {
148161
if err == ErrResponse {
149162
return hlrList, err
150163
}
@@ -163,7 +176,7 @@ func (c *Client) NewHLR(msisdn, reference string) (*HLR, error) {
163176
}
164177

165178
hlr := &HLR{}
166-
if err := c.request(hlr, "hlr", params); err != nil {
179+
if err := c.request(hlr, HLRPath, params); err != nil {
167180
if err == ErrResponse {
168181
return hlr, err
169182
}
@@ -177,7 +190,7 @@ func (c *Client) NewHLR(msisdn, reference string) (*HLR, error) {
177190
// Message retrieves the information of an existing Message.
178191
func (c *Client) Message(id string) (*Message, error) {
179192
message := &Message{}
180-
if err := c.request(message, "messages/"+id, nil); err != nil {
193+
if err := c.request(message, MessagePath+"/"+id, nil); err != nil {
181194
if err == ErrResponse {
182195
return message, err
183196
}
@@ -191,7 +204,7 @@ func (c *Client) Message(id string) (*Message, error) {
191204
// Messages retrieves all messages of the user represented as a MessageList object.
192205
func (c *Client) Messages() (*MessageList, error) {
193206
messageList := &MessageList{}
194-
if err := c.request(messageList, "messages", nil); err != nil {
207+
if err := c.request(messageList, MessagePath, nil); err != nil {
195208
if err == ErrResponse {
196209
return messageList, err
197210
}
@@ -214,7 +227,7 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
214227
params.Set("recipients", strings.Join(recipients, ","))
215228

216229
message := &Message{}
217-
if err := c.request(message, "messages", params); err != nil {
230+
if err := c.request(message, MessagePath, params); err != nil {
218231
if err == ErrResponse {
219232
return message, err
220233
}
@@ -228,7 +241,7 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
228241
// VoiceMessage retrieves the information of an existing VoiceMessage.
229242
func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
230243
message := &VoiceMessage{}
231-
if err := c.request(message, "voicemessages/"+id, nil); err != nil {
244+
if err := c.request(message, VoiceMessagePath+"/"+id, nil); err != nil {
232245
if err == ErrResponse {
233246
return message, err
234247
}
@@ -242,7 +255,7 @@ func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
242255
// VoiceMessages retrieves all VoiceMessages of the user.
243256
func (c *Client) VoiceMessages() (*VoiceMessageList, error) {
244257
messageList := &VoiceMessageList{}
245-
if err := c.request(messageList, "voicemessages", nil); err != nil {
258+
if err := c.request(messageList, VoiceMessagePath, nil); err != nil {
246259
if err == ErrResponse {
247260
return messageList, err
248261
}
@@ -260,7 +273,7 @@ func (c *Client) NewVoiceMessage(recipients []string, body string, params *Voice
260273
urlParams.Set("recipients", strings.Join(recipients, ","))
261274

262275
message := &VoiceMessage{}
263-
if err := c.request(message, "voicemessages", urlParams); err != nil {
276+
if err := c.request(message, VoiceMessagePath, urlParams); err != nil {
264277
if err == ErrResponse {
265278
return message, err
266279
}
@@ -277,7 +290,7 @@ func (c *Client) NewVerify(recipient string, params *VerifyParams) (*Verify, err
277290
urlParams.Set("recipient", recipient)
278291

279292
verify := &Verify{}
280-
if err := c.request(verify, "verify", urlParams); err != nil {
293+
if err := c.request(verify, VerifyPath, urlParams); err != nil {
281294
if err == ErrResponse {
282295
return verify, err
283296
}
@@ -293,7 +306,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
293306
params := &url.Values{}
294307
params.Set("token", token)
295308

296-
path := "verify/" + id + "?" + params.Encode()
309+
path := VerifyPath + "/" + id + "?" + params.Encode()
297310

298311
verify := &Verify{}
299312
if err := c.request(verify, path, nil); err != nil {
@@ -310,7 +323,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
310323
// Lookup performs a new lookup for the specified number.
311324
func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, error) {
312325
urlParams := paramsForLookup(params)
313-
path := "lookup/" + phoneNumber + "?" + urlParams.Encode()
326+
path := LookupPath + "/" + phoneNumber + "?" + urlParams.Encode()
314327

315328
lookup := &Lookup{}
316329
if err := c.request(lookup, path, nil); err != nil {
@@ -327,7 +340,7 @@ func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, erro
327340
// NewLookupHLR creates a new HLR lookup for the specified number.
328341
func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
329342
urlParams := paramsForLookup(params)
330-
path := "lookup/" + phoneNumber + "/hlr"
343+
path := LookupPath + "/" + phoneNumber + "/hlr"
331344

332345
hlr := &HLR{}
333346
if err := c.request(hlr, path, urlParams); err != nil {
@@ -344,7 +357,7 @@ func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, e
344357
// LookupHLR performs a HLR lookup for the specified number.
345358
func (c *Client) LookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
346359
urlParams := paramsForLookup(params)
347-
path := "lookup/" + phoneNumber + "/hlr?" + urlParams.Encode()
360+
path := LookupPath + "/" + phoneNumber + "/hlr?" + urlParams.Encode()
348361

349362
hlr := &HLR{}
350363
if err := c.request(hlr, path, nil); err != nil {

hlr_test.go

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package messagebird
22

33
import (
4+
"net/http"
45
"testing"
56
"time"
67
)
78

89
var hlrObject []byte = []byte(`{
910
"id":"27978c50354a93ca0ca8de6h54340177",
10-
"href":"https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
11+
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
1112
"msisdn":31612345678,
1213
"network":20406,
1314
"reference":"MyReference",
@@ -17,75 +18,76 @@ var hlrObject []byte = []byte(`{
1718
}`)
1819

1920
var hlrListObject = []byte(`{
20-
"offset": 0,
21-
"limit": 20,
22-
"count": 2,
23-
"totalCount": 2,
24-
"links": {
25-
"first": "https://rest.messagebird.com/hlr/?offset=0",
26-
"previous": null,
27-
"next": null,
28-
"last": "https://rest.messagebird.com/hlr/?offset=0"
21+
"offset":0,
22+
"limit":20,
23+
"count":2,
24+
"totalCount":2,
25+
"links":{
26+
"first":"https://rest.messagebird.com/hlr/?offset=0",
27+
"previous":null,
28+
"next":null,
29+
"last":"https://rest.messagebird.com/hlr/?offset=0"
30+
},
31+
"items":[
32+
{
33+
"id":"27978c50354a93ca0ca8de6h54340177",
34+
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
35+
"msisdn":31612345678,
36+
"network":20406,
37+
"reference":"MyReference",
38+
"status":"sent",
39+
"createdDatetime":"2015-01-04T13:14:08+00:00",
40+
"statusDatetime":"2015-01-04T13:14:09+00:00"
2941
},
30-
"items": [
31-
{
32-
"id":"27978c50354a93ca0ca8de6h54340177",
33-
"href":"https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
34-
"msisdn":31612345678,
35-
"network":20406,
36-
"reference":"MyReference",
37-
"status":"sent",
38-
"createdDatetime":"2015-01-04T13:14:08+00:00",
39-
"statusDatetime":"2015-01-04T13:14:09+00:00"
40-
},
41-
{
42-
"id":"27978c50354a93ca0ca8de6h54340177",
43-
"href":"https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
44-
"msisdn":31612345678,
45-
"network":20406,
46-
"reference":"MyReference",
47-
"status":"sent",
48-
"createdDatetime":"2015-01-04T13:14:08+00:00",
49-
"statusDatetime":"2015-01-04T13:14:09+00:00"
50-
}]
42+
{
43+
"id":"27978c50354a93ca0ca8de6h54340177",
44+
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
45+
"msisdn":31612345678,
46+
"network":20406,
47+
"reference":"MyReference",
48+
"status":"sent",
49+
"createdDatetime":"2015-01-04T13:14:08+00:00",
50+
"statusDatetime":"2015-01-04T13:14:09+00:00"
51+
}
52+
]
5153
}`)
5254

5355
func assertHLRObject(t *testing.T, hlr *HLR) {
5456
if hlr.Id != "27978c50354a93ca0ca8de6h54340177" {
55-
t.Errorf("Unexpected result for HLR Id: %s", hlr.Id)
57+
t.Errorf("Unexpected result for HLR id: %s, expected: 27978c50354a93ca0ca8de6h54340177", hlr.Id)
5658
}
5759

5860
if hlr.HRef != "https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177" {
59-
t.Errorf("Unexpected HLR href: %s", hlr.HRef)
61+
t.Errorf("Unexpected HLR href: %s, expected: https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177", hlr.HRef)
6062
}
6163

6264
if hlr.MSISDN != 31612345678 {
63-
t.Errorf("Unexpected HLR msisdn: %d", hlr.MSISDN)
65+
t.Errorf("Unexpected HLR msisdn: %d, expected: 31612345678", hlr.MSISDN)
6466
}
6567

6668
if hlr.Network != 20406 {
67-
t.Errorf("Unexpected HLR network: %d", hlr.Network)
69+
t.Errorf("Unexpected HLR network: %d, expected: 20406", hlr.Network)
6870
}
6971

7072
if hlr.Reference != "MyReference" {
71-
t.Errorf("Unexpected HLR reference: %s", hlr.Reference)
73+
t.Errorf("Unexpected HLR reference: %s, expected: MyReference", hlr.Reference)
7274
}
7375

7476
if hlr.Status != "sent" {
75-
t.Errorf("Unexpected HLR status: %s", hlr.Status)
77+
t.Errorf("Unexpected HLR status: %s, expected: sent", hlr.Status)
7678
}
7779

7880
if hlr.CreatedDatetime == nil || hlr.CreatedDatetime.Format(time.RFC3339) != "2015-01-04T13:14:08Z" {
79-
t.Errorf("Unexpected HLR created datetime: %s", hlr.CreatedDatetime.Format(time.RFC3339))
81+
t.Errorf("Unexpected HLR created datetime: %s, expected: 2015-01-04T13:14:08Z", hlr.CreatedDatetime.Format(time.RFC3339))
8082
}
8183

8284
if hlr.StatusDatetime == nil || hlr.StatusDatetime.Format(time.RFC3339) != "2015-01-04T13:14:09Z" {
83-
t.Errorf("Unexpected HLR status datetime: %s", hlr.StatusDatetime.Format(time.RFC3339))
85+
t.Errorf("Unexpected HLR status datetime: %s, expected: 2015-01-04T13:14:09Z", hlr.StatusDatetime.Format(time.RFC3339))
8486
}
8587
}
8688

8789
func TestHLR(t *testing.T) {
88-
SetServerResponse(200, hlrObject)
90+
SetServerResponse(http.StatusOK, hlrObject)
8991

9092
hlr, err := mbClient.HLR("27978c50354a93ca0ca8de6h54340177")
9193
if err != nil {
@@ -96,7 +98,7 @@ func TestHLR(t *testing.T) {
9698
}
9799

98100
func TestNewHLR(t *testing.T) {
99-
SetServerResponse(200, hlrObject)
101+
SetServerResponse(http.StatusOK, hlrObject)
100102

101103
hlr, err := mbClient.NewHLR("31612345678", "MyReference")
102104
if err != nil {
@@ -107,45 +109,45 @@ func TestNewHLR(t *testing.T) {
107109
}
108110

109111
func TestHLRError(t *testing.T) {
110-
SetServerResponse(405, accessKeyErrorObject)
112+
SetServerResponse(http.StatusMethodNotAllowed, accessKeyErrorObject)
111113

112114
hlr, err := mbClient.HLR("dummy_hlr_id")
113115
if err != ErrResponse {
114116
t.Fatalf("Expected ErrResponse to be returned, instead I got %s", err)
115117
}
116118

117119
if len(hlr.Errors) != 1 {
118-
t.Fatalf("Unexpected number of errors: %d", len(hlr.Errors))
120+
t.Fatalf("Unexpected number of errors: %d, expected: 1", len(hlr.Errors))
119121
}
120122

121123
if hlr.Errors[0].Code != 2 {
122-
t.Errorf("Unexpected error code: %d", hlr.Errors[0].Code)
124+
t.Errorf("Unexpected error code: %d, expected: 2", hlr.Errors[0].Code)
123125
}
124126

125127
if hlr.Errors[0].Parameter != "access_key" {
126-
t.Errorf("Unexpected error parameter: %s", hlr.Errors[0].Parameter)
128+
t.Errorf("Unexpected error parameter: %s, expected: access_key", hlr.Errors[0].Parameter)
127129
}
128130
}
129131

130132
func TestHLRList(t *testing.T) {
131-
SetServerResponse(200, hlrListObject)
133+
SetServerResponse(http.StatusOK, hlrListObject)
132134

133135
hlrList, err := mbClient.HLRs()
134136
if err != nil {
135137
t.Fatalf("Didn't expect an error while requesting HLRs: %s", err)
136138
}
137139

138140
if hlrList.Offset != 0 {
139-
t.Errorf("Unexpected result for the HLRList Offset: %d\n", hlrList.Offset)
141+
t.Errorf("Unexpected result for the HLRList offset: %d, expected: 0", hlrList.Offset)
140142
}
141143
if hlrList.Limit != 20 {
142-
t.Errorf("Unexpected result for the HLRList Limit: %d\n", hlrList.Limit)
144+
t.Errorf("Unexpected result for the HLRList limit: %d, expected: 20", hlrList.Limit)
143145
}
144146
if hlrList.Count != 2 {
145-
t.Errorf("Unexpected result for the HLRList Count: %d\n", hlrList.Count)
147+
t.Errorf("Unexpected result for the HLRList count: %d, expected: 2", hlrList.Count)
146148
}
147149
if hlrList.TotalCount != 2 {
148-
t.Errorf("Unexpected result for the HLRList TotalCount: %d\n", hlrList.TotalCount)
150+
t.Errorf("Unexpected result for the HLRList total count: %d, expected: 2", hlrList.TotalCount)
149151
}
150152

151153
for _, hlr := range hlrList.Items {

0 commit comments

Comments
 (0)