-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_test.go
57 lines (48 loc) · 1.29 KB
/
user_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
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 TestSuccessGetUser(t *testing.T) {
client := &mocks.MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
json := `{"User": {"FirstName": "John", "LastName": "Doe"}}`
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"
expectedUser := &golibrus.User{
FirstName: "John",
LastName: "Doe",
}
user, err := l.GetUser(123)
assert.Nil(t, err)
assert.Equal(t, expectedUser, user)
}
func TestFailGetUser(t *testing.T) {
client := &mocks.MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
json := `{"User": 1}` // invalid json
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"
user, err := l.GetUser(123)
assert.NotNil(t, err)
assert.Nil(t, user)
}