forked from michimani/gotwi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_test.go
198 lines (159 loc) · 4.92 KB
/
mock_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package gotwi_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/NHypocrite/gotwi"
"github.com/NHypocrite/gotwi/internal/util"
"github.com/NHypocrite/gotwi/resources"
)
type RoundTripFunc func(req *http.Request) *http.Response
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
func newMockClient(fn RoundTripFunc) *http.Client {
return &http.Client{
Transport: RoundTripFunc(fn),
}
}
type mockInput struct {
ResponseStatusCode int
ResponseHeader map[string][]string
ResponseBody io.ReadCloser
}
func newMockHTTPClient(in *mockInput) *http.Client {
if in == nil {
return nil
}
return newMockClient(func(req *http.Request) *http.Response {
return &http.Response{
Status: "mock response status",
StatusCode: in.ResponseStatusCode,
Body: in.ResponseBody,
Header: in.ResponseHeader,
}
})
}
type mockAPIParameter struct{}
func (mp mockAPIParameter) SetAccessToken(token string) {}
func (mp mockAPIParameter) AccessToken() string { return "" }
func (mp mockAPIParameter) ResolveEndpoint(endpointBase string) string { return "" }
func (mp mockAPIParameter) Body() (io.Reader, error) { return nil, nil }
func (mp mockAPIParameter) ParameterMap() map[string]string { return map[string]string{} }
type mockAPIResponse struct{}
func (mr mockAPIResponse) HasPartialError() bool { return false }
type MockGotwiClient struct {
Client *http.Client
MockExec func(req *http.Request, i util.Response) (*resources.Non2XXError, error)
MockIsReady func() bool
MockAccessToken func() string
MockAuthenticationMethod func() gotwi.AuthenticationMethod
MockOAuthToken func() string
MockOAuthConsumerKey func() string
MockSigningKey func() string
}
func (m *MockGotwiClient) Exec(req *http.Request, i util.Response) (*resources.Non2XXError, error) {
return m.MockExec(req, i)
}
func (m *MockGotwiClient) IsReady() bool {
if m == nil {
return false
}
if !m.AuthenticationMethod().Valid() {
return false
}
switch m.AuthenticationMethod() {
case gotwi.AuthenMethodOAuth1UserContext:
if m.OAuthToken() == "" || m.SigningKey() == "" {
return false
}
case gotwi.AuthenMethodOAuth2BearerToken:
if m.AccessToken() == "" {
return false
}
}
return true
}
func (m *MockGotwiClient) AccessToken() string {
return m.MockAccessToken()
}
func (m *MockGotwiClient) AuthenticationMethod() gotwi.AuthenticationMethod {
return m.MockAuthenticationMethod()
}
func (m *MockGotwiClient) OAuthToken() string {
return m.MockAccessToken()
}
func (m *MockGotwiClient) OAuthConsumerKey() string {
return m.MockAccessToken()
}
func (m *MockGotwiClient) SigningKey() string {
return m.MockAccessToken()
}
func newMockGotwiClient(returnedToken string, execHasError, hasNot200Error bool) *MockGotwiClient {
fn := func(req *http.Request, i util.Response) (*resources.Non2XXError, error) {
if execHasError {
return nil, fmt.Errorf("has error")
}
if hasNot200Error {
return &resources.Non2XXError{}, nil
}
resBody := strings.NewReader(`{"token_type":"token_type","access_token":"` + returnedToken + `"}`)
if err := json.NewDecoder(resBody).Decode(i); err != nil {
return nil, err
}
return nil, nil
}
return &MockGotwiClient{
MockExec: fn,
}
}
type mockFuncInput struct {
MockExec func(req *http.Request, i util.Response) (*resources.Non2XXError, error)
MockIsReady func() bool
MockAccessToken func() string
MockAuthenticationMethod func() gotwi.AuthenticationMethod
MockOAuthToken func() string
MockOAuthConsumerKey func() string
MockSigningKey func() string
}
func newMockGotwiClientWithFunc(in mockFuncInput) *MockGotwiClient {
m := MockGotwiClient{}
if in.MockExec != nil {
m.MockExec = in.MockExec
} else {
m.MockExec = func(req *http.Request, i util.Response) (*resources.Non2XXError, error) { return nil, nil }
}
if in.MockIsReady != nil {
m.MockIsReady = in.MockIsReady
} else {
m.MockIsReady = func() bool { return false }
}
if in.MockAccessToken != nil {
m.MockAccessToken = in.MockAccessToken
} else {
m.MockAccessToken = func() string { return "" }
}
if in.MockAuthenticationMethod != nil {
m.MockAuthenticationMethod = in.MockAuthenticationMethod
} else {
m.MockAuthenticationMethod = func() gotwi.AuthenticationMethod { return gotwi.AuthenticationMethod("") }
}
if in.MockOAuthToken != nil {
m.MockOAuthToken = in.MockOAuthToken
} else {
m.MockOAuthToken = func() string { return "" }
}
if in.MockOAuthConsumerKey != nil {
m.MockOAuthConsumerKey = in.MockOAuthConsumerKey
} else {
m.MockOAuthConsumerKey = func() string { return "" }
}
if in.MockSigningKey != nil {
m.MockSigningKey = in.MockSigningKey
} else {
m.MockSigningKey = func() string { return "" }
}
return &m
}