Skip to content

Commit 7a939b6

Browse files
committed
Merge branch 'use-testdata' into rename-message
2 parents 2e0a055 + d090eb0 commit 7a939b6

28 files changed

+576
-571
lines changed

balance/balance_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import (
88
"github.com/messagebird/go-rest-api/internal/messagebirdtest"
99
)
1010

11-
var balanceObject = []byte(`{
12-
"payment":"prepaid",
13-
"type":"credits",
14-
"amount":9.2
15-
}`)
16-
1711
const Epsilon float32 = 0.001
1812

1913
func cmpFloat32(a, b float32) bool {
@@ -25,7 +19,7 @@ func TestMain(m *testing.M) {
2519
}
2620

2721
func TestRead(t *testing.T) {
28-
messagebirdtest.WillReturn(balanceObject, http.StatusOK)
22+
messagebirdtest.WillReturnTestdata(t, "balance.json", http.StatusOK)
2923
client := messagebirdtest.Client(t)
3024

3125
balance, err := Read(client)

balance/testdata/balance.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"payment": "prepaid",
3+
"type": "credits",
4+
"amount": 9.2
5+
}

hlr/hlr_test.go

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,6 @@ import (
1010
"github.com/messagebird/go-rest-api/internal/messagebirdtest"
1111
)
1212

13-
var hlrObject = []byte(`{
14-
"id":"27978c50354a93ca0ca8de6h54340177",
15-
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
16-
"msisdn":31612345678,
17-
"network":20406,
18-
"reference":"MyReference",
19-
"status":"sent",
20-
"createdDatetime":"2015-01-04T13:14:08+00:00",
21-
"statusDatetime":"2015-01-04T13:14:09+00:00"
22-
}`)
23-
24-
var hlrListObject = []byte(`{
25-
"offset":0,
26-
"limit":20,
27-
"count":2,
28-
"totalCount":2,
29-
"links":{
30-
"first":"https://rest.messagebird.com/hlr/?offset=0",
31-
"previous":null,
32-
"next":null,
33-
"last":"https://rest.messagebird.com/hlr/?offset=0"
34-
},
35-
"items":[
36-
{
37-
"id":"27978c50354a93ca0ca8de6h54340177",
38-
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
39-
"msisdn":31612345678,
40-
"network":20406,
41-
"reference":"MyReference",
42-
"status":"sent",
43-
"createdDatetime":"2015-01-04T13:14:08+00:00",
44-
"statusDatetime":"2015-01-04T13:14:09+00:00"
45-
},
46-
{
47-
"id":"27978c50354a93ca0ca8de6h54340177",
48-
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
49-
"msisdn":31612345678,
50-
"network":20406,
51-
"reference":"MyReference",
52-
"status":"sent",
53-
"createdDatetime":"2015-01-04T13:14:08+00:00",
54-
"statusDatetime":"2015-01-04T13:14:09+00:00"
55-
}
56-
]
57-
}`)
58-
5913
func TestMain(m *testing.M) {
6014
messagebirdtest.EnableServer(m)
6115
}
@@ -95,7 +49,7 @@ func assertHLRObject(t *testing.T, hlr *HLR) {
9549
}
9650

9751
func TestRead(t *testing.T) {
98-
messagebirdtest.WillReturn(hlrObject, http.StatusOK)
52+
messagebirdtest.WillReturnTestdata(t, "hlrObject.json", http.StatusOK)
9953
client := messagebirdtest.Client(t)
10054

10155
hlr, err := Read(client, "27978c50354a93ca0ca8de6h54340177")
@@ -122,7 +76,7 @@ func TestRequestDataForHLR(t *testing.T) {
12276
}
12377

12478
func TestCreate(t *testing.T) {
125-
messagebirdtest.WillReturn(hlrObject, http.StatusOK)
79+
messagebirdtest.WillReturnTestdata(t, "hlrObject.json", http.StatusOK)
12680
client := messagebirdtest.Client(t)
12781

12882
hlr, err := Create(client, "31612345678", "MyReference")
@@ -158,7 +112,7 @@ func TestHLRError(t *testing.T) {
158112
}
159113

160114
func TestList(t *testing.T) {
161-
messagebirdtest.WillReturn(hlrListObject, http.StatusOK)
115+
messagebirdtest.WillReturnTestdata(t, "hlrListObject.json", http.StatusOK)
162116
client := messagebirdtest.Client(t)
163117

164118
hlrList, err := List(client)

hlr/testdata/hlrListObject.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"offset": 0,
3+
"limit": 20,
4+
"count": 2,
5+
"totalCount": 2,
6+
"links": {
7+
"first": "https:\/\/rest.messagebird.com\/hlr\/?offset=0",
8+
"previous": null,
9+
"next": null,
10+
"last": "https:\/\/rest.messagebird.com\/hlr\/?offset=0"
11+
},
12+
"items": [
13+
{
14+
"id": "27978c50354a93ca0ca8de6h54340177",
15+
"href": "https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
16+
"msisdn": 31612345678,
17+
"network": 20406,
18+
"reference": "MyReference",
19+
"status": "sent",
20+
"createdDatetime": "2015-01-04T13:14:08+00:00",
21+
"statusDatetime": "2015-01-04T13:14:09+00:00"
22+
},
23+
{
24+
"id": "27978c50354a93ca0ca8de6h54340177",
25+
"href": "https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
26+
"msisdn": 31612345678,
27+
"network": 20406,
28+
"reference": "MyReference",
29+
"status": "sent",
30+
"createdDatetime": "2015-01-04T13:14:08+00:00",
31+
"statusDatetime": "2015-01-04T13:14:09+00:00"
32+
}
33+
]
34+
}

hlr/testdata/hlrObject.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "27978c50354a93ca0ca8de6h54340177",
3+
"href": "https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
4+
"msisdn": 31612345678,
5+
"network": 20406,
6+
"reference": "MyReference",
7+
"status": "sent",
8+
"createdDatetime": "2015-01-04T13:14:08+00:00",
9+
"statusDatetime": "2015-01-04T13:14:09+00:00"
10+
}

internal/messagebirdtest/helper.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package messagebirdtest
2+
3+
import (
4+
"io/ioutil"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
const testdataDir = "testdata"
10+
11+
// testdata returns a file's bytes based on the path relative to the testdata
12+
// directory. It fails the test if the testdata file can not be read.
13+
func testdata(t *testing.T, relativePath string) []byte {
14+
path := filepath.Join(testdataDir, relativePath)
15+
16+
b, err := ioutil.ReadFile(path)
17+
if err != nil {
18+
t.Fatalf("%s", err)
19+
}
20+
21+
return b
22+
}

internal/messagebirdtest/test_server.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ func closeServer() {
3636
server.Close()
3737
}
3838

39-
// WillReturn sets the body (r) and status (s) for the test server to respond with.
40-
func WillReturn(b []byte, s int) {
41-
responseBody = b
39+
// WillReturnTestdata sets the status (s) for the test server to respond with.
40+
// Additionally it reads the bytes from the relativePath file and returns that
41+
// for requests. It fails the test if the file can not be read. The path is
42+
// relative to the testdata directory (the go tool ignores directories named
43+
// "testdata" in test packages: https://golang.org/cmd/go/#hdr-Test_packages).
44+
func WillReturnTestdata(t *testing.T, relativePath string, s int) {
45+
responseBody = testdata(t, relativePath)
4246
status = s
4347
}
4448

49+
// WillReturnAccessKeyError sets the response body and status for requests to
50+
// indicate the request is not allowed due to an incorrect access key.
4551
func WillReturnAccessKeyError() {
4652
responseBody = []byte(`
4753
{

lookup/lookup_test.go

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,12 @@ import (
99
"github.com/messagebird/go-rest-api/internal/messagebirdtest"
1010
)
1111

12-
var lookupObject = []byte(`{
13-
"href":"https://rest.messagebird.com/lookup/31624971134",
14-
"countryCode":"NL",
15-
"countryPrefix":31,
16-
"phoneNumber":31624971134,
17-
"type":"mobile",
18-
"formats":{
19-
"e164":"+31624971134",
20-
"international":"+31 6 24971134",
21-
"national":"06 24971134",
22-
"rfc3966":"tel:+31-6-24971134"
23-
},
24-
"hlr":{
25-
"id":"6118d3f06566fcd0cdc8962h65065907",
26-
"network":20416,
27-
"reference":"referece2000",
28-
"status":"active",
29-
"createdDatetime":"2015-12-15T08:19:24+00:00",
30-
"statusDatetime":"2015-12-15T08:19:25+00:00"
31-
}
32-
}`)
33-
34-
var lookupHLRObject = []byte(`{
35-
"id":"6118d3f06566fcd0cdc8962h65065907",
36-
"network":20416,
37-
"reference":"referece2000",
38-
"status":"active",
39-
"createdDatetime":"2015-12-15T08:19:24+00:00",
40-
"statusDatetime":"2015-12-15T08:19:25+00:00"
41-
}`)
42-
4312
func TestMain(m *testing.M) {
4413
messagebirdtest.EnableServer(m)
4514
}
4615

4716
func TestCreate(t *testing.T) {
48-
messagebirdtest.WillReturn(lookupObject, http.StatusOK)
17+
messagebirdtest.WillReturnTestdata(t, "lookupObject.json", http.StatusOK)
4918
client := messagebirdtest.Client(t)
5019

5120
phoneNumber := "31624971134"
@@ -91,7 +60,7 @@ func checkHLR(t *testing.T, hlr *hlr.HLR) {
9160
}
9261

9362
func TestReadHLR(t *testing.T) {
94-
messagebirdtest.WillReturn(lookupHLRObject, http.StatusOK)
63+
messagebirdtest.WillReturnTestdata(t, "lookupHLRObject.json", http.StatusOK)
9564
client := messagebirdtest.Client(t)
9665

9766
hlr, err := ReadHLR(client, "31624971134", &Params{CountryCode: "NL"})
@@ -117,7 +86,7 @@ func TestRequestDataForLookupHLR(t *testing.T) {
11786
}
11887

11988
func TestCreateHLR(t *testing.T) {
120-
messagebirdtest.WillReturn(lookupHLRObject, http.StatusCreated)
89+
messagebirdtest.WillReturnTestdata(t, "lookupHLRObject.json", http.StatusCreated)
12190
client := messagebirdtest.Client(t)
12291

12392
hlr, err := CreateHLR(client, "31624971134", &Params{CountryCode: "NL", Reference: "reference2000"})

lookup/testdata/lookupHLRObject.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "6118d3f06566fcd0cdc8962h65065907",
3+
"network": 20416,
4+
"reference": "referece2000",
5+
"status": "active",
6+
"createdDatetime": "2015-12-15T08:19:24+00:00",
7+
"statusDatetime": "2015-12-15T08:19:25+00:00"
8+
}

lookup/testdata/lookupObject.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"href": "https:\/\/rest.messagebird.com\/lookup\/31624971134",
3+
"countryCode": "NL",
4+
"countryPrefix": 31,
5+
"phoneNumber": 31624971134,
6+
"type": "mobile",
7+
"formats": {
8+
"e164": "+31624971134",
9+
"international": "+31 6 24971134",
10+
"national": "06 24971134",
11+
"rfc3966": "tel:+31-6-24971134"
12+
},
13+
"hlr": {
14+
"id": "6118d3f06566fcd0cdc8962h65065907",
15+
"network": 20416,
16+
"reference": "referece2000",
17+
"status": "active",
18+
"createdDatetime": "2015-12-15T08:19:24+00:00",
19+
"statusDatetime": "2015-12-15T08:19:25+00:00"
20+
}
21+
}

0 commit comments

Comments
 (0)