diff --git a/download/download_test.go b/download/download_test.go new file mode 100644 index 0000000..8a7532a --- /dev/null +++ b/download/download_test.go @@ -0,0 +1,25 @@ +package download + +import "testing" + +func TestClean(t *testing.T) { + cases := []struct { + path string + want string + }{ + {"github.com/foo/bar", "github.com/foo/bar"}, + {"https://github.com/foo/bar", "github.com/foo/bar"}, + {"https://user@github.com/foo/bar", "github.com/foo/bar"}, + } + + for _, tt := range cases { + got, err := Clean(tt.path) + if err != nil { + t.Fatal(err) + } + + if got != tt.want { + t.Errorf("Clean(%q) = %q, want %q", tt.path, got, tt.want) + } + } +} diff --git a/download/proxy.go b/download/proxy.go index 3e96628..d51124b 100644 --- a/download/proxy.go +++ b/download/proxy.go @@ -13,26 +13,45 @@ import ( ) const ( - proxyLatestURL = "https://proxy.golang.org/%s/@latest" - proxyZipURL = "https://proxy.golang.org/%s/@v/%s.zip" - proxyModURL = "https://proxy.golang.org/%s/@v/%s.mod" - reposDir = "_repos/src" + reposDir = "_repos/src" ) type moduleVersion struct { Version string } +// ProxyClient is a client for the module proxy +type ProxyClient struct { + URL string +} + +// NewProxyClient returns a new ProxyClient +func NewProxyClient(url string) ProxyClient { + return ProxyClient{URL: url} +} + +func (c *ProxyClient) latestURL(module string) string { + return fmt.Sprintf("%s/%s/@latest", c.URL, module) +} + +func (c *ProxyClient) zipURL(module, version string) string { + return fmt.Sprintf("%s/%s/@v/%s.zip", c.URL, module, version) +} + +func (c *ProxyClient) modURL(module, version string) string { + return fmt.Sprintf("%s/%s/@v/%s.mod", c.URL, module, version) +} + // ModuleName gets the name of a module from the proxy -func ModuleName(path string) (string, error) { +func (c *ProxyClient) ModuleName(path string) (string, error) { lowerPath := strings.ToLower(path) - ver, err := LatestVersion(path) + ver, err := c.LatestVersion(path) if err != nil { return "", err } - u := fmt.Sprintf(proxyModURL, lowerPath, ver) + u := c.modURL(lowerPath, ver) resp, err := http.Get(u) if err != nil { return "", err @@ -62,9 +81,9 @@ func ModuleName(path string) (string, error) { } // LatestVersion gets the latest module version from the proxy -func LatestVersion(path string) (string, error) { +func (c *ProxyClient) LatestVersion(path string) (string, error) { lowerPath := strings.ToLower(path) - u := fmt.Sprintf(proxyLatestURL, lowerPath) + u := fmt.Sprintf(c.latestURL(lowerPath)) resp, err := http.Get(u) if err != nil { return "", err @@ -88,15 +107,15 @@ func LatestVersion(path string) (string, error) { } // ProxyDownload downloads a package from proxy.golang.org -func ProxyDownload(path string) (string, error) { +func (c *ProxyClient) ProxyDownload(path string) (string, error) { lowerPath := strings.ToLower(path) - ver, err := LatestVersion(path) + ver, err := c.LatestVersion(path) if err != nil { return "", err } - resp, err := http.Get(fmt.Sprintf(proxyZipURL, lowerPath, ver)) + resp, err := http.Get(c.zipURL(lowerPath, ver)) if err != nil { return "", err } diff --git a/download/proxy_test.go b/download/proxy_test.go new file mode 100644 index 0000000..4f3f79f --- /dev/null +++ b/download/proxy_test.go @@ -0,0 +1,54 @@ +package download + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +func TestModuleName(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + u := r.URL + switch u.Path { + case "/github.com/user/module/@latest": + fmt.Fprintf(w, `{"Version":"v0.1.0","Time":"2019-08-07T08:30:46Z"}`) + return + case "/github.com/user/module/@v/v0.1.0.mod": + fmt.Fprintf(w, `module github.com/user/module`) + return + } + })) + defer ts.Close() + + c := NewProxyClient(ts.URL) + + got, err := c.ModuleName("github.com/user/module") + if err != nil { + t.Fatal(err) + } + + want := "github.com/user/module" + if got != want { + t.Errorf("got module name = %q, want %q", got, want) + } +} + +func TestLatestVersion(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `{"Version":"v0.1.0","Time":"2019-08-07T08:30:46Z"}`) + })) + defer ts.Close() + + c := NewProxyClient(ts.URL) + + got, err := c.LatestVersion("github.com/user/module") + if err != nil { + t.Fatal(err) + } + + want := "v0.1.0" + if got != want { + t.Errorf("got latest version = %q, want %q", got, want) + } +} diff --git a/handlers/check.go b/handlers/check.go index a4fbc7e..2a711b7 100644 --- a/handlers/check.go +++ b/handlers/check.go @@ -28,7 +28,8 @@ func CheckHandler(w http.ResponseWriter, r *http.Request, db *badger.DB) { return } - moduleName, err := download.ModuleName(repo) + c := download.NewProxyClient("https://proxy.golang.org") + moduleName, err := c.ModuleName(repo) if err != nil { log.Println("ERROR: could not get module name:", err) } diff --git a/handlers/checks.go b/handlers/checks.go index 168a1df..df9cb99 100644 --- a/handlers/checks.go +++ b/handlers/checks.go @@ -92,7 +92,8 @@ func newChecksResp(db *badger.DB, repo string, forceRefresh bool) (checksResp, e } } - ver, err := download.ProxyDownload(repo) + c := download.NewProxyClient("https://proxy.golang.org") + ver, err := c.ProxyDownload(repo) if err != nil { log.Println("ERROR:", err) return checksResp{}, fmt.Errorf("could not download repo: %v", err)