-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
56 lines (45 loc) · 1.04 KB
/
api_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
package qrand
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type MockClient struct {
MockDo func(req *http.Request) (*http.Response, error)
}
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
return m.MockDo(req)
}
func setupMockClient(mockResponse string, statusCode int, err error) {
r := ioutil.NopCloser(bytes.NewReader([]byte(mockResponse)))
Client = &MockClient{
MockDo: func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: statusCode,
Body: r,
}, err
},
}
}
func teardownMockClient() {
Client = &http.Client{}
}
func TestAPICallSuccess(t *testing.T) {
mockResponse := `{
"type": "string",
"length": 1,
"size": 10,
"data": [
"0195a96a618e47f02bbf"
],
"success": true
}`
setupMockClient(mockResponse, 200, nil)
response, err := Get(1, "hex16", 32)
require.NoError(t, err)
assert.Equal(t, response.Success, true)
teardownMockClient()
}