-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.go
78 lines (70 loc) · 2.73 KB
/
lib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gokhttp
import (
"crypto/tls"
"crypto/x509"
"fmt"
gokhttp_client "github.com/BRUHItsABunny/gOkHttp/client"
"net/http"
"os"
)
func NewHTTPClient(options ...gokhttp_client.Option) (*http.Client, error) {
return gokhttp_client.NewHTTPClient(options...)
}
// 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
func NewTestHTTPClient(baseOpts []gokhttp_client.Option, options ...gokhttp_client.Option) (*http.Client, error) {
if os.Getenv("USE_PROXY") == "true" {
baseOpts = append(baseOpts, gokhttp_client.NewProxyOption(os.Getenv("PROXY_URL")))
}
if os.Getenv("USE_MTLS") == "true" {
opt := gokhttp_client.NewMTLSOption(x509.NewCertPool(), []tls.Certificate{})
err := opt.AddCAFromFile(os.Getenv("CA_CERT"))
if err != nil {
return nil, fmt.Errorf("opt.AddCAFromFile: %w", err)
}
err = opt.AddClientCertFromFile(os.Getenv("CLIENT_CERT"), os.Getenv("CLIENT_KEY"))
if err != nil {
return nil, fmt.Errorf("opt.AddClientCertFromFile: %w", err)
}
baseOpts = append(baseOpts, opt)
}
if os.Getenv("USE_WIRESHARK") == "true" {
opt, err := gokhttp_client.NewTLSKeyLoggingOptionToFile(os.Getenv("WIRESHARK_LOGFILE"))
if err != nil {
return nil, fmt.Errorf("gokhttp_client.NewTLSKeyLoggingOptionToFile: %w", err)
}
baseOpts = append(baseOpts, opt)
}
baseOpts = append(baseOpts, options...)
hClient, err := gokhttp_client.NewHTTPClient(baseOpts...)
return hClient, err
}
// 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
func TestHTTPClient(options ...gokhttp_client.Option) (*http.Client, error) {
hClient := http.DefaultClient
opts := []gokhttp_client.Option{}
if os.Getenv("USE_PROXY") == "true" {
opts = append(opts, gokhttp_client.NewProxyOption(os.Getenv("PROXY_URL")))
}
if os.Getenv("USE_MTLS") == "true" {
opt := gokhttp_client.NewMTLSOption(x509.NewCertPool(), []tls.Certificate{})
err := opt.AddCAFromFile(os.Getenv("CA_CERT"))
if err != nil {
return nil, fmt.Errorf("opt.AddCAFromFile: %w", err)
}
err = opt.AddClientCertFromFile(os.Getenv("CLIENT_CERT"), os.Getenv("CLIENT_KEY"))
if err != nil {
return nil, fmt.Errorf("opt.AddClientCertFromFile: %w", err)
}
opts = append(opts, opt)
}
if os.Getenv("USE_WIRESHARK") == "true" {
opt, err := gokhttp_client.NewTLSKeyLoggingOptionToFile(os.Getenv("WIRESHARK_LOGFILE"))
if err != nil {
return nil, fmt.Errorf("gokhttp_client.NewTLSKeyLoggingOptionToFile: %w", err)
}
opts = append(opts, opt)
}
opts = append(opts, options...)
hClient, err := gokhttp_client.NewHTTPClient(opts...)
return hClient, err
}