download: add ProxyClient, TestClean, TestModuleName, TestLatestVersion

This commit is contained in:
Shawn Smith
2021-11-26 13:06:26 +09:00
parent 6f946a0aa3
commit 7407fea655
5 changed files with 114 additions and 14 deletions

25
download/download_test.go Normal file
View File

@@ -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)
}
}
}

View File

@@ -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
}

54
download/proxy_test.go Normal file
View File

@@ -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)
}
}