diff options
author | Paulo Gomes <pjbgf@linux.com> | 2023-05-04 22:10:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-04 22:10:19 +0100 |
commit | 191f4ba946c768221dd914fcf0675572fc36c55d (patch) | |
tree | 52c3450287073b3d8f65b2c001f9c7150cc66b9d /plumbing/transport/http/internal/test/test_utils.go | |
parent | da73c5f950fb399611e3eb608f6ee99f23eb53b5 (diff) | |
parent | a830187d90a6bc36f9466c075ed49076f591efa9 (diff) | |
download | go-git-191f4ba946c768221dd914fcf0675572fc36c55d.tar.gz |
Merge pull request #744 from aryan9600/proxy-options
Add support for custom proxy settings
Diffstat (limited to 'plumbing/transport/http/internal/test/test_utils.go')
-rw-r--r-- | plumbing/transport/http/internal/test/test_utils.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/plumbing/transport/http/internal/test/test_utils.go b/plumbing/transport/http/internal/test/test_utils.go new file mode 100644 index 0000000..6665fb3 --- /dev/null +++ b/plumbing/transport/http/internal/test/test_utils.go @@ -0,0 +1,43 @@ +package test + +import ( + "encoding/base64" + "strings" + "sync/atomic" + + "github.com/elazarl/goproxy" +) + +func SetupHTTPSProxy(proxy *goproxy.ProxyHttpServer, proxiedRequests *int32) { + var proxyHandler goproxy.FuncHttpsHandler = func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) { + if strings.Contains(host, "github.com") { + user, pass, _ := ParseBasicAuth(ctx.Req.Header.Get("Proxy-Authorization")) + if user != "user" || pass != "pass" { + return goproxy.RejectConnect, host + } + atomic.AddInt32(proxiedRequests, 1) + return goproxy.OkConnect, host + } + // Reject if it isn't our request. + return goproxy.RejectConnect, host + } + proxy.OnRequest().HandleConnect(proxyHandler) +} + +// adapted from https://github.com/golang/go/blob/2ef70d9d0f98832c8103a7968b195e560a8bb262/src/net/http/request.go#L959 +func ParseBasicAuth(auth string) (username, password string, ok bool) { + const prefix = "Basic " + if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) { + return "", "", false + } + c, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) + if err != nil { + return "", "", false + } + cs := string(c) + username, password, ok = strings.Cut(cs, ":") + if !ok { + return "", "", false + } + return username, password, true +} |