Skip to content

Commit 87ef416

Browse files
committed
interfacing http operations to facilitate testing
1 parent dec831c commit 87ef416

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

httpclient/client.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,37 @@ package httpclient
99

1010
import (
1111
"fmt"
12+
"io"
1213
"net/http"
14+
"net/url"
1315
"time"
1416

1517
"github.com/deploymenttheory/go-api-http-client/concurrency"
1618
"go.uber.org/zap"
1719
)
1820

19-
const ()
20-
21-
// TODO all struct comments
21+
type httpExecutor interface {
22+
// Inherited
23+
CloseIdleConnections()
24+
Do(req *http.Request) (*http.Response, error)
25+
Get(url string) (resp *http.Response, err error)
26+
Head(url string) (resp *http.Response, err error)
27+
Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)
28+
PostForm(url string, data url.Values) (resp *http.Response, err error)
29+
30+
// Additional
31+
SetCookieJar(jar http.CookieJar)
32+
SetCookies(url *url.URL, cookies []*http.Cookie)
33+
SetCustomTimeout(time.Duration)
34+
Cookies(*url.URL) []*http.Cookie
35+
SetRedirectPolicy(*func(req *http.Request, via []*http.Request) error)
36+
}
2237

2338
// Master struct/object
2439
type Client struct {
2540
config *ClientConfig
2641
Integration *APIIntegration
27-
http *http.Client
42+
http httpExecutor
2843
Sugar *zap.SugaredLogger
2944
Concurrency *concurrency.ConcurrencyHandler
3045
}
@@ -104,12 +119,10 @@ func (c *ClientConfig) Build() (*Client, error) {
104119

105120
c.Sugar.Debug("configuration valid")
106121

107-
httpClient := &http.Client{
108-
Timeout: c.CustomTimeout,
109-
}
122+
httpClient := &prodClient{}
110123

111124
if c.CustomRedirectPolicy != nil {
112-
httpClient.CheckRedirect = *c.CustomRedirectPolicy
125+
httpClient.SetRedirectPolicy(c.CustomRedirectPolicy)
113126
}
114127

115128
// TODO refactor concurrency

httpclient/cookies.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ func (c *Client) loadCustomCookies() error {
1414
return err
1515
}
1616

17-
c.http.Jar = cookieJar
17+
c.http.SetCookieJar(cookieJar)
1818

1919
cookieUrl, err := url.Parse((*c.Integration).GetFQDN())
2020
c.Sugar.Debug("cookie URL set globally to: %s", cookieUrl)
2121
if err != nil {
2222
return err
2323
}
2424

25-
c.http.Jar.SetCookies(cookieUrl, c.config.CustomCookies)
25+
c.http.SetCookies(cookieUrl, c.config.CustomCookies)
2626

2727
if c.config.HideSensitiveData {
2828
c.Sugar.Debug("[REDACTED] cookies set successfully")
2929
} else {
30-
c.Sugar.Debug("custom cookies set: %v", c.http.Jar.Cookies(cookieUrl))
30+
c.Sugar.Debug("custom cookies set: %v", c.http.Cookies(cookieUrl))
3131
}
3232

3333
return nil

httpclient/http.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package httpclient
2+
3+
import (
4+
"net/http"
5+
"net/url"
6+
"time"
7+
)
8+
9+
type prodClient struct {
10+
*http.Client
11+
}
12+
13+
func (c *prodClient) SetCookieJar(jar http.CookieJar) {
14+
c.Jar = jar
15+
}
16+
17+
func (c *prodClient) SetCookies(url *url.URL, cookies []*http.Cookie) {
18+
c.Jar.SetCookies(url, cookies)
19+
}
20+
21+
func (c *prodClient) SetCustomTimeout(timeout time.Duration) {
22+
c.Timeout = timeout
23+
}
24+
25+
func (c *prodClient) Cookies(url *url.URL) []*http.Cookie {
26+
return c.Jar.Cookies(url)
27+
}
28+
29+
func (c *prodClient) SetRedirectPolicy(policy *func(req *http.Request, via []*http.Request) error) {
30+
c.CheckRedirect = *policy
31+
}

mock/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package mock

0 commit comments

Comments
 (0)