github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/api/uaa/version_test.go (about)

     1  package uaa_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/uaa"
     7  	. "code.cloudfoundry.org/cli/api/uaa"
     8  	"code.cloudfoundry.org/cli/api/uaa/uaafakes"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Version", func() {
    16  	var (
    17  		client     *Client
    18  		fakeConfig *uaafakes.FakeConfig
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeConfig = NewTestConfig()
    23  		client = NewClient(fakeConfig)
    24  
    25  		client.Info.Links.Login = "https://" + TestAuthorizationResource
    26  	})
    27  
    28  	Describe("GetAPIVersion", func() {
    29  		var (
    30  			version string
    31  			err     error
    32  		)
    33  
    34  		JustBeforeEach(func() {
    35  			version, err = client.GetAPIVersion()
    36  		})
    37  
    38  		When("the request succeeds", func() {
    39  			BeforeEach(func() {
    40  				response := `{
    41  					"app": { "version": "1.2.3" }
    42  				}`
    43  
    44  				server.AppendHandlers(
    45  					CombineHandlers(
    46  						verifyRequestHost(TestAuthorizationResource),
    47  						VerifyRequest(http.MethodGet, "/login"),
    48  						RespondWith(http.StatusOK, response),
    49  					),
    50  				)
    51  			})
    52  
    53  			It("returns the login prompts", func() {
    54  				Expect(err).NotTo(HaveOccurred())
    55  				Expect(version).To(Equal("1.2.3"))
    56  			})
    57  		})
    58  
    59  		When("the request fails", func() {
    60  			BeforeEach(func() {
    61  				server.AppendHandlers(
    62  					CombineHandlers(
    63  						verifyRequestHost(TestAuthorizationResource),
    64  						VerifyRequest(http.MethodGet, "/login"),
    65  						RespondWith(http.StatusTeapot, `{}`),
    66  					),
    67  				)
    68  			})
    69  
    70  			It("returns the error", func() {
    71  				Expect(err).To(MatchError(uaa.RawHTTPStatusError{
    72  					StatusCode:  http.StatusTeapot,
    73  					RawResponse: []byte(`{}`),
    74  				}))
    75  			})
    76  		})
    77  	})
    78  })