Skip to content

Commit 40a046d

Browse files
committed
feat: add ability to run options before the test options from env
1 parent cac27d4 commit 40a046d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib.go

+31
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,37 @@ func NewHTTPClient(options ...gokhttp_client.Option) (*http.Client, error) {
1313
return gokhttp_client.NewHTTPClient(options...)
1414
}
1515

16+
// NewTestHTTPClient is a function i end up using a lot in test files to control how my http.Client is initialized depending on the ENV variables USE_PROXY and PROXY_URL
17+
func NewTestHTTPClient(baseOpts []gokhttp_client.Option, options ...gokhttp_client.Option) (*http.Client, error) {
18+
if os.Getenv("USE_PROXY") == "true" {
19+
baseOpts = append(baseOpts, gokhttp_client.NewProxyOption(os.Getenv("PROXY_URL")))
20+
}
21+
if os.Getenv("USE_MTLS") == "true" {
22+
opt := gokhttp_client.NewMTLSOption(x509.NewCertPool(), []tls.Certificate{})
23+
err := opt.AddCAFromFile(os.Getenv("CA_CERT"))
24+
if err != nil {
25+
return nil, fmt.Errorf("opt.AddCAFromFile: %w", err)
26+
}
27+
err = opt.AddClientCertFromFile(os.Getenv("CLIENT_CERT"), os.Getenv("CLIENT_KEY"))
28+
if err != nil {
29+
return nil, fmt.Errorf("opt.AddClientCertFromFile: %w", err)
30+
}
31+
baseOpts = append(baseOpts, opt)
32+
}
33+
if os.Getenv("USE_WIRESHARK") == "true" {
34+
opt, err := gokhttp_client.NewTLSKeyLoggingOptionToFile(os.Getenv("WIRESHARK_LOGFILE"))
35+
if err != nil {
36+
return nil, fmt.Errorf("gokhttp_client.NewTLSKeyLoggingOptionToFile: %w", err)
37+
}
38+
baseOpts = append(baseOpts, opt)
39+
}
40+
41+
baseOpts = append(baseOpts, options...)
42+
43+
hClient, err := gokhttp_client.NewHTTPClient(baseOpts...)
44+
return hClient, err
45+
}
46+
1647
// TestHTTPClient is a function i end up using a lot in test files to control how my http.Client is initialized depending on the ENV variables USE_PROXY and PROXY_URL
1748
func TestHTTPClient(options ...gokhttp_client.Option) (*http.Client, error) {
1849
hClient := http.DefaultClient

0 commit comments

Comments
 (0)