@@ -24,6 +24,7 @@ import (
24
24
"net/url"
25
25
"os"
26
26
"strings"
27
+ "sync/atomic"
27
28
"testing"
28
29
"time"
29
30
@@ -48,7 +49,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
48
49
gitImpl git.Implementation
49
50
url string
50
51
branch string
51
- setupGitProxy func (g * WithT , proxy * goproxy.ProxyHttpServer , proxyGotRequest * bool ) (* git.AuthOptions , cleanupFunc )
52
+ setupGitProxy func (g * WithT , proxy * goproxy.ProxyHttpServer , proxiedRequests * int32 ) (* git.AuthOptions , cleanupFunc )
52
53
shortTimeout bool
53
54
wantUsedProxy bool
54
55
wantError bool
@@ -69,7 +70,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
69
70
gitImpl : libgit2 .Implementation ,
70
71
url : "https://example.com/bar/test-reponame" ,
71
72
branch : "main" ,
72
- setupGitProxy : func (g * WithT , proxy * goproxy.ProxyHttpServer , proxyGotRequest * bool ) (* git.AuthOptions , cleanupFunc ) {
73
+ setupGitProxy : func (g * WithT , proxy * goproxy.ProxyHttpServer , proxiedRequests * int32 ) (* git.AuthOptions , cleanupFunc ) {
73
74
// Create the git server.
74
75
gitServer , err := gittestserver .NewTempGitServer ()
75
76
g .Expect (err ).ToNot (HaveOccurred ())
@@ -103,7 +104,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
103
104
// Check if the host matches with the git server address and the user-agent is the expected git client.
104
105
userAgent := ctx .Req .Header .Get ("User-Agent" )
105
106
if strings .Contains (host , "example.com" ) && strings .Contains (userAgent , "libgit2" ) {
106
- * proxyGotRequest = true
107
+ atomic . AddInt32 ( proxiedRequests , 1 )
107
108
return goproxy .OkConnect , u .Host
108
109
}
109
110
// Reject if it isn't our request.
@@ -130,7 +131,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
130
131
gitImpl : gogit .Implementation ,
131
132
url : "http://example.com/bar/test-reponame" ,
132
133
branch : "main" ,
133
- setupGitProxy : func (g * WithT , proxy * goproxy.ProxyHttpServer , proxyGotRequest * bool ) (* git.AuthOptions , cleanupFunc ) {
134
+ setupGitProxy : func (g * WithT , proxy * goproxy.ProxyHttpServer , proxiedRequests * int32 ) (* git.AuthOptions , cleanupFunc ) {
134
135
// Create the git server.
135
136
gitServer , err := gittestserver .NewTempGitServer ()
136
137
g .Expect (err ).ToNot (HaveOccurred ())
@@ -153,7 +154,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
153
154
var proxyHandler goproxy.FuncReqHandler = func (req * http.Request , ctx * goproxy.ProxyCtx ) (* http.Request , * http.Response ) {
154
155
userAgent := req .Header .Get ("User-Agent" )
155
156
if strings .Contains (req .Host , "example.com" ) && strings .Contains (userAgent , "git" ) {
156
- * proxyGotRequest = true
157
+ atomic . AddInt32 ( proxiedRequests , 1 )
157
158
req .Host = u .Host
158
159
req .URL .Host = req .Host
159
160
return req , nil
@@ -181,13 +182,13 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
181
182
gitImpl : gogit .Implementation ,
182
183
url : "https://github.com/git-fixtures/basic" ,
183
184
branch : "master" ,
184
- setupGitProxy : func (g * WithT , proxy * goproxy.ProxyHttpServer , proxyGotRequest * bool ) (* git.AuthOptions , cleanupFunc ) {
185
+ setupGitProxy : func (g * WithT , proxy * goproxy.ProxyHttpServer , proxiedRequests * int32 ) (* git.AuthOptions , cleanupFunc ) {
185
186
var proxyHandler goproxy.FuncHttpsHandler = func (host string , ctx * goproxy.ProxyCtx ) (* goproxy.ConnectAction , string ) {
186
187
// We don't check for user agent as this handler is only going to process CONNECT requests, and because Go's net/http
187
188
// is the one making such a request on behalf of go-git, adding a check for the go net/http user agent (Go-http-client)
188
189
// would only allow false positives from any request originating from Go's net/http.
189
190
if strings .Contains (host , "github.com" ) {
190
- * proxyGotRequest = true
191
+ atomic . AddInt32 ( proxiedRequests , 1 )
191
192
return goproxy .OkConnect , host
192
193
}
193
194
// Reject if it isnt our request.
@@ -208,10 +209,10 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
208
209
gitImpl : gogit .Implementation ,
209
210
url : "https://192.0.2.1/bar/test-reponame" ,
210
211
branch : "main" ,
211
- setupGitProxy : func (g * WithT , proxy * goproxy.ProxyHttpServer , proxyGotRequest * bool ) (* git.AuthOptions , cleanupFunc ) {
212
+ setupGitProxy : func (g * WithT , proxy * goproxy.ProxyHttpServer , proxiedRequests * int32 ) (* git.AuthOptions , cleanupFunc ) {
212
213
var proxyHandler goproxy.FuncHttpsHandler = func (host string , ctx * goproxy.ProxyCtx ) (* goproxy.ConnectAction , string ) {
213
214
// We shouldn't hit the proxy so we just want to check for any interaction, then reject.
214
- * proxyGotRequest = true
215
+ atomic . AddInt32 ( proxiedRequests , 1 )
215
216
return goproxy .RejectConnect , host
216
217
}
217
218
proxy .OnRequest ().HandleConnect (proxyHandler )
@@ -235,8 +236,8 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
235
236
proxy := goproxy .NewProxyHttpServer ()
236
237
proxy .Verbose = true
237
238
238
- proxyGotRequest := false
239
- authOpts , cleanup := tt .setupGitProxy (g , proxy , & proxyGotRequest )
239
+ proxiedRequests := int32 ( 0 )
240
+ authOpts , cleanup := tt .setupGitProxy (g , proxy , & proxiedRequests )
240
241
defer cleanup ()
241
242
242
243
proxyServer := http.Server {
@@ -281,7 +282,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
281
282
g .Expect (err ).ToNot (HaveOccurred ())
282
283
}
283
284
284
- g .Expect (proxyGotRequest ).To (Equal (tt .wantUsedProxy ))
285
+ g .Expect (atomic . LoadInt32 ( & proxiedRequests ) > 0 ).To (Equal (tt .wantUsedProxy ))
285
286
286
287
})
287
288
}
0 commit comments