func TestNetrcWithBadHost(t *testing.T) { SetupTestCredentialsFunc() defer RestoreCredentialsFunc() cfg := config.New() cfg.SetNetrc(&fakeNetrc{}) u, err := url.Parse("http://other-host/foo/bar") if err != nil { t.Fatal(err) } req := &http.Request{ URL: u, Header: http.Header{}, } if setCredURLFromNetrc(cfg, req) { t.Fatalf("unexpected netrc match") } auth := req.Header.Get("Authorization") if auth != "" { t.Fatalf("bad basic auth: %q", auth) } }
func testBasicAdapterExists(t *testing.T) { cfg := config.New() m := ConfigureManifest(NewManifest(), cfg) assert := assert.New(t) dls := m.GetDownloadAdapterNames() if assert.NotNil(dls) { assert.Equal([]string{"basic"}, dls) } uls := m.GetUploadAdapterNames() if assert.NotNil(uls) { assert.Equal([]string{"basic"}, uls) } da := m.NewDownloadAdapter("basic") if assert.NotNil(da) { assert.Equal("basic", da.Name()) assert.Equal(Download, da.Direction()) } ua := m.NewUploadAdapter("basic") if assert.NotNil(ua) { assert.Equal("basic", ua.Name()) assert.Equal(Upload, ua.Direction()) } }
func TestNetrcWithHostAndPort(t *testing.T) { SetupTestCredentialsFunc() defer RestoreCredentialsFunc() cfg := config.New() cfg.SetNetrc(&fakeNetrc{}) u, err := url.Parse("http://some-host:123/foo/bar") if err != nil { t.Fatal(err) } req := &http.Request{ URL: u, Header: http.Header{}, } if !setCredURLFromNetrc(cfg, req) { t.Fatal("no netrc match") } auth := req.Header.Get("Authorization") if auth != "Basic YWJjOmRlZg==" { t.Fatalf("bad basic auth: %q", auth) } }
func TestErrorStatusWithCustomMessage(t *testing.T) { cfg := config.New() u, err := url.Parse("https://lfs-server.com/objects/oid") if err != nil { t.Fatal(err) } statuses := map[int]string{ 400: "not panic", 401: "not panic", 403: "not panic", 404: "not panic", 405: "not panic", 406: "not panic", 429: "not panic", 500: "panic", 501: "not panic", 503: "panic", 504: "panic", 507: "not panic", 509: "not panic", } for status, panicMsg := range statuses { cliErr := &ClientError{ Message: fmt.Sprintf("custom error for %d", status), } by, err := json.Marshal(cliErr) if err != nil { t.Errorf("Error building json for status %d: %s", status, err) continue } res := &http.Response{ StatusCode: status, Header: make(http.Header), Body: ioutil.NopCloser(bytes.NewReader(by)), Request: &http.Request{URL: u}, } res.Header.Set("Content-Type", "application/vnd.git-lfs+json; charset=utf-8") err = handleResponse(cfg, res, nil) if err == nil { t.Errorf("No error from HTTP %d", status) continue } expected := fmt.Sprintf("custom error for %d", status) if actual := err.Error(); !strings.HasSuffix(actual, expected) { t.Errorf("Expected for HTTP %d:\n%s\nACTUAL:\n%s", status, expected, actual) continue } if errors.IsFatalError(err) == (panicMsg != "panic") { t.Errorf("Error for HTTP %d should %s", status, panicMsg) continue } } }
func TestCertVerifyDisabledGlobalConfig(t *testing.T) { def := config.New() assert.False(t, isCertVerificationDisabledForHost(def, "anyhost.com")) cfg := config.NewFrom(config.Values{ Git: map[string]string{"http.sslverify": "false"}, }) assert.True(t, isCertVerificationDisabledForHost(cfg, "anyhost.com")) }
func TestSuccessStatus(t *testing.T) { cfg := config.New() for _, status := range []int{200, 201, 202} { res := &http.Response{StatusCode: status} if err := handleResponse(cfg, res, nil); err != nil { t.Errorf("Unexpected error for HTTP %d: %s", status, err.Error()) } } }
func TestNtlmClientSession(t *testing.T) { cfg := config.New() creds := auth.Creds{"username": "******", "password": "******"} _, err := ntlmClientSession(cfg, creds) assert.Nil(t, err) //The second call should ignore creds and give the session we just created. badCreds := auth.Creds{"username": "******", "password": "******"} _, err = ntlmClientSession(cfg, badCreds) assert.Nil(t, err) }
func TestProxyIsNil(t *testing.T) { cfg := config.New() req, err := http.NewRequest("GET", "http://some-host.com:123/foo/bar", nil) if err != nil { t.Fatal(err) } proxyURL, err := ProxyFromGitConfigOrEnvironment(cfg)(req) assert.Nil(t, proxyURL) assert.Nil(t, err) }
func TestCertVerifyDisabledHostConfig(t *testing.T) { def := config.New() assert.False(t, isCertVerificationDisabledForHost(def, "specifichost.com")) assert.False(t, isCertVerificationDisabledForHost(def, "otherhost.com")) cfg := config.NewFrom(config.Values{ Git: map[string]string{ "http.https://specifichost.com/.sslverify": "false", }, }) assert.True(t, isCertVerificationDisabledForHost(cfg, "specifichost.com")) assert.False(t, isCertVerificationDisabledForHost(cfg, "otherhost.com")) }
func testAdapterRegAndOverride(t *testing.T) { cfg := config.New() m := ConfigureManifest(NewManifest(), cfg) assert := assert.New(t) assert.Nil(m.NewDownloadAdapter("test")) assert.Nil(m.NewUploadAdapter("test")) m.RegisterNewTransferAdapterFunc("test", Upload, newTestAdapter) assert.Nil(m.NewDownloadAdapter("test")) assert.NotNil(m.NewUploadAdapter("test")) m.RegisterNewTransferAdapterFunc("test", Download, newTestAdapter) da := m.NewDownloadAdapter("test") if assert.NotNil(da) { assert.Equal("test", da.Name()) assert.Equal(Download, da.Direction()) } ua := m.NewUploadAdapter("test") if assert.NotNil(ua) { assert.Equal("test", ua.Name()) assert.Equal(Upload, ua.Direction()) } // Test override m.RegisterNewTransferAdapterFunc("test", Upload, newRenamedTestAdapter) ua = m.NewUploadAdapter("test") if assert.NotNil(ua) { assert.Equal("RENAMED", ua.Name()) assert.Equal(Upload, ua.Direction()) } da = m.NewDownloadAdapter("test") if assert.NotNil(da) { assert.Equal("test", da.Name()) assert.Equal(Download, da.Direction()) } m.RegisterNewTransferAdapterFunc("test", Download, newRenamedTestAdapter) da = m.NewDownloadAdapter("test") if assert.NotNil(da) { assert.Equal("RENAMED", da.Name()) assert.Equal(Download, da.Direction()) } }
"encoding/json" "fmt" "io" "io/ioutil" "os" "strconv" "strings" "time" "github.com/github/git-lfs/config" "github.com/github/git-lfs/httputil" "github.com/github/git-lfs/progress" "github.com/github/git-lfs/tools" ) var cfg = config.New() // This test custom adapter just acts as a bridge for uploads/downloads // in order to demonstrate & test the custom transfer adapter protocols // All we actually do is relay the requests back to the normal storage URLs // of our test server for simplicity, but this proves the principle func main() { scanner := bufio.NewScanner(os.Stdin) writer := bufio.NewWriter(os.Stdout) errWriter := bufio.NewWriter(os.Stderr) for scanner.Scan() { line := scanner.Text() var req request if err := json.Unmarshal([]byte(line), &req); err != nil {
func TestNtlmClientSessionBadCreds(t *testing.T) { cfg := config.New() creds := auth.Creds{"username": "******", "password": "******"} _, err := ntlmClientSession(cfg, creds) assert.NotNil(t, err) }
func TestErrorStatusWithDefaultMessage(t *testing.T) { cfg := config.New() rawurl := "https://lfs-server.com/objects/oid" u, err := url.Parse(rawurl) if err != nil { t.Fatal(err) } statuses := map[int][]string{ 400: {defaultErrors[400], "not panic"}, 401: {defaultErrors[401], "not panic"}, 403: {defaultErrors[401], "not panic"}, 404: {defaultErrors[404], "not panic"}, 405: {defaultErrors[400] + " from HTTP 405", "not panic"}, 406: {defaultErrors[400] + " from HTTP 406", "not panic"}, 429: {defaultErrors[429], "not panic"}, 500: {defaultErrors[500], "panic"}, 501: {defaultErrors[500] + " from HTTP 501", "not panic"}, 503: {defaultErrors[500] + " from HTTP 503", "panic"}, 504: {defaultErrors[500] + " from HTTP 504", "panic"}, 507: {defaultErrors[507], "not panic"}, 509: {defaultErrors[509], "not panic"}, } for status, results := range statuses { cliErr := &ClientError{ Message: fmt.Sprintf("custom error for %d", status), } by, err := json.Marshal(cliErr) if err != nil { t.Errorf("Error building json for status %d: %s", status, err) continue } res := &http.Response{ StatusCode: status, Header: make(http.Header), Body: ioutil.NopCloser(bytes.NewReader(by)), Request: &http.Request{URL: u}, } // purposely wrong content type so it falls back to default res.Header.Set("Content-Type", "application/vnd.git-lfs+json2") err = handleResponse(cfg, res, nil) if err == nil { t.Errorf("No error from HTTP %d", status) continue } expected := fmt.Sprintf(results[0], rawurl) if actual := err.Error(); !strings.HasSuffix(actual, expected) { t.Errorf("Expected for HTTP %d:\n%s\nACTUAL:\n%s", status, expected, actual) continue } if errors.IsFatalError(err) == (results[1] != "panic") { t.Errorf("Error for HTTP %d should %s", status, results[1]) continue } } }