github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/lfsapi/netrc_test.go (about) 1 package lfsapi 2 3 import ( 4 "net/http" 5 "net/url" 6 "strings" 7 "testing" 8 9 "github.com/bgentry/go-netrc/netrc" 10 ) 11 12 func TestNetrcWithHostAndPort(t *testing.T) { 13 netrcFinder := &fakeNetrc{} 14 u, err := url.Parse("http://netrc-host:123/foo/bar") 15 if err != nil { 16 t.Fatal(err) 17 } 18 19 req := &http.Request{ 20 URL: u, 21 Header: http.Header{}, 22 } 23 24 if !setAuthFromNetrc(netrcFinder, req) { 25 t.Fatal("no netrc match") 26 } 27 28 auth := req.Header.Get("Authorization") 29 if auth != "Basic YWJjOmRlZg==" { 30 t.Fatalf("bad basic auth: %q", auth) 31 } 32 } 33 34 func TestNetrcWithHost(t *testing.T) { 35 netrcFinder := &fakeNetrc{} 36 u, err := url.Parse("http://netrc-host/foo/bar") 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 req := &http.Request{ 42 URL: u, 43 Header: http.Header{}, 44 } 45 46 if !setAuthFromNetrc(netrcFinder, req) { 47 t.Fatalf("no netrc match") 48 } 49 50 auth := req.Header.Get("Authorization") 51 if auth != "Basic YWJjOmRlZg==" { 52 t.Fatalf("bad basic auth: %q", auth) 53 } 54 } 55 56 func TestNetrcWithBadHost(t *testing.T) { 57 netrcFinder := &fakeNetrc{} 58 u, err := url.Parse("http://other-host/foo/bar") 59 if err != nil { 60 t.Fatal(err) 61 } 62 63 req := &http.Request{ 64 URL: u, 65 Header: http.Header{}, 66 } 67 68 if setAuthFromNetrc(netrcFinder, req) { 69 t.Fatalf("unexpected netrc match") 70 } 71 72 auth := req.Header.Get("Authorization") 73 if auth != "" { 74 t.Fatalf("bad basic auth: %q", auth) 75 } 76 } 77 78 type fakeNetrc struct{} 79 80 func (n *fakeNetrc) FindMachine(host string) *netrc.Machine { 81 if strings.Contains(host, "netrc") { 82 return &netrc.Machine{Login: "abc", Password: "def"} 83 } 84 return nil 85 }