Skip to content

Commit 9028167

Browse files
committed
testing implementations
1 parent feb620c commit 9028167

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

httpclient/http.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package httpclient
22

33
import (
4+
"fmt"
5+
"io"
46
"net/http"
57
"net/url"
68
"time"
79
)
810

11+
// Production
12+
913
type ProdClient struct {
1014
*http.Client
1115
}
@@ -30,10 +34,52 @@ func (c *ProdClient) SetRedirectPolicy(policy *func(req *http.Request, via []*ht
3034
c.CheckRedirect = *policy
3135
}
3236

33-
type testClient struct {
37+
// Mocking
38+
39+
type mockClient struct {
40+
lockedResponseCode int
41+
}
42+
43+
func (m *mockClient) CloseIdleConnections() {
44+
panic("invalid function call")
45+
}
46+
47+
func (m *mockClient) Do(req *http.Request) (*http.Response, error) {
48+
statusString := http.StatusText(m.lockedResponseCode)
49+
50+
if statusString == "" {
51+
return nil, fmt.Errorf("unknown response code requested: %d", m.lockedResponseCode)
52+
}
53+
54+
response := &http.Response{StatusCode: m.lockedResponseCode}
55+
56+
return response, nil
57+
}
58+
59+
func (m *mockClient) Get(_ string) (*http.Response, error) {
60+
return m.Do(nil)
3461
}
3562

36-
func (m *testClient) Do(req *http.Request) (*http.Response, error) {
37-
// do some stuff which makes a response you like
38-
return nil, nil
63+
func (m *mockClient) Head(_ string) (*http.Response, error) {
64+
return m.Do(nil)
3965
}
66+
67+
func (m *mockClient) Post(_ string, _ string, _ io.Reader) (*http.Response, error) {
68+
return m.Do(nil)
69+
}
70+
71+
func (m *mockClient) PostForm(_ string, _ url.Values) (*http.Response, error) {
72+
return m.Do(nil)
73+
}
74+
75+
func (m *mockClient) SetCookieJar(jar http.CookieJar) {}
76+
77+
func (m *mockClient) SetCookies(url *url.URL, cookies []*http.Cookie) {}
78+
79+
func (m *mockClient) SetCustomTimeout(time.Duration) {}
80+
81+
func (m *mockClient) Cookies(*url.URL) []*http.Cookie {
82+
return nil
83+
}
84+
85+
func (m *mockClient) SetRedirectPolicy(*func(req *http.Request, via []*http.Request) error) {}

0 commit comments

Comments
 (0)