diff options
author | Santiago M. Mola <santi@mola.io> | 2016-11-23 15:30:34 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-23 15:38:12 +0100 |
commit | 08e08d771ef03df80248c80d81475fe7c5ea6fe7 (patch) | |
tree | d12e9befa22409e8cf50c5bbc4895e69fd8a5f48 /plumbing/transport/http/common_test.go | |
parent | 844169a739fb8bf1f252d416f10d8c7034db9fe2 (diff) | |
download | go-git-08e08d771ef03df80248c80d81475fe7c5ea6fe7.tar.gz |
transport: create Client interface (#132)
* plumbing: move plumbing/client package to plumbing/transport.
* transport: create Client interface.
* A Client can instantiate any client transport service.
* InstallProtocol installs a Client for a given protocol,
instead of just a UploadPackService.
* A Client can open a session for fetch-pack or send-pack
for a specific Endpoint.
* Adapt ssh and http clients to the new client interface.
* updated doc
Diffstat (limited to 'plumbing/transport/http/common_test.go')
-rw-r--r-- | plumbing/transport/http/common_test.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/plumbing/transport/http/common_test.go b/plumbing/transport/http/common_test.go new file mode 100644 index 0000000..1d09fba --- /dev/null +++ b/plumbing/transport/http/common_test.go @@ -0,0 +1,89 @@ +package http + +import ( + "crypto/tls" + "net/http" + "testing" + + "gopkg.in/src-d/go-git.v4/plumbing/transport" + + . "gopkg.in/check.v1" +) + +func Test(t *testing.T) { TestingT(t) } + +type ClientSuite struct { + Endpoint transport.Endpoint +} + +var _ = Suite(&ClientSuite{}) + +func (s *ClientSuite) SetUpSuite(c *C) { + var err error + s.Endpoint, err = transport.NewEndpoint("https://github.com/git-fixtures/basic") + c.Assert(err, IsNil) +} + +func (s *FetchPackSuite) TestNewClient(c *C) { + roundTripper := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} + client := &http.Client{Transport: roundTripper} + r := NewClient(client).(*Client) + + c.Assert(r.c, Equals, client) +} + +func (s *ClientSuite) 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 *ClientSuite) TestNewErrOK(c *C) { + res := &http.Response{StatusCode: http.StatusOK} + err := NewErr(res) + c.Assert(err, IsNil) +} + +func (s *ClientSuite) TestNewErrUnauthorized(c *C) { + s.testNewHTTPError(c, http.StatusUnauthorized, "authorization required") +} + +func (s *ClientSuite) TestNewErrNotFound(c *C) { + s.testNewHTTPError(c, http.StatusNotFound, "repository not found") +} + +func (s *ClientSuite) TestNewHTTPError40x(c *C) { + s.testNewHTTPError(c, http.StatusPaymentRequired, "unexpected client error.*") +} + +func (s *ClientSuite) testNewHTTPError(c *C, code int, msg string) { + req, _ := http.NewRequest("GET", "foo", nil) + res := &http.Response{ + StatusCode: code, + Request: req, + } + + err := NewErr(res) + c.Assert(err, NotNil) + c.Assert(err, ErrorMatches, msg) +} + +func (s *ClientSuite) TestSetAuth(c *C) { + auth := &BasicAuth{} + r, err := DefaultClient.NewFetchPackSession(s.Endpoint) + c.Assert(err, IsNil) + r.SetAuth(auth) + c.Assert(auth, Equals, r.(*fetchPackSession).auth) +} + +type mockAuth struct{} + +func (*mockAuth) Name() string { return "" } +func (*mockAuth) String() string { return "" } + +func (s *ClientSuite) TestSetAuthWrongType(c *C) { + r, err := DefaultClient.NewFetchPackSession(s.Endpoint) + c.Assert(err, IsNil) + c.Assert(r.SetAuth(&mockAuth{}), Equals, transport.ErrInvalidAuthMethod) +} |