| 1 |
package http |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net/url" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
|
| 11 |
. "github.com/go-git/go-git/v5/internal/test" |
| 12 |
"github.com/go-git/go-git/v5/plumbing" |
| 13 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 15 |
"github.com/go-git/go-git/v5/plumbing/transport/test" |
| 16 |
|
| 17 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 18 |
. "gopkg.in/check.v1" |
| 19 |
) |
| 20 |
|
| 21 |
type UploadPackSuite struct { |
| 22 |
test.UploadPackSuite |
| 23 |
BaseSuite |
| 24 |
} |
| 25 |
|
| 26 |
var _ = Suite(&UploadPackSuite{}) |
| 27 |
|
| 28 |
func (s *UploadPackSuite) SetUpSuite(c *C) { |
| 29 |
s.BaseSuite.SetUpTest(c) |
| 30 |
s.UploadPackSuite.Client = DefaultClient |
| 31 |
s.UploadPackSuite.Endpoint = s.prepareRepository(c, fixtures.Basic().One(), "basic.git") |
| 32 |
s.UploadPackSuite.EmptyEndpoint = s.prepareRepository(c, fixtures.ByTag("empty").One(), "empty.git") |
| 33 |
s.UploadPackSuite.NonExistentEndpoint = s.newEndpoint(c, "non-existent.git") |
| 34 |
} |
| 35 |
|
| 36 |
// Overwritten, different behaviour for HTTP. |
| 37 |
func (s *UploadPackSuite) TestAdvertisedReferencesNotExists(c *C) { |
| 38 |
r, err := s.Client.NewUploadPackSession(s.NonExistentEndpoint, s.EmptyAuth) |
| 39 |
c.Assert(err, IsNil) |
| 40 |
info, err := r.AdvertisedReferences() |
| 41 |
c.Assert(err, ErrorIs, transport.ErrRepositoryNotFound) |
| 42 |
c.Assert(info, IsNil) |
| 43 |
} |
| 44 |
|
| 45 |
func (s *UploadPackSuite) TestuploadPackRequestToReader(c *C) { |
| 46 |
r := packp.NewUploadPackRequest() |
| 47 |
r.Wants = append(r.Wants, plumbing.NewHash("d82f291cde9987322c8a0c81a325e1ba6159684c")) |
| 48 |
r.Wants = append(r.Wants, plumbing.NewHash("2b41ef280fdb67a9b250678686a0c3e03b0a9989")) |
| 49 |
r.Haves = append(r.Haves, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
| 50 |
|
| 51 |
sr, err := uploadPackRequestToReader(r) |
| 52 |
c.Assert(err, IsNil) |
| 53 |
b, _ := io.ReadAll(sr) |
| 54 |
c.Assert(string(b), Equals, |
| 55 |
"0032want 2b41ef280fdb67a9b250678686a0c3e03b0a9989\n"+ |
| 56 |
"0032want d82f291cde9987322c8a0c81a325e1ba6159684c\n0000"+ |
| 57 |
"0032have 6ecf0ef2c2dffb796033e5a02219af86ec6584e5\n"+ |
| 58 |
"0009done\n", |
| 59 |
) |
| 60 |
} |
| 61 |
|
| 62 |
func (s *UploadPackSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) *transport.Endpoint { |
| 63 |
fs := f.DotGit() |
| 64 |
|
| 65 |
err := fixtures.EnsureIsBare(fs) |
| 66 |
c.Assert(err, IsNil) |
| 67 |
|
| 68 |
path := filepath.Join(s.base, name) |
| 69 |
err = os.Rename(fs.Root(), path) |
| 70 |
c.Assert(err, IsNil) |
| 71 |
|
| 72 |
return s.newEndpoint(c, name) |
| 73 |
} |
| 74 |
|
| 75 |
func (s *UploadPackSuite) newEndpoint(c *C, name string) *transport.Endpoint { |
| 76 |
ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/%s", s.port, name)) |
| 77 |
c.Assert(err, IsNil) |
| 78 |
|
| 79 |
return ep |
| 80 |
} |
| 81 |
|
| 82 |
func (s *UploadPackSuite) TestAdvertisedReferencesRedirectPath(c *C) { |
| 83 |
endpoint, _ := transport.NewEndpoint("https://gitlab.com/gitlab-org/gitter/webapp") |
| 84 |
|
| 85 |
session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth) |
| 86 |
c.Assert(err, IsNil) |
| 87 |
|
| 88 |
info, err := session.AdvertisedReferences() |
| 89 |
c.Assert(err, IsNil) |
| 90 |
c.Assert(info, NotNil) |
| 91 |
|
| 92 |
url := session.(*upSession).endpoint.String() |
| 93 |
c.Assert(url, Equals, "https://gitlab.com/gitlab-org/gitter/webapp.git") |
| 94 |
} |
| 95 |
|
| 96 |
func (s *UploadPackSuite) TestAdvertisedReferencesRedirectSchema(c *C) { |
| 97 |
endpoint, _ := transport.NewEndpoint("http://github.com/git-fixtures/basic") |
| 98 |
|
| 99 |
session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth) |
| 100 |
c.Assert(err, IsNil) |
| 101 |
|
| 102 |
info, err := session.AdvertisedReferences() |
| 103 |
c.Assert(err, IsNil) |
| 104 |
c.Assert(info, NotNil) |
| 105 |
|
| 106 |
url := session.(*upSession).endpoint.String() |
| 107 |
c.Assert(url, Equals, "https://github.com/git-fixtures/basic") |
| 108 |
} |
| 109 |
|
| 110 |
func (s *UploadPackSuite) TestAdvertisedReferencesContext(c *C) { |
| 111 |
ctx, cancel := context.WithCancel(context.Background()) |
| 112 |
defer cancel() |
| 113 |
endpoint, _ := transport.NewEndpoint("http://github.com/git-fixtures/basic") |
| 114 |
|
| 115 |
session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth) |
| 116 |
c.Assert(err, IsNil) |
| 117 |
|
| 118 |
info, err := session.AdvertisedReferencesContext(ctx) |
| 119 |
c.Assert(err, IsNil) |
| 120 |
c.Assert(info, NotNil) |
| 121 |
|
| 122 |
url := session.(*upSession).endpoint.String() |
| 123 |
c.Assert(url, Equals, "https://github.com/git-fixtures/basic") |
| 124 |
} |
| 125 |
|
| 126 |
func (s *UploadPackSuite) TestAdvertisedReferencesContextCanceled(c *C) { |
| 127 |
ctx, cancel := context.WithCancel(context.Background()) |
| 128 |
cancel() |
| 129 |
endpoint, _ := transport.NewEndpoint("http://github.com/git-fixtures/basic") |
| 130 |
|
| 131 |
session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth) |
| 132 |
c.Assert(err, IsNil) |
| 133 |
|
| 134 |
info, err := session.AdvertisedReferencesContext(ctx) |
| 135 |
c.Assert(err, DeepEquals, &url.Error{Op: "Get", URL: "http://github.com/git-fixtures/basic/info/refs?service=git-upload-pack", Err: context.Canceled}) |
| 136 |
c.Assert(info, IsNil) |
| 137 |
} |
| 138 |
|
| 139 |
func (s *UploadPackSuite) TestUploadPackWithContextOnRead(c *C) { |
| 140 |
c.Skip("flaky tests, looks like sometimes the request body is cached, so doesn't fail on context cancel") |
| 141 |
} |