github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/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  		Context("when the url points to a valid CF CLI plugin repo", func() {
    24  			BeforeEach(func() {
    25  				response := `{
    26  					"plugins": [
    27  						{
    28  							"name": "plugin-1",
    29  							"description": "useful plugin for useful things",
    30  							"version": "1.0.0",
    31  							"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"}]
    32  						},
    33  						{
    34  							"name": "plugin-2",
    35  							"description": "amazing plugin",
    36  							"version": "1.0.0"
    37  						}
    38  					]
    39  				}`
    40  				server.AppendHandlers(
    41  					CombineHandlers(
    42  						VerifyRequest(http.MethodGet, "/list"),
    43  						RespondWith(http.StatusOK, response),
    44  					),
    45  				)
    46  			})
    47  
    48  			It("returns the plugin repository", func() {
    49  				pluginRepository, err := client.GetPluginRepository(server.URL())
    50  				Expect(err).ToNot(HaveOccurred())
    51  				Expect(pluginRepository).To(Equal(PluginRepository{
    52  					Plugins: []Plugin{
    53  						{
    54  							Name:        "plugin-1",
    55  							Description: "useful plugin for useful things",
    56  							Version:     "1.0.0",
    57  							Binaries: []PluginBinary{
    58  								{Platform: "osx", URL: "http://some-url", Checksum: "somechecksum"},
    59  								{Platform: "win64", URL: "http://another-url", Checksum: "anotherchecksum"},
    60  								{Platform: "linux64", URL: "http://last-url", Checksum: "lastchecksum"},
    61  							},
    62  						},
    63  						{
    64  							Name:        "plugin-2",
    65  							Description: "amazing plugin",
    66  							Version:     "1.0.0",
    67  						},
    68  					},
    69  				}))
    70  			})
    71  
    72  			Context("when the URL has a trailing slash", func() {
    73  				It("still hits the /list endpoint on the URL", func() {
    74  					_, err := client.GetPluginRepository(fmt.Sprintf("%s/", server.URL()))
    75  					Expect(err).ToNot(HaveOccurred())
    76  				})
    77  			})
    78  
    79  			Context("when the URL has a trailing '/list'", func() {
    80  				It("still hits the /list endpoint on the URL", func() {
    81  					_, err := client.GetPluginRepository(fmt.Sprintf("%s/list", server.URL()))
    82  					Expect(err).ToNot(HaveOccurred())
    83  				})
    84  			})
    85  		})
    86  
    87  		Context("when the repository URL in invalid", func() {
    88  			It("returns an error", func() {
    89  				_, err := client.GetPluginRepository("http://not a valid URL")
    90  				Expect(err).To(BeAssignableToTypeOf(&url.Error{}))
    91  			})
    92  		})
    93  
    94  		Context("when the server returns an error", func() {
    95  			BeforeEach(func() {
    96  				server.AppendHandlers(
    97  					CombineHandlers(
    98  						VerifyRequest(http.MethodGet, "/list"),
    99  						RespondWith(http.StatusNotFound, nil),
   100  					),
   101  				)
   102  			})
   103  
   104  			It("returns the error", func() {
   105  				_, err := client.GetPluginRepository(server.URL())
   106  				Expect(err).To(MatchError(pluginerror.RawHTTPStatusError{Status: "404 Not Found", RawResponse: []byte{}}))
   107  			})
   108  		})
   109  	})
   110  })