-
Notifications
You must be signed in to change notification settings - Fork 0
/
luckynumber_test.go
72 lines (60 loc) · 1.86 KB
/
luckynumber_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package librus_api_go_test
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
golibrus "github.com/0x113/librus-api-go"
"github.com/0x113/librus-api-go/mocks"
"github.com/stretchr/testify/assert"
)
func TestSuccessGetLuckyNumber(t *testing.T) {
client := &mocks.MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
json := `{"LuckyNumber": {"LuckyNumber": 1, "LuckyNumberDay": "2020-01-03"}}`
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(json))),
}, nil
},
}
l := &golibrus.Librus{Client: client}
// set authorization token
golibrus.Headers[0].Value = "Bearer HESOYAM"
luckyNumber, err := l.GetLuckyNumber()
assert.Nil(t, err)
assert.Equal(t, 1, luckyNumber.LuckyNumber)
assert.Equal(t, "2020-01-03", luckyNumber.LuckyNumberDay)
}
func TestFailGetLuckyNumber(t *testing.T) {
client := &mocks.MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
json := `{"LuckyNumber": "1", "LuckyNumberDay": "2029-01-93"}` // invalid json (LuckyNumber is string instead of int)
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(json))),
}, nil
},
}
l := &golibrus.Librus{Client: client}
// set authorization token
golibrus.Headers[0].Value = "Bearer HESOYAM"
luckyNumber, err := l.GetLuckyNumber()
assert.NotNil(t, err)
assert.Nil(t, luckyNumber)
}
func TestInternalServerErrorGetLuckyNumber(t *testing.T) {
client := &mocks.MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
}, nil
},
}
l := &golibrus.Librus{Client: client}
// set authorization token
golibrus.Headers[0].Value = "Bearer HESOYAM"
luckyNumber, err := l.GetLuckyNumber()
assert.NotNil(t, err)
assert.Nil(t, luckyNumber)
}