diff --git a/cf/actors/pluginrepo/plugin_repo.go b/cf/actors/pluginrepo/plugin_repo.go index 56a6837867b..ea5869d9afc 100644 --- a/cf/actors/pluginrepo/plugin_repo.go +++ b/cf/actors/pluginrepo/plugin_repo.go @@ -1,10 +1,14 @@ package pluginrepo import ( + "code.cloudfoundry.org/cli/version" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" + "os" + "path/filepath" + "runtime" "strings" clipr "code.cloudfoundry.org/cli-plugin-repo/web" @@ -27,18 +31,37 @@ func NewPluginRepo() PluginRepo { func (r pluginRepo) GetPlugins(repos []models.PluginRepo) (map[string][]clipr.Plugin, []string) { var pluginList clipr.PluginsJson - repoError := []string{} + var repoError []string repoPlugins := make(map[string][]clipr.Plugin) for _, repo := range repos { - resp, err := http.Get(getListEndpoint(repo.URL)) + client := &http.Client{} + req, err := http.NewRequest("GET", getListEndpoint(repo.URL), nil) + if err != nil { + repoError = append(repoError, fmt.Sprintf(T("Error creating a request")+" '%s' - %s", repo.Name, err.Error())) + continue + } + userAgent := fmt.Sprintf("%s/%s (%s; %s %s)", + filepath.Base(os.Args[0]), + version.VersionString(), + runtime.Version(), + runtime.GOARCH, + runtime.GOOS, + ) + req.Header.Set("User-Agent", userAgent) + resp, err := client.Do(req) if err != nil { repoError = append(repoError, fmt.Sprintf(T("Error requesting from")+" '%s' - %s", repo.Name, err.Error())) continue } else { - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + repoError = append(repoError, fmt.Sprintf(T("Error closing body")+" '%s' - %s", repo.Name, err.Error())) + } + }(resp.Body) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { repoError = append(repoError, fmt.Sprintf(T("Error reading response from")+" '%s' - %s ", repo.Name, err.Error())) continue diff --git a/cf/actors/pluginrepo/plugin_repo_test.go b/cf/actors/pluginrepo/plugin_repo_test.go index 913be5f931a..64883af1436 100644 --- a/cf/actors/pluginrepo/plugin_repo_test.go +++ b/cf/actors/pluginrepo/plugin_repo_test.go @@ -30,15 +30,19 @@ var _ = Describe("PluginRepo", func() { BeforeEach(func() { testServer1CallCount = 0 h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer GinkgoRecover() testServer1CallCount++ fmt.Fprintln(w, `{"plugins":[]}`) + Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) }) testServer1 = httptest.NewServer(h1) testServer2CallCount = 0 h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer GinkgoRecover() testServer2CallCount++ fmt.Fprintln(w, `{"plugins":[]}`) + Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) }) testServer2 = httptest.NewServer(h2) @@ -87,6 +91,7 @@ var _ = Describe("PluginRepo", func() { Context("When data is valid", func() { BeforeEach(func() { h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer GinkgoRecover() fmt.Fprintln(w, `{"plugins":[ { "name":"plugin1", @@ -111,6 +116,7 @@ var _ = Describe("PluginRepo", func() { ] }] }`) + Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) }) testServer1 = httptest.NewServer(h1) @@ -146,7 +152,9 @@ var _ = Describe("PluginRepo", func() { Context("json is invalid", func() { BeforeEach(func() { h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer GinkgoRecover() fmt.Fprintln(w, `"plugins":[]}`) + Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) }) testServer1 = httptest.NewServer(h1) }) @@ -174,7 +182,9 @@ var _ = Describe("PluginRepo", func() { Context("when data is valid json, but not valid plugin repo data", func() { BeforeEach(func() { h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer GinkgoRecover() fmt.Fprintln(w, `{"bad_plugin_tag":[]}`) + Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) }) testServer1 = httptest.NewServer(h1) }) diff --git a/cf/actors/pluginrepo/pluginrepofakes/fake_plugin_repo.go b/cf/actors/pluginrepo/pluginrepofakes/fake_plugin_repo.go index 25c659ee0c6..8971d8783c6 100644 --- a/cf/actors/pluginrepo/pluginrepofakes/fake_plugin_repo.go +++ b/cf/actors/pluginrepo/pluginrepofakes/fake_plugin_repo.go @@ -38,15 +38,16 @@ func (fake *FakePluginRepo) GetPlugins(arg1 []models.PluginRepo) (map[string][]w fake.getPluginsArgsForCall = append(fake.getPluginsArgsForCall, struct { arg1 []models.PluginRepo }{arg1Copy}) + stub := fake.GetPluginsStub + fakeReturns := fake.getPluginsReturns fake.recordInvocation("GetPlugins", []interface{}{arg1Copy}) fake.getPluginsMutex.Unlock() - if fake.GetPluginsStub != nil { - return fake.GetPluginsStub(arg1) + if stub != nil { + return stub(arg1) } if specificReturn { return ret.result1, ret.result2 } - fakeReturns := fake.getPluginsReturns return fakeReturns.result1, fakeReturns.result2 }