aboutsummaryrefslogtreecommitdiffstats
path: root/remote_test.go
diff options
context:
space:
mode:
authorferhat elmas <elmas.ferhat@gmail.com>2016-11-15 01:18:53 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2016-11-15 01:18:53 +0100
commit16d86605732ba3198c0acd4317b53cf4991a7d4d (patch)
tree3306f0438235f7dfe19fd37c5393a4794abe0535 /remote_test.go
parenteb89d2dd9a36440d58aea224c055b364e49785f7 (diff)
downloadgo-git-16d86605732ba3198c0acd4317b53cf4991a7d4d.tar.gz
Add configurable http client factory (fixes #120) (#121)
* new http client factory ready to install/override default http(s) * mv GitUploadPackServiceFactory to clients.common pkg * rename http.HTTPError to http.Err * rename http.HTTPAuthMethod to http.AuthMethod * add doc and examples/ usage * general improvements: - update install link in readme to v4 (example are already pointing v4) - fix indentation in package doc (styling for godoc.org) - use http.Status constants instead of integers - close leaked response body - rm named returns which stutter in doc - fix one format string - rm unnecessary if checks - documentation fixes
Diffstat (limited to 'remote_test.go')
-rw-r--r--remote_test.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/remote_test.go b/remote_test.go
index f129c68..bfda15d 100644
--- a/remote_test.go
+++ b/remote_test.go
@@ -1,12 +1,18 @@
package git
import (
+ "crypto/tls"
+ "fmt"
"io"
"io/ioutil"
+ "net/http"
"os"
+ "time"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/client"
+ githttp "gopkg.in/src-d/go-git.v4/plumbing/client/http"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
"gopkg.in/src-d/go-git.v4/storage/memory"
@@ -202,3 +208,43 @@ func (s *RemoteSuite) TestString(c *C) {
"foo\thttps://github.com/git-fixtures/basic.git (push)",
)
}
+
+// Here is an example to configure http client according to our own needs.
+func Example_customHTTPClient() {
+ const url = "https://github.com/git-fixtures/basic.git"
+
+ // Create a custom http(s) client with your config
+ customClient := &http.Client{
+ Transport: &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ }, // accept any certificate (might be useful for testing)
+ Timeout: 15 * time.Second, // 15 second timeout
+ CheckRedirect: func(req *http.Request, via []*http.Request) error { // don't follow redirect
+ return http.ErrUseLastResponse
+ },
+ }
+
+ // Override http(s) default protocol to use our custom client
+ clients.InstallProtocol(
+ "https",
+ githttp.NewGitUploadPackServiceFactory(customClient))
+
+ // Create an in-memory repository
+ r := NewMemoryRepository()
+
+ // Clone repo
+ if err := r.Clone(&CloneOptions{URL: url}); err != nil {
+ panic(err)
+ }
+
+ // Retrieve the branch pointed by HEAD
+ head, err := r.Head()
+ if err != nil {
+ panic(err)
+ }
+
+ // Print latest commit hash
+ fmt.Println(head.Hash())
+ // Output:
+ // 6ecf0ef2c2dffb796033e5a02219af86ec6584e5
+}