github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/cloudcontrollerv2_suite_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"strings"
     9  
    10  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/ghttp"
    15  
    16  	"testing"
    17  )
    18  
    19  func TestCloudcontrollerv2(t *testing.T) {
    20  	RegisterFailHandler(Fail)
    21  	RunSpecs(t, "Cloud Controller V2 Suite")
    22  }
    23  
    24  var server *Server
    25  
    26  var _ = BeforeEach(func() {
    27  	server = NewTLSServer()
    28  	// Suppresses ginkgo server logs
    29  	server.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
    30  })
    31  
    32  var _ = AfterEach(func() {
    33  	server.Close()
    34  })
    35  
    36  func NewTestClient(passed ...Config) *Client {
    37  	return NewClientWithCustomAPIVersion("2.23.0", passed...)
    38  }
    39  
    40  func NewClientWithCustomAPIVersion(apiVersion string, passed ...Config) *Client {
    41  	SetupInfoResponses(apiVersion)
    42  
    43  	var config Config
    44  	if len(passed) > 0 {
    45  		config = passed[0]
    46  	} else {
    47  		config = Config{}
    48  	}
    49  	config.AppName = "CF CLI API V2 Test"
    50  	config.AppVersion = "Unknown"
    51  
    52  	client := NewClient(config)
    53  	warnings, err := client.TargetCF(TargetSettings{
    54  		SkipSSLValidation: true,
    55  		URL:               server.URL() + "/",
    56  	})
    57  	Expect(err).ToNot(HaveOccurred())
    58  	Expect(warnings).To(BeEmpty())
    59  	return client
    60  }
    61  
    62  func SetupInfoResponses(apiVersion string) {
    63  	serverAPIURL := server.URL()[8:]
    64  	response := fmt.Sprintf(`{
    65  		"name":"",
    66  		"build":"",
    67  		"support":"http://support.cloudfoundry.com",
    68  		"version":0,
    69  		"description":"",
    70  		"authorization_endpoint":"https://login.APISERVER",
    71  		"min_cli_version":null,
    72  		"min_recommended_cli_version":null,
    73  		"api_version":"%s",
    74  		"app_ssh_endpoint":"ssh.APISERVER",
    75  		"app_ssh_host_key_fingerprint":"a6:d1:08:0b:b0:cb:9b:5f:c4:ba:44:2a:97:26:19:8a",
    76  		"routing_endpoint": "https://APISERVER/routing",
    77  		"app_ssh_oauth_client":"ssh-proxy",
    78  		"logging_endpoint":"wss://loggregator.APISERVER",
    79  		"doppler_logging_endpoint":"wss://doppler.APISERVER"
    80  	}`, apiVersion)
    81  	response = strings.Replace(response, "APISERVER", serverAPIURL, -1)
    82  	server.AppendHandlers(
    83  		CombineHandlers(
    84  			VerifyRequest(http.MethodGet, "/v2/info"),
    85  			RespondWith(http.StatusOK, response),
    86  		),
    87  		CombineHandlers(
    88  			VerifyRequest(http.MethodGet, "/"),
    89  			RespondWith(http.StatusOK, `{ "links": {"log_cache": {"href": "api.coolbeans.log-cache"}}}`),
    90  		),
    91  	)
    92  }
    93  
    94  func validateV2InfoPlusNumberOfRequests(numberOfPostInfoRequests int) {
    95  	Expect(server.ReceivedRequests()).To(HaveLen(numberOfPostInfoRequests + 1))
    96  }