@@ -9,43 +9,49 @@ import (
9
9
"time"
10
10
)
11
11
12
- // Production
13
-
12
+ // ProdExecutor wraps http.Client and implements functions to adjust some of it's attrs
14
13
type ProdExecutor struct {
15
14
* http.Client
16
15
}
17
16
17
+ // SetCookieJar func to wrap c.Jar = jar
18
18
func (c * ProdExecutor ) SetCookieJar (jar http.CookieJar ) {
19
19
c .Jar = jar
20
20
}
21
21
22
+ // SetCookies wraps http.Client.Jar.SetCookies
22
23
func (c * ProdExecutor ) SetCookies (url * url.URL , cookies []* http.Cookie ) {
23
24
c .Jar .SetCookies (url , cookies )
24
25
}
25
26
27
+ // SetCustomTimeout wraps http.Client.Timeout = timeout
26
28
func (c * ProdExecutor ) SetCustomTimeout (timeout time.Duration ) {
27
29
c .Timeout = timeout
28
30
}
29
31
32
+ // Cookies wraps http.Client.Jar.Cookies()
30
33
func (c * ProdExecutor ) Cookies (url * url.URL ) []* http.Cookie {
31
34
return c .Jar .Cookies (url )
32
35
}
33
36
37
+ // SetRedirectPolicy wraps http.Client.Jar.CheckRedirect =
34
38
func (c * ProdExecutor ) SetRedirectPolicy (policy * func (req * http.Request , via []* http.Request ) error ) {
35
39
c .CheckRedirect = * policy
36
40
}
37
41
38
42
// Mocking
39
43
44
+ // MockExecutor implements the same function pattern above but allows controllable responses for mocking/testing
40
45
type MockExecutor struct {
41
46
LockedResponseCode int
42
47
ResponseBody string
43
48
}
44
49
50
+ // CloseIdleConnections does nothing.
45
51
func (m * MockExecutor ) CloseIdleConnections () {
46
- panic ("invalid function call" )
47
52
}
48
53
54
+ // Do returns a http.Response with controllable body and status code.
49
55
func (m * MockExecutor ) Do (req * http.Request ) (* http.Response , error ) {
50
56
statusString := http .StatusText (m .LockedResponseCode )
51
57
@@ -62,30 +68,39 @@ func (m *MockExecutor) Do(req *http.Request) (*http.Response, error) {
62
68
return response , nil
63
69
}
64
70
71
+ // Get returns Do
65
72
func (m * MockExecutor ) Get (_ string ) (* http.Response , error ) {
66
73
return m .Do (nil )
67
74
}
68
75
76
+ // Head returns Do
69
77
func (m * MockExecutor ) Head (_ string ) (* http.Response , error ) {
70
78
return m .Do (nil )
71
79
}
72
80
81
+ // Post returns Do
73
82
func (m * MockExecutor ) Post (_ string , _ string , _ io.Reader ) (* http.Response , error ) {
74
83
return m .Do (nil )
75
84
}
76
85
86
+ // PostForm returns Do
77
87
func (m * MockExecutor ) PostForm (_ string , _ url.Values ) (* http.Response , error ) {
78
88
return m .Do (nil )
79
89
}
80
90
91
+ // SetCookieJar does nothing yet
81
92
func (m * MockExecutor ) SetCookieJar (jar http.CookieJar ) {}
82
93
94
+ // SetCookies does nothing yet
83
95
func (m * MockExecutor ) SetCookies (url * url.URL , cookies []* http.Cookie ) {}
84
96
97
+ // SetCustomTimeout does nothing yet
85
98
func (m * MockExecutor ) SetCustomTimeout (time.Duration ) {}
86
99
100
+ // Cookies does nothing yet
87
101
func (m * MockExecutor ) Cookies (* url.URL ) []* http.Cookie {
88
102
return nil
89
103
}
90
104
105
+ // SetRedirectPolicy does nothing
91
106
func (m * MockExecutor ) SetRedirectPolicy (* func (req * http.Request , via []* http.Request ) error ) {}
0 commit comments