github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/target_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/ccv3fakes"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("Target", func() {
    17  	var (
    18  		client    *Client
    19  		serverURL string
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		client = NewClient(Config{AppName: "CF CLI API V3 Target Test", AppVersion: "Unknown"})
    24  	})
    25  
    26  	Describe("TargetCF", func() {
    27  		BeforeEach(func() {
    28  			server.Reset()
    29  
    30  			serverURL = server.URL()
    31  			rootResponse := fmt.Sprintf(`{
    32  				"links": {
    33  					"self": {
    34  						"href": "%s"
    35  					},
    36  					"cloud_controller_v2": {
    37  						"href": "%s/v2",
    38  						"meta": {
    39  							"version": "2.64.0"
    40  						}
    41  					},
    42  					"cloud_controller_v3": {
    43  						"href": "%s/v3",
    44  						"meta": {
    45  							"version": "3.0.0-alpha.5"
    46  						}
    47  					},
    48  					"uaa": {
    49  						"href": "https://uaa.bosh-lite.com"
    50  					}
    51  				}
    52  			}`, serverURL, serverURL, serverURL)
    53  
    54  			server.AppendHandlers(
    55  				CombineHandlers(
    56  					VerifyRequest(http.MethodGet, "/"),
    57  					RespondWith(
    58  						http.StatusOK,
    59  						rootResponse,
    60  						http.Header{"X-Cf-Warnings": {"warning 1"}}),
    61  				),
    62  			)
    63  		})
    64  
    65  		When("client has wrappers", func() {
    66  			var fakeWrapper1 *ccv3fakes.FakeConnectionWrapper
    67  			var fakeWrapper2 *ccv3fakes.FakeConnectionWrapper
    68  
    69  			BeforeEach(func() {
    70  				fakeWrapper1 = new(ccv3fakes.FakeConnectionWrapper)
    71  				fakeWrapper1.WrapReturns(fakeWrapper1)
    72  				fakeWrapper2 = new(ccv3fakes.FakeConnectionWrapper)
    73  				fakeWrapper2.WrapReturns(fakeWrapper2)
    74  
    75  				fakeWrapper2.MakeStub = func(request *cloudcontroller.Request, passedResponse *cloudcontroller.Response) error {
    76  					apiInfo, ok := passedResponse.DecodeJSONResponseInto.(*Info)
    77  					if ok { // Only caring about the first time Make is called, ignore all others
    78  						apiInfo.Links.CCV3.HREF = server.URL() + "/v3"
    79  					}
    80  					return nil
    81  				}
    82  
    83  				client = NewClient(Config{
    84  					AppName:    "CF CLI API Target Test",
    85  					AppVersion: "Unknown",
    86  					Wrappers:   []ConnectionWrapper{fakeWrapper1, fakeWrapper2},
    87  				})
    88  			})
    89  
    90  			It("calls wrap on all the wrappers", func() {
    91  				client.TargetCF(TargetSettings{
    92  					SkipSSLValidation: true,
    93  					URL:               server.URL(),
    94  				})
    95  
    96  				Expect(fakeWrapper1.WrapCallCount()).To(Equal(1))
    97  				Expect(fakeWrapper2.WrapCallCount()).To(Equal(1))
    98  				Expect(fakeWrapper2.WrapArgsForCall(0)).To(Equal(fakeWrapper1))
    99  			})
   100  		})
   101  	})
   102  })