2016-12-19 21:53:18 +00:00
|
|
|
package lfsapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-12-23 18:13:33 +00:00
|
|
|
"strings"
|
2016-12-19 21:53:18 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/bgentry/go-netrc/netrc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetrcWithHostAndPort(t *testing.T) {
|
|
|
|
netrcFinder := &fakeNetrc{}
|
2016-12-23 18:13:33 +00:00
|
|
|
u, err := url.Parse("http://netrc-host:123/foo/bar")
|
2016-12-19 21:53:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &http.Request{
|
|
|
|
URL: u,
|
|
|
|
Header: http.Header{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !setAuthFromNetrc(netrcFinder, req) {
|
|
|
|
t.Fatal("no netrc match")
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := req.Header.Get("Authorization")
|
|
|
|
if auth != "Basic YWJjOmRlZg==" {
|
|
|
|
t.Fatalf("bad basic auth: %q", auth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetrcWithHost(t *testing.T) {
|
|
|
|
netrcFinder := &fakeNetrc{}
|
2016-12-23 18:13:33 +00:00
|
|
|
u, err := url.Parse("http://netrc-host/foo/bar")
|
2016-12-19 21:53:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &http.Request{
|
|
|
|
URL: u,
|
|
|
|
Header: http.Header{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !setAuthFromNetrc(netrcFinder, req) {
|
|
|
|
t.Fatalf("no netrc match")
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := req.Header.Get("Authorization")
|
|
|
|
if auth != "Basic YWJjOmRlZg==" {
|
|
|
|
t.Fatalf("bad basic auth: %q", auth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetrcWithBadHost(t *testing.T) {
|
|
|
|
netrcFinder := &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 setAuthFromNetrc(netrcFinder, req) {
|
|
|
|
t.Fatalf("unexpected netrc match")
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := req.Header.Get("Authorization")
|
|
|
|
if auth != "" {
|
|
|
|
t.Fatalf("bad basic auth: %q", auth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeNetrc struct{}
|
|
|
|
|
|
|
|
func (n *fakeNetrc) FindMachine(host string) *netrc.Machine {
|
2016-12-23 18:13:33 +00:00
|
|
|
if strings.Contains(host, "netrc") {
|
2016-12-19 21:53:18 +00:00
|
|
|
return &netrc.Machine{Login: "abc", Password: "def"}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|