| 1 |
package http |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/tls" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"net" |
| 10 |
"net/http" |
| 11 |
"net/http/cgi" |
| 12 |
"net/url" |
| 13 |
"os" |
| 14 |
"os/exec" |
| 15 |
"path/filepath" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/go-git/go-git/v5/plumbing" |
| 20 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 21 |
|
| 22 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 23 |
. "gopkg.in/check.v1" |
| 24 |
) |
| 25 |
|
| 26 |
func Test(t *testing.T) { TestingT(t) } |
| 27 |
|
| 28 |
type ClientSuite struct { |
| 29 |
Endpoint *transport.Endpoint |
| 30 |
EmptyAuth transport.AuthMethod |
| 31 |
} |
| 32 |
|
| 33 |
var _ = Suite(&ClientSuite{}) |
| 34 |
|
| 35 |
func (s *ClientSuite) SetUpSuite(c *C) { |
| 36 |
var err error |
| 37 |
s.Endpoint, err = transport.NewEndpoint( |
| 38 |
"https://github.com/git-fixtures/basic", |
| 39 |
) |
| 40 |
c.Assert(err, IsNil) |
| 41 |
} |
| 42 |
|
| 43 |
func (s *UploadPackSuite) TestNewClient(c *C) { |
| 44 |
roundTripper := &http.Transport{ |
| 45 |
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 46 |
} |
| 47 |
cl := &http.Client{Transport: roundTripper} |
| 48 |
r, ok := NewClient(cl).(*client) |
| 49 |
c.Assert(ok, Equals, true) |
| 50 |
c.Assert(r.client, Equals, cl) |
| 51 |
c.Assert(r.follow, Equals, FollowInitialRedirects) |
| 52 |
} |
| 53 |
|
| 54 |
func (s *ClientSuite) TestNewBasicAuth(c *C) { |
| 55 |
a := &BasicAuth{"foo", "qux"} |
| 56 |
|
| 57 |
c.Assert(a.Name(), Equals, "http-basic-auth") |
| 58 |
c.Assert(a.String(), Equals, "http-basic-auth - foo:*******") |
| 59 |
} |
| 60 |
|
| 61 |
func (s *ClientSuite) TestNewTokenAuth(c *C) { |
| 62 |
a := &TokenAuth{"OAUTH-TOKEN-TEXT"} |
| 63 |
|
| 64 |
c.Assert(a.Name(), Equals, "http-token-auth") |
| 65 |
c.Assert(a.String(), Equals, "http-token-auth - *******") |
| 66 |
|
| 67 |
// Check header is set correctly |
| 68 |
req, err := http.NewRequest("GET", "https://github.com/git-fixtures/basic", nil) |
| 69 |
c.Assert(err, Equals, nil) |
| 70 |
a.SetAuth(req) |
| 71 |
c.Assert(req.Header.Get("Authorization"), Equals, "Bearer OAUTH-TOKEN-TEXT") |
| 72 |
} |
| 73 |
|
| 74 |
func (s *ClientSuite) TestNewErrOK(c *C) { |
| 75 |
res := &http.Response{StatusCode: http.StatusOK} |
| 76 |
err := NewErr(res) |
| 77 |
c.Assert(err, IsNil) |
| 78 |
} |
| 79 |
|
| 80 |
func (s *ClientSuite) TestNewErrUnauthorized(c *C) { |
| 81 |
s.testNewHTTPError(c, http.StatusUnauthorized, ".*authentication required.*") |
| 82 |
} |
| 83 |
|
| 84 |
func (s *ClientSuite) TestNewErrForbidden(c *C) { |
| 85 |
s.testNewHTTPError(c, http.StatusForbidden, ".*authorization failed.*") |
| 86 |
} |
| 87 |
|
| 88 |
func (s *ClientSuite) TestNewErrNotFound(c *C) { |
| 89 |
s.testNewHTTPError(c, http.StatusNotFound, ".*repository not found.*") |
| 90 |
} |
| 91 |
|
| 92 |
func (s *ClientSuite) TestNewHTTPError40x(c *C) { |
| 93 |
s.testNewHTTPError(c, http.StatusPaymentRequired, |
| 94 |
"unexpected client error.*") |
| 95 |
} |
| 96 |
|
| 97 |
func (s *ClientSuite) TestNewUnexpectedError(c *C) { |
| 98 |
res := &http.Response{ |
| 99 |
StatusCode: 500, |
| 100 |
Body: io.NopCloser(strings.NewReader("Unexpected error")), |
| 101 |
} |
| 102 |
|
| 103 |
err := NewErr(res) |
| 104 |
c.Assert(err, NotNil) |
| 105 |
c.Assert(err, FitsTypeOf, &plumbing.UnexpectedError{}) |
| 106 |
|
| 107 |
unexpectedError, _ := err.(*plumbing.UnexpectedError) |
| 108 |
c.Assert(unexpectedError.Err, FitsTypeOf, &Err{}) |
| 109 |
|
| 110 |
httpError, _ := unexpectedError.Err.(*Err) |
| 111 |
c.Assert(httpError.Reason, Equals, "Unexpected error") |
| 112 |
} |
| 113 |
|
| 114 |
func (s *ClientSuite) Test_newSession(c *C) { |
| 115 |
cl := NewClientWithOptions(nil, &ClientOptions{ |
| 116 |
CacheMaxEntries: 3, |
| 117 |
}).(*client) |
| 118 |
|
| 119 |
insecureEP := *s.Endpoint |
| 120 |
insecureEP.InsecureSkipTLS = true |
| 121 |
session, err := newSession(cl, &insecureEP, nil) |
| 122 |
c.Assert(err, IsNil) |
| 123 |
|
| 124 |
sessionTransport := session.client.Transport.(*http.Transport) |
| 125 |
c.Assert(sessionTransport.TLSClientConfig.InsecureSkipVerify, Equals, true) |
| 126 |
t, ok := cl.fetchTransport(transportOptions{ |
| 127 |
insecureSkipTLS: true, |
| 128 |
}) |
| 129 |
// transport should be cached. |
| 130 |
c.Assert(ok, Equals, true) |
| 131 |
// cached transport should be the one that's used. |
| 132 |
c.Assert(sessionTransport, Equals, t) |
| 133 |
|
| 134 |
caEndpoint := insecureEP |
| 135 |
caEndpoint.CaBundle = []byte("this is the way") |
| 136 |
session, err = newSession(cl, &caEndpoint, nil) |
| 137 |
c.Assert(err, IsNil) |
| 138 |
|
| 139 |
sessionTransport = session.client.Transport.(*http.Transport) |
| 140 |
c.Assert(sessionTransport.TLSClientConfig.InsecureSkipVerify, Equals, true) |
| 141 |
c.Assert(sessionTransport.TLSClientConfig.RootCAs, NotNil) |
| 142 |
t, ok = cl.fetchTransport(transportOptions{ |
| 143 |
insecureSkipTLS: true, |
| 144 |
caBundle: "this is the way", |
| 145 |
}) |
| 146 |
// transport should be cached. |
| 147 |
c.Assert(ok, Equals, true) |
| 148 |
// cached transport should be the one that's used. |
| 149 |
c.Assert(sessionTransport, Equals, t) |
| 150 |
|
| 151 |
session, err = newSession(cl, &caEndpoint, nil) |
| 152 |
c.Assert(err, IsNil) |
| 153 |
sessionTransport = session.client.Transport.(*http.Transport) |
| 154 |
// transport that's going to be used should be cached already. |
| 155 |
c.Assert(sessionTransport, Equals, t) |
| 156 |
// no new transport got cached. |
| 157 |
c.Assert(cl.transports.Len(), Equals, 2) |
| 158 |
|
| 159 |
// if the cache does not exist, the transport should still be correctly configured. |
| 160 |
cl.transports = nil |
| 161 |
session, err = newSession(cl, &insecureEP, nil) |
| 162 |
c.Assert(err, IsNil) |
| 163 |
|
| 164 |
sessionTransport = session.client.Transport.(*http.Transport) |
| 165 |
c.Assert(sessionTransport.TLSClientConfig.InsecureSkipVerify, Equals, true) |
| 166 |
} |
| 167 |
|
| 168 |
func (s *ClientSuite) Test_newSessionWrapsCustomClientRedirectPolicy(c *C) { |
| 169 |
called := false |
| 170 |
customTransport := &http.Transport{} |
| 171 |
customClient := &http.Client{ |
| 172 |
Transport: customTransport, |
| 173 |
CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 174 |
called = true |
| 175 |
return nil |
| 176 |
}, |
| 177 |
} |
| 178 |
|
| 179 |
cl := NewClientWithOptions(customClient, &ClientOptions{}).(*client) |
| 180 |
session, err := newSession(cl, s.Endpoint, nil) |
| 181 |
c.Assert(err, IsNil) |
| 182 |
c.Assert(session.client, Not(Equals), customClient) |
| 183 |
c.Assert(session.client.Transport, Equals, customTransport) |
| 184 |
|
| 185 |
target, err := url.Parse("http://example.com/repo.git") |
| 186 |
c.Assert(err, IsNil) |
| 187 |
|
| 188 |
req := (&http.Request{URL: target, Header: http.Header{}}).WithContext(withInitialRequest(context.Background())) |
| 189 |
err = session.client.CheckRedirect(req, []*http.Request{{}}) |
| 190 |
c.Assert(err, IsNil) |
| 191 |
c.Assert(called, Equals, true) |
| 192 |
|
| 193 |
req = req.WithContext(context.Background()) |
| 194 |
err = session.client.CheckRedirect(req, []*http.Request{{}}) |
| 195 |
c.Assert(err, ErrorMatches, ".*non-initial request.*") |
| 196 |
} |
| 197 |
|
| 198 |
func (s *ClientSuite) testNewHTTPError(c *C, code int, msg string) { |
| 199 |
req, _ := http.NewRequest("GET", "foo", nil) |
| 200 |
res := &http.Response{ |
| 201 |
StatusCode: code, |
| 202 |
Request: req, |
| 203 |
} |
| 204 |
|
| 205 |
err := NewErr(res) |
| 206 |
c.Assert(err, NotNil) |
| 207 |
c.Assert(err, ErrorMatches, msg) |
| 208 |
} |
| 209 |
|
| 210 |
func (s *ClientSuite) TestSetAuth(c *C) { |
| 211 |
auth := &BasicAuth{} |
| 212 |
r, err := DefaultClient.NewUploadPackSession(s.Endpoint, auth) |
| 213 |
c.Assert(err, IsNil) |
| 214 |
c.Assert(auth, Equals, r.(*upSession).auth) |
| 215 |
} |
| 216 |
|
| 217 |
type mockAuth struct{} |
| 218 |
|
| 219 |
func (*mockAuth) Name() string { return "" } |
| 220 |
func (*mockAuth) String() string { return "" } |
| 221 |
|
| 222 |
func (s *ClientSuite) TestSetAuthWrongType(c *C) { |
| 223 |
_, err := DefaultClient.NewUploadPackSession(s.Endpoint, &mockAuth{}) |
| 224 |
c.Assert(err, Equals, transport.ErrInvalidAuthMethod) |
| 225 |
} |
| 226 |
|
| 227 |
func (s *ClientSuite) TestModifyEndpointIfRedirect(c *C) { |
| 228 |
sess := &session{endpoint: nil} |
| 229 |
u, _ := url.Parse("https://example.com/info/refs") |
| 230 |
res := &http.Response{Request: &http.Request{URL: u}} |
| 231 |
err := sess.ModifyEndpointIfRedirect(res) |
| 232 |
c.Assert(err, ErrorMatches, ".*nil endpoint.*") |
| 233 |
|
| 234 |
sess = &session{endpoint: nil} |
| 235 |
// no-op - should return and not panic |
| 236 |
err = sess.ModifyEndpointIfRedirect(&http.Response{}) |
| 237 |
c.Assert(err, IsNil) |
| 238 |
|
| 239 |
data := []struct { |
| 240 |
url string |
| 241 |
endpoint *transport.Endpoint |
| 242 |
expected *transport.Endpoint |
| 243 |
err string |
| 244 |
}{ |
| 245 |
{"https://example.com/foo/bar", &transport.Endpoint{}, &transport.Endpoint{}, ".*does not end with.*"}, |
| 246 |
{"https://example.com/foo.git/info/refs", |
| 247 |
&transport.Endpoint{Protocol: "https"}, |
| 248 |
&transport.Endpoint{Protocol: "https", Host: "example.com", Path: "/foo.git"}, ""}, |
| 249 |
{"https://example.com:8080/foo.git/info/refs", |
| 250 |
&transport.Endpoint{Protocol: "https"}, |
| 251 |
&transport.Endpoint{Protocol: "https", Host: "example.com", Port: 8080, Path: "/foo.git"}, ""}, |
| 252 |
{"http://example.com/foo.git/info/refs", |
| 253 |
&transport.Endpoint{Protocol: "https"}, |
| 254 |
&transport.Endpoint{Protocol: "https"}, |
| 255 |
".*changes scheme.*"}, |
| 256 |
} |
| 257 |
|
| 258 |
for _, d := range data { |
| 259 |
u, _ := url.Parse(d.url) |
| 260 |
sess := &session{endpoint: d.endpoint} |
| 261 |
err := sess.ModifyEndpointIfRedirect(&http.Response{ |
| 262 |
Request: &http.Request{URL: u}, |
| 263 |
}) |
| 264 |
if d.err != "" { |
| 265 |
c.Assert(err, ErrorMatches, d.err) |
| 266 |
} else { |
| 267 |
c.Assert(err, IsNil) |
| 268 |
} |
| 269 |
c.Assert(d.endpoint, DeepEquals, d.expected) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
func (s *ClientSuite) TestModifyEndpointIfRedirectClearsCredentialsOnCrossHost(c *C) { |
| 274 |
sess := &session{ |
| 275 |
auth: &BasicAuth{Username: "user", Password: "pass"}, |
| 276 |
endpoint: &transport.Endpoint{ |
| 277 |
Protocol: "https", |
| 278 |
User: "user", |
| 279 |
Password: "pass", |
| 280 |
Host: "old.example.com", |
| 281 |
Path: "/repo.git", |
| 282 |
}, |
| 283 |
} |
| 284 |
|
| 285 |
u, err := url.Parse("https://new.example.com/repo.git/info/refs") |
| 286 |
c.Assert(err, IsNil) |
| 287 |
err = sess.ModifyEndpointIfRedirect(&http.Response{ |
| 288 |
Request: &http.Request{URL: u}, |
| 289 |
}) |
| 290 |
c.Assert(err, IsNil) |
| 291 |
c.Assert(sess.auth, IsNil) |
| 292 |
c.Assert(sess.endpoint.User, Equals, "") |
| 293 |
c.Assert(sess.endpoint.Password, Equals, "") |
| 294 |
c.Assert(sess.endpoint.Host, Equals, "new.example.com") |
| 295 |
} |
| 296 |
|
| 297 |
func (s *ClientSuite) TestModifyEndpointIfRedirectPreservesCredentialsOnEquivalentAuthority(c *C) { |
| 298 |
tests := []struct { |
| 299 |
name string |
| 300 |
endpoint *transport.Endpoint |
| 301 |
redirectURL string |
| 302 |
expectedHost string |
| 303 |
expectedPort int |
| 304 |
expectedPath string |
| 305 |
expectedString string |
| 306 |
}{ |
| 307 |
{ |
| 308 |
name: "same host", |
| 309 |
endpoint: &transport.Endpoint{ |
| 310 |
Protocol: "https", |
| 311 |
User: "user", |
| 312 |
Password: "pass", |
| 313 |
Host: "example.com", |
| 314 |
Path: "/old.git", |
| 315 |
}, |
| 316 |
redirectURL: "https://example.com/new.git/info/refs", |
| 317 |
expectedHost: "example.com", |
| 318 |
expectedPort: 0, |
| 319 |
expectedPath: "/new.git", |
| 320 |
expectedString: "https://user:[email protected]/new.git", |
| 321 |
}, |
| 322 |
{ |
| 323 |
name: "default https port normalization", |
| 324 |
endpoint: &transport.Endpoint{ |
| 325 |
Protocol: "https", |
| 326 |
User: "user", |
| 327 |
Password: "pass", |
| 328 |
Host: "example.com", |
| 329 |
Port: 443, |
| 330 |
Path: "/old.git", |
| 331 |
}, |
| 332 |
redirectURL: "https://example.com/new.git/info/refs", |
| 333 |
expectedHost: "example.com", |
| 334 |
expectedPort: 0, |
| 335 |
expectedPath: "/new.git", |
| 336 |
expectedString: "https://user:[email protected]/new.git", |
| 337 |
}, |
| 338 |
{ |
| 339 |
name: "ipv6 loopback implicit https port", |
| 340 |
endpoint: &transport.Endpoint{ |
| 341 |
Protocol: "https", |
| 342 |
User: "user", |
| 343 |
Password: "pass", |
| 344 |
Host: "[::1]", |
| 345 |
Port: 443, |
| 346 |
Path: "/old.git", |
| 347 |
}, |
| 348 |
redirectURL: "https://[::1]/new.git/info/refs", |
| 349 |
expectedHost: "[::1]", |
| 350 |
expectedPort: 0, |
| 351 |
expectedPath: "/new.git", |
| 352 |
expectedString: "https://user:pass@[::1]/new.git", |
| 353 |
}, |
| 354 |
{ |
| 355 |
name: "ipv6 documentation prefix explicit https port", |
| 356 |
endpoint: &transport.Endpoint{ |
| 357 |
Protocol: "https", |
| 358 |
User: "user", |
| 359 |
Password: "pass", |
| 360 |
Host: "[2001:db8::1]", |
| 361 |
Path: "/old.git", |
| 362 |
}, |
| 363 |
redirectURL: "https://[2001:db8::1]:443/new.git/info/refs", |
| 364 |
expectedHost: "[2001:db8::1]", |
| 365 |
expectedPort: 443, |
| 366 |
expectedPath: "/new.git", |
| 367 |
expectedString: "https://user:pass@[2001:db8::1]/new.git", |
| 368 |
}, |
| 369 |
{ |
| 370 |
name: "ipv6 mapped address non-default port", |
| 371 |
endpoint: &transport.Endpoint{ |
| 372 |
Protocol: "https", |
| 373 |
User: "user", |
| 374 |
Password: "pass", |
| 375 |
Host: "[::ffff:192.0.2.1]", |
| 376 |
Port: 8443, |
| 377 |
Path: "/old.git", |
| 378 |
}, |
| 379 |
redirectURL: "https://[::ffff:192.0.2.1]:8443/new.git/info/refs", |
| 380 |
expectedHost: "[::ffff:192.0.2.1]", |
| 381 |
expectedPort: 8443, |
| 382 |
expectedPath: "/new.git", |
| 383 |
expectedString: "https://user:pass@[::ffff:192.0.2.1]:8443/new.git", |
| 384 |
}, |
| 385 |
} |
| 386 |
|
| 387 |
for _, tt := range tests { |
| 388 |
auth := &BasicAuth{Username: "user", Password: "pass"} |
| 389 |
sess := &session{ |
| 390 |
auth: auth, |
| 391 |
endpoint: cloneEndpoint(tt.endpoint), |
| 392 |
} |
| 393 |
|
| 394 |
u, err := url.Parse(tt.redirectURL) |
| 395 |
c.Assert(err, IsNil, Commentf(tt.name)) |
| 396 |
err = sess.ModifyEndpointIfRedirect(&http.Response{ |
| 397 |
Request: &http.Request{URL: u}, |
| 398 |
}) |
| 399 |
c.Assert(err, IsNil, Commentf(tt.name)) |
| 400 |
c.Assert(sess.auth, Equals, auth, Commentf(tt.name)) |
| 401 |
c.Assert(sess.endpoint.User, Equals, "user", Commentf(tt.name)) |
| 402 |
c.Assert(sess.endpoint.Password, Equals, "pass", Commentf(tt.name)) |
| 403 |
c.Assert(sess.endpoint.Host, Equals, tt.expectedHost, Commentf(tt.name)) |
| 404 |
c.Assert(sess.endpoint.Port, Equals, tt.expectedPort, Commentf(tt.name)) |
| 405 |
c.Assert(sess.endpoint.Path, Equals, tt.expectedPath, Commentf(tt.name)) |
| 406 |
c.Assert(sess.endpoint.String(), Equals, tt.expectedString, Commentf(tt.name)) |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
func cloneEndpoint(ep *transport.Endpoint) *transport.Endpoint { |
| 411 |
cloned := *ep |
| 412 |
return &cloned |
| 413 |
} |
| 414 |
|
| 415 |
func (s *ClientSuite) TestCheckRedirectPolicy(c *C) { |
| 416 |
tests := []struct { |
| 417 |
name string |
| 418 |
policy RedirectPolicy |
| 419 |
targetURL string |
| 420 |
initial bool |
| 421 |
redirectCount int |
| 422 |
err string |
| 423 |
}{ |
| 424 |
{ |
| 425 |
name: "initial blocks non-initial request", |
| 426 |
policy: FollowInitialRedirects, |
| 427 |
targetURL: "http://example.com/repo.git", |
| 428 |
err: ".*non-initial request.*", |
| 429 |
}, |
| 430 |
{ |
| 431 |
name: "initial allows initial request", |
| 432 |
policy: FollowInitialRedirects, |
| 433 |
targetURL: "http://example.com/repo.git", |
| 434 |
initial: true, |
| 435 |
}, |
| 436 |
{ |
| 437 |
name: "true allows non-initial request", |
| 438 |
policy: FollowRedirects, |
| 439 |
targetURL: "http://example.com/repo.git", |
| 440 |
}, |
| 441 |
{ |
| 442 |
name: "false blocks redirects", |
| 443 |
policy: NoFollowRedirects, |
| 444 |
targetURL: "http://example.com/repo.git", |
| 445 |
initial: true, |
| 446 |
err: ".*redirects disabled.*", |
| 447 |
}, |
| 448 |
{ |
| 449 |
name: "blocks unsupported scheme", |
| 450 |
policy: FollowRedirects, |
| 451 |
targetURL: "file:///etc/passwd", |
| 452 |
initial: true, |
| 453 |
err: ".*unsupported scheme.*", |
| 454 |
}, |
| 455 |
{ |
| 456 |
name: "blocks too many redirects", |
| 457 |
policy: FollowRedirects, |
| 458 |
targetURL: "http://example.com/repo.git", |
| 459 |
initial: true, |
| 460 |
redirectCount: 10, |
| 461 |
err: ".*too many redirects.*", |
| 462 |
}, |
| 463 |
{ |
| 464 |
name: "rejects invalid policy", |
| 465 |
policy: RedirectPolicy("bogus"), |
| 466 |
targetURL: "http://example.com/repo.git", |
| 467 |
initial: true, |
| 468 |
err: ".*invalid redirect policy.*", |
| 469 |
}, |
| 470 |
} |
| 471 |
|
| 472 |
for _, tt := range tests { |
| 473 |
target, err := url.Parse(tt.targetURL) |
| 474 |
c.Assert(err, IsNil) |
| 475 |
|
| 476 |
req := &http.Request{URL: target, Header: http.Header{}} |
| 477 |
if tt.initial { |
| 478 |
req = req.WithContext(withInitialRequest(context.Background())) |
| 479 |
} else { |
| 480 |
req = req.WithContext(context.Background()) |
| 481 |
} |
| 482 |
|
| 483 |
via := make([]*http.Request, tt.redirectCount) |
| 484 |
for i := range via { |
| 485 |
via[i] = &http.Request{} |
| 486 |
} |
| 487 |
|
| 488 |
err = checkRedirect(req, via, tt.policy) |
| 489 |
if tt.err != "" { |
| 490 |
c.Assert(err, ErrorMatches, tt.err, Commentf(tt.name)) |
| 491 |
continue |
| 492 |
} |
| 493 |
c.Assert(err, IsNil, Commentf(tt.name)) |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
type BaseSuite struct { |
| 498 |
fixtures.Suite |
| 499 |
|
| 500 |
base string |
| 501 |
host string |
| 502 |
port int |
| 503 |
} |
| 504 |
|
| 505 |
func (s *BaseSuite) SetUpTest(c *C) { |
| 506 |
l, err := net.Listen("tcp", "localhost:0") |
| 507 |
c.Assert(err, IsNil) |
| 508 |
|
| 509 |
base, err := os.MkdirTemp(c.MkDir(), fmt.Sprintf("go-git-http-%d", s.port)) |
| 510 |
c.Assert(err, IsNil) |
| 511 |
|
| 512 |
s.port = l.Addr().(*net.TCPAddr).Port |
| 513 |
s.base = filepath.Join(base, s.host) |
| 514 |
|
| 515 |
err = os.MkdirAll(s.base, 0755) |
| 516 |
c.Assert(err, IsNil) |
| 517 |
|
| 518 |
cmd := exec.Command("git", "--exec-path") |
| 519 |
out, err := cmd.CombinedOutput() |
| 520 |
c.Assert(err, IsNil) |
| 521 |
|
| 522 |
server := &http.Server{ |
| 523 |
Handler: &cgi.Handler{ |
| 524 |
Path: filepath.Join(strings.Trim(string(out), "\n"), "git-http-backend"), |
| 525 |
Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", s.base)}, |
| 526 |
}, |
| 527 |
} |
| 528 |
go func() { |
| 529 |
log.Fatal(server.Serve(l)) |
| 530 |
}() |
| 531 |
} |
| 532 |
|
| 533 |
func (s *BaseSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) *transport.Endpoint { |
| 534 |
fs := f.DotGit() |
| 535 |
|
| 536 |
err := fixtures.EnsureIsBare(fs) |
| 537 |
c.Assert(err, IsNil) |
| 538 |
|
| 539 |
path := filepath.Join(s.base, name) |
| 540 |
err = os.Rename(fs.Root(), path) |
| 541 |
c.Assert(err, IsNil) |
| 542 |
|
| 543 |
return s.newEndpoint(c, name) |
| 544 |
} |
| 545 |
|
| 546 |
func (s *BaseSuite) newEndpoint(c *C, name string) *transport.Endpoint { |
| 547 |
ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/%s", s.port, name)) |
| 548 |
c.Assert(err, IsNil) |
| 549 |
|
| 550 |
return ep |
| 551 |
} |