1
1
package httpclient
2
2
3
3
import (
4
+ "fmt"
5
+ "io"
4
6
"net/http"
5
7
"net/url"
6
8
"time"
7
9
)
8
10
11
+ // Production
12
+
9
13
type ProdClient struct {
10
14
* http.Client
11
15
}
@@ -30,10 +34,52 @@ func (c *ProdClient) SetRedirectPolicy(policy *func(req *http.Request, via []*ht
30
34
c .CheckRedirect = * policy
31
35
}
32
36
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 )
34
61
}
35
62
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 )
39
65
}
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