Debian Codegraph @

plumbing/transport/http/internal/test/proxy_test.go

raw
1 package test
2
3 import (
4 "context"
5 "crypto/tls"
6 "fmt"
7 "net"
8 nethttp "net/http"
9 "os"
10 "sync/atomic"
11 "testing"
12
13 "github.com/elazarl/goproxy"
14
15 "github.com/go-git/go-git/v5/plumbing/transport"
16 "github.com/go-git/go-git/v5/plumbing/transport/http"
17
18 . "gopkg.in/check.v1"
19 )
20
21 // Hook up gocheck into the "go test" runner.
22 func Test(t *testing.T) { TestingT(t) }
23
24 type ProxySuite struct{}
25
26 var _ = Suite(&ProxySuite{})
27
28 var proxiedRequests int32
29
30 // This test tests proxy support via an env var, i.e. `HTTPS_PROXY`.
31 // Its located in a separate package because golang caches the value
32 // of proxy env vars leading to misleading/unexpected test results.
33 func (s *ProxySuite) TestAdvertisedReferences(c *C) {
34 proxy := goproxy.NewProxyHttpServer()
35 proxy.Verbose = true
36 SetupHTTPSProxy(proxy, &proxiedRequests)
37 httpsListener, err := net.Listen("tcp", ":0")
38 c.Assert(err, IsNil)
39 defer httpsListener.Close()
40 httpProxyAddr := fmt.Sprintf("localhost:%d", httpsListener.Addr().(*net.TCPAddr).Port)
41
42 proxyServer := nethttp.Server{
43 Addr: httpProxyAddr,
44 Handler: proxy,
45 // Due to how golang manages http/2 when provided with custom TLS config,
46 // servers and clients running in the same process leads to issues.
47 // Ref: https://github.com/golang/go/issues/21336
48 TLSConfig: &tls.Config{
49 NextProtos: []string{"http/1.1"},
50 },
51 }
52 go proxyServer.ServeTLS(httpsListener, "../../testdata/certs/server.crt", "../../testdata/certs/server.key")
53 defer proxyServer.Close()
54 os.Setenv("HTTPS_PROXY", fmt.Sprintf("https://user:pass@%s", httpProxyAddr))
55 defer os.Unsetenv("HTTPS_PROXY")
56
57 endpoint, err := transport.NewEndpoint("https://github.com/git-fixtures/basic.git")
58 c.Assert(err, IsNil)
59 endpoint.InsecureSkipTLS = true
60
61 client := http.DefaultClient
62 session, err := client.NewUploadPackSession(endpoint, nil)
63 c.Assert(err, IsNil)
64
65 ctx, cancel := context.WithCancel(context.Background())
66 defer cancel()
67 info, err := session.AdvertisedReferencesContext(ctx)
68 c.Assert(err, IsNil)
69 c.Assert(info, NotNil)
70 proxyUsed := atomic.LoadInt32(&proxiedRequests) > 0
71 c.Assert(proxyUsed, Equals, true)
72 }