This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 774
/
mock_http_util.go
118 lines (103 loc) · 3.82 KB
/
mock_http_util.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
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
"time"
)
type postJSONFunc = func(url string, body interface{},
timeout time.Duration) (int, []byte, error)
type getFunc = func(url string, timeout time.Duration) (int, []byte, error)
type postJSONWithHeadersFunc = func(url string, headers map[string]string, body interface{},
timeout time.Duration) (int, []byte, error)
type getWithHeadersFunc = func(url string, headers map[string]string,
timeout time.Duration) (int, []byte, error)
// MockHTTPClient fakes a customized implementation of util.SimpleHTTPClient.
type MockHTTPClient struct {
PostJSONFunc postJSONFunc
GetFunc getFunc
PostJSONWithHeadersFunc postJSONWithHeadersFunc
GetWithHeadersFunc getWithHeadersFunc
}
// NewMockHTTPClient returns a new MockHTTPClient instance.
func NewMockHTTPClient() *MockHTTPClient {
return &MockHTTPClient{}
}
// PostJSON mocks base method.
func (m *MockHTTPClient) PostJSON(url string, body interface{}, timeout time.Duration) (
int, []byte, error) {
if m.PostJSONFunc != nil {
return m.PostJSONFunc(url, body, timeout)
}
return 0, nil, nil
}
// Get mocks base method.
func (m *MockHTTPClient) Get(url string, timeout time.Duration) (int, []byte, error) {
if m.GetFunc != nil {
return m.GetFunc(url, timeout)
}
return 0, nil, nil
}
// PostJSONWithHeaders mocks base method.
func (m *MockHTTPClient) PostJSONWithHeaders(url string, headers map[string]string, body interface{}, timeout time.Duration) (
int, []byte, error) {
if m.PostJSONWithHeadersFunc != nil {
return m.PostJSONWithHeadersFunc(url, headers, body, timeout)
}
return 0, nil, nil
}
// GetWithHeaders mocks base method.
func (m *MockHTTPClient) GetWithHeaders(url string, headers map[string]string, timeout time.Duration) (
int, []byte, error) {
if m.GetWithHeadersFunc != nil {
return m.GetWithHeadersFunc(url, headers, timeout)
}
return 0, nil, nil
}
// Reset the MockHTTPClient.
func (m *MockHTTPClient) Reset() {
m.PostJSONFunc = nil
m.GetFunc = nil
m.PostJSONWithHeadersFunc = nil
m.GetWithHeadersFunc = nil
}
// CreatePostJSONFunc returns a mock postJSONFunc func
// which will always return the specific results.
func (m *MockHTTPClient) CreatePostJSONFunc(code int, res []byte, e error) postJSONFunc {
return func(string, interface{}, time.Duration) (int, []byte, error) {
return code, res, e
}
}
// CreateGetFunc returns a mock getFunc func
// which will always return the specific results.
func (m *MockHTTPClient) CreateGetFunc(code int, res []byte, e error) getFunc {
return func(string, time.Duration) (int, []byte, error) {
return code, res, e
}
}
// CreatePostJSONWithHeadersFunc returns a mock postJSONWithHeadersFunc func
/// which will always return the specific results.
func (m *MockHTTPClient) CreatePostJSONWithHeadersFunc(code int, res []byte, e error) postJSONWithHeadersFunc {
return func(string, map[string]string, interface{}, time.Duration) (int, []byte, error) {
return code, res, e
}
}
// CreateGetWithHeadersFunc returns a mock getWithHeadersFunc func
// which will always return the specific results.
func (m *MockHTTPClient) CreateGetWithHeadersFunc(code int, res []byte, e error) getWithHeadersFunc {
return func(string, map[string]string, time.Duration) (int, []byte, error) {
return code, res, e
}
}