aboutsummaryrefslogtreecommitdiffstats
path: root/clients/http
diff options
context:
space:
mode:
Diffstat (limited to 'clients/http')
-rw-r--r--clients/http/common.go5
-rw-r--r--clients/http/common_test.go7
-rw-r--r--clients/http/git_upload_pack.go31
-rw-r--r--clients/http/git_upload_pack_test.go49
4 files changed, 53 insertions, 39 deletions
diff --git a/clients/http/common.go b/clients/http/common.go
index e7c43d5..a4b86bd 100644
--- a/clients/http/common.go
+++ b/clients/http/common.go
@@ -2,7 +2,6 @@
package http
import (
- "errors"
"fmt"
"net/http"
@@ -10,8 +9,6 @@ import (
"gopkg.in/src-d/go-git.v4/core"
)
-var InvalidAuthMethodErr = errors.New("invalid http auth method: a http.HTTPAuthMethod should be provided.")
-
type HTTPAuthMethod interface {
common.AuthMethod
setAuth(r *http.Request)
@@ -53,7 +50,7 @@ func NewHTTPError(r *http.Response) error {
err := &HTTPError{r}
if r.StatusCode == 404 || r.StatusCode == 401 {
- return core.NewPermanentError(common.NotFoundErr)
+ return core.NewPermanentError(common.ErrNotFound)
}
return core.NewUnexpectedError(err)
diff --git a/clients/http/common_test.go b/clients/http/common_test.go
index 7bd9708..d3e2c9d 100644
--- a/clients/http/common_test.go
+++ b/clients/http/common_test.go
@@ -13,6 +13,13 @@ type SuiteCommon struct{}
var _ = Suite(&SuiteCommon{})
+func (s *SuiteCommon) TestNewBasicAuth(c *C) {
+ a := NewBasicAuth("foo", "qux")
+
+ c.Assert(a.Name(), Equals, "http-basic-auth")
+ c.Assert(a.String(), Equals, "http-basic-auth - foo:*******")
+}
+
func (s *SuiteCommon) TestNewHTTPError200(c *C) {
res := &http.Response{StatusCode: 200}
res.StatusCode = 200
diff --git a/clients/http/git_upload_pack.go b/clients/http/git_upload_pack.go
index 860318f..96535de 100644
--- a/clients/http/git_upload_pack.go
+++ b/clients/http/git_upload_pack.go
@@ -12,37 +12,38 @@ import (
)
type GitUploadPackService struct {
- Client *http.Client
-
+ client *http.Client
endpoint common.Endpoint
auth HTTPAuthMethod
}
-func NewGitUploadPackService() *GitUploadPackService {
+func NewGitUploadPackService(endpoint common.Endpoint) common.GitUploadPackService {
return &GitUploadPackService{
- Client: http.DefaultClient,
+ client: http.DefaultClient,
+ endpoint: endpoint,
}
}
-func (s *GitUploadPackService) Connect(url common.Endpoint) error {
- s.endpoint = url
+func (s *GitUploadPackService) Connect() error {
return nil
}
-func (s *GitUploadPackService) ConnectWithAuth(url common.Endpoint, auth common.AuthMethod) error {
+func (s *GitUploadPackService) ConnectWithAuth(auth common.AuthMethod) error {
httpAuth, ok := auth.(HTTPAuthMethod)
if !ok {
- return InvalidAuthMethodErr
+ return common.ErrInvalidAuthMethod
}
- s.endpoint = url
s.auth = httpAuth
-
return nil
}
func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
- url := fmt.Sprintf("%s/info/refs?service=%s", s.endpoint, common.GitUploadPackServiceName)
+ url := fmt.Sprintf(
+ "%s/info/refs?service=%s",
+ s.endpoint.String(), common.GitUploadPackServiceName,
+ )
+
res, err := s.doRequest("GET", url, nil)
if err != nil {
return nil, err
@@ -55,7 +56,11 @@ func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
}
func (s *GitUploadPackService) Fetch(r *common.GitUploadPackRequest) (io.ReadCloser, error) {
- url := fmt.Sprintf("%s/%s", s.endpoint, common.GitUploadPackServiceName)
+ url := fmt.Sprintf(
+ "%s/%s",
+ s.endpoint.String(), common.GitUploadPackServiceName,
+ )
+
res, err := s.doRequest("POST", url, r.Reader())
if err != nil {
return nil, err
@@ -83,7 +88,7 @@ func (s *GitUploadPackService) doRequest(method, url string, content *strings.Re
s.applyHeadersToRequest(req, content)
s.applyAuthToRequest(req)
- res, err := s.Client.Do(req)
+ res, err := s.client.Do(req)
if err != nil {
return nil, core.NewUnexpectedError(err)
}
diff --git a/clients/http/git_upload_pack_test.go b/clients/http/git_upload_pack_test.go
index db4a273..7e8cc36 100644
--- a/clients/http/git_upload_pack_test.go
+++ b/clients/http/git_upload_pack_test.go
@@ -8,22 +8,27 @@ import (
"gopkg.in/src-d/go-git.v4/core"
)
-type SuiteRemote struct{}
+type RemoteSuite struct {
+ Endpoint common.Endpoint
+}
-var _ = Suite(&SuiteRemote{})
+var _ = Suite(&RemoteSuite{})
-const RepositoryFixture = "https://github.com/tyba/git-fixture"
+func (s *RemoteSuite) SetUpSuite(c *C) {
+ var err error
+ s.Endpoint, err = common.NewEndpoint("https://github.com/tyba/git-fixture")
+ c.Assert(err, IsNil)
+}
-func (s *SuiteRemote) TestConnect(c *C) {
- r := NewGitUploadPackService()
- c.Assert(r.Connect(RepositoryFixture), IsNil)
+func (s *RemoteSuite) TestConnect(c *C) {
+ r := NewGitUploadPackService(s.Endpoint)
+ c.Assert(r.Connect(), IsNil)
}
-func (s *SuiteRemote) TestConnectWithAuth(c *C) {
+func (s *RemoteSuite) TestConnectWithAuth(c *C) {
auth := &BasicAuth{}
- r := NewGitUploadPackService()
- c.Assert(r.ConnectWithAuth(RepositoryFixture, auth), IsNil)
- c.Assert(r.auth, Equals, auth)
+ r := NewGitUploadPackService(s.Endpoint)
+ c.Assert(r.ConnectWithAuth(auth), IsNil)
}
type mockAuth struct{}
@@ -31,32 +36,32 @@ type mockAuth struct{}
func (*mockAuth) Name() string { return "" }
func (*mockAuth) String() string { return "" }
-func (s *SuiteRemote) TestConnectWithAuthWrongType(c *C) {
- r := NewGitUploadPackService()
- c.Assert(r.ConnectWithAuth(RepositoryFixture, &mockAuth{}), Equals, InvalidAuthMethodErr)
+func (s *RemoteSuite) TestConnectWithAuthWrongType(c *C) {
+ r := NewGitUploadPackService(s.Endpoint)
+ c.Assert(r.ConnectWithAuth(&mockAuth{}), Equals, common.ErrInvalidAuthMethod)
}
-func (s *SuiteRemote) TestDefaultBranch(c *C) {
- r := NewGitUploadPackService()
- c.Assert(r.Connect(RepositoryFixture), IsNil)
+func (s *RemoteSuite) TestDefaultBranch(c *C) {
+ r := NewGitUploadPackService(s.Endpoint)
+ c.Assert(r.Connect(), IsNil)
info, err := r.Info()
c.Assert(err, IsNil)
c.Assert(info.Capabilities.SymbolicReference("HEAD"), Equals, "refs/heads/master")
}
-func (s *SuiteRemote) TestCapabilities(c *C) {
- r := NewGitUploadPackService()
- c.Assert(r.Connect(RepositoryFixture), IsNil)
+func (s *RemoteSuite) TestCapabilities(c *C) {
+ r := NewGitUploadPackService(s.Endpoint)
+ c.Assert(r.Connect(), IsNil)
info, err := r.Info()
c.Assert(err, IsNil)
c.Assert(info.Capabilities.Get("agent").Values, HasLen, 1)
}
-func (s *SuiteRemote) TestFetch(c *C) {
- r := NewGitUploadPackService()
- c.Assert(r.Connect(RepositoryFixture), IsNil)
+func (s *RemoteSuite) TestFetch(c *C) {
+ r := NewGitUploadPackService(s.Endpoint)
+ c.Assert(r.Connect(), IsNil)
req := &common.GitUploadPackRequest{}
req.Want(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))