github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/plugin/plugin_repository_test.go (about)

     1  package plugin_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	. "code.cloudfoundry.org/cli/api/plugin"
     9  	"code.cloudfoundry.org/cli/api/plugin/pluginerror"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("PluginRepository", func() {
    16  	var client *Client
    17  
    18  	BeforeEach(func() {
    19  		client = NewTestClient()
    20  	})
    21  
    22  	Describe("GetPluginRepository", func() {
    23  		When("the url points to a valid CF CLI plugin repo", func() {
    24  			var response string
    25  
    26  			BeforeEach(func() {
    27  				response = `{
    28  					"plugins": [
    29  						{
    30  							"name": "plugin-1",
    31  							"description": "useful plugin for useful things",
    32  							"version": "1.0.0",
    33  							"binaries": [{"platform":"osx","url":"http://some-url","checksum":"somechecksum"},{"platform":"win64","url":"http://another-url","checksum":"anotherchecksum"},{"platform":"linux64","url":"http://last-url","checksum":"lastchecksum"}]
    34  						},
    35  						{
    36  							"name": "plugin-2",
    37  							"description": "amazing plugin",
    38  							"version": "1.0.0"
    39  						}
    40  					]
    41  				}`
    42  				server.AppendHandlers(
    43  					CombineHandlers(
    44  						VerifyRequest(http.MethodGet, "/list"),
    45  						RespondWith(http.StatusOK, response),
    46  					),
    47  				)
    48  			})
    49  
    50  			It("returns the plugin repository", func() {
    51  				pluginRepository, err := client.GetPluginRepository(server.URL())
    52  				Expect(err).ToNot(HaveOccurred())
    53  				Expect(pluginRepository).To(Equal(PluginRepository{
    54  					Plugins: []Plugin{
    55  						{
    56  							Name:        "plugin-1",
    57  							Description: "useful plugin for useful things",
    58  							Version:     "1.0.0",
    59  							Binaries: []PluginBinary{
    60  								{Platform: "osx", URL: "http://some-url", Checksum: "somechecksum"},
    61  								{Platform: "win64", URL: "http://another-url", Checksum: "anotherchecksum"},
    62  								{Platform: "linux64", URL: "http://last-url", Checksum: "lastchecksum"},
    63  							},
    64  						},
    65  						{
    66  							Name:        "plugin-2",
    67  							Description: "amazing plugin",
    68  							Version:     "1.0.0",
    69  						},
    70  					},
    71  				}))
    72  			})
    73  
    74  			When("the URL has a trailing slash", func() {
    75  				It("still hits the /list endpoint on the URL", func() {
    76  					_, err := client.GetPluginRepository(fmt.Sprintf("%s/", server.URL()))
    77  					Expect(err).ToNot(HaveOccurred())
    78  				})
    79  			})
    80  
    81  			When("the URL has a trailing '/list'", func() {
    82  				It("still hits the /list endpoint on the URL", func() {
    83  					_, err := client.GetPluginRepository(fmt.Sprintf("%s/list", server.URL()))
    84  					Expect(err).ToNot(HaveOccurred())
    85  				})
    86  			})
    87  
    88  			When("the URL has a trailing '/list/'", func() {
    89  				It("still hits the /list endpoint on the URL", func() {
    90  					_, err := client.GetPluginRepository(fmt.Sprintf("%s/list/", server.URL()))
    91  					Expect(err).ToNot(HaveOccurred())
    92  				})
    93  			})
    94  
    95  			When("the URL has path different from /list", func() {
    96  				BeforeEach(func() {
    97  					server.SetHandler(0,
    98  						CombineHandlers(
    99  							VerifyRequest(http.MethodGet, "/cli/list"),
   100  							RespondWith(http.StatusOK, response),
   101  						),
   102  					)
   103  				})
   104  
   105  				It("appends /list to the path", func() {
   106  					_, err := client.GetPluginRepository(fmt.Sprintf("%s/cli", server.URL()))
   107  					Expect(err).ToNot(HaveOccurred())
   108  				})
   109  			})
   110  		})
   111  
   112  		When("the repository URL in invalid", func() {
   113  			It("returns an error", func() {
   114  				_, err := client.GetPluginRepository("http://not a valid URL")
   115  				Expect(err).To(BeAssignableToTypeOf(&url.Error{}))
   116  			})
   117  		})
   118  
   119  		When("the server returns an error", func() {
   120  			BeforeEach(func() {
   121  				server.AppendHandlers(
   122  					CombineHandlers(
   123  						VerifyRequest(http.MethodGet, "/list"),
   124  						RespondWith(http.StatusNotFound, nil),
   125  					),
   126  				)
   127  			})
   128  
   129  			It("returns the error", func() {
   130  				_, err := client.GetPluginRepository(server.URL())
   131  				Expect(err).To(MatchError(pluginerror.RawHTTPStatusError{Status: "404 Not Found", RawResponse: []byte{}}))
   132  			})
   133  		})
   134  	})
   135  })