-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
139 lines (116 loc) · 4.37 KB
/
http_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package budpay
import (
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
const (
testBaseURL = "notreal.com"
)
type RoundTripFunc func(req *http.Request) *http.Response
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
func TestGet(t *testing.T) {
requestResponseJSON := `{"status":true,"message":"Successful Message","data":["randomData"]}`
hclient := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
require.Equal(t, testBaseURL+"v2/anyRoute", req.URL.String())
require.Equal(t, http.MethodGet, req.Method)
require.Equal(t, "application/json", req.Header.Get("Accept"))
require.Equal(t, "application/json", req.Header.Get("Content-Type"))
require.Equal(t, "Bearer testApiKey", req.Header.Get(AuthorizationHeaderKey))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(requestResponseJSON)),
}
})}
client := BudPayClient{HTTPClient: hclient, apiKey: "testApiKey", encryptionkey: []byte("encryptKey"), BaseURL: testBaseURL + "v2/"}
expectedResponse := &struct {
Status bool `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}{}
err := client.Get("anyRoute", expectedResponse)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestPost(t *testing.T) {
requestResponseJSON := `{"status":true,"message":"Successful Message","data":["randomData"]}`
hclient := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
require.Equal(t, testBaseURL+"v2/anyRoute", req.URL.String())
require.Equal(t, http.MethodPost, req.Method)
require.Equal(t, "application/json", req.Header.Get("Accept"))
require.Equal(t, "application/json", req.Header.Get("Content-Type"))
require.Equal(t, "Bearer testApiKey", req.Header.Get(AuthorizationHeaderKey))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(requestResponseJSON)),
}
})}
client := BudPayClient{HTTPClient: hclient, apiKey: "testApiKey", encryptionkey: []byte("encryptKey"), BaseURL: testBaseURL + "v2/"}
expectedResponse := &struct {
Status bool `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}{}
err := client.Post("anyRoute", "randomBody", expectedResponse)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestDelete(t *testing.T) {
requestResponseJSON := `{"status":true,"message":"Successful Message","data":["randomData"]}`
hclient := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
require.Equal(t, testBaseURL+"v2/anyRoute", req.URL.String())
require.Equal(t, http.MethodDelete, req.Method)
require.Equal(t, "application/json", req.Header.Get("Accept"))
require.Equal(t, "application/json", req.Header.Get("Content-Type"))
require.Equal(t, "Bearer testApiKey", req.Header.Get(AuthorizationHeaderKey))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(requestResponseJSON)),
}
})}
client := BudPayClient{HTTPClient: hclient, apiKey: "testApiKey", encryptionkey: []byte("encryptKey"), BaseURL: testBaseURL + "v2/"}
expectedResponse := &struct {
Status bool `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}{}
err := client.Delete("anyRoute", expectedResponse)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestGenerateRequest(t *testing.T) {
client := BudPayClient{apiKey: "testApiKey", encryptionkey: []byte("encryptKey"), BaseURL: testBaseURL + "v2/"}
testCases := []struct {
method string
endpoint string
body interface{}
}{
{
method: http.MethodPost,
endpoint: "randomEndpoint",
body: &struct{}{},
},
{
method: http.MethodGet,
endpoint: "randomEndpoint",
body: nil,
},
}
for _, tc := range testCases {
req, err := client.generateRequest(tc.method, tc.endpoint, tc.body)
require.NoError(t, err)
require.NotNil(t, req)
require.Equal(t, testBaseURL+"v2/randomEndpoint", req.URL.String())
require.Equal(t, tc.method, req.Method)
require.Equal(t, "application/json", req.Header.Get("Accept"))
require.Equal(t, "application/json", req.Header.Get("Content-Type"))
require.Equal(t, "Bearer testApiKey", req.Header.Get(AuthorizationHeaderKey))
}
}